diff options
-rw-r--r-- | config.h.guess | 13 | ||||
-rw-r--r-- | doc/luaossl.tex | 4 | ||||
-rw-r--r-- | src/openssl.c | 76 |
3 files changed, 82 insertions, 11 deletions
diff --git a/config.h.guess b/config.h.guess index db17fc7..ee08de7 100644 --- a/config.h.guess +++ b/config.h.guess @@ -610,7 +610,12 @@ #endif #ifndef HAVE_DECL_RANDOM_UUID -#define HAVE_DECL_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ +/* RANDOM_UUID is an enum, not macro */ +#if (HAVE_SYS_SYSCTL_H && defined __linux__) +#define HAVE_DECL_RANDOM_UUID 1 +#else +#define HAVE_DECL_RANDOM_UUID 0 +#endif #endif #ifndef HAVE_DECL_STRERROR_R @@ -622,7 +627,11 @@ #endif #ifndef HAVE_DECL_SYS_GETRANDOM -#define HAVE_DECL_SYS_GETRANDOM (defined SYS_getrandom) +#ifdef SYS_getrandom +#define HAVE_DECL_SYS_GETRANDOM 1 +#else +#define HAVE_DECL_SYS_GETRANDOM 0 +#endif #endif diff --git a/doc/luaossl.tex b/doc/luaossl.tex index 60af781..8695949 100644 --- a/doc/luaossl.tex +++ b/doc/luaossl.tex @@ -1029,6 +1029,10 @@ Add or interpose an ssl class method. Returns the previous method, if any. Replaces the \module{openssl.ssl.context} used by $ssl$ with $context$. +\subsubsection[\fn{ssl:getContext}]{\fn{ssl:getContext()}} + +Returns the \module{openssl.ssl.context} used by $ssl$. + \subsubsection[\fn{ssl:setOptions}]{\fn{ssl:setOptions($flags$)}} Adds the option flags of the SSL connection instance. See \fn{openssl.ssl.context:setOptions}. diff --git a/src/openssl.c b/src/openssl.c index 6ada254..5d757a2 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -311,6 +311,10 @@ #define HAVE_SSL_CTX_SET1_PARAM (OPENSSL_PREREQ(1,0,2) || LIBRESSL_PREREQ(2,1,0)) #endif +#ifndef HAVE_SSL_CTX_UP_REF +#define HAVE_SSL_CTX_UP_REF (OPENSSL_PREREQ(1,1,0) || LIBRESSL_PREREQ(2,7,0)) +#endif + #ifndef HAVE_SSL_CTX_CERT_STORE #define HAVE_SSL_CTX_CERT_STORE (!OPENSSL_PREREQ(1,1,0)) #endif @@ -1853,6 +1857,18 @@ static int compat_SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) { } /* compat_SSL_CTX_set1_param() */ #endif +#if !HAVE_SSL_CTX_UP_REF +#define SSL_CTX_up_ref(...) EXPAND( compat_SSL_CTX_up_ref(__VA_ARGS__) ) + +static int compat_SSL_CTX_up_ref(SSL_CTX *ctx) { + /* our caller should already have had a proper reference */ + if (CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX) < 2) + return 0; /* fail */ + + return 1; +} /* compat_SSL_CTX_up_ref() */ +#endif + #if !HAVE_STACK_OPENSSL_STRING_FUNCS #define sk_OPENSSL_STRING_num(s) sk_num(s) #define sk_OPENSSL_STRING_value(s, i) sk_value((s), (i)) @@ -8341,6 +8357,26 @@ EXPORT int luaopen__openssl_pkcs12(lua_State *L) { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +static void sx_push(lua_State *L, SSL_CTX *ctx) { + lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)&initall); + if (LUA_TNIL == lua_rawgetp(L, -1, ctx)) { + SSL_CTX **ud; + + lua_pop(L, 1); /* pop nil */ + + ud = prepsimple(L, SSL_CTX_CLASS); + + SSL_CTX_up_ref(ctx); + *ud = ctx; + + /* Add to cache */ + lua_pushvalue(L, -1); + lua_rawsetp(L, -3, ctx); + } + lua_remove(L, -2); +} /* sx_push() */ + + static int sx_new(lua_State *L) { static const char *const opts[] = { [0] = "SSL", @@ -8468,6 +8504,12 @@ static int sx_new(lua_State *L) { return auxL_error(L, auxL_EOPENSSL, "ssl.context.new"); #endif + /* Add to cache */ + lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)&initall); + lua_pushvalue(L, -2); + lua_rawsetp(L, -2, *ud); + lua_pop(L, 1); + return 1; } /* sx_new() */ @@ -9236,6 +9278,18 @@ static int ssl_setContext(lua_State *L) { return 1; } /* ssl_setContext() */ + +static int ssl_getContext(lua_State *L) { + SSL *ssl = checksimple(L, 1, SSL_CLASS); + + SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); + + sx_push(L, ctx); + + return 1; +} /* ssl_getContext() */ + + static int ssl_setOptions(lua_State *L) { SSL *ssl = checksimple(L, 1, SSL_CLASS); auxL_Integer options = auxL_checkinteger(L, 2); @@ -9742,6 +9796,7 @@ static int ssl__gc(lua_State *L) { static const auxL_Reg ssl_methods[] = { { "setContext", &ssl_setContext }, + { "getContext", &ssl_getContext }, { "setOptions", &ssl_setOptions }, { "getOptions", &ssl_getOptions }, { "clearOptions", &ssl_clearOptions }, @@ -11337,14 +11392,17 @@ static void initall(lua_State *L) { auxL_addclass(L, OCSP_RESPONSE_CLASS, or_methods, or_metatable, 0); auxL_addclass(L, OCSP_BASICRESP_CLASS, ob_methods, ob_metatable, 0); - /* Create cache for pointers */ - lua_newtable(L); - lua_createtable(L, 0, 2); - lua_pushliteral(L, "kv"); - lua_setfield(L, -2, "__mode"); - lua_pushliteral(L, "luaossl cache"); - lua_setfield(L, -2, "__name"); - lua_setmetatable(L, -2); - lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)&initall); + if (LUA_TNIL == lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)&initall)) { + /* Create cache for pointers */ + lua_newtable(L); + lua_createtable(L, 0, 2); + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); + lua_pushliteral(L, "luaossl cache"); + lua_setfield(L, -2, "__name"); + lua_setmetatable(L, -2); + lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)&initall); + } + lua_pop(L, 1); } /* initall() */ |