diff --git a/mongoose.c b/mongoose.c index cf523a96..31ef2ee8 100644 --- a/mongoose.c +++ b/mongoose.c @@ -526,6 +526,10 @@ int mg_get_reply_status_code(const struct mg_connection *conn) { return conn == NULL ? -1 : conn->status_code; } +void *mg_get_ssl_context(const struct mg_connection *conn) { + return conn == NULL || conn->ctx == NULL ? NULL : conn->ctx->ssl_ctx; +} + static int get_option_index(const char *name) { int i; @@ -3883,17 +3887,18 @@ static int set_ssl_option(struct mg_context *ctx) { return 0; } - if (ctx->user_callback != NULL) { - ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) ctx->ssl_ctx); - } - - if (SSL_CTX_use_certificate_file(ctx->ssl_ctx, pem, SSL_FILETYPE_PEM) == 0 || - SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, pem, SSL_FILETYPE_PEM) == 0) { + // If user callback returned non-NULL, that means that user callback has + // set up certificate itself. In this case, skip sertificate setting. + if (call_user(fc(ctx), MG_INIT_SSL) == NULL && pem != NULL && + (SSL_CTX_use_certificate_file(ctx->ssl_ctx, pem, SSL_FILETYPE_PEM) == 0 || + SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, pem, SSL_FILETYPE_PEM) == 0)) { cry(fc(ctx), "%s: cannot open %s: %s", __func__, pem, ssl_error()); return 0; } - (void) SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx, pem); + if (pem != NULL) { + (void) SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx, pem); + } // Initialize locking callbacks, needed for thread safety. // http://www.openssl.org/support/faq.html#PROG1 @@ -4445,7 +4450,7 @@ struct mg_context *mg_start(mg_callback_t user_callback, void *user_data, // be initialized before listening ports. UID must be set last. if (!set_gpass_option(ctx) || #if !defined(NO_SSL) - (ctx->config[SSL_CERTIFICATE] != NULL && !set_ssl_option(ctx)) || + !set_ssl_option(ctx) || #endif !set_ports_option(ctx) || #if !defined(_WIN32) diff --git a/mongoose.h b/mongoose.h index 90336769..4923d9aa 100644 --- a/mongoose.h +++ b/mongoose.h @@ -154,6 +154,7 @@ const struct mg_request_info *mg_get_request_info(const struct mg_connection *); void *mg_get_user_data(struct mg_connection *); const char *mg_get_log_message(const struct mg_connection *); int mg_get_reply_status_code(const struct mg_connection *); +void *mg_get_ssl_context(const struct mg_connection *); // Send data to the client.