]> git.kaiwu.me - nginx.git/commitdiff
OCSP: removed padding of serial in cache keys
authorAleksei Bavshin <a.bavshin@nginx.com>
Thu, 17 Sep 2026 20:22:33 +0000 (13:22 -0700)
committerSergey Kandaurov <pluknet@nginx.com>
Thu, 5 Nov 2026 14:27:12 +0000 (18:27 +0400)
Right-padding the serials makes it impossible to distinguish between n,
n * 256 and so on.  This has no practical impact, as small, predictable
serial numbers have been discouraged for the last two decades.

While the change has a potential of introducing memory fragmentation,
current allocations stay within a single slab bucket on all supported
architectures.

Reported by Gyejin Lee, QED Audit.

src/event/ngx_event_openssl_stapling.c

index 4f91c9193d1e08b382a251300a5b1eb33cd6daee..2b815ade65ee5ebde5c6e5654980193831be932d 100644 (file)
@@ -2668,13 +2668,20 @@ ngx_ssl_ocsp_create_key(ngx_ssl_ocsp_ctx_t *ctx)
     ASN1_INTEGER     *serial;
     const X509_NAME  *name;
 
-    p = ngx_pnalloc(ctx->pool, 60);
+    serial = X509_get_serialNumber(ctx->cert);
+    length = ASN1_STRING_length(serial);
+
+    if (length > 20) {
+        return NGX_ERROR;
+    }
+
+    p = ngx_pnalloc(ctx->pool, 40 + length);
     if (p == NULL) {
         return NGX_ERROR;
     }
 
     ctx->key.data = p;
-    ctx->key.len = 60;
+    ctx->key.len = 40 + length;
 
     name = X509_get_subject_name(ctx->issuer);
     if (X509_NAME_digest(name, EVP_sha1(), p, NULL) == 0) {
@@ -2689,15 +2696,7 @@ ngx_ssl_ocsp_create_key(ngx_ssl_ocsp_ctx_t *ctx)
 
     p += 20;
 
-    serial = X509_get_serialNumber(ctx->cert);
-    length = ASN1_STRING_length(serial);
-
-    if (length > 20) {
-        return NGX_ERROR;
-    }
-
-    p = ngx_cpymem(p, ASN1_STRING_get0_data(serial), length);
-    ngx_memzero(p, 20 - length);
+    ngx_memcpy(p, ASN1_STRING_get0_data(serial), length);
 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ctx->log, 0,
                    "ssl ocsp key %xV", &ctx->key);