Skip to content

Commit 1d7b030

Browse files
committed
This commit adds two new methods to openssl.ssl.ctx objects.
- `ctx:setCertificteFromFile` calls `SSL_CTX_use_certificate_chain_file` to add a certificate chain from a pem encoded file specified by the string argument path. - `ctx:setPrivateKeyFromFile` calls `SSL_CTX_use_private_key_file` to add a private key from a PEM or ASN1 encoded file using the string argument path and filetype integer flag argument. The filetype is optional and will default to PEM if not specified. - `openssl.filetypes` is a new table in the openssl module which contains the two filetypes used by `setPrivateKeyFromFile`. The `.PEM` field is the value of `SSL_FILETYPE_PEM` and the `.ASN1` field is the value of `SSL_FILETYPE_ASN1`.
1 parent 1054d12 commit 1d7b030

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/openssl.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,10 @@
613613
#define HMAC_INIT_EX_INT OPENSSL_PREREQ(1,0,0)
614614
#endif
615615

616+
#ifndef HAVE_USE_CERTIFICATE_CHAIN_FILE
617+
#define HAVE_USE_CERTIFICATE_CHAIN_FILE (OPENSSL_PREREQ(0,9,4) || LIBRESSL_PREREQ(2,0,0))
618+
#endif
619+
616620
#if HAVE_EVP_PKEY_CTX_KDF || HAVE_EVP_KDF_CTX
617621
#include <openssl/kdf.h>
618622
#endif
@@ -3248,6 +3252,12 @@ static const auxL_IntegerReg openssl_integers[] = {
32483252
{ NULL, 0 },
32493253
};
32503254

3255+
static const auxL_IntegerReg openssl_filetypes[] = {
3256+
{"PEM", SSL_FILETYPE_PEM},
3257+
{"ASN1", SSL_FILETYPE_ASN1},
3258+
{NULL, 0}
3259+
};
3260+
32513261
EXPORT int luaopen__openssl(lua_State *L) {
32523262
size_t i;
32533263

@@ -3271,6 +3281,12 @@ EXPORT int luaopen__openssl(lua_State *L) {
32713281
lua_pushstring(L, SHLIB_VERSION_NUMBER);
32723282
lua_setfield(L, -2, "SHLIB_VERSION_NUMBER");
32733283

3284+
3285+
lua_newtable(L);
3286+
auxL_setintegers(L, openssl_filetypes);
3287+
3288+
lua_setfield(L, -2, "filetypes");
3289+
32743290
return 1;
32753291
} /* luaopen__openssl() */
32763292

@@ -9481,6 +9497,18 @@ static int sx_setCertificateChain(lua_State *L) {
94819497
} /* sx_setCertificateChain() */
94829498
#endif
94839499

9500+
#if HAVE_USE_CERTIFICATE_CHAIN_FILE
9501+
static int sx_useCertificateChainFile(lua_State* L) {
9502+
SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS);
9503+
const char *filepath = luaL_checkstring(L, 2);
9504+
9505+
if (!SSL_CTX_use_certificate_chain_file(ctx, filepath))
9506+
return auxL_error(L, auxL_EOPENSSL, "ssl.context:setCertificateChainFromFile");
9507+
9508+
lua_pushboolean(L, 1);
9509+
return 1;
9510+
}
9511+
#endif
94849512

94859513
#if HAVE_SSL_CTX_GET0_CHAIN_CERTS
94869514
static int sx_getCertificateChain(lua_State *L) {
@@ -9496,7 +9524,6 @@ static int sx_getCertificateChain(lua_State *L) {
94969524
} /* sx_getCertificateChain() */
94979525
#endif
94989526

9499-
95009527
static int sx_setPrivateKey(lua_State *L) {
95019528
SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS);
95029529
EVP_PKEY *key = checksimple(L, 2, PKEY_CLASS);
@@ -9519,6 +9546,19 @@ static int sx_setPrivateKey(lua_State *L) {
95199546
} /* sx_setPrivateKey() */
95209547

95219548

9549+
static int sx_usePrivateKeyFile(lua_State* L) {
9550+
SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS);
9551+
const char* filepath = luaL_checkstring(L, 2);
9552+
int typ = luaL_optinteger(L, 3, SSL_FILETYPE_PEM);
9553+
9554+
if (!SSL_CTX_use_PrivateKey_file(ctx, filepath, typ))
9555+
return auxL_error(L, auxL_EOPENSSL, "ssl.context:setPrivateKeyFromFile");
9556+
9557+
lua_pushboolean(L, 1);
9558+
9559+
return 1;
9560+
}
9561+
95229562
static int sx_setCipherList(lua_State *L) {
95239563
SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS);
95249564
const char *ciphers = luaL_checkstring(L, 2);
@@ -10270,7 +10310,6 @@ static int sx__gc(lua_State *L) {
1027010310
return 0;
1027110311
} /* sx__gc() */
1027210312

10273-
1027410313
static const auxL_Reg sx_methods[] = {
1027510314
{ "setOptions", &sx_setOptions },
1027610315
{ "getOptions", &sx_getOptions },
@@ -10292,8 +10331,12 @@ static const auxL_Reg sx_methods[] = {
1029210331
#endif
1029310332
#if HAVE_SSL_CTX_GET0_CHAIN_CERTS
1029410333
{ "getCertificateChain", &sx_getCertificateChain },
10334+
#endif
10335+
#if HAVE_USE_CERTIFICATE_CHAIN_FILE
10336+
{"setCertificateChainFromFile", &sx_useCertificateChainFile},
1029510337
#endif
1029610338
{ "setPrivateKey", &sx_setPrivateKey },
10339+
{ "setPrivateKeyFromFile", &sx_usePrivateKeyFile},
1029710340
{ "setCipherList", &sx_setCipherList },
1029810341
#if HAVE_SSL_CTX_SET_CIPHERSUITES
1029910342
{ "setCipherSuites", &sx_setCipherSuites },

0 commit comments

Comments
 (0)