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