mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-31 19:24:42 +02:00
Fixes in PKCS7 for handling hardware based devId and no private key. Fix to handle scenario where kari->decoded
is allocated, but not initalized (was causing use of unitliaized in FreeDecodedCert
). Fix to handle hardware base RSA key size.
This commit is contained in:
@@ -14537,6 +14537,7 @@ static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
|
||||
printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
|
||||
testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
|
||||
for (i = 0; i < testSz; i++) {
|
||||
AssertIntEQ(wc_PKCS7_Init(&pkcs7, HEAP_HINT, devId), 0);
|
||||
AssertIntEQ(wc_PKCS7_InitWithCert(&pkcs7, (testVectors + i)->cert,
|
||||
(word32)(testVectors + i)->certSz), 0);
|
||||
|
||||
|
@@ -259,7 +259,7 @@ int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz)
|
||||
{
|
||||
int ret = 0;
|
||||
void* heap;
|
||||
|
||||
int devId;
|
||||
|
||||
if (pkcs7 == NULL || (cert == NULL && certSz != 0)) {
|
||||
return BAD_FUNC_ARG;
|
||||
@@ -270,9 +270,11 @@ int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz)
|
||||
#else
|
||||
heap = pkcs7->heap;
|
||||
#endif
|
||||
devId = pkcs7->devId;
|
||||
|
||||
XMEMSET(pkcs7, 0, sizeof(PKCS7));
|
||||
pkcs7->heap = heap;
|
||||
pkcs7->devId = devId;
|
||||
|
||||
if (cert != NULL && certSz > 0) {
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
@@ -590,9 +592,9 @@ static int wc_PKCS7_RsaSign(PKCS7* pkcs7, byte* in, word32 inSz, ESD* esd)
|
||||
RsaKey* privKey = &stack_privKey;
|
||||
#endif
|
||||
|
||||
if (pkcs7 == NULL || pkcs7->privateKey == NULL || pkcs7->rng == NULL ||
|
||||
in == NULL || esd == NULL)
|
||||
if (pkcs7 == NULL || pkcs7->rng == NULL || in == NULL || esd == NULL) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
privKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
@@ -602,11 +604,15 @@ static int wc_PKCS7_RsaSign(PKCS7* pkcs7, byte* in, word32 inSz, ESD* esd)
|
||||
|
||||
ret = wc_InitRsaKey_ex(privKey, pkcs7->heap, pkcs7->devId);
|
||||
if (ret == 0) {
|
||||
idx = 0;
|
||||
ret = wc_RsaPrivateKeyDecode(pkcs7->privateKey, &idx, privKey,
|
||||
pkcs7->privateKeySz);
|
||||
if (pkcs7->privateKey != NULL && pkcs7->privateKeySz > 0) {
|
||||
idx = 0;
|
||||
ret = wc_RsaPrivateKeyDecode(pkcs7->privateKey, &idx, privKey,
|
||||
pkcs7->privateKeySz);
|
||||
}
|
||||
else if (pkcs7->devId == INVALID_DEVID) {
|
||||
ret = BAD_FUNC_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ret = wc_RsaSSL_Sign(in, inSz, esd->encContentDigest,
|
||||
sizeof(esd->encContentDigest),
|
||||
@@ -638,9 +644,9 @@ static int wc_PKCS7_EcdsaSign(PKCS7* pkcs7, byte* in, word32 inSz, ESD* esd)
|
||||
ecc_key* privKey = &stack_privKey;
|
||||
#endif
|
||||
|
||||
if (pkcs7 == NULL || pkcs7->privateKey == NULL || pkcs7->rng == NULL ||
|
||||
in == NULL || esd == NULL)
|
||||
if (pkcs7 == NULL || pkcs7->rng == NULL || in == NULL || esd == NULL) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
privKey = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
@@ -649,13 +655,16 @@ static int wc_PKCS7_EcdsaSign(PKCS7* pkcs7, byte* in, word32 inSz, ESD* esd)
|
||||
#endif
|
||||
|
||||
ret = wc_ecc_init_ex(privKey, pkcs7->heap, pkcs7->devId);
|
||||
|
||||
if (ret == 0) {
|
||||
idx = 0;
|
||||
ret = wc_EccPrivateKeyDecode(pkcs7->privateKey, &idx, privKey,
|
||||
pkcs7->privateKeySz);
|
||||
if (pkcs7->privateKey != NULL && pkcs7->privateKeySz > 0) {
|
||||
idx = 0;
|
||||
ret = wc_EccPrivateKeyDecode(pkcs7->privateKey, &idx, privKey,
|
||||
pkcs7->privateKeySz);
|
||||
}
|
||||
else if (pkcs7->devId == INVALID_DEVID) {
|
||||
ret = BAD_FUNC_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
outSz = sizeof(esd->encContentDigest);
|
||||
ret = wc_ecc_sign_hash(in, inSz, esd->encContentDigest,
|
||||
@@ -1032,9 +1041,9 @@ int wc_PKCS7_EncodeSignedData(PKCS7* pkcs7, byte* output, word32 outputSz)
|
||||
if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0 ||
|
||||
pkcs7->encryptOID == 0 || pkcs7->hashOID == 0 || pkcs7->rng == 0 ||
|
||||
pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0 ||
|
||||
pkcs7->privateKey == NULL || pkcs7->privateKeySz == 0 ||
|
||||
output == NULL || outputSz == 0)
|
||||
output == NULL || outputSz == 0) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
esd = (ESD*)XMALLOC(sizeof(ESD), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
@@ -2136,6 +2145,7 @@ typedef struct WC_PKCS7_KARI {
|
||||
word32 sharedInfoSz; /* size of ECC-CMS-SharedInfo encoded */
|
||||
byte ukmOwner; /* do we own ukm buffer? 1:yes, 0:no */
|
||||
byte direction; /* WC_PKCS7_ENCODE | WC_PKCS7_DECODE */
|
||||
byte decodedInit : 1; /* indicates decoded was intiialized */
|
||||
} WC_PKCS7_KARI;
|
||||
|
||||
|
||||
@@ -2247,6 +2257,7 @@ static WC_PKCS7_KARI* wc_PKCS7_KariNew(PKCS7* pkcs7, byte direction)
|
||||
kari->sharedInfo = NULL;
|
||||
kari->sharedInfoSz = 0;
|
||||
kari->direction = direction;
|
||||
kari->decodedInit = 0;
|
||||
|
||||
kari->heap = pkcs7->heap;
|
||||
kari->devId = pkcs7->devId;
|
||||
@@ -2264,7 +2275,9 @@ static int wc_PKCS7_KariFree(WC_PKCS7_KARI* kari)
|
||||
heap = kari->heap;
|
||||
|
||||
if (kari->decoded) {
|
||||
FreeDecodedCert(kari->decoded);
|
||||
if (kari->decodedInit) {
|
||||
FreeDecodedCert(kari->decoded);
|
||||
}
|
||||
XFREE(kari->decoded, heap, DYNAMIC_TYPE_PKCS7);
|
||||
}
|
||||
if (kari->senderKey) {
|
||||
@@ -2318,12 +2331,9 @@ static int wc_PKCS7_KariParseRecipCert(WC_PKCS7_KARI* kari, const byte* cert,
|
||||
cert == NULL || certSz == 0)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
if (kari->direction == WC_PKCS7_DECODE &&
|
||||
(key == NULL || keySz == 0))
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
/* decode certificate */
|
||||
InitDecodedCert(kari->decoded, (byte*)cert, certSz, kari->heap);
|
||||
kari->decodedInit = 1;
|
||||
ret = ParseCert(kari->decoded, CA_TYPE, NO_VERIFY, 0);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@@ -2349,9 +2359,13 @@ static int wc_PKCS7_KariParseRecipCert(WC_PKCS7_KARI* kari, const byte* cert,
|
||||
}
|
||||
/* get recip private key */
|
||||
else if (kari->direction == WC_PKCS7_DECODE) {
|
||||
|
||||
idx = 0;
|
||||
ret = wc_EccPrivateKeyDecode(key, &idx, kari->recipKey, keySz);
|
||||
if (key != NULL && keySz > 0) {
|
||||
idx = 0;
|
||||
ret = wc_EccPrivateKeyDecode(key, &idx, kari->recipKey, keySz);
|
||||
}
|
||||
else if (kari->devId == INVALID_DEVID) {
|
||||
ret = BAD_FUNC_ARG;
|
||||
}
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
|
||||
@@ -3722,9 +3736,14 @@ static int wc_PKCS7_DecodeKtri(PKCS7* pkcs7, byte* pkiMsg, word32 pkiMsgSz,
|
||||
return ret;
|
||||
}
|
||||
|
||||
keyIdx = 0;
|
||||
ret = wc_RsaPrivateKeyDecode(pkcs7->privateKey, &keyIdx, privKey,
|
||||
pkcs7->privateKeySz);
|
||||
if (pkcs7->privateKey != NULL && pkcs7->privateKeySz > 0) {
|
||||
keyIdx = 0;
|
||||
ret = wc_RsaPrivateKeyDecode(pkcs7->privateKey, &keyIdx, privKey,
|
||||
pkcs7->privateKeySz);
|
||||
}
|
||||
else if (pkcs7->devId == INVALID_DEVID) {
|
||||
ret = BAD_FUNC_ARG;
|
||||
}
|
||||
if (ret != 0) {
|
||||
WOLFSSL_MSG("Failed to decode RSA private key");
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
@@ -4381,8 +4400,7 @@ WOLFSSL_API int wc_PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg,
|
||||
int explicitOctet;
|
||||
|
||||
if (pkcs7 == NULL || pkcs7->singleCert == NULL ||
|
||||
pkcs7->singleCertSz == 0 || pkcs7->privateKey == NULL ||
|
||||
pkcs7->privateKeySz == 0)
|
||||
pkcs7->singleCertSz == 0)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
if (pkiMsg == NULL || pkiMsgSz == 0 ||
|
||||
|
@@ -2282,10 +2282,21 @@ int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out, word32 outLen,
|
||||
|
||||
int wc_RsaEncryptSize(RsaKey* key)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (key == NULL) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
return mp_unsigned_bin_size(&key->n);
|
||||
|
||||
ret = mp_unsigned_bin_size(&key->n);
|
||||
|
||||
#ifdef WOLF_CRYPTO_DEV
|
||||
if (ret == 0 && key->devId != INVALID_DEVID) {
|
||||
ret = 2048/8; /* hardware handles, use 2048-bit as default */
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user