From: Aleksei Bavshin Date: Thu, 17 Sep 2026 20:22:33 +0000 (-0700) Subject: OCSP: removed padding of serial in cache keys X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=d56c3fb5dc9903cd3d6b9eee5617a0f14700df7a;p=nginx.git OCSP: removed padding of serial in cache keys 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. --- diff --git a/src/event/ngx_event_openssl_stapling.c b/src/event/ngx_event_openssl_stapling.c index 4f91c9193..2b815ade6 100644 --- a/src/event/ngx_event_openssl_stapling.c +++ b/src/event/ngx_event_openssl_stapling.c @@ -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);