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