From d45e6c5c49b54a8146c79df81a9caa0196698eb9 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Thu, 17 Sep 2026 13:22:33 -0700 Subject: [PATCH] OCSP: preserved serial number sign in cache keys As OpenSSL does not store the sign in the ASN1_INTEGER string data, it is not possible to distinguish positive and negative serial numbers. While negative serials are forbidden in RFC 5280 4.1.2.2, it also says that we SHOULD expect these from non-conforming CAs. The solution is to use full DER-encoding of a signed integer obtained with i2d_ASN1_INTEGER. The change also addresses a correctness issue, as ASN1_STRING accessors should not be used on ASN1_INTEGER. --- src/event/ngx_event_openssl.h | 5 ----- src/event/ngx_event_openssl_stapling.c | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h index 010711481..1538cf062 100644 --- a/src/event/ngx_event_openssl.h +++ b/src/event/ngx_event_openssl.h @@ -68,11 +68,6 @@ #endif -#if (OPENSSL_VERSION_NUMBER < 0x1010000fL) -#define ASN1_STRING_get0_data(x) (x)->data -#endif - - #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined SSL_get_peer_certificate) #define SSL_get_peer_certificate(s) SSL_get1_peer_certificate(s) #endif diff --git a/src/event/ngx_event_openssl_stapling.c b/src/event/ngx_event_openssl_stapling.c index 2b815ade6..ec44e8bc6 100644 --- a/src/event/ngx_event_openssl_stapling.c +++ b/src/event/ngx_event_openssl_stapling.c @@ -2669,9 +2669,9 @@ ngx_ssl_ocsp_create_key(ngx_ssl_ocsp_ctx_t *ctx) const X509_NAME *name; serial = X509_get_serialNumber(ctx->cert); - length = ASN1_STRING_length(serial); + length = i2d_ASN1_INTEGER(serial, NULL); - if (length > 20) { + if (length <= 2 || length > 23) { return NGX_ERROR; } @@ -2696,7 +2696,7 @@ ngx_ssl_ocsp_create_key(ngx_ssl_ocsp_ctx_t *ctx) p += 20; - ngx_memcpy(p, ASN1_STRING_get0_data(serial), length); + (void) i2d_ASN1_INTEGER(serial, &p); ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ctx->log, 0, "ssl ocsp key %xV", &ctx->key); -- 2.47.3