Skip to content

Commit 370d3b1

Browse files
committed
ocsp support other hash algos
1 parent a6247de commit 370d3b1

File tree

4 files changed

+237
-62
lines changed

4 files changed

+237
-62
lines changed

scripts/ocsp-responder-openssl-interop.test

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,29 @@ echo
338338

339339
echo "=== Negative tests: unsupported features ==="
340340

341-
# Test 1: Non-SHA-1 hash algorithms (should fail with OCSP error)
341+
# Test 1: Non-SHA-1 hash algorithms (should be supported)
342342
tests_run=$((tests_run+1))
343-
printf " TEST %2d: %-55s " "$tests_run" "SHA-256 hash (should return OCSP error)"
343+
printf " TEST %2d: %-55s " "$tests_run" "SHA-384 hash -> good"
344344

345345
output=$($OPENSSL ocsp \
346-
-sha256 \
346+
-sha384 \
347347
-issuer "$OCSP_DIR/intermediate1-ca-cert.pem" \
348348
-cert "$OCSP_DIR/server1-cert.pem" \
349349
-url "http://127.0.0.1:$port1/" \
350350
-noverify \
351351
-resp_text 2>&1)
352352
rc=$?
353353

354-
# Expect OCSP error response: internalerror (2)
355-
# OpenSSL shows "Responder Error: internalerror (2)"
356-
if echo "$output" | grep -q "Responder Error: internalerror"; then
357-
printf "PASSED (OCSP internalerror)\n"
354+
# Extract the cert status line
355+
status=$(echo "$output" | grep "Cert Status:" | head -1 | \
356+
sed 's/.*Cert Status: *//')
357+
358+
# Expect "good" status (feature should be supported)
359+
if [ "$status" = "good" ]; then
360+
printf "PASSED (status: %s)\n" "$status"
358361
tests_passed=$((tests_passed+1))
359362
else
360-
printf "FAILED (expected OCSP internalerror, got rc: %d)\n" "$rc"
363+
printf "FAILED (expected: good, got: %s, rc: %d)\n" "$status" "$rc"
361364
tests_failed=$((tests_failed+1))
362365
echo "--- openssl output ---"
363366
echo "$output"

src/ocsp.c

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,8 +1376,9 @@ WOLFSSL_OCSP_ONEREQ* wolfSSL_OCSP_request_add0_id(OcspRequest *req,
13761376
/* Keep to free */
13771377
req->cid = (void*)cid;
13781378

1379-
XMEMCPY(req->issuerHash, cid->issuerHash, KEYID_SIZE);
1380-
XMEMCPY(req->issuerKeyHash, cid->issuerKeyHash, KEYID_SIZE);
1379+
XMEMCPY(req->issuerHash, cid->issuerHash, WC_MAX_DIGEST_SIZE);
1380+
XMEMCPY(req->issuerKeyHash, cid->issuerKeyHash, WC_MAX_DIGEST_SIZE);
1381+
req->hashAlg = (int)cid->hashAlgoOID;
13811382
if (cid->status->serialSz > req->serialSz) {
13821383
XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
13831384
req->serial = (byte*)XMALLOC((size_t)cid->status->serialSz,
@@ -2293,8 +2294,23 @@ int wc_OcspResponder_AddCA(OcspResponder* responder,
22932294

22942295
/* Extract necessary info from decoded cert */
22952296
XMEMCPY(ca->subject, decoded->subject, WC_ASN_NAME_MAX);
2296-
XMEMCPY(ca->issuerHash, decoded->subjectHash, KEYID_SIZE);
2297-
XMEMCPY(ca->issuerKeyHash, decoded->subjectKeyHash, KEYID_SIZE);
2297+
if (decoded->subjectRawForHash == NULL ||
2298+
decoded->subjectRawForHashLen <= 0) {
2299+
ret = BAD_FUNC_ARG;
2300+
goto out;
2301+
}
2302+
ret = AsnHashesHash(&ca->issuerHashes, decoded->subjectRawForHash,
2303+
(word32)decoded->subjectRawForHashLen);
2304+
if (ret != 0)
2305+
goto out;
2306+
if (decoded->publicKeyForHash == NULL || decoded->pubKeyForHashSize == 0) {
2307+
ret = BAD_FUNC_ARG;
2308+
goto out;
2309+
}
2310+
ret = AsnHashesHash(&ca->issuerKeyHash, decoded->publicKeyForHash,
2311+
decoded->pubKeyForHashSize);
2312+
if (ret != 0)
2313+
goto out;
22982314
keyOID = decoded->keyOID;
22992315

23002316
/* Store raw certificate DER if sendCerts is enabled */
@@ -2359,13 +2375,20 @@ int wc_OcspResponder_AddCA(OcspResponder* responder,
23592375
/* Find CA by comparing cert DER */
23602376
/* Find CA by issuer hashes from request */
23612377
static OcspResponderCa* FindCaByHashes(OcspResponder* responder,
2362-
const byte* issuerHash, const byte* issuerKeyHash)
2378+
const byte* issuerHash, const byte* issuerKeyHash, int hashAlg)
23632379
{
23642380
OcspResponderCa* ca = responder->caList;
23652381

23662382
while (ca != NULL) {
2367-
if (XMEMCMP(ca->issuerHash, issuerHash, KEYID_SIZE) == 0 &&
2368-
XMEMCMP(ca->issuerKeyHash, issuerKeyHash, KEYID_SIZE) == 0) {
2383+
int hashSz = 0;
2384+
const byte* caIssuerHash = AsnHashesGetHash(&ca->issuerHashes,
2385+
hashAlg, &hashSz);
2386+
const byte* caKeyHash = AsnHashesGetHash(&ca->issuerKeyHash,
2387+
hashAlg, &hashSz);
2388+
2389+
if (caIssuerHash != NULL && caKeyHash != NULL && hashSz > 0 &&
2390+
XMEMCMP(caIssuerHash, issuerHash, (size_t)hashSz) == 0 &&
2391+
XMEMCMP(caKeyHash, issuerKeyHash, (size_t)hashSz) == 0) {
23692392
return ca;
23702393
}
23712394
ca = ca->next;
@@ -2514,8 +2537,10 @@ static int OcspResponse_WriteResponse(OcspResponder* responder, byte* response,
25142537
RsaKey* rsaKey = NULL;
25152538
ecc_key* eccKey = NULL;
25162539
int respInited = 0;
2517-
2518-
wc_static_assert((int)KEYID_SIZE == (int)WC_SHA_DIGEST_SIZE);
2540+
int hashSz = 0;
2541+
const byte* caIssuerHash = NULL;
2542+
const byte* caKeyHash = NULL;
2543+
int hashAlg = -1;
25192544

25202545
WOLFSSL_ENTER("OcspResponse_WriteResponse");
25212546

@@ -2579,13 +2604,25 @@ static int OcspResponse_WriteResponse(OcspResponder* responder, byte* response,
25792604
resp.nonceSz = req->nonceSz;
25802605
}
25812606

2582-
/* Only support sha-1 hashes for now */
2583-
entry.hashAlgoOID = SHAh;
2584-
XMEMCPY(entry.issuerHash, ca->issuerHash, KEYID_SIZE);
2585-
XMEMCPY(entry.issuerKeyHash, ca->issuerKeyHash, KEYID_SIZE);
2607+
/* Echo the hash algorithm from the request */
2608+
if (req == NULL) {
2609+
hashAlg = SHAh; /* Fall back to SHA-1 as its required for OCSP */
2610+
}
2611+
else {
2612+
hashAlg = req->hashAlg;
2613+
}
2614+
caIssuerHash = AsnHashesGetHash(&ca->issuerHashes, hashAlg, &hashSz);
2615+
caKeyHash = AsnHashesGetHash(&ca->issuerKeyHash, hashAlg, &hashSz);
2616+
if (caIssuerHash == NULL || caKeyHash == NULL || hashSz <= 0) {
2617+
ret = ASN_SIG_HASH_E;
2618+
goto out;
2619+
}
2620+
entry.hashAlgoOID = (word32)hashAlg;
2621+
XMEMCPY(entry.issuerHash, caIssuerHash, (size_t)hashSz);
2622+
XMEMCPY(entry.issuerKeyHash, caKeyHash, (size_t)hashSz);
25862623

25872624
resp.responderIdType = OCSP_RESPONDER_ID_KEY;
2588-
XMEMCPY(resp.responderId.keyHash, ca->issuerKeyHash, KEYID_SIZE);
2625+
XMEMCPY(resp.responderId.keyHash, ca->issuerKeyHash.sha, KEYID_SIZE);
25892626

25902627
/* TODO allow user to set algo */
25912628
if (ca->keyType == RSAk) {
@@ -2640,7 +2677,8 @@ int wc_OcspResponder_WriteResponse(OcspResponder* responder,
26402677
reqInited = 1;
26412678

26422679
/* Find the CA by issuer hashes */
2643-
ca = FindCaByHashes(responder, req.issuerHash, req.issuerKeyHash);
2680+
ca = FindCaByHashes(responder, req.issuerHash, req.issuerKeyHash,
2681+
req.hashAlg);
26442682
if (ca == NULL) {
26452683
WOLFSSL_MSG("No matching CA found for request");
26462684
ret = ASN_NO_SIGNER_E;

0 commit comments

Comments
 (0)