static int tlsext_servername_callback(SSL *ssl, int *ad, void *arg) { SSL_CTX *newctx = NULL; SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); lua_State *L = SSL_CTX_get_app_data(ctx); const char *name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); /* No name, use default context */ if (!name) return SSL_TLSEXT_ERR_NOACK; /* Search for the name in the map */ openssl_getvalue(L, ctx, "tlsext_servername"); if (lua_istable(L, -1)) { lua_getfield(L, -1, name); if (auxiliar_isclass(L, "openssl.ssl_ctx", -1)) { newctx = CHECK_OBJECT(-1, SSL_CTX, "openssl.ssl_ctx"); SSL_set_SSL_CTX(ssl, newctx); lua_pop(L, 2); return SSL_TLSEXT_ERR_OK; } } else if (lua_isfunction(L, -1)) { } else { } lua_pop(L, 1); return SSL_TLSEXT_ERR_ALERT_FATAL; }
static int openssl_ssl_getpeerverification(lua_State *L) { long err; SSL* ssl = CHECK_OBJECT(1, SSL, "openssl.ssl"); SSL_CTX* ctx = SSL_get_SSL_CTX(ssl); err = SSL_get_verify_result(ssl); lua_pushboolean(L, err == X509_V_OK); openssl_getvalue(L, ssl, "verify_cert"); return 2; }
static EC_KEY *tmp_ecdh_callback(SSL *ssl, int is_export, int keylength) { BIO *bio; EC_KEY *ec_tmp = NULL; SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); lua_State *L = SSL_CTX_get_app_data(ctx); int ret = 0; /* get callback function */ openssl_getvalue(L, ctx, "tmp_ecdh_callback"); /* Invoke the callback */ lua_pushboolean(L, is_export); lua_pushnumber(L, keylength); ret = lua_pcall(L, 2, 1, 0); if (ret == 0) { /* Load parameters from returned value */ if (lua_type(L, -1) != LUA_TSTRING) { lua_pop(L, 2); /* Remove values from stack */ return NULL; } bio = BIO_new_mem_buf((void*)lua_tostring(L, -1), lua_rawlen(L, -1)); if (bio) { ec_tmp = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL); BIO_free(bio); } } else { lua_error(L); } lua_pop(L, 2); /* Remove values from stack */ return ec_tmp; }
static LUA_FUNCTION(openssl_bio_free) { BIO* bio = CHECK_OBJECT(1, BIO, "openssl.bio"); int all = 0; if (lua_isboolean(L, 2)) all = lua_toboolean(L, 2); else { openssl_getvalue(L, bio, "free_all"); all = lua_toboolean(L, -1); lua_pop(L, 1); } if (all) BIO_free_all(bio); else BIO_free(bio); lua_pushnil(L); lua_setmetatable(L, 1); return 0; }