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