Browse Source

added mg_get_ssl_context. passing fake connection to the MG_INIT_SSL event

Sergey Lyubka 12 years ago
parent
commit
9aad922983
2 changed files with 14 additions and 8 deletions
  1. 13 8
      mongoose.c
  2. 1 0
      mongoose.h

+ 13 - 8
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)

+ 1 - 0
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.