Skip to content

Commit 97b82b5

Browse files
committed
Add nonce length validation for PKCS#7
1 parent b7f6e77 commit 97b82b5

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

wolfcrypt/src/pkcs7.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14726,6 +14726,11 @@ int wc_PKCS7_DecodeAuthEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1472614726
break;
1472714727
}
1472814728
pkiMsgSz = (pkcs7->stream->length > 0)? pkcs7->stream->length: inSz;
14729+
14730+
/* Restore encOID across WANT_READ re-entries so the nonce
14731+
* length validation below always sees the content-cipher
14732+
* algorithm parsed in AUTHENV_3. */
14733+
wc_PKCS7_StreamGetVar(pkcs7, &encOID, &blockKeySz, NULL);
1472914734
#endif
1473014735
/* get length of optional parameter sequence */
1473114736
if (ret == 0 && GetLength(pkiMsg, &idx, &length, pkiMsgSz) < 0) {
@@ -14743,6 +14748,46 @@ int wc_PKCS7_DecodeAuthEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1474314748
ret = ASN_PARSE_E;
1474414749
}
1474514750

14751+
/* Enforce algorithm-specific nonce length bounds at the parser
14752+
* layer so malformed lengths (notably zero-length, which would
14753+
* catastrophically break AEAD uniqueness on HW backends that
14754+
* skip their own checks) cannot reach the cipher engine.
14755+
* - AES-GCM in CMS: RFC 5084 Sec. 3.2 mandates a 12-octet IV.
14756+
* - AES-CCM: RFC 3610 Sec. 2.3 requires 7..13 octets.
14757+
* Any other encOID here is a parser-state invariant violation. */
14758+
if (ret == 0) {
14759+
int nonceMin = 0, nonceMax = 0;
14760+
switch (encOID) {
14761+
#ifdef HAVE_AESGCM
14762+
case AES128GCMb:
14763+
case AES192GCMb:
14764+
case AES256GCMb:
14765+
nonceMin = GCM_NONCE_MID_SZ;
14766+
nonceMax = GCM_NONCE_MID_SZ;
14767+
break;
14768+
#endif
14769+
#ifdef HAVE_AESCCM
14770+
case AES128CCMb:
14771+
case AES192CCMb:
14772+
case AES256CCMb:
14773+
nonceMin = CCM_NONCE_MIN_SZ;
14774+
nonceMax = CCM_NONCE_MAX_SZ;
14775+
break;
14776+
#endif
14777+
default:
14778+
WOLFSSL_MSG(
14779+
"AuthEnvelopedData unexpected content cipher");
14780+
ret = ALGO_ID_E;
14781+
break;
14782+
}
14783+
if (ret == 0 &&
14784+
(nonceSz < nonceMin || nonceSz > nonceMax)) {
14785+
WOLFSSL_MSG(
14786+
"AuthEnvelopedData nonce length invalid for cipher");
14787+
ret = ASN_PARSE_E;
14788+
}
14789+
}
14790+
1474614791
if (ret == 0) {
1474714792
XMEMCPY(nonce, &pkiMsg[idx], (word32)nonceSz);
1474814793
idx += (word32)nonceSz;

wolfssl/wolfcrypt/wc_encrypt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
#ifndef CCM_NONCE_MIN_SZ
7474
#define CCM_NONCE_MIN_SZ 7
7575
#endif
76+
#ifndef CCM_NONCE_MAX_SZ
77+
#define CCM_NONCE_MAX_SZ 13
78+
#endif
7679
#endif
7780

7881

0 commit comments

Comments
 (0)