638e8a
From 4e5f12d6584536ead82d20554d8f3f2ab0107b0b Mon Sep 17 00:00:00 2001
638e8a
From: Lubos Uhliarik <luhliari@redhat.com>
638e8a
Date: Fri, 30 Apr 2021 13:07:45 +0000
638e8a
Subject: [PATCH 3/3] Support loading certificates from hardware token (PKCS#11)
638e8a
638e8a
---
638e8a
 src/event/ngx_event_openssl.c | 65 +++++++++++++++++++++++++++++++++++
638e8a
 1 file changed, 65 insertions(+)
638e8a
638e8a
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
638e8a
index d762d6b..270b200 100644
638e8a
--- a/src/event/ngx_event_openssl.c
638e8a
+++ b/src/event/ngx_event_openssl.c
638e8a
@@ -617,6 +617,71 @@ ngx_ssl_load_certificate(ngx_pool_t *pool, char **err, ngx_str_t *cert,
638e8a
     X509    *x509, *temp;
638e8a
     u_long   n;
638e8a
 
638e8a
+    if (ngx_strncmp(cert->data, "engine:", sizeof("engine:") - 1) == 0) {
638e8a
+
638e8a
+#ifndef OPENSSL_NO_ENGINE
638e8a
+
638e8a
+        u_char  *p, *last;
638e8a
+        ENGINE  *engine;
638e8a
+
638e8a
+        p = cert->data + sizeof("engine:") - 1;
638e8a
+        last = (u_char *) ngx_strchr(p, ':');
638e8a
+
638e8a
+        if (last == NULL) {
638e8a
+            *err = "invalid syntax";
638e8a
+            return NULL;
638e8a
+        }
638e8a
+
638e8a
+        *last = '\0';
638e8a
+
638e8a
+        engine = ENGINE_by_id((char *) p);
638e8a
+
638e8a
+        if (engine == NULL) {
638e8a
+            *err = "ENGINE_by_id() failed";
638e8a
+            return NULL;
638e8a
+        }
638e8a
+
638e8a
+        if (!ENGINE_init(engine)) {
638e8a
+            *err = "ENGINE_init() failed";
638e8a
+            ENGINE_free(engine);
638e8a
+            return NULL;
638e8a
+        }
638e8a
+
638e8a
+        *last++ = ':';
638e8a
+
638e8a
+        struct {
638e8a
+            const char *cert_id;
638e8a
+            X509 *cert;
638e8a
+        } params = { (char *) last, NULL };
638e8a
+
638e8a
+        if (!ENGINE_ctrl_cmd(engine, "LOAD_CERT_CTRL", 0, &params, NULL, 1)) {
638e8a
+            *err = "ENGINE_ctrl_cmd() failed - Unable to get the certificate";
638e8a
+            ENGINE_free(engine);
638e8a
+            return NULL;
638e8a
+        }
638e8a
+
638e8a
+        ENGINE_finish(engine);
638e8a
+        ENGINE_free(engine);
638e8a
+
638e8a
+        /* set chain to null */
638e8a
+
638e8a
+        *chain = sk_X509_new_null();
638e8a
+        if (*chain == NULL) {
638e8a
+           *err = "sk_X509_new_null() failed";
638e8a
+           X509_free(params.cert);
638e8a
+           return NULL;
638e8a
+        }
638e8a
+
638e8a
+        return params.cert;
638e8a
+
638e8a
+#else
638e8a
+
638e8a
+        *err = "loading \"engine:...\" certificate is not supported";
638e8a
+        return NULL;
638e8a
+
638e8a
+#endif
638e8a
+    }
638e8a
+
638e8a
     if (ngx_strncmp(cert->data, "data:", sizeof("data:") - 1) == 0) {
638e8a
 
638e8a
         bio = BIO_new_mem_buf(cert->data + sizeof("data:") - 1,
638e8a
-- 
638e8a
2.26.3
638e8a