Skip to content

Commit 855f70d

Browse files
committed
Fix PKCS#7/CMS EnvelopedData decrypt of definite-length constructed [0]
wc_PKCS7_DecodeEnvelopedData() failed to decrypt a valid CMS/SCEP EnvelopedData whose encryptedContent is a definite-length constructed context [0] wrapping a single definite-length OCTET STRING (A0 82 xx xx 04 82 yy yy <ciphertext>). This is the encoding emitted by Go's crypto/pkcs7 stacks (e.g. micromdm/scep), so SCEP enrollment against those servers failed at the CertRep decrypt step. The decoder recognized the constructed [0] tag but its handler assumed the BER indefinite-length fragmented form: a run of OCTET STRINGs terminated by an EOC (00 00). The definite-length single-OCTET-STRING form has no EOC, so after decrypting the one OCTET STRING the loop kept scanning for a terminator that never comes. Streaming builds returned WC_PKCS7_WANT_READ_E; NO_PKCS7_STREAM builds spun. The primitive (80 ...) and constructed-indefinite (A0 80 ... 00 00) encodings were already handled. Right after reading the [0] length, detect the case where the [0] definite length is filled exactly by a single inner OCTET STRING, skip that inner header, and fall into the existing primitive single-shot decrypt path. The check runs before the NO_PKCS7_STREAM bounds check so both streaming and non-streaming builds are corrected, and the inner length is read with NO_USER_CHECK because the ciphertext may not be fully buffered yet in streaming mode; the size-equality test validates the structure instead. That equality is computed in word32 to avoid signed-overflow undefined behavior on a crafted oversized inner length. The unwrap only fires for the constructed-definite single-OCTET-STRING shape; primitive and indefinite encodings are untouched. Add test_wc_PKCS7_DecodeEnvelopedData_constructedDefiniteOctet (tests/api/test_pkcs7.c). wolfSSL's encoder never emits this form, so the test transcodes a normal encode into it. It covers the positive case plus three negatives that pin the unwrap guard: two OCTET STRINGs, a [0] longer than the inner OCTET STRING, and a non-OCTET-STRING inner.
1 parent a09c0db commit 855f70d

3 files changed

Lines changed: 489 additions & 0 deletions

File tree

0 commit comments

Comments
 (0)