Commit 9aad9229 authored by Sergey Lyubka's avatar Sergey Lyubka

added mg_get_ssl_context. passing fake connection to the MG_INIT_SSL event

parent d9ab7af8
...@@ -526,6 +526,10 @@ int mg_get_reply_status_code(const struct mg_connection *conn) { ...@@ -526,6 +526,10 @@ int mg_get_reply_status_code(const struct mg_connection *conn) {
return conn == NULL ? -1 : conn->status_code; 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) { static int get_option_index(const char *name) {
int i; int i;
...@@ -3883,17 +3887,18 @@ static int set_ssl_option(struct mg_context *ctx) { ...@@ -3883,17 +3887,18 @@ static int set_ssl_option(struct mg_context *ctx) {
return 0; return 0;
} }
if (ctx->user_callback != NULL) { // If user callback returned non-NULL, that means that user callback has
ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) ctx->ssl_ctx); // 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 ||
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)) {
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()); cry(fc(ctx), "%s: cannot open %s: %s", __func__, pem, ssl_error());
return 0; 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. // Initialize locking callbacks, needed for thread safety.
// http://www.openssl.org/support/faq.html#PROG1 // 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, ...@@ -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. // be initialized before listening ports. UID must be set last.
if (!set_gpass_option(ctx) || if (!set_gpass_option(ctx) ||
#if !defined(NO_SSL) #if !defined(NO_SSL)
(ctx->config[SSL_CERTIFICATE] != NULL && !set_ssl_option(ctx)) || !set_ssl_option(ctx) ||
#endif #endif
!set_ports_option(ctx) || !set_ports_option(ctx) ||
#if !defined(_WIN32) #if !defined(_WIN32)
......
...@@ -154,6 +154,7 @@ const struct mg_request_info *mg_get_request_info(const struct mg_connection *); ...@@ -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 *); void *mg_get_user_data(struct mg_connection *);
const char *mg_get_log_message(const struct mg_connection *); const char *mg_get_log_message(const struct mg_connection *);
int mg_get_reply_status_code(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. // Send data to the client.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment