Skip to content

Commit

Permalink
Add null checks in System.Security.Cryptography
Browse files Browse the repository at this point in the history
  • Loading branch information
krwq authored and carlossanlop committed Jan 15, 2024
1 parent b04063a commit 2ffe97a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Expand Up @@ -402,6 +402,12 @@ public static void UseAfterDispose()
}
}

[Fact]
public static void EmptyPkcs7ThrowsException()
{
Assert.ThrowsAny<CryptographicException>(() => new X509Certificate2(TestData.EmptyPkcs7));
}

[Fact]
public static void ExportPublicKeyAsPkcs12()
{
Expand Down
Expand Up @@ -4208,5 +4208,7 @@ internal static DSAParameters GetDSA1024Params()
"C0CC2B115B9D33BD6E528E35670E5A6A8D9CF52199F8D693315C60D9ADAD54EF7FDCED36" +
"0C8C79E84D42AB5CB6355A70951B1ABF1F2B3FB8BEB7E3A8D6BA2293C0DB8C86B0BB060F" +
"0D6DB9939E88B998662A27F092634BBF21F58EEAAA").HexToByteArray();

internal static readonly byte[] EmptyPkcs7 = "300B06092A864886F70D010702".HexToByteArray();
}
}
17 changes: 16 additions & 1 deletion src/native/libs/System.Security.Cryptography.Native/apibridge.c
Expand Up @@ -112,7 +112,7 @@ int32_t local_X509_get_version(const X509* x509)

X509_PUBKEY* local_X509_get_X509_PUBKEY(const X509* x509)
{
if (x509)
if (x509 && x509->cert_info)
{
return x509->cert_info->key;
}
Expand All @@ -123,13 +123,28 @@ X509_PUBKEY* local_X509_get_X509_PUBKEY(const X509* x509)
int32_t local_X509_PUBKEY_get0_param(
ASN1_OBJECT** palgOid, const uint8_t** pkeyBytes, int* pkeyBytesLen, X509_ALGOR** palg, X509_PUBKEY* pubkey)
{
if (!pubkey)
{
return 0;
}

if (palgOid)
{
if (!pubkey->algor)
{
return 0;
}

*palgOid = pubkey->algor->algorithm;
}

if (pkeyBytes)
{
if (!pubkey->public_key)
{
return 0;
}

*pkeyBytes = pubkey->public_key->data;
*pkeyBytesLen = pubkey->public_key->length;
}
Expand Down
5 changes: 5 additions & 0 deletions src/native/libs/System.Security.Cryptography.Native/openssl.c
Expand Up @@ -669,6 +669,11 @@ BIO* CryptoNative_GetX509NameInfo(X509* x509, int32_t nameType, int32_t forIssue
0 == strncmp(localOid, szOidUpn, sizeof(szOidUpn)))
{
// OTHERNAME->ASN1_TYPE->union.field
if (!value->value)
{
return NULL;
}

str = value->value->value.asn1_string;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/native/libs/System.Security.Cryptography.Native/pal_pkcs7.c
Expand Up @@ -53,9 +53,19 @@ int32_t CryptoNative_GetPkcs7Certificates(PKCS7* p7, X509Stack** certs)
switch (OBJ_obj2nid(p7->type))
{
case NID_pkcs7_signed:
if (!p7->d.sign)
{
return 0;
}

*certs = p7->d.sign->cert;
return 1;
case NID_pkcs7_signedAndEnveloped:
if (!p7->d.signed_and_enveloped)
{
return 0;
}

*certs = p7->d.signed_and_enveloped->cert;
return 1;
}
Expand Down

0 comments on commit 2ffe97a

Please sign in to comment.