diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/Pkcs5Pbkdf2Tests.c b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/Pkcs5Pbkdf2Tests.c index 376188f9a3b..d090ddc6039 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/Pkcs5Pbkdf2Tests.c +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/Pkcs5Pbkdf2Tests.c @@ -33,6 +33,10 @@ TestVerifyPkcs5Pbkdf2 ( UINT8 *OutKey; OutKey = AllocatePool (KeyLen); + if (OutKey == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for OutKey.\n"); + return UNIT_TEST_ERROR_TEST_FAILED; + } // // Verify PKCS#5 PBKDF2 Key Derivation Function diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPkcs7Tests.c b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPkcs7Tests.c index fc7960a2cf2..2705cf36cfd 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPkcs7Tests.c +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPkcs7Tests.c @@ -246,18 +246,26 @@ TestVerifyRsaCertPkcs1SignVerify ( IN UNIT_TEST_CONTEXT Context ) { - BOOLEAN Status; - VOID *RsaPrivKey; - VOID *RsaPubKey; - UINT8 *Signature; - UINTN SigSize; - UINT8 *Subject; - UINTN SubjectSize; - RETURN_STATUS ReturnStatus; - CHAR8 CommonName[64]; - UINTN CommonNameSize; - CHAR8 OrgName[64]; - UINTN OrgNameSize; + BOOLEAN Status; + VOID *RsaPrivKey; + VOID *RsaPubKey; + UINT8 *Signature; + UINTN SigSize; + UINT8 *Subject; + UINTN SubjectSize; + RETURN_STATUS ReturnStatus; + CHAR8 CommonName[64]; + UINTN CommonNameSize; + CHAR8 OrgName[64]; + UINTN OrgNameSize; + UNIT_TEST_STATUS TestStatus; + + RsaPrivKey = NULL; + RsaPubKey = NULL; + Signature = NULL; + Subject = NULL; + + TestStatus = UNIT_TEST_ERROR_TEST_FAILED; // // Retrieve RSA private key from encrypted PEM data. @@ -281,7 +289,12 @@ TestVerifyRsaCertPkcs1SignVerify ( UT_ASSERT_NOT_EQUAL (SigSize, 0); Signature = AllocatePool (SigSize); - Status = RsaPkcs1Sign (RsaPrivKey, MsgHash, SHA1_DIGEST_SIZE, Signature, &SigSize); + if (Signature == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for Signature.\n"); + goto Exit; + } + + Status = RsaPkcs1Sign (RsaPrivKey, MsgHash, SHA1_DIGEST_SIZE, Signature, &SigSize); UT_ASSERT_TRUE (Status); // @@ -296,7 +309,12 @@ TestVerifyRsaCertPkcs1SignVerify ( SubjectSize = 0; Status = X509GetSubjectName (TestCert, sizeof (TestCert), NULL, &SubjectSize); Subject = (UINT8 *)AllocatePool (SubjectSize); - Status = X509GetSubjectName (TestCert, sizeof (TestCert), Subject, &SubjectSize); + if (Subject == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for Subject.\n"); + goto Exit; + } + + Status = X509GetSubjectName (TestCert, sizeof (TestCert), Subject, &SubjectSize); UT_ASSERT_TRUE (Status); // @@ -324,15 +342,28 @@ TestVerifyRsaCertPkcs1SignVerify ( Status = X509VerifyCert (TestCert, sizeof (TestCert), TestCACert, sizeof (TestCACert)); UT_ASSERT_TRUE (Status); + TestStatus = UNIT_TEST_PASSED; +Exit: // // Release Resources. // - RsaFree (RsaPubKey); - RsaFree (RsaPrivKey); - FreePool (Signature); - FreePool (Subject); + if (Subject != NULL) { + FreePool (Subject); + } - return UNIT_TEST_PASSED; + if (Signature != NULL) { + FreePool (Signature); + } + + if (RsaPubKey != NULL) { + RsaFree (RsaPubKey); + } + + if (RsaPrivKey != NULL) { + RsaFree (RsaPrivKey); + } + + return TestStatus; } UNIT_TEST_STATUS @@ -369,8 +400,8 @@ TestVerifyPkcs7SignVerify ( (CONST UINT8 *)PemPass, (UINT8 *)Payload, AsciiStrLen (Payload), - TestCert, // MU_CHANGE [TCBZ3925] - Pkcs7Sign is broken - sizeof (TestCert), // MU_CHANGE [TCBZ3925] - Pkcs7Sign is broken + TestCert, // MU_CHANGE [TCBZ3925] - Pkcs7Sign is broken + sizeof (TestCert), // MU_CHANGE [TCBZ3925] - Pkcs7Sign is broken NULL, &P7SignedData, &P7SignedDataSize diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c index 42baf88524c..48c1b7a4508 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaPssTests.c @@ -159,7 +159,12 @@ TestVerifyRsaPssSignVerify ( UT_ASSERT_NOT_EQUAL (SigSize, 0); Signature = AllocatePool (SigSize); - Status = RsaPssSign (mRsa, PssMessage, sizeof (PssMessage), SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE, Signature, &SigSize); + if (Signature == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for Signature"); + return UNIT_TEST_ERROR_TEST_FAILED; + } + + Status = RsaPssSign (mRsa, PssMessage, sizeof (PssMessage), SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE, Signature, &SigSize); UT_ASSERT_TRUE (Status); // diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c index 3f06e89b3cc..3e0ae23098d 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/RsaTests.c @@ -128,7 +128,12 @@ TestVerifyRsaSetGetKeyComponents ( UT_ASSERT_EQUAL (KeySize, sizeof (RsaN)); KeyBuffer = AllocatePool (KeySize); - Status = RsaGetKey (mRsa, RsaKeyN, KeyBuffer, &KeySize); + if (KeyBuffer == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for KeyBuffer"); + return UNIT_TEST_ERROR_TEST_FAILED; + } + + Status = RsaGetKey (mRsa, RsaKeyN, KeyBuffer, &KeySize); UT_ASSERT_TRUE (Status); UT_ASSERT_EQUAL (KeySize, sizeof (RsaN)); @@ -148,7 +153,12 @@ TestVerifyRsaSetGetKeyComponents ( UT_ASSERT_EQUAL (KeySize, sizeof (RsaE)); KeyBuffer = AllocatePool (KeySize); - Status = RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize); + if (KeyBuffer == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for KeyBuffer"); + return UNIT_TEST_ERROR_TEST_FAILED; + } + + Status = RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize); UT_ASSERT_TRUE (Status); UT_ASSERT_EQUAL (KeySize, sizeof (RsaE)); @@ -204,7 +214,12 @@ TestVerifyRsaGenerateKeyComponents ( KeySize = RSA_MODULUS_LENGTH / 8; KeyBuffer = AllocatePool (KeySize); - Status = RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize); + if (KeyBuffer == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for KeyBuffer"); + return UNIT_TEST_ERROR_TEST_FAILED; + } + + Status = RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize); UT_ASSERT_TRUE (Status); UT_ASSERT_EQUAL (KeySize, 3); UT_ASSERT_MEM_EQUAL (KeyBuffer, DefaultPublicKey, 3); @@ -283,7 +298,12 @@ TestVerifyRsaPkcs1SignVerify ( UT_ASSERT_NOT_EQUAL (SigSize, 0); Signature = AllocatePool (SigSize); - Status = RsaPkcs1Sign (mRsa, HashValue, HashSize, Signature, &SigSize); + if (Signature == NULL) { + UT_LOG_ERROR ("Failed to allocate memory for Signature"); + return UNIT_TEST_ERROR_TEST_FAILED; + } + + Status = RsaPkcs1Sign (mRsa, HashValue, HashSize, Signature, &SigSize); UT_ASSERT_TRUE (Status); UT_ASSERT_EQUAL (SigSize, sizeof (RsaPkcs1Signature));