Blame SOURCES/httpd-2.4.37-sslkeylogfile-support.patch

fa0499
diff --git a/modules/ssl/ssl_engine_config.c b/modules/ssl/ssl_engine_config.c
fa0499
index 1d201d9..0c4bf1f 100644
fa0499
--- a/modules/ssl/ssl_engine_config.c
fa0499
+++ b/modules/ssl/ssl_engine_config.c
fa0499
@@ -75,6 +75,10 @@ SSLModConfigRec *ssl_config_global_create(server_rec *s)
fa0499
     mc->stapling_refresh_mutex = NULL;
fa0499
 #endif
fa0499
 
fa0499
+#ifdef HAVE_OPENSSL_KEYLOG
fa0499
+    mc->keylog_file = NULL;
fa0499
+#endif
fa0499
+
fa0499
     apr_pool_userdata_set(mc, SSL_MOD_CONFIG_KEY,
fa0499
                           apr_pool_cleanup_null,
fa0499
                           pool);
fa0499
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
fa0499
index ef631c1..b286053 100644
fa0499
--- a/modules/ssl/ssl_engine_init.c
fa0499
+++ b/modules/ssl/ssl_engine_init.c
fa0499
@@ -437,6 +437,28 @@ apr_status_t ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
fa0499
     init_bio_methods();
fa0499
 #endif
fa0499
 
fa0499
+#ifdef HAVE_OPENSSL_KEYLOG
fa0499
+    {
fa0499
+        const char *logfn = getenv("SSLKEYLOGFILE");
fa0499
+
fa0499
+        if (logfn) {
fa0499
+            rv = apr_file_open(&mc->keylog_file, logfn,
fa0499
+                               APR_FOPEN_CREATE|APR_FOPEN_WRITE|APR_FOPEN_APPEND|APR_FOPEN_LARGEFILE,
fa0499
+                               APR_FPROT_UREAD|APR_FPROT_UWRITE,
fa0499
+                               mc->pPool);
fa0499
+            if (rv) {
fa0499
+                ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, s, APLOGNO(10226)
fa0499
+                             "Could not open log file '%s' configured via SSLKEYLOGFILE",
fa0499
+                             logfn);
fa0499
+                return rv;
fa0499
+            }
fa0499
+
fa0499
+            ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, APLOGNO(10227)
fa0499
+                         "Init: Logging SSL private key material to %s", logfn);
fa0499
+        }
fa0499
+    }
fa0499
+#endif
fa0499
+    
fa0499
     return OK;
fa0499
 }
fa0499
 
fa0499
@@ -796,6 +818,12 @@ static apr_status_t ssl_init_ctx_protocol(server_rec *s,
fa0499
      * https://github.com/openssl/openssl/issues/7178 */
fa0499
     SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
fa0499
 #endif
fa0499
+
fa0499
+#ifdef HAVE_OPENSSL_KEYLOG
fa0499
+    if (mctx->sc->mc->keylog_file) {
fa0499
+        SSL_CTX_set_keylog_callback(ctx, modssl_callback_keylog);
fa0499
+    }
fa0499
+#endif
fa0499
     
fa0499
     return APR_SUCCESS;
fa0499
 }
fa0499
diff --git a/modules/ssl/ssl_engine_kernel.c b/modules/ssl/ssl_engine_kernel.c
fa0499
index 6611610..7058865 100644
fa0499
--- a/modules/ssl/ssl_engine_kernel.c
fa0499
+++ b/modules/ssl/ssl_engine_kernel.c
fa0499
@@ -2719,3 +2719,17 @@ int ssl_callback_SRPServerParams(SSL *ssl, int *ad, void *arg)
fa0499
 }
fa0499
 
fa0499
 #endif /* HAVE_SRP */
fa0499
+
fa0499
+
fa0499
+#ifdef HAVE_OPENSSL_KEYLOG
fa0499
+/* Callback used with SSL_CTX_set_keylog_callback. */
fa0499
+void modssl_callback_keylog(const SSL *ssl, const char *line)
fa0499
+{
fa0499
+    conn_rec *conn = SSL_get_app_data(ssl);
fa0499
+    SSLSrvConfigRec *sc = mySrvConfig(conn->base_server);
fa0499
+
fa0499
+    if (sc && sc->mc->keylog_file) {
fa0499
+        apr_file_printf(sc->mc->keylog_file, "%s\n", line);
fa0499
+    }
fa0499
+}
fa0499
+#endif
fa0499
diff --git a/modules/ssl/ssl_private.h b/modules/ssl/ssl_private.h
fa0499
index 0fac5d1..2514407 100644
fa0499
--- a/modules/ssl/ssl_private.h
fa0499
+++ b/modules/ssl/ssl_private.h
fa0499
@@ -250,6 +250,10 @@ void free_bio_methods(void);
fa0499
 #endif
fa0499
 #endif
fa0499
 
fa0499
+#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
fa0499
+#define HAVE_OPENSSL_KEYLOG
fa0499
+#endif
fa0499
+
fa0499
 /* mod_ssl headers */
fa0499
 #include "ssl_util_ssl.h"
fa0499
 
fa0499
@@ -617,6 +621,12 @@ typedef struct {
fa0499
     apr_global_mutex_t   *stapling_cache_mutex;
fa0499
     apr_global_mutex_t   *stapling_refresh_mutex;
fa0499
 #endif
fa0499
+
fa0499
+#ifdef HAVE_OPENSSL_KEYLOG
fa0499
+    /* Used for logging if SSLKEYLOGFILE is set at startup. */
fa0499
+    apr_file_t      *keylog_file;
fa0499
+#endif
fa0499
+
fa0499
 } SSLModConfigRec;
fa0499
 
fa0499
 /** Structure representing configured filenames for certs and keys for
fa0499
@@ -970,6 +980,11 @@ int          ssl_stapling_init_cert(server_rec *, apr_pool_t *, apr_pool_t *,
fa0499
 int          ssl_callback_SRPServerParams(SSL *, int *, void *);
fa0499
 #endif
fa0499
 
fa0499
+#ifdef HAVE_OPENSSL_KEYLOG
fa0499
+/* Callback used with SSL_CTX_set_keylog_callback. */
fa0499
+void         modssl_callback_keylog(const SSL *ssl, const char *line);
fa0499
+#endif
fa0499
+
fa0499
 /**  I/O  */
fa0499
 void         ssl_io_filter_init(conn_rec *, request_rec *r, SSL *);
fa0499
 void         ssl_io_filter_register(apr_pool_t *);