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