Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a432fda

Browse files
committed
Add back support for reading PEM files as an X509Certificate2.
The code did all of the work for reading the PEM-formatted certificate, but then didn't return the object. Added TestByteArrayConstructor_PEM, and broke the existing TestByteArrayConstructor into TestByteArrayConstructor_DER and UseAfterDispose; because it was really doing two separate things.
1 parent 27f77aa commit a432fda

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificatePal.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static unsafe ICertificatePal FromBlob(byte[] rawData, string password, X
3535
}
3636

3737
Interop.libcrypto.CheckValidOpenSslHandle(cert);
38+
39+
return new OpenSslX509CertificateReader(cert);
3840
}
3941

4042
// DER-X509

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,33 @@ public static void X509Cert2CreateFromPfxWithPassword()
165165
Assert.Equal("1.2.840.113549.1.1.1", cert2.GetKeyAlgorithm());
166166
}
167167
}
168+
169+
[Fact]
170+
public static void UseAfterDispose()
171+
{
172+
using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
173+
{
174+
IntPtr h = c.Handle;
175+
176+
// Do a couple of things that would only be true on a valid certificate, as a precondition.
177+
Assert.NotEqual(IntPtr.Zero, h);
178+
byte[] actualThumbprint = c.GetCertHash();
179+
180+
c.Dispose();
181+
182+
// For compat reasons, Dispose() acts like the now-defunct Reset() method rather than
183+
// causing ObjectDisposedExceptions.
184+
h = c.Handle;
185+
Assert.Equal(IntPtr.Zero, h);
186+
Assert.Throws<CryptographicException>(() => c.GetCertHash());
187+
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
188+
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
189+
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
190+
Assert.Throws<CryptographicException>(() => c.GetPublicKey());
191+
Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
192+
Assert.Throws<CryptographicException>(() => c.Issuer);
193+
Assert.Throws<CryptographicException>(() => c.Subject);
194+
}
195+
}
168196
}
169197
}

src/System.Security.Cryptography.X509Certificates/tests/CtorTests.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void TestDefaultConstructor()
3737
}
3838

3939
[Fact]
40-
public static void TestByteArrayConstructor()
40+
public static void TestByteArrayConstructor_DER()
4141
{
4242
byte[] expectedThumbPrint = new byte[]
4343
{
@@ -49,24 +49,28 @@ public static void TestByteArrayConstructor()
4949
using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
5050
{
5151
IntPtr h = c.Handle;
52-
object ignored;
5352
Assert.NotEqual(IntPtr.Zero, h);
5453
byte[] actualThumbprint = c.GetCertHash();
5554
Assert.Equal(expectedThumbPrint, actualThumbprint);
55+
}
56+
}
5657

57-
c.Dispose();
58+
[Fact]
59+
public static void TestByteArrayConstructor_PEM()
60+
{
61+
byte[] expectedThumbPrint =
62+
{
63+
0x10, 0x8e, 0x2b, 0xa2, 0x36, 0x32, 0x62, 0x0c,
64+
0x42, 0x7c, 0x57, 0x0b, 0x6d, 0x9d, 0xb5, 0x1a,
65+
0xc3, 0x13, 0x87, 0xfe,
66+
};
5867

59-
// For compat reasons, Dispose() acts like the now-defunct Reset() method rather than causing ObjectDisposedExceptions.
60-
h = c.Handle;
61-
Assert.Equal(IntPtr.Zero, h);
62-
Assert.Throws<CryptographicException>(() => c.GetCertHash());
63-
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
64-
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
65-
Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
66-
Assert.Throws<CryptographicException>(() => c.GetPublicKey());
67-
Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
68-
Assert.Throws<CryptographicException>(() => ignored = c.Issuer);
69-
Assert.Throws<CryptographicException>(() => ignored = c.Subject);
68+
using (X509Certificate2 cert = new X509Certificate2(TestData.MsCertificatePemBytes))
69+
{
70+
IntPtr h = cert.Handle;
71+
Assert.NotEqual(IntPtr.Zero, h);
72+
byte[] actualThumbprint = cert.GetCertHash();
73+
Assert.Equal(expectedThumbPrint, actualThumbprint);
7074
}
7175
}
7276

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ internal static class TestData
5050
"aee25d3ef575c7e6666360ccd59a84878d2430f7ef34d0631db142674a0e4bbf" +
5151
"3a0eefb6953aa738e4259208a6886682").HexToByteArray();
5252

53+
public static readonly byte[] MsCertificatePemBytes = ByteUtils.AsciiBytes(
54+
@"-----BEGIN CERTIFICATE-----
55+
MIIE7DCCA9SgAwIBAgITMwAAALARrwqL0Duf3QABAAAAsDANBgkqhkiG9w0BAQUF
56+
ADB5MQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMH
57+
UmVkbW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9uMSMwIQYDVQQD
58+
ExpNaWNyb3NvZnQgQ29kZSBTaWduaW5nIFBDQTAeFw0xMzAxMjQyMjMzMzlaFw0x
59+
NDA0MjQyMjMzMzlaMIGDMQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3Rv
60+
bjEQMA4GA1UEBxMHUmVkbW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0
61+
aW9uMQ0wCwYDVQQLEwRNT1BSMR4wHAYDVQQDExVNaWNyb3NvZnQgQ29ycG9yYXRp
62+
b24wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDor1yiIA34KHy8BXt/
63+
re7rdqwoUz8620B9s44z5lc/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+
aAko+QKX7xEDumdSrcHps3Om0mPNSuI+5PNO/f+h4LsCEztdIN5VP6OukEAxOHUo
80+
XgSpRm3m9Xp5QL0fzehF1a7iXT71dcfmZmNgzNWahIeNJDD37zTQYx2xQmdKDku/
81+
Og7vtpU6pzjkJZIIpohmgg==
82+
-----END CERTIFICATE-----
83+
");
84+
5385
public const string PfxDataPassword = "12345";
5486

5587
public static byte[] PfxData = (

0 commit comments

Comments
 (0)