mirror of https://github.com/wolfSSL/wolfssl.git
refactoring and fixes for new PKCS7 stream code
parent
089e57371f
commit
a07d92d3d3
|
@ -2430,6 +2430,72 @@ static int wc_PKCS7_SignedDataBuildSignature(PKCS7* pkcs7,
|
||||||
#define BER_OCTET_LENGTH 4096
|
#define BER_OCTET_LENGTH 4096
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This helper function encodes a chunk of content stream and writes it out.
|
||||||
|
*
|
||||||
|
* @param pkcs7 Pointer to a PKCS7 structure.
|
||||||
|
* @param cipherType The type of cipher to use for encryption.
|
||||||
|
* @param aes Optional pointer to an Aes structure for AES encryption.
|
||||||
|
* @param encContentOut Buffer to hold the encrypted content.
|
||||||
|
* @param contentData Buffer holding the content to be encrypted.
|
||||||
|
* @param contentDataSz Size of the content to be encrypted.
|
||||||
|
* @param out Buffer to hold the output data.
|
||||||
|
* @param outIdx Pointer to an index into the output buffer.
|
||||||
|
* @param esd Pointer to an ESD structure for digest calculation.
|
||||||
|
* @return Returns 0 on success, and a negative value on failure.
|
||||||
|
*/
|
||||||
|
static int wc_PKCS7_EncodeContentStreamHelper(PKCS7* pkcs7, int cipherType,
|
||||||
|
Aes* aes, byte* encContentOut, byte* contentData, int contentDataSz,
|
||||||
|
byte* out, word32* outIdx, ESD* esd)
|
||||||
|
{
|
||||||
|
int ret = BAD_FUNC_ARG;
|
||||||
|
byte encContentOutOct[MAX_OCTET_STR_SZ];
|
||||||
|
word32 encContentOutOctSz = 0;
|
||||||
|
|
||||||
|
switch (cipherType) {
|
||||||
|
case WC_CIPHER_NONE:
|
||||||
|
XMEMCPY(encContentOut, contentData, contentDataSz);
|
||||||
|
if (esd && esd->contentDigestSet != 1) {
|
||||||
|
ret = wc_HashUpdate(&esd->hash, esd->hashType,
|
||||||
|
contentData, contentDataSz);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
#ifndef NO_AES
|
||||||
|
case WC_CIPHER_AES_CBC:
|
||||||
|
ret = wc_AesCbcEncrypt(aes, encContentOut,
|
||||||
|
contentData, contentDataSz);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_AESGCM_STREAM
|
||||||
|
case WC_CIPHER_AES_GCM:
|
||||||
|
ret = wc_AesGcmEncryptUpdate(aes, encContentOut,
|
||||||
|
contentData, contentDataSz, NULL, 0);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_ASYNC_CRYPT
|
||||||
|
/* async encrypt not available here, so block till done */
|
||||||
|
if (ret == WC_PENDING_E && cipherType != WC_CIPHER_NONE) {
|
||||||
|
ret = wc_AsyncWait(ret, &aes->asyncDev, WC_ASYNC_FLAG_NONE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0) {
|
||||||
|
encContentOutOctSz = SetOctetString(contentDataSz, encContentOutOct);
|
||||||
|
wc_PKCS7_WriteOut(pkcs7, (out)? out + *outIdx: NULL,
|
||||||
|
encContentOutOct, encContentOutOctSz);
|
||||||
|
*outIdx += encContentOutOctSz;
|
||||||
|
wc_PKCS7_WriteOut(pkcs7, (out)? out + *outIdx : NULL,
|
||||||
|
encContentOut, contentDataSz);
|
||||||
|
*outIdx += contentDataSz;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Used for encoding the content, potentially one octet chunck at a time if
|
/* Used for encoding the content, potentially one octet chunck at a time if
|
||||||
* in streaming mode with IO callbacks set.
|
* in streaming mode with IO callbacks set.
|
||||||
|
@ -2457,8 +2523,6 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
|
||||||
if (pkcs7->encodeStream) {
|
if (pkcs7->encodeStream) {
|
||||||
int sz;
|
int sz;
|
||||||
word32 totalSz = 0;
|
word32 totalSz = 0;
|
||||||
byte encContentOutOct[MAX_OCTET_STR_SZ];
|
|
||||||
word32 encContentOutOctSz = 0;
|
|
||||||
byte* buf;
|
byte* buf;
|
||||||
byte* encContentOut;
|
byte* encContentOut;
|
||||||
byte* contentData;
|
byte* contentData;
|
||||||
|
@ -2487,9 +2551,6 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
|
||||||
return MEMORY_E;
|
return MEMORY_E;
|
||||||
}
|
}
|
||||||
|
|
||||||
encContentOutOctSz = SetOctetString(BER_OCTET_LENGTH,
|
|
||||||
encContentOutOct);
|
|
||||||
|
|
||||||
/* keep pulling from content until empty */
|
/* keep pulling from content until empty */
|
||||||
do {
|
do {
|
||||||
int contentDataRead = 0;
|
int contentDataRead = 0;
|
||||||
|
@ -2498,6 +2559,14 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
|
||||||
if (pkcs7->getContentCb) {
|
if (pkcs7->getContentCb) {
|
||||||
contentDataRead = pkcs7->getContentCb(pkcs7,
|
contentDataRead = pkcs7->getContentCb(pkcs7,
|
||||||
&buf, pkcs7->streamCtx);
|
&buf, pkcs7->streamCtx);
|
||||||
|
|
||||||
|
if (buf == NULL) {
|
||||||
|
WOLFSSL_MSG("Get content callback returned null "
|
||||||
|
"buffer pointer");
|
||||||
|
XFREE(encContentOut, heap, DYNAMIC_TYPE_PKCS7);
|
||||||
|
XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7);
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
|
@ -2515,14 +2584,6 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
|
||||||
/* no more data returned from callback */
|
/* no more data returned from callback */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf == NULL) {
|
|
||||||
WOLFSSL_MSG("Get content callback returned null "
|
|
||||||
"buffer pointer");
|
|
||||||
XFREE(encContentOut, heap, DYNAMIC_TYPE_PKCS7);
|
|
||||||
XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7);
|
|
||||||
return BAD_FUNC_ARG;
|
|
||||||
}
|
|
||||||
totalSz += (word32)contentDataRead;
|
totalSz += (word32)contentDataRead;
|
||||||
|
|
||||||
/* check and handle octet boundary */
|
/* check and handle octet boundary */
|
||||||
|
@ -2532,50 +2593,9 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
|
||||||
contentDataRead -= sz;
|
contentDataRead -= sz;
|
||||||
|
|
||||||
XMEMCPY(contentData + idx, buf, sz);
|
XMEMCPY(contentData + idx, buf, sz);
|
||||||
|
ret = wc_PKCS7_EncodeContentStreamHelper(pkcs7, cipherType,
|
||||||
/* encrypt and flush out data */
|
aes, encContentOut, contentData, BER_OCTET_LENGTH, out,
|
||||||
switch (cipherType) {
|
&outIdx, esd);
|
||||||
case WC_CIPHER_NONE:
|
|
||||||
XMEMCPY(encContentOut, contentData, BER_OCTET_LENGTH);
|
|
||||||
if (esd && esd->contentDigestSet != 1) {
|
|
||||||
ret = wc_HashUpdate(&esd->hash, esd->hashType,
|
|
||||||
contentData, BER_OCTET_LENGTH);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
#ifndef NO_AES
|
|
||||||
case WC_CIPHER_AES_CBC:
|
|
||||||
ret = wc_AesCbcEncrypt(aes, encContentOut,
|
|
||||||
contentData, BER_OCTET_LENGTH);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef WOLFSSL_AESGCM_STREAM
|
|
||||||
case WC_CIPHER_AES_GCM:
|
|
||||||
ret = wc_AesGcmEncryptUpdate(aes, encContentOut,
|
|
||||||
contentData, BER_OCTET_LENGTH, NULL, 0);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
|
||||||
/* async encrypt not available here, so block till done */
|
|
||||||
if (cipherType != WC_CIPHER_NONE) {
|
|
||||||
ret = wc_AsyncWait(ret, &aes->asyncDev, WC_ASYNC_FLAG_NONE);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (pkcs7->encodeStream) {
|
|
||||||
wc_PKCS7_WriteOut(pkcs7,
|
|
||||||
(out)? out + outIdx: NULL,
|
|
||||||
encContentOutOct,
|
|
||||||
encContentOutOctSz);
|
|
||||||
outIdx += encContentOutOctSz;
|
|
||||||
}
|
|
||||||
wc_PKCS7_WriteOut(pkcs7,
|
|
||||||
(out)? out + outIdx : NULL,
|
|
||||||
encContentOut,
|
|
||||||
BER_OCTET_LENGTH);
|
|
||||||
outIdx += BER_OCTET_LENGTH;
|
|
||||||
|
|
||||||
/* copy over any remaining data */
|
/* copy over any remaining data */
|
||||||
XMEMCPY(contentData, buf + sz, contentDataRead);
|
XMEMCPY(contentData, buf + sz, contentDataRead);
|
||||||
|
@ -2605,45 +2625,9 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
|
||||||
idx += padSz;
|
idx += padSz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* encrypt and flush out remainder of content data */
|
/* encrypt and flush out remainder of content data */
|
||||||
switch (cipherType) {
|
ret = wc_PKCS7_EncodeContentStreamHelper(pkcs7, cipherType, aes,
|
||||||
case WC_CIPHER_NONE:
|
encContentOut, contentData, idx, out, &outIdx, esd);
|
||||||
XMEMCPY(encContentOut, contentData, idx);
|
|
||||||
if (esd && esd->contentDigestSet != 1) {
|
|
||||||
ret = wc_HashUpdate(&esd->hash, esd->hashType, contentData,
|
|
||||||
idx);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
#ifndef NO_AES
|
|
||||||
case WC_CIPHER_AES_CBC:
|
|
||||||
ret = wc_AesCbcEncrypt(aes, encContentOut, contentData, idx);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef WOLFSSL_AESGCM_STREAM
|
|
||||||
case WC_CIPHER_AES_GCM:
|
|
||||||
ret = wc_AesGcmEncryptUpdate(aes, encContentOut,
|
|
||||||
contentData, idx, NULL, 0);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
|
||||||
/* async encrypt not available here, so block till done */
|
|
||||||
if (cipherType != WC_CIPHER_NONE) {
|
|
||||||
ret = wc_AsyncWait(ret, &aes->asyncDev, WC_ASYNC_FLAG_NONE);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (pkcs7->encodeStream) {
|
|
||||||
encContentOutOctSz = SetOctetString(idx,
|
|
||||||
encContentOutOct);
|
|
||||||
wc_PKCS7_WriteOut(pkcs7, (out)? out + outIdx: NULL,
|
|
||||||
encContentOutOct, encContentOutOctSz);
|
|
||||||
outIdx += encContentOutOctSz;
|
|
||||||
}
|
|
||||||
wc_PKCS7_WriteOut(pkcs7, (out)? out + outIdx : NULL,
|
|
||||||
encContentOut, idx);
|
|
||||||
|
|
||||||
if (cipherType == WC_CIPHER_NONE && esd && esd->contentDigestSet != 1) {
|
if (cipherType == WC_CIPHER_NONE && esd && esd->contentDigestSet != 1) {
|
||||||
ret = wc_HashFinal(&esd->hash, esd->hashType,
|
ret = wc_HashFinal(&esd->hash, esd->hashType,
|
||||||
|
@ -7574,7 +7558,7 @@ int wc_PKCS7_WriteOut(PKCS7* pkcs7, byte* output, const byte* input,
|
||||||
XMEMCPY(output, input, inputSz);
|
XMEMCPY(output, input, inputSz);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
WOLFSSL_MSG("No provided way to output bundle");
|
WOLFSSL_MSG("No way provided to output bundle");
|
||||||
ret = BUFFER_E;
|
ret = BUFFER_E;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue