Blame SOURCES/httpd-2.4.35-r1830819+.patch

59234c
# ./pullrev.sh 1830819 1830836 1830912 1830913 1830927 1831168 1831173
59234c
59234c
http://svn.apache.org/viewvc?view=revision&revision=1830819
59234c
http://svn.apache.org/viewvc?view=revision&revision=1830912
59234c
http://svn.apache.org/viewvc?view=revision&revision=1830913
59234c
http://svn.apache.org/viewvc?view=revision&revision=1830927
59234c
http://svn.apache.org/viewvc?view=revision&revision=1831168
59234c
http://svn.apache.org/viewvc?view=revision&revision=1831173
59234c
http://svn.apache.org/viewvc?view=revision&revision=1835240
59234c
http://svn.apache.org/viewvc?view=revision&revision=1835242
59234c
http://svn.apache.org/viewvc?view=revision&revision=1835615
59234c
59234c
diff --git a/modules/ssl/ssl_engine_config.c b/modules/ssl/ssl_engine_config.c
59234c
index 43397f9..ff8f429 100644
59234c
--- httpd-2.4.35/modules/ssl/ssl_engine_config.c.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_engine_config.c
59234c
@@ -899,7 +899,9 @@
59234c
     SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
59234c
     const char *err;
59234c
 
59234c
-    if ((err = ssl_cmd_check_file(cmd, &arg))) {
59234c
+    /* Only check for non-ENGINE based certs. */
59234c
+    if (!modssl_is_engine_id(arg)
59234c
+        && (err = ssl_cmd_check_file(cmd, &arg))) {
59234c
         return err;
59234c
     }
59234c
 
59234c
@@ -915,7 +917,9 @@
59234c
     SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
59234c
     const char *err;
59234c
 
59234c
-    if ((err = ssl_cmd_check_file(cmd, &arg))) {
59234c
+    /* Check keyfile exists for non-ENGINE keys. */
59234c
+    if (!modssl_is_engine_id(arg)
59234c
+        && (err = ssl_cmd_check_file(cmd, &arg))) {
59234c
         return err;
59234c
     }
59234c
 
59234c
--- httpd-2.4.35/modules/ssl/ssl_engine_init.c.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_engine_init.c
59234c
@@ -1186,12 +1186,18 @@
59234c
                 (certfile = APR_ARRAY_IDX(mctx->pks->cert_files, i,
59234c
                                           const char *));
59234c
          i++) {
59234c
+        EVP_PKEY *pkey;
59234c
+        const char *engine_certfile = NULL;
59234c
+
59234c
         key_id = apr_psprintf(ptemp, "%s:%d", vhost_id, i);
59234c
 
59234c
         ERR_clear_error();
59234c
 
59234c
         /* first the certificate (public key) */
59234c
-        if (mctx->cert_chain) {
59234c
+        if (modssl_is_engine_id(certfile)) {
59234c
+            engine_certfile = certfile;
59234c
+        }
59234c
+        else if (mctx->cert_chain) {
59234c
             if ((SSL_CTX_use_certificate_file(mctx->ssl_ctx, certfile,
59234c
                                               SSL_FILETYPE_PEM) < 1)) {
59234c
                 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02561)
59234c
@@ -1220,12 +1226,46 @@
59234c
 
59234c
         ERR_clear_error();
59234c
 
59234c
-        if ((SSL_CTX_use_PrivateKey_file(mctx->ssl_ctx, keyfile,
59234c
-                                         SSL_FILETYPE_PEM) < 1) &&
59234c
-            (ERR_GET_FUNC(ERR_peek_last_error())
59234c
-                != X509_F_X509_CHECK_PRIVATE_KEY)) {
59234c
+        if (modssl_is_engine_id(keyfile)) {
59234c
+            apr_status_t rv;
59234c
+
59234c
+            cert = NULL;
59234c
+            
59234c
+            if ((rv = modssl_load_engine_keypair(s, ptemp, vhost_id,
59234c
+                                                 engine_certfile, keyfile,
59234c
+                                                 &cert, &pkey))) {
59234c
+                return rv;
59234c
+            }
59234c
+
59234c
+            if (cert) {
59234c
+                if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) < 1) {
59234c
+                    ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10137)
59234c
+                                 "Failed to configure engine certificate %s, check %s",
59234c
+                                 key_id, certfile);
59234c
+                    ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
59234c
+                    return APR_EGENERAL;
59234c
+                }
59234c
+
59234c
+                /* SSL_CTX now owns the cert. */
59234c
+                X509_free(cert);
59234c
+            }                    
59234c
+            
59234c
+            if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) < 1) {
59234c
+                ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10130)
59234c
+                             "Failed to configure private key %s from engine",
59234c
+                             keyfile);
59234c
+                ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
59234c
+                return APR_EGENERAL;
59234c
+            }
59234c
+
59234c
+            /* SSL_CTX now owns the key */
59234c
+            EVP_PKEY_free(pkey);
59234c
+        }
59234c
+        else if ((SSL_CTX_use_PrivateKey_file(mctx->ssl_ctx, keyfile,
59234c
+                                              SSL_FILETYPE_PEM) < 1)
59234c
+                 && (ERR_GET_FUNC(ERR_peek_last_error())
59234c
+                     != X509_F_X509_CHECK_PRIVATE_KEY)) {
59234c
             ssl_asn1_t *asn1;
59234c
-            EVP_PKEY *pkey;
59234c
             const unsigned char *ptr;
59234c
 
59234c
             ERR_clear_error();
59234c
@@ -1312,8 +1352,9 @@
59234c
     /*
59234c
      * Try to read DH parameters from the (first) SSLCertificateFile
59234c
      */
59234c
-    if ((certfile = APR_ARRAY_IDX(mctx->pks->cert_files, 0, const char *)) &&
59234c
-        (dhparams = ssl_dh_GetParamFromFile(certfile))) {
59234c
+    certfile = APR_ARRAY_IDX(mctx->pks->cert_files, 0, const char *);
59234c
+    if (certfile && !modssl_is_engine_id(certfile)
59234c
+        && (dhparams = ssl_dh_GetParamFromFile(certfile))) {
59234c
         SSL_CTX_set_tmp_dh(mctx->ssl_ctx, dhparams);
59234c
         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02540)
59234c
                      "Custom DH parameters (%d bits) for %s loaded from %s",
59234c
@@ -1325,10 +1366,10 @@
59234c
     /*
59234c
      * Similarly, try to read the ECDH curve name from SSLCertificateFile...
59234c
      */
59234c
-    if ((certfile != NULL) && 
59234c
-        (ecparams = ssl_ec_GetParamFromFile(certfile)) &&
59234c
-        (nid = EC_GROUP_get_curve_name(ecparams)) &&
59234c
-        (eckey = EC_KEY_new_by_curve_name(nid))) {
59234c
+    if (certfile && !modssl_is_engine_id(certfile)
59234c
+        && (ecparams = ssl_ec_GetParamFromFile(certfile))
59234c
+        && (nid = EC_GROUP_get_curve_name(ecparams)) 
59234c
+        && (eckey = EC_KEY_new_by_curve_name(nid))) {
59234c
         SSL_CTX_set_tmp_ecdh(mctx->ssl_ctx, eckey);
59234c
         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02541)
59234c
                      "ECDH curve %s for %s specified in %s",
59234c
--- httpd-2.4.35/modules/ssl/ssl_engine_pphrase.c.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_engine_pphrase.c
59234c
@@ -143,9 +143,6 @@
59234c
     const char *key_id = asn1_table_vhost_key(mc, p, sc->vhost_id, idx);
59234c
     EVP_PKEY *pPrivateKey = NULL;
59234c
     ssl_asn1_t *asn1;
59234c
-    unsigned char *ucp;
59234c
-    long int length;
59234c
-    BOOL bReadable;
59234c
     int nPassPhrase = (*pphrases)->nelts;
59234c
     int nPassPhraseRetry = 0;
59234c
     apr_time_t pkey_mtime = 0;
59234c
@@ -222,16 +219,12 @@
59234c
          * is not empty. */
59234c
         ERR_clear_error();
59234c
 
59234c
-        bReadable = ((pPrivateKey = modssl_read_privatekey(ppcb_arg.pkey_file,
59234c
-                     NULL, ssl_pphrase_Handle_CB, &ppcb_arg)) != NULL ?
59234c
-                     TRUE : FALSE);
59234c
-
59234c
-        /*
59234c
-         * when the private key file now was readable,
59234c
-         * it's fine and we go out of the loop
59234c
-         */
59234c
-        if (bReadable)
59234c
-           break;
59234c
+        pPrivateKey = modssl_read_privatekey(ppcb_arg.pkey_file,
59234c
+                                             ssl_pphrase_Handle_CB, &ppcb_arg);
59234c
+        /* If the private key was successfully read, nothing more to
59234c
+           do here. */
59234c
+        if (pPrivateKey != NULL)
59234c
+            break;
59234c
 
59234c
         /*
59234c
          * when we have more remembered pass phrases
59234c
@@ -356,19 +349,12 @@
59234c
         nPassPhrase++;
59234c
     }
59234c
 
59234c
-    /*
59234c
-     * Insert private key into the global module configuration
59234c
-     * (we convert it to a stand-alone DER byte sequence
59234c
-     * because the SSL library uses static variables inside a
59234c
-     * RSA structure which do not survive DSO reloads!)
59234c
-     */
59234c
-    length = i2d_PrivateKey(pPrivateKey, NULL);
59234c
-    ucp = ssl_asn1_table_set(mc->tPrivateKey, key_id, length);
59234c
-    (void)i2d_PrivateKey(pPrivateKey, &ucp;; /* 2nd arg increments */
59234c
+    /* Cache the private key in the global module configuration so it
59234c
+     * can be used after subsequent reloads. */
59234c
+    asn1 = ssl_asn1_table_set(mc->tPrivateKey, key_id, pPrivateKey);
59234c
 
59234c
     if (ppcb_arg.nPassPhraseDialogCur != 0) {
59234c
         /* remember mtime of encrypted keys */
59234c
-        asn1 = ssl_asn1_table_get(mc->tPrivateKey, key_id);
59234c
         asn1->source_mtime = pkey_mtime;
59234c
     }
59234c
 
59234c
@@ -619,3 +605,303 @@
59234c
      */
59234c
     return (len);
59234c
 }
59234c
+
59234c
+
59234c
+#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
59234c
+
59234c
+/* OpenSSL UI implementation for passphrase entry; largely duplicated
59234c
+ * from ssl_pphrase_Handle_CB but adjusted for UI API. TODO: Might be
59234c
+ * worth trying to shift pphrase handling over to the UI API
59234c
+ * completely. */
59234c
+static int passphrase_ui_open(UI *ui)
59234c
+{
59234c
+    pphrase_cb_arg_t *ppcb = UI_get0_user_data(ui);
59234c
+    SSLSrvConfigRec *sc = mySrvConfig(ppcb->s);
59234c
+
59234c
+    ppcb->nPassPhraseDialog++;
59234c
+    ppcb->nPassPhraseDialogCur++;
59234c
+
59234c
+    /*
59234c
+     * Builtin or Pipe dialog
59234c
+     */
59234c
+    if (sc->server->pphrase_dialog_type == SSL_PPTYPE_BUILTIN
59234c
+        || sc->server->pphrase_dialog_type == SSL_PPTYPE_PIPE) {
59234c
+        if (sc->server->pphrase_dialog_type == SSL_PPTYPE_PIPE) {
59234c
+            if (!readtty) {
59234c
+                ap_log_error(APLOG_MARK, APLOG_INFO, 0, ppcb->s,
59234c
+                             APLOGNO(10143)
59234c
+                             "Init: Creating pass phrase dialog pipe child "
59234c
+                             "'%s'", sc->server->pphrase_dialog_path);
59234c
+                if (ssl_pipe_child_create(ppcb->p,
59234c
+                            sc->server->pphrase_dialog_path)
59234c
+                        != APR_SUCCESS) {
59234c
+                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, ppcb->s,
59234c
+                                 APLOGNO(10144)
59234c
+                                 "Init: Failed to create pass phrase pipe '%s'",
59234c
+                                 sc->server->pphrase_dialog_path);
59234c
+                    return 0;
59234c
+                }
59234c
+            }
59234c
+            ap_log_error(APLOG_MARK, APLOG_INFO, 0, ppcb->s, APLOGNO(10145)
59234c
+                         "Init: Requesting pass phrase via piped dialog");
59234c
+        }
59234c
+        else { /* sc->server->pphrase_dialog_type == SSL_PPTYPE_BUILTIN */
59234c
+#ifdef WIN32
59234c
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, ppcb->s, APLOGNO(10146)
59234c
+                         "Init: Failed to create pass phrase pipe '%s'",
59234c
+                         sc->server->pphrase_dialog_path);
59234c
+            return 0;
59234c
+#else
59234c
+            /*
59234c
+             * stderr has already been redirected to the error_log.
59234c
+             * rather than attempting to temporarily rehook it to the terminal,
59234c
+             * we print the prompt to stdout before EVP_read_pw_string turns
59234c
+             * off tty echo
59234c
+             */
59234c
+            apr_file_open_stdout(&writetty, ppcb->p);
59234c
+
59234c
+            ap_log_error(APLOG_MARK, APLOG_INFO, 0, ppcb->s, APLOGNO(10147)
59234c
+                         "Init: Requesting pass phrase via builtin terminal "
59234c
+                         "dialog");
59234c
+#endif
59234c
+        }
59234c
+
59234c
+        /*
59234c
+         * The first time display a header to inform the user about what
59234c
+         * program he actually speaks to, which module is responsible for
59234c
+         * this terminal dialog and why to the hell he has to enter
59234c
+         * something...
59234c
+         */
59234c
+        if (ppcb->nPassPhraseDialog == 1) {
59234c
+            apr_file_printf(writetty, "%s mod_ssl (Pass Phrase Dialog)\n",
59234c
+                            AP_SERVER_BASEVERSION);
59234c
+            apr_file_printf(writetty,
59234c
+                            "A pass phrase is required to access the private key.\n");
59234c
+        }
59234c
+        if (ppcb->bPassPhraseDialogOnce) {
59234c
+            ppcb->bPassPhraseDialogOnce = FALSE;
59234c
+            apr_file_printf(writetty, "\n");
59234c
+            apr_file_printf(writetty, "Private key %s (%s)\n",
59234c
+                            ppcb->key_id, ppcb->pkey_file);
59234c
+        }
59234c
+    }
59234c
+
59234c
+    return 1;
59234c
+}
59234c
+
59234c
+static int passphrase_ui_read(UI *ui, UI_STRING *uis)
59234c
+{
59234c
+    pphrase_cb_arg_t *ppcb = UI_get0_user_data(ui);
59234c
+    SSLSrvConfigRec *sc = mySrvConfig(ppcb->s);
59234c
+    const char *prompt;
59234c
+    int i;
59234c
+    int bufsize;
59234c
+    int len;
59234c
+    char *buf;
59234c
+
59234c
+    prompt = UI_get0_output_string(uis);
59234c
+    if (prompt == NULL) {
59234c
+        prompt = "Enter pass phrase:";
59234c
+    }
59234c
+
59234c
+    /*
59234c
+     * Get the maximum expected size and allocate the buffer
59234c
+     */
59234c
+    bufsize = UI_get_result_maxsize(uis);
59234c
+    buf = apr_pcalloc(ppcb->p, bufsize);
59234c
+
59234c
+    if (sc->server->pphrase_dialog_type == SSL_PPTYPE_BUILTIN
59234c
+        || sc->server->pphrase_dialog_type == SSL_PPTYPE_PIPE) {
59234c
+        /*
59234c
+         * Get the pass phrase through a callback.
59234c
+         * Empty input is not accepted.
59234c
+         */
59234c
+        for (;;) {
59234c
+            if (sc->server->pphrase_dialog_type == SSL_PPTYPE_PIPE) {
59234c
+                i = pipe_get_passwd_cb(buf, bufsize, "", FALSE);
59234c
+            }
59234c
+            else { /* sc->server->pphrase_dialog_type == SSL_PPTYPE_BUILTIN */
59234c
+                i = EVP_read_pw_string(buf, bufsize, "", FALSE);
59234c
+            }
59234c
+            if (i != 0) {
59234c
+                OPENSSL_cleanse(buf, bufsize);
59234c
+                return 0;
59234c
+            }
59234c
+            len = strlen(buf);
59234c
+            if (len < 1){
59234c
+                apr_file_printf(writetty, "Apache:mod_ssl:Error: Pass phrase"
59234c
+                                "empty (needs to be at least 1 character).\n");
59234c
+                apr_file_puts(prompt, writetty);
59234c
+            }
59234c
+            else {
59234c
+                break;
59234c
+            }
59234c
+        }
59234c
+    }
59234c
+    /*
59234c
+     * Filter program
59234c
+     */
59234c
+    else if (sc->server->pphrase_dialog_type == SSL_PPTYPE_FILTER) {
59234c
+        const char *cmd = sc->server->pphrase_dialog_path;
59234c
+        const char **argv = apr_palloc(ppcb->p, sizeof(char *) * 3);
59234c
+        char *result;
59234c
+
59234c
+        ap_log_error(APLOG_MARK, APLOG_INFO, 0, ppcb->s, APLOGNO(10148)
59234c
+                     "Init: Requesting pass phrase from dialog filter "
59234c
+                     "program (%s)", cmd);
59234c
+
59234c
+        argv[0] = cmd;
59234c
+        argv[1] = ppcb->key_id;
59234c
+        argv[2] = NULL;
59234c
+
59234c
+        result = ssl_util_readfilter(ppcb->s, ppcb->p, cmd, argv);
59234c
+        apr_cpystrn(buf, result, bufsize);
59234c
+        len = strlen(buf);
59234c
+    }
59234c
+
59234c
+    /*
59234c
+     * Ok, we now have the pass phrase, so give it back
59234c
+     */
59234c
+    ppcb->cpPassPhraseCur = apr_pstrdup(ppcb->p, buf);
59234c
+    UI_set_result(ui, uis, buf);
59234c
+
59234c
+    /* Clear sensitive data. */
59234c
+    OPENSSL_cleanse(buf, bufsize);
59234c
+    return 1;
59234c
+}
59234c
+
59234c
+static int passphrase_ui_write(UI *ui, UI_STRING *uis)
59234c
+{
59234c
+    pphrase_cb_arg_t *ppcb = UI_get0_user_data(ui);
59234c
+    SSLSrvConfigRec *sc;
59234c
+    const char *prompt;
59234c
+
59234c
+    sc = mySrvConfig(ppcb->s);
59234c
+
59234c
+    if (sc->server->pphrase_dialog_type == SSL_PPTYPE_BUILTIN
59234c
+        || sc->server->pphrase_dialog_type == SSL_PPTYPE_PIPE) {
59234c
+        prompt = UI_get0_output_string(uis);
59234c
+        apr_file_puts(prompt, writetty);
59234c
+    }
59234c
+
59234c
+    return 1;
59234c
+}
59234c
+
59234c
+static int passphrase_ui_close(UI *ui)
59234c
+{
59234c
+    /*
59234c
+     * Close the pipes if they were opened
59234c
+     */
59234c
+    if (readtty) {
59234c
+        apr_file_close(readtty);
59234c
+        apr_file_close(writetty);
59234c
+        readtty = writetty = NULL;
59234c
+    }
59234c
+    return 1;
59234c
+}
59234c
+
59234c
+static apr_status_t pp_ui_method_cleanup(void *uip)
59234c
+{
59234c
+    UI_METHOD *uim = uip;
59234c
+    
59234c
+    UI_destroy_method(uim);
59234c
+
59234c
+    return APR_SUCCESS;
59234c
+}
59234c
+
59234c
+static UI_METHOD *get_passphrase_ui(apr_pool_t *p)
59234c
+{
59234c
+    UI_METHOD *ui_method = UI_create_method("Passphrase UI");
59234c
+
59234c
+    UI_method_set_opener(ui_method, passphrase_ui_open);
59234c
+    UI_method_set_reader(ui_method, passphrase_ui_read);
59234c
+    UI_method_set_writer(ui_method, passphrase_ui_write);
59234c
+    UI_method_set_closer(ui_method, passphrase_ui_close);
59234c
+
59234c
+    apr_pool_cleanup_register(p, ui_method, pp_ui_method_cleanup,
59234c
+                              pp_ui_method_cleanup);
59234c
+    
59234c
+    return ui_method;
59234c
+}
59234c
+
59234c
+
59234c
+apr_status_t modssl_load_engine_keypair(server_rec *s, apr_pool_t *p,
59234c
+                                        const char *vhostid,
59234c
+                                        const char *certid, const char *keyid,
59234c
+                                        X509 **pubkey, EVP_PKEY **privkey)
59234c
+{
59234c
+    const char *c, *scheme;
59234c
+    ENGINE *e;
59234c
+    UI_METHOD *ui_method = get_passphrase_ui(p);
59234c
+    pphrase_cb_arg_t ppcb;
59234c
+
59234c
+    memset(&ppcb, 0, sizeof ppcb);
59234c
+    ppcb.s = s;
59234c
+    ppcb.p = p;
59234c
+    ppcb.bPassPhraseDialogOnce = TRUE;
59234c
+    ppcb.key_id = vhostid;
59234c
+    ppcb.pkey_file = keyid;
59234c
+
59234c
+    c = ap_strchr_c(keyid, ':');
59234c
+    if (!c || c == keyid) {
59234c
+        ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10131)
59234c
+                     "Init: Unrecognized private key identifier `%s'",
59234c
+                     keyid);
59234c
+        return ssl_die(s);
59234c
+    }
59234c
+
59234c
+    scheme = apr_pstrmemdup(p, keyid, c - keyid);
59234c
+    if (!(e = ENGINE_by_id(scheme))) {
59234c
+        ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10132)
59234c
+                     "Init: Failed to load engine for private key %s",
59234c
+                     keyid);
59234c
+        ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
59234c
+        return ssl_die(s);
59234c
+    }
59234c
+
59234c
+    if (!ENGINE_init(e)) {
59234c
+        ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10149)
59234c
+                     "Init: Failed to initialize engine %s for private key %s",
59234c
+                     scheme, keyid);
59234c
+        ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
59234c
+        return ssl_die(s);
59234c
+    }
59234c
+
59234c
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, 
59234c
+                 "Init: Initialized engine %s for private key %s",
59234c
+                 scheme, keyid);
59234c
+
59234c
+    if (APLOGdebug(s)) {
59234c
+        ENGINE_ctrl_cmd_string(e, "VERBOSE", NULL, 0);
59234c
+    }
59234c
+
59234c
+    if (certid) {
59234c
+        struct {
59234c
+            const char *cert_id;
59234c
+            X509 *cert;
59234c
+        } params = { certid, NULL };
59234c
+
59234c
+        if (!ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &params, NULL, 1)) {
59234c
+            ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10136)
59234c
+                         "Init: Unable to get the certificate");
59234c
+            ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
59234c
+            return ssl_die(s);
59234c
+        }
59234c
+
59234c
+        *pubkey = params.cert;
59234c
+    }
59234c
+
59234c
+    *privkey = ENGINE_load_private_key(e, keyid, ui_method, &ppcb);
59234c
+    if (*privkey == NULL) {
59234c
+        ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10133)
59234c
+                     "Init: Unable to get the private key");
59234c
+        ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
59234c
+        return ssl_die(s);
59234c
+    }
59234c
+
59234c
+    ENGINE_finish(e);
59234c
+    ENGINE_free(e);
59234c
+
59234c
+    return APR_SUCCESS;
59234c
+}
59234c
+#endif
59234c
--- httpd-2.4.35/modules/ssl/ssl_private.h.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_private.h
59234c
@@ -986,21 +986,28 @@
59234c
 apr_status_t ssl_load_encrypted_pkey(server_rec *, apr_pool_t *, int,
59234c
                                      const char *, apr_array_header_t **);
59234c
 
59234c
+/* Load public and/or private key from the configured ENGINE. Private
59234c
+ * key returned as *pkey.  certid can be NULL, in which case *pubkey
59234c
+ * is not altered.  Errors logged on failure. */
59234c
+apr_status_t modssl_load_engine_keypair(server_rec *s, apr_pool_t *p,
59234c
+                                        const char *vhostid,
59234c
+                                        const char *certid, const char *keyid,
59234c
+                                        X509 **pubkey, EVP_PKEY **privkey);
59234c
+
59234c
 /**  Diffie-Hellman Parameter Support  */
59234c
 DH           *ssl_dh_GetParamFromFile(const char *);
59234c
 #ifdef HAVE_ECC
59234c
 EC_GROUP     *ssl_ec_GetParamFromFile(const char *);
59234c
 #endif
59234c
 
59234c
-unsigned char *ssl_asn1_table_set(apr_hash_t *table,
59234c
-                                  const char *key,
59234c
-                                  long int length);
59234c
-
59234c
-ssl_asn1_t *ssl_asn1_table_get(apr_hash_t *table,
59234c
-                               const char *key);
59234c
-
59234c
-void ssl_asn1_table_unset(apr_hash_t *table,
59234c
-                          const char *key);
59234c
+/* Store the EVP_PKEY key (serialized into DER) in the hash table with
59234c
+ * key, returning the ssl_asn1_t structure pointer. */
59234c
+ssl_asn1_t *ssl_asn1_table_set(apr_hash_t *table, const char *key,
59234c
+                               EVP_PKEY *pkey);
59234c
+/* Retrieve the ssl_asn1_t structure with given key from the hash. */
59234c
+ssl_asn1_t *ssl_asn1_table_get(apr_hash_t *table, const char *key);
59234c
+/* Remove and free the ssl_asn1_t structure with given key. */
59234c
+void ssl_asn1_table_unset(apr_hash_t *table, const char *key);
59234c
 
59234c
 /**  Mutex Support  */
59234c
 int          ssl_mutex_init(server_rec *, apr_pool_t *);
59234c
@@ -1088,6 +1095,10 @@
59234c
 int ssl_is_challenge(conn_rec *c, const char *servername, 
59234c
                      X509 **pcert, EVP_PKEY **pkey);
59234c
 
59234c
+/* Returns non-zero if the cert/key filename should be handled through
59234c
+ * the configured ENGINE. */
59234c
+int modssl_is_engine_id(const char *name);
59234c
+
59234c
 #endif /* SSL_PRIVATE_H */
59234c
 /** @} */
59234c
 
59234c
--- httpd-2.4.35/modules/ssl/ssl_util.c.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_util.c
59234c
@@ -175,45 +175,37 @@
59234c
     return TRUE;
59234c
 }
59234c
 
59234c
-/*
59234c
- * certain key data needs to survive restarts,
59234c
- * which are stored in the user data table of s->process->pool.
59234c
- * to prevent "leaking" of this data, we use malloc/free
59234c
- * rather than apr_palloc and these wrappers to help make sure
59234c
- * we do not leak the malloc-ed data.
59234c
- */
59234c
-unsigned char *ssl_asn1_table_set(apr_hash_t *table,
59234c
-                                  const char *key,
59234c
-                                  long int length)
59234c
+/* Decrypted private keys are cached to survive restarts.  The cached
59234c
+ * data must have lifetime of the process (hence malloc/free rather
59234c
+ * than pools), and uses raw DER since the EVP_PKEY structure
59234c
+ * internals may not survive across a module reload. */
59234c
+ssl_asn1_t *ssl_asn1_table_set(apr_hash_t *table, const char *key,
59234c
+                               EVP_PKEY *pkey)
59234c
 {
59234c
     apr_ssize_t klen = strlen(key);
59234c
     ssl_asn1_t *asn1 = apr_hash_get(table, key, klen);
59234c
+    apr_size_t length = i2d_PrivateKey(pkey, NULL);
59234c
+    unsigned char *p;
59234c
 
59234c
-    /*
59234c
-     * if a value for this key already exists,
59234c
-     * reuse as much of the already malloc-ed data
59234c
-     * as possible.
59234c
-     */
59234c
+    /* Re-use structure if cached previously. */
59234c
     if (asn1) {
59234c
         if (asn1->nData != length) {
59234c
-            free(asn1->cpData); /* XXX: realloc? */
59234c
-            asn1->cpData = NULL;
59234c
+            asn1->cpData = ap_realloc(asn1->cpData, length);
59234c
         }
59234c
     }
59234c
     else {
59234c
         asn1 = ap_malloc(sizeof(*asn1));
59234c
         asn1->source_mtime = 0; /* used as a note for encrypted private keys */
59234c
-        asn1->cpData = NULL;
59234c
-    }
59234c
-
59234c
-    asn1->nData = length;
59234c
-    if (!asn1->cpData) {
59234c
         asn1->cpData = ap_malloc(length);
59234c
+
59234c
+        apr_hash_set(table, key, klen, asn1);
59234c
     }
59234c
 
59234c
-    apr_hash_set(table, key, klen, asn1);
59234c
+    asn1->nData = length;
59234c
+    p = asn1->cpData;
59234c
+    i2d_PrivateKey(pkey, &p); /* increases p by length */
59234c
 
59234c
-    return asn1->cpData; /* caller will assign a value to this */
59234c
+    return asn1;
59234c
 }
59234c
 
59234c
 ssl_asn1_t *ssl_asn1_table_get(apr_hash_t *table,
59234c
@@ -463,3 +455,13 @@
59234c
 }
59234c
 
59234c
 #endif /* #if APR_HAS_THREADS && MODSSL_USE_OPENSSL_PRE_1_1_API */
59234c
+
59234c
+int modssl_is_engine_id(const char *name)
59234c
+{
59234c
+#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
59234c
+    /* ### Can handle any other special ENGINE key names here? */
59234c
+    return strncmp(name, "pkcs11:", 7) == 0;
59234c
+#else
59234c
+    return 0;
59234c
+#endif
59234c
+}
59234c
--- httpd-2.4.35/modules/ssl/ssl_util_ssl.c.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_util_ssl.c
59234c
@@ -74,7 +74,7 @@
59234c
 **  _________________________________________________________________
59234c
 */
59234c
 
59234c
-EVP_PKEY *modssl_read_privatekey(const char* filename, EVP_PKEY **key, pem_password_cb *cb, void *s)
59234c
+EVP_PKEY *modssl_read_privatekey(const char *filename, pem_password_cb *cb, void *s)
59234c
 {
59234c
     EVP_PKEY *rc;
59234c
     BIO *bioS;
59234c
@@ -83,7 +83,7 @@
59234c
     /* 1. try PEM (= DER+Base64+headers) */
59234c
     if ((bioS=BIO_new_file(filename, "r")) == NULL)
59234c
         return NULL;
59234c
-    rc = PEM_read_bio_PrivateKey(bioS, key, cb, s);
59234c
+    rc = PEM_read_bio_PrivateKey(bioS, NULL, cb, s);
59234c
     BIO_free(bioS);
59234c
 
59234c
     if (rc == NULL) {
59234c
@@ -107,41 +107,9 @@
59234c
             BIO_free(bioS);
59234c
         }
59234c
     }
59234c
-    if (rc != NULL && key != NULL) {
59234c
-        if (*key != NULL)
59234c
-            EVP_PKEY_free(*key);
59234c
-        *key = rc;
59234c
-    }
59234c
     return rc;
59234c
 }
59234c
 
59234c
-typedef struct {
59234c
-    const char *pass;
59234c
-    int pass_len;
59234c
-} pass_ctx;
59234c
-
59234c
-static int provide_pass(char *buf, int size, int rwflag, void *baton)
59234c
-{
59234c
-    pass_ctx *ctx = baton;
59234c
-    if (ctx->pass_len > 0) {
59234c
-        if (ctx->pass_len < size) {
59234c
-            size = (int)ctx->pass_len;
59234c
-        }
59234c
-        memcpy(buf, ctx->pass, size);
59234c
-    }
59234c
-    return ctx->pass_len;
59234c
-}
59234c
-
59234c
-EVP_PKEY   *modssl_read_encrypted_pkey(const char *filename, EVP_PKEY **key, 
59234c
-                                       const char *pass, apr_size_t pass_len)
59234c
-{
59234c
-    pass_ctx ctx;
59234c
-    
59234c
-    ctx.pass = pass;
59234c
-    ctx.pass_len = pass_len;
59234c
-    return modssl_read_privatekey(filename, key, provide_pass, &ctx;;
59234c
-}
59234c
-
59234c
 /*  _________________________________________________________________
59234c
 **
59234c
 **  Smart shutdown
59234c
--- httpd-2.4.35/modules/ssl/ssl_util_ssl.h.r1830819+
59234c
+++ httpd-2.4.35/modules/ssl/ssl_util_ssl.h
59234c
@@ -64,8 +64,11 @@
59234c
 void        modssl_init_app_data2_idx(void);
59234c
 void       *modssl_get_app_data2(SSL *);
59234c
 void        modssl_set_app_data2(SSL *, void *);
59234c
-EVP_PKEY   *modssl_read_privatekey(const char *, EVP_PKEY **, pem_password_cb *, void *);
59234c
-EVP_PKEY   *modssl_read_encrypted_pkey(const char *, EVP_PKEY **, const char *, apr_size_t);
59234c
+
59234c
+/* Read private key from filename in either PEM or raw base64(DER)
59234c
+ * format, using password entry callback cb and userdata. */
59234c
+EVP_PKEY   *modssl_read_privatekey(const char *filename, pem_password_cb *cb, void *ud);
59234c
+
59234c
 int         modssl_smart_shutdown(SSL *ssl);
59234c
 BOOL        modssl_X509_getBC(X509 *, int *, int *);
59234c
 char       *modssl_X509_NAME_ENTRY_to_string(apr_pool_t *p, X509_NAME_ENTRY *xsne,