Free DH parameter right after handshake

This commit is contained in:
Bruno Silvestre 2018-07-26 11:21:54 -03:00
parent 953a363a59
commit d9d0cd620d
2 changed files with 9 additions and 19 deletions

View File

@ -196,7 +196,6 @@ static DH *dhparam_cb(SSL *ssl, int is_export, int keylength)
{ {
BIO *bio; BIO *bio;
lua_State *L; lua_State *L;
DH *dh_tmp = NULL;
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
p_context pctx = (p_context)SSL_CTX_get_app_data(ctx); p_context pctx = (p_context)SSL_CTX_get_app_data(ctx);
@ -217,24 +216,15 @@ static DH *dhparam_cb(SSL *ssl, int is_export, int keylength)
lua_pop(L, 2); /* Remove values from stack */ lua_pop(L, 2); /* Remove values from stack */
return NULL; return NULL;
} }
bio = BIO_new_mem_buf((void*)lua_tostring(L, -1),
lua_rawlen(L, -1)); bio = BIO_new_mem_buf((void*)lua_tostring(L, -1), lua_rawlen(L, -1));
if (bio) { if (bio) {
dh_tmp = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); pctx->dh_param = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
BIO_free(bio); BIO_free(bio);
} }
/*
* OpenSSL exepcts the callback to maintain a reference to the DH*. So,
* cache it here, and clean up the previous set of parameters. Any remaining
* set is cleaned up when destroying the LuaSec context.
*/
if (pctx->dh_param)
DH_free(pctx->dh_param);
pctx->dh_param = dh_tmp;
lua_pop(L, 2); /* Remove values from stack */ lua_pop(L, 2); /* Remove values from stack */
return dh_tmp; return pctx->dh_param;
} }
/** /**
@ -668,11 +658,6 @@ static int meth_destroy(lua_State *L)
SSL_CTX_free(ctx->context); SSL_CTX_free(ctx->context);
ctx->context = NULL; ctx->context = NULL;
} }
if (ctx->dh_param) {
DH_free(ctx->dh_param);
ctx->dh_param = NULL;
}
return 0; return 0;
} }

View File

@ -384,6 +384,11 @@ static int meth_handshake(lua_State *L)
{ {
p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection"); p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
int err = handshake(ssl); int err = handshake(ssl);
p_context ctx = (p_context)SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl->ssl));
if (ctx->dh_param) {
DH_free(ctx->dh_param);
ctx->dh_param = NULL;
}
if (err == IO_DONE) { if (err == IO_DONE) {
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
return 1; return 1;