Skip to content

Commit d0abc48

Browse files
authored
- Update ssl:setPrivateKeyFromFile and ctx.setPrivateKeyFromFile methods, optional filetype is now a string (luaL_checkoption) (#1)
- Update doc/luaossl.tex : document 'context:setCertificateChainFromFile', 'context:setPrivateKeyFromFile', 'ssl:setCertificateChainFromFile' and 'ssl:setPrivateKeyFromFile' methods
1 parent 124d32b commit d0abc48

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

doc/luaossl.pdf

186 KB
Binary file not shown.

doc/luaossl.tex

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,10 @@ \section{Modules}
10691069

10701070
\emph{Only supported since OpenSSL 1.0.2.}
10711071

1072+
\subsubsection[\fn{context:setCertificateChainFromFile}]{\fn{context:setCertificateChainFromFile($filepath$[, $format$])}}
1073+
1074+
Sets the X.509 certificate chain \module{openssl.x509.chain} object to send during SSL connection instance handshakes, load the certificate chain from the file $filepath$. $format$ is either ``ASN1'' or ``PEM'' (default).
1075+
10721076
\subsubsection[\fn{context:setCertificateChain}]{\fn{context:setCertificateChain($chain$)}}
10731077

10741078
Sets the X.509 certificate chain \module{openssl.x509.chain} object $chain$ to send during SSL connection instance handshakes.
@@ -1081,6 +1085,10 @@ \section{Modules}
10811085

10821086
\emph{Only supported since OpenSSL 1.0.2.}
10831087

1088+
\subsubsection[\fn{context:setPrivateKeyFromFile}]{\fn{context:setPrivateKeyFromFile($filepath$[, $format$])}}
1089+
1090+
Sets the private key \module{openssl.pkey} object to send during SSL connection instance handshakes, load the key from the file $filepath$. $format$ is either ``ASN1'' or ``PEM'' (default).
1091+
10841092
\subsubsection[\fn{context:setPrivateKey}]{\fn{context:setPrivateKey($key$)}}
10851093

10861094
Sets the private key \module{openssl.pkey} object $key$ for use during SSL connection instance handshakes.
@@ -1286,20 +1294,32 @@ \section{Modules}
12861294
Sets the X.509 certificate \module{openssl.x509} object $crt$ to send during SSL connection instance handshakes.
12871295
See \fn{openssl.ssl.context:setCertificate}.
12881296

1297+
\subsubsection[\fn{ssl:setCertificateChainFromFile}]{\fn{ssl:setCertificateChainFromFile($filepath$[, $format$])}}
1298+
1299+
Sets the X.509 certificate chain \module{openssl.x509.chain} object to send during SSL connection instance handshakes, load the certificate chain from the file $filepath$. $format$ is either ``ASN1'' or ``PEM'' (default).
1300+
See \fn{openssl.ssl.context:setCertificateChainFromFile}.
1301+
1302+
\emph{Only supported since OpenSSL 1.1.0.}
1303+
12891304
\subsubsection[\fn{ssl:setCertificateChain}]{\fn{ssl:setCertificateChain($chain$)}}
12901305

12911306
Sets the X.509 certificate chain \module{openssl.x509.chain} object $chain$ to send during SSL connection instance handshakes.
12921307
See \fn{openssl.ssl.context:setCertificateChain}.
12931308

12941309
\emph{Only supported since OpenSSL 1.0.2.}
12951310

1296-
\subsubsection[\fn{context:getCertificateChain}]{\fn{context:getCertificateChain()}}
1311+
\subsubsection[\fn{ssl:getCertificateChain}]{\fn{ssl:getCertificateChain()}}
12971312

12981313
Returns the X.509 certificate chain \module{openssl.x509.chain} object to be sent during SSL connection instance handshakes.
12991314
See \fn{openssl.ssl.context:getCertificateChain}.
13001315

13011316
\emph{Only supported since OpenSSL 1.0.2.}
13021317

1318+
\subsubsection[\fn{ssl:setPrivateKeyFromFile}]{\fn{ssl:setPrivateKeyFromFile($filepath$[, $format$])}}
1319+
1320+
Sets the private key \module{openssl.pkey} object to send during SSL connection instance handshakes, load the key from the file $filepath$. $format$ is either ``ASN1'' or ``PEM'' (default).
1321+
See \fn{openssl.ssl.context:setPrivateKeyFromFile}.
1322+
13031323
\subsubsection[\fn{ssl:setPrivateKey}]{\fn{ssl:setPrivateKey($key$)}}
13041324

13051325
Sets the private key \module{openssl.pkey} object $key$ for use during SSL connection instance handshakes.

src/openssl.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,25 @@ static int optencoding(lua_State *L, int index, const char *def, int allow) {
860860
return type;
861861
} /* optencoding() */
862862

863+
static int optfiletype(lua_State *L, int index, const char *def) {
864+
static const char *const opts[] = { "pem", "asn1", NULL };
865+
int type = 0;
866+
867+
switch (auxL_checkoption(L, index, def, opts, 1)) {
868+
case 0:
869+
type = SSL_FILETYPE_PEM;
870+
break;
871+
case 1:
872+
type = SSL_FILETYPE_ASN1;
873+
break;
874+
}
875+
876+
if (!type) {
877+
luaL_argerror(L, index, lua_pushfstring(L, "invalid option %s", luaL_checkstring(L, index)));
878+
}
879+
880+
return type;
881+
}
863882

864883
static _Bool rawgeti(lua_State *L, int index, int n) {
865884
lua_rawgeti(L, index, n);
@@ -3256,12 +3275,6 @@ static const auxL_IntegerReg openssl_integers[] = {
32563275
{ NULL, 0 },
32573276
};
32583277

3259-
static const auxL_IntegerReg openssl_filetypes[] = {
3260-
{"PEM", SSL_FILETYPE_PEM},
3261-
{"ASN1", SSL_FILETYPE_ASN1},
3262-
{NULL, 0}
3263-
};
3264-
32653278
EXPORT int luaopen__openssl(lua_State *L) {
32663279
size_t i;
32673280

@@ -3285,12 +3298,6 @@ EXPORT int luaopen__openssl(lua_State *L) {
32853298
lua_pushstring(L, SHLIB_VERSION_NUMBER);
32863299
lua_setfield(L, -2, "SHLIB_VERSION_NUMBER");
32873300

3288-
3289-
lua_newtable(L);
3290-
auxL_setintegers(L, openssl_filetypes);
3291-
3292-
lua_setfield(L, -2, "filetypes");
3293-
32943301
return 1;
32953302
} /* luaopen__openssl() */
32963303

@@ -9556,9 +9563,9 @@ static int sx_setPrivateKey(lua_State *L) {
95569563
static int sx_setPrivateKeyFromFile(lua_State* L) {
95579564
SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS);
95589565
const char* filepath = luaL_checkstring(L, 2);
9559-
int typ = luaL_optinteger(L, 3, SSL_FILETYPE_PEM);
9566+
int type = optfiletype(L, 3, "PEM");
95609567

9561-
if (!SSL_CTX_use_PrivateKey_file(ctx, filepath, typ))
9568+
if (!SSL_CTX_use_PrivateKey_file(ctx, filepath, type))
95629569
return auxL_error(L, auxL_EOPENSSL, "ssl.context:setPrivateKeyFromFile");
95639570

95649571
lua_pushboolean(L, 1);
@@ -10896,9 +10903,9 @@ static int ssl_setPrivateKey(lua_State *L) {
1089610903
static int ssl_setPrivateKeyFromFile(lua_State* L) {
1089710904
SSL *ssl = checksimple(L, 1, SSL_CLASS);
1089810905
const char* filepath = luaL_checkstring(L, 2);
10899-
int typ = luaL_optinteger(L, 3, SSL_FILETYPE_PEM);
10906+
int type = optfiletype(L, 3, "PEM");
1090010907

10901-
if (!SSL_use_PrivateKey_file(ssl, filepath, typ))
10908+
if (!SSL_use_PrivateKey_file(ssl, filepath, type))
1090210909
return auxL_error(L, auxL_EOPENSSL, "ssl:setPrivateKeyFromFile");
1090310910

1090410911
lua_pushboolean(L, 1);

0 commit comments

Comments
 (0)