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