aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c76
1 files changed, 67 insertions, 9 deletions
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() */