Skip to content

Commit 7c2d8f8

Browse files
krwqcarlossanlop
authored andcommitted
Add null checks in System.Security.Cryptography
1 parent dd209b9 commit 7c2d8f8

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

src/libraries/Native/Unix/System.Security.Cryptography.Native/apibridge.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int32_t local_X509_get_version(const X509* x509)
112112

113113
X509_PUBKEY* local_X509_get_X509_PUBKEY(const X509* x509)
114114
{
115-
if (x509)
115+
if (x509 && x509->cert_info)
116116
{
117117
return x509->cert_info->key;
118118
}
@@ -123,13 +123,28 @@ X509_PUBKEY* local_X509_get_X509_PUBKEY(const X509* x509)
123123
int32_t local_X509_PUBKEY_get0_param(
124124
ASN1_OBJECT** palgOid, const uint8_t** pkeyBytes, int* pkeyBytesLen, X509_ALGOR** palg, X509_PUBKEY* pubkey)
125125
{
126+
if (!pubkey)
127+
{
128+
return 0;
129+
}
130+
126131
if (palgOid)
127132
{
133+
if (!pubkey->algor)
134+
{
135+
return 0;
136+
}
137+
128138
*palgOid = pubkey->algor->algorithm;
129139
}
130140

131141
if (pkeyBytes)
132142
{
143+
if (!pubkey->public_key)
144+
{
145+
return 0;
146+
}
147+
133148
*pkeyBytes = pubkey->public_key->data;
134149
*pkeyBytesLen = pubkey->public_key->length;
135150
}

src/libraries/Native/Unix/System.Security.Cryptography.Native/openssl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,11 @@ BIO* CryptoNative_GetX509NameInfo(X509* x509, int32_t nameType, int32_t forIssue
635635
0 == strncmp(localOid, szOidUpn, sizeof(szOidUpn)))
636636
{
637637
// OTHERNAME->ASN1_TYPE->union.field
638+
if (!value->value)
639+
{
640+
return NULL;
641+
}
642+
638643
str = value->value->value.asn1_string;
639644
}
640645
}

src/libraries/Native/Unix/System.Security.Cryptography.Native/pal_pkcs7.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,19 @@ int32_t CryptoNative_GetPkcs7Certificates(PKCS7* p7, X509Stack** certs)
4646
switch (OBJ_obj2nid(p7->type))
4747
{
4848
case NID_pkcs7_signed:
49+
if (!p7->d.sign)
50+
{
51+
return 0;
52+
}
53+
4954
*certs = p7->d.sign->cert;
5055
return 1;
5156
case NID_pkcs7_signedAndEnveloped:
57+
if (!p7->d.signed_and_enveloped)
58+
{
59+
return 0;
60+
}
61+
5262
*certs = p7->d.signed_and_enveloped->cert;
5363
return 1;
5464
}

src/libraries/System.Security.Cryptography.X509Certificates/tests/CertTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ public static void UseAfterDispose()
399399
}
400400
}
401401

402+
[Fact]
403+
public static void EmptyPkcs7ThrowsException()
404+
{
405+
Assert.ThrowsAny<CryptographicException>(() => new X509Certificate2(TestData.EmptyPkcs7));
406+
}
407+
402408
[Fact]
403409
public static void ExportPublicKeyAsPkcs12()
404410
{

src/libraries/System.Security.Cryptography.X509Certificates/tests/TestData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,5 +4175,7 @@ internal static DSAParameters GetDSA1024Params()
41754175
"C0CC2B115B9D33BD6E528E35670E5A6A8D9CF52199F8D693315C60D9ADAD54EF7FDCED36" +
41764176
"0C8C79E84D42AB5CB6355A70951B1ABF1F2B3FB8BEB7E3A8D6BA2293C0DB8C86B0BB060F" +
41774177
"0D6DB9939E88B998662A27F092634BBF21F58EEAAA").HexToByteArray();
4178+
4179+
internal static readonly byte[] EmptyPkcs7 = "300B06092A864886F70D010702".HexToByteArray();
41784180
}
41794181
}

0 commit comments

Comments
 (0)