Skip to content

Commit 23a4a9c

Browse files
committed
Fix OCSP key-based responder ID lookup when SM2/SM3 is enabled.
When WOLFSSL_SM2 and WOLFSSL_SM3 are both defined, KEYID_SIZE becomes 32 (WC_SM3_DIGEST_SIZE) but OCSP_RESPONDER_ID_KEY_SZ remains 20 (SHA-1 per RFC 6960). The guard (int)KEYID_SIZE == OCSP_RESPONDER_ID_KEY_SZ in OcspFindSigner() and OcspRespIdMatch() evaluated to false (32 != 20), completely disabling key-based OCSP responder ID matching. This caused OCSP stapling to fail with BAD_CERTIFICATE_STATUS_ERROR (-406) against any server using a key-based responder ID (e.g. login.live.com). Fix by comparing only OCSP_RESPONDER_ID_KEY_SZ bytes for the responder ID match, and zero-padding the 20-byte key hash to KEYID_SIZE before passing to CA lookup functions that compare the full KEYID_SIZE.
1 parent 17e6859 commit 23a4a9c

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/ocsp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ static int OcspRespIdMatches(OcspResponse* resp, const byte* NameHash,
950950
SIGNER_DIGEST_SIZE) == 0;
951951
}
952952
else if (resp->responderIdType == OCSP_RESPONDER_ID_KEY) {
953-
return XMEMCMP(keyHash, resp->responderId.keyHash, KEYID_SIZE) == 0;
953+
return XMEMCMP(keyHash, resp->responderId.keyHash,
954+
OCSP_RESPONDER_ID_KEY_SZ) == 0;
954955
}
955956

956957
return 0;

wolfcrypt/src/asn.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39569,8 +39569,9 @@ static int OcspRespIdMatch(OcspResponse *resp, const byte *NameHash,
3956939569
return XMEMCMP(NameHash, resp->responderId.nameHash,
3957039570
SIGNER_DIGEST_SIZE) == 0;
3957139571
/* OCSP_RESPONDER_ID_KEY */
39572-
return ((int)KEYID_SIZE == OCSP_RESPONDER_ID_KEY_SZ) &&
39573-
XMEMCMP(keyHash, resp->responderId.keyHash, KEYID_SIZE) == 0;
39572+
return (KEYID_SIZE >= OCSP_RESPONDER_ID_KEY_SZ) &&
39573+
XMEMCMP(keyHash, resp->responderId.keyHash,
39574+
OCSP_RESPONDER_ID_KEY_SZ) == 0;
3957439575
}
3957539576

3957639577
#ifndef WOLFSSL_NO_OCSP_ISSUER_CHECK
@@ -39609,8 +39610,15 @@ static Signer *OcspFindSigner(OcspResponse *resp, WOLFSSL_CERT_MANAGER *cm)
3960939610
if (s)
3961039611
return s;
3961139612
}
39612-
else if ((int)KEYID_SIZE == OCSP_RESPONDER_ID_KEY_SZ) {
39613-
s = GetCAByKeyHash(cm, resp->responderId.keyHash);
39613+
else if (KEYID_SIZE >= OCSP_RESPONDER_ID_KEY_SZ) {
39614+
/* Responder key hash is OCSP_RESPONDER_ID_KEY_SZ bytes (SHA-1 per
39615+
* RFC 6960) but lookup functions compare KEYID_SIZE bytes. Zero-pad
39616+
* to avoid buffer over-read when KEYID_SIZE > OCSP_RESPONDER_ID_KEY_SZ
39617+
* (e.g. when SM2/SM3 is enabled). */
39618+
byte keyHash[KEYID_SIZE];
39619+
XMEMSET(keyHash, 0, KEYID_SIZE);
39620+
XMEMCPY(keyHash, resp->responderId.keyHash, OCSP_RESPONDER_ID_KEY_SZ);
39621+
s = GetCAByKeyHash(cm, keyHash);
3961439622
if (s)
3961539623
return s;
3961639624
}
@@ -39623,8 +39631,11 @@ static Signer *OcspFindSigner(OcspResponse *resp, WOLFSSL_CERT_MANAGER *cm)
3962339631
if (s)
3962439632
return s;
3962539633
}
39626-
else {
39627-
s = findSignerByKeyHash(resp->pendingCAs, resp->responderId.keyHash);
39634+
else if (KEYID_SIZE >= OCSP_RESPONDER_ID_KEY_SZ) {
39635+
byte keyHash[KEYID_SIZE];
39636+
XMEMSET(keyHash, 0, KEYID_SIZE);
39637+
XMEMCPY(keyHash, resp->responderId.keyHash, OCSP_RESPONDER_ID_KEY_SZ);
39638+
s = findSignerByKeyHash(resp->pendingCAs, keyHash);
3962839639
if (s)
3962939640
return s;
3963039641
}

0 commit comments

Comments
 (0)