mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 12:14:38 +02:00
15451 correct padding for pkcs7 (#6260)
* Update to ensure full blocks for crypto in En/DecodeAuthEnvelopedData. * Corrected spacing and comments * Set plain to NULL after free on non-error path.
This commit is contained in:
@@ -11009,7 +11009,7 @@ int wc_PKCS7_EncodeAuthEnvelopedData(PKCS7* pkcs7, byte* output,
|
|||||||
{
|
{
|
||||||
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
|
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
|
||||||
int ret, idx = 0;
|
int ret, idx = 0;
|
||||||
int totalSz, encryptedOutSz;
|
int totalSz, encryptedAllocSz, encryptedOutSz;
|
||||||
|
|
||||||
int contentInfoSeqSz, outerContentTypeSz, outerContentSz;
|
int contentInfoSeqSz, outerContentTypeSz, outerContentSz;
|
||||||
byte contentInfoSeq[MAX_SEQ_SZ];
|
byte contentInfoSeq[MAX_SEQ_SZ];
|
||||||
@@ -11022,6 +11022,7 @@ int wc_PKCS7_EncodeAuthEnvelopedData(PKCS7* pkcs7, byte* output,
|
|||||||
|
|
||||||
WC_RNG rng;
|
WC_RNG rng;
|
||||||
int blockSz, blockKeySz;
|
int blockSz, blockKeySz;
|
||||||
|
byte* plain;
|
||||||
byte* encryptedContent;
|
byte* encryptedContent;
|
||||||
|
|
||||||
Pkcs7EncodedRecip* tmpRecip = NULL;
|
Pkcs7EncodedRecip* tmpRecip = NULL;
|
||||||
@@ -11334,11 +11335,38 @@ int wc_PKCS7_EncodeAuthEnvelopedData(PKCS7* pkcs7, byte* output,
|
|||||||
unauthAttribSet);
|
unauthAttribSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* allocate encrypted content buffer */
|
/* AES-GCM/CCM does NOT require padding for plaintext content or
|
||||||
|
* AAD inputs RFC 5084 section 3.1 and 3.2, but we must alloc
|
||||||
|
* full blocks to ensure crypto only gets full blocks */
|
||||||
encryptedOutSz = pkcs7->contentSz;
|
encryptedOutSz = pkcs7->contentSz;
|
||||||
encryptedContent = (byte*)XMALLOC(encryptedOutSz, pkcs7->heap,
|
encryptedAllocSz = (encryptedOutSz % blockSz) ?
|
||||||
|
encryptedOutSz + blockSz -
|
||||||
|
(encryptedOutSz % blockSz) :
|
||||||
|
encryptedOutSz;
|
||||||
|
|
||||||
|
/* Copy content to plain buffer (zero-padded) to encrypt in full,
|
||||||
|
* contiguous blocks */
|
||||||
|
plain = (byte*)XMALLOC(encryptedAllocSz, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
|
||||||
|
if (plain == NULL) {
|
||||||
|
wc_PKCS7_FreeEncodedRecipientSet(pkcs7);
|
||||||
|
if (aadBuffer)
|
||||||
|
XFREE(aadBuffer, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
if (flatUnauthAttribs)
|
||||||
|
XFREE(flatUnauthAttribs, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
|
||||||
|
if (flatAuthAttribs)
|
||||||
|
XFREE(flatAuthAttribs, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
|
||||||
|
return MEMORY_E;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMEMCPY(plain, pkcs7->content, pkcs7->contentSz);
|
||||||
|
if ((encryptedAllocSz - encryptedOutSz) > 0) {
|
||||||
|
XMEMSET(plain + encryptedOutSz, 0, encryptedAllocSz - encryptedOutSz);
|
||||||
|
}
|
||||||
|
|
||||||
|
encryptedContent = (byte*)XMALLOC(encryptedAllocSz, pkcs7->heap,
|
||||||
DYNAMIC_TYPE_PKCS7);
|
DYNAMIC_TYPE_PKCS7);
|
||||||
if (encryptedContent == NULL) {
|
if (encryptedContent == NULL) {
|
||||||
|
XFREE(plain, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
|
||||||
wc_PKCS7_FreeEncodedRecipientSet(pkcs7);
|
wc_PKCS7_FreeEncodedRecipientSet(pkcs7);
|
||||||
if (aadBuffer)
|
if (aadBuffer)
|
||||||
XFREE(aadBuffer, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
XFREE(aadBuffer, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
@@ -11352,9 +11380,12 @@ int wc_PKCS7_EncodeAuthEnvelopedData(PKCS7* pkcs7, byte* output,
|
|||||||
/* encrypt content */
|
/* encrypt content */
|
||||||
ret = wc_PKCS7_EncryptContent(pkcs7->encryptOID, pkcs7->cek,
|
ret = wc_PKCS7_EncryptContent(pkcs7->encryptOID, pkcs7->cek,
|
||||||
pkcs7->cekSz, nonce, nonceSz, aadBuffer, aadBufferSz, authTag,
|
pkcs7->cekSz, nonce, nonceSz, aadBuffer, aadBufferSz, authTag,
|
||||||
sizeof(authTag), pkcs7->content, encryptedOutSz, encryptedContent,
|
sizeof(authTag), plain, encryptedOutSz, encryptedContent,
|
||||||
pkcs7->devId, pkcs7->heap);
|
pkcs7->devId, pkcs7->heap);
|
||||||
|
|
||||||
|
XFREE(plain, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
|
||||||
|
plain = NULL;
|
||||||
|
|
||||||
if (aadBuffer) {
|
if (aadBuffer) {
|
||||||
XFREE(aadBuffer, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
XFREE(aadBuffer, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
aadBuffer = NULL;
|
aadBuffer = NULL;
|
||||||
@@ -11561,6 +11592,7 @@ WOLFSSL_API int wc_PKCS7_DecodeAuthEnvelopedData(PKCS7* pkcs7, byte* in,
|
|||||||
byte decryptedKey[MAX_ENCRYPTED_KEY_SZ];
|
byte decryptedKey[MAX_ENCRYPTED_KEY_SZ];
|
||||||
#endif
|
#endif
|
||||||
int encryptedContentSz = 0;
|
int encryptedContentSz = 0;
|
||||||
|
int encryptedAllocSz = 0;
|
||||||
byte* encryptedContent = NULL;
|
byte* encryptedContent = NULL;
|
||||||
int explicitOctet = 0;
|
int explicitOctet = 0;
|
||||||
|
|
||||||
@@ -11839,8 +11871,13 @@ WOLFSSL_API int wc_PKCS7_DecodeAuthEnvelopedData(PKCS7* pkcs7, byte* in,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* AES-GCM/CCM does NOT require padding for plaintext content or
|
/* AES-GCM/CCM does NOT require padding for plaintext content or
|
||||||
* AAD inputs RFC 5084 section 3.1 and 3.2 */
|
* AAD inputs RFC 5084 section 3.1 and 3.2, but we must alloc
|
||||||
encryptedContent = (byte*)XMALLOC(encryptedContentSz, pkcs7->heap,
|
* full blocks to ensure crypto only gets full blocks */
|
||||||
|
encryptedAllocSz = (encryptedContentSz % expBlockSz) ?
|
||||||
|
encryptedContentSz + expBlockSz -
|
||||||
|
(encryptedContentSz % expBlockSz) :
|
||||||
|
encryptedContentSz;
|
||||||
|
encryptedContent = (byte*)XMALLOC(encryptedAllocSz, pkcs7->heap,
|
||||||
DYNAMIC_TYPE_PKCS7);
|
DYNAMIC_TYPE_PKCS7);
|
||||||
if (ret == 0 && encryptedContent == NULL) {
|
if (ret == 0 && encryptedContent == NULL) {
|
||||||
ret = MEMORY_E;
|
ret = MEMORY_E;
|
||||||
|
Reference in New Issue
Block a user