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

Commit baaa017

Browse files
committed
Merge pull request #2006 from bartonjs/enable-rsa-x509-unix-tests
Fix and enable RSA and X509Certificates tests on Unix
2 parents 959c018 + 6059798 commit baaa017

File tree

15 files changed

+160
-11
lines changed

15 files changed

+160
-11
lines changed

src/System.Security.Cryptography.RSA/src/Internal/Cryptography/RsaOpenSsl.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public override RSAParameters ExportParameters(bool includePrivateParameters)
145145

146146
public override unsafe void ImportParameters(RSAParameters parameters)
147147
{
148+
ValidateParameters(ref parameters);
149+
148150
SafeRsaHandle key = Interop.libcrypto.RSA_new();
149151
bool imported = false;
150152

@@ -209,6 +211,35 @@ private void FreeKey()
209211
}
210212
}
211213

214+
private static void ValidateParameters(ref RSAParameters parameters)
215+
{
216+
if (parameters.Modulus == null || parameters.Exponent == null)
217+
throw new CryptographicException(SR.Argument_InvalidValue);
218+
219+
if (parameters.D == null)
220+
{
221+
if (parameters.P != null ||
222+
parameters.DP != null ||
223+
parameters.Q != null ||
224+
parameters.DQ != null ||
225+
parameters.InverseQ != null)
226+
{
227+
throw new CryptographicException(SR.Argument_InvalidValue);
228+
}
229+
}
230+
else
231+
{
232+
if (parameters.P == null ||
233+
parameters.DP == null ||
234+
parameters.Q == null ||
235+
parameters.DQ == null ||
236+
parameters.InverseQ == null)
237+
{
238+
throw new CryptographicException(SR.Argument_InvalidValue);
239+
}
240+
}
241+
}
242+
212243
private static void CheckInvalidKey(SafeRsaHandle key)
213244
{
214245
if (key == null || key.IsInvalid)

src/System.Security.Cryptography.RSA/tests/System.Security.Cryptography.RSA.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<OutputType>Library</OutputType>
99
<AssemblyName>System.Security.Cryptography.RSA.Tests</AssemblyName>
1010
<RootNamespace>System.Security.Cryptography.Rsa.Tests</RootNamespace>
11-
<!-- Disabled on Linux/OSX (Issue 1986) -->
12-
<UnsupportedPlatforms>Linux;OSX</UnsupportedPlatforms>
1311
</PropertyGroup>
1412
<ItemGroup>
1513
<ProjectReference Include="..\src\System.Security.Cryptography.RSA.csproj">

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ internal unsafe OpenSslX509CertificateReader(byte[] data)
3030

3131
_cert = Interop.libcrypto.d2i_X509(IntPtr.Zero, ppData, data.Length);
3232

33+
if (_cert.IsInvalid)
34+
{
35+
throw new CryptographicException();
36+
}
37+
3338
// X509_check_purpose has the effect of populating the sha1_hash value,
3439
// and other "initialize" type things.
3540
bool init = Interop.libcrypto.X509_check_purpose(_cert, -1, 0);
@@ -53,7 +58,7 @@ public AsymmetricAlgorithm PrivateKey
5358

5459
public IntPtr Handle
5560
{
56-
get { return IntPtr.Zero; }
61+
get { return _cert == null ? IntPtr.Zero : _cert.DangerousGetHandle(); }
5762
}
5863

5964
public string Issuer
@@ -135,7 +140,12 @@ public byte[] SerialNumber
135140
get
136141
{
137142
IntPtr serialNumberPtr = Interop.libcrypto.X509_get_serialNumber(_cert);
138-
return Interop.NativeCrypto.GetAsn1StringBytes(serialNumberPtr);
143+
byte[] serial = Interop.NativeCrypto.GetAsn1StringBytes(serialNumberPtr);
144+
145+
// Windows returns this in BigInteger Little-Endian,
146+
// OpenSSL returns this in BigInteger Big-Endian.
147+
Array.Reverse(serial);
148+
return serial;
139149
}
140150
}
141151

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public static void X509Cert2Test()
8181
/// This test is for excerising X509Store and X509Chain code without actually installing any certificate
8282
/// </summary>
8383
[Fact]
84+
[ActiveIssue(1993, PlatformID.AnyUnix)]
8485
public static void X509CertStoreChain()
8586
{
8687
X509Store store = new X509Store("My", StoreLocation.LocalMachine);
@@ -104,6 +105,7 @@ public static void X509CertStoreChain()
104105
}
105106

106107
[Fact]
108+
[ActiveIssue(1993, PlatformID.AnyUnix)]
107109
public static void X509Cert2ToStringVerbose()
108110
{
109111
X509Store store = new X509Store("My", StoreLocation.CurrentUser);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public static void ImportNull()
154154
}
155155

156156
[Fact]
157+
[ActiveIssue(1993, PlatformID.AnyUnix)]
157158
public static void ImportPfx()
158159
{
159160
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
@@ -175,6 +176,7 @@ public static void ImportPfx()
175176
}
176177

177178
[Fact]
179+
[ActiveIssue(1993, PlatformID.AnyUnix)]
178180
public static void ImportStoreSavedAsCerData()
179181
{
180182
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
@@ -196,6 +198,7 @@ public static void ImportStoreSavedAsCerData()
196198
}
197199

198200
[Fact]
201+
[ActiveIssue(1993, PlatformID.AnyUnix)]
199202
public static void ImportStoreSavedAsSerializedCerData()
200203
{
201204
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
@@ -217,6 +220,7 @@ public static void ImportStoreSavedAsSerializedCerData()
217220
}
218221

219222
[Fact]
223+
[ActiveIssue(1993, PlatformID.AnyUnix)]
220224
public static void ImportStoreSavedAsSerializedStoreData()
221225
{
222226
using (var msCer = new X509Certificate2(TestData.MsCertificate))
@@ -241,6 +245,7 @@ public static void ImportStoreSavedAsSerializedStoreData()
241245
}
242246

243247
[Fact]
248+
[ActiveIssue(1993, PlatformID.AnyUnix)]
244249
public static void ImportStoreSavedAsPfxData()
245250
{
246251
using (var msCer = new X509Certificate2(TestData.MsCertificate))
@@ -285,24 +290,28 @@ public static void ImportFromFileTests()
285290
}
286291

287292
[Fact]
293+
[ActiveIssue(1993, PlatformID.AnyUnix)]
288294
public static void ExportCert()
289295
{
290296
TestExportSingleCert(X509ContentType.Cert);
291297
}
292298

293299
[Fact]
300+
[ActiveIssue(1993, PlatformID.AnyUnix)]
294301
public static void ExportSerializedCert()
295302
{
296303
TestExportSingleCert(X509ContentType.SerializedCert);
297304
}
298305

299306
[Fact]
307+
[ActiveIssue(1993, PlatformID.AnyUnix)]
300308
public static void ExportSerializedStore()
301309
{
302310
TestExportStore(X509ContentType.SerializedStore);
303311
}
304312

305313
[Fact]
314+
[ActiveIssue(1993, PlatformID.AnyUnix)]
306315
public static void ExportPkcs7()
307316
{
308317
TestExportStore(X509ContentType.Pkcs7);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static void TestByteArrayConstructor()
7171
}
7272
}
7373

74+
[Fact]
7475
public static void TestNullConstructorArguments()
7576
{
7677
Assert.Throws<ArgumentException>(() => new X509Certificate2((byte[])null, (String)null));
@@ -98,5 +99,11 @@ public static void TestNullConstructorArguments()
9899
}
99100
}
100101
}
102+
103+
[Fact]
104+
public static void InvalidCertificateBlob()
105+
{
106+
Assert.Throws<CryptographicException>(() => new X509Certificate2(new byte[] { 0x01, 0x02, 0x03 }));
107+
}
101108
}
102109
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace System.Security.Cryptography.X509Certificates.Tests
88
public static class ExportTests
99
{
1010
[Fact]
11+
[ActiveIssue(1993, PlatformID.AnyUnix)]
1112
public static void ExportAsCert()
1213
{
1314
using (X509Certificate2 c1 = new X509Certificate2(TestData.MsCertificate))
@@ -19,6 +20,7 @@ public static void ExportAsCert()
1920
}
2021

2122
[Fact]
23+
[ActiveIssue(1993, PlatformID.AnyUnix)]
2224
public static void ExportAsSerializedCert()
2325
{
2426
using (X509Certificate2 c1 = new X509Certificate2(TestData.MsCertificate))
@@ -36,6 +38,7 @@ public static void ExportAsSerializedCert()
3638
}
3739

3840
[Fact]
41+
[ActiveIssue(1993, PlatformID.AnyUnix)]
3942
public static void ExportAsPfx()
4043
{
4144
using (X509Certificate2 c1 = new X509Certificate2(TestData.MsCertificate))
@@ -52,6 +55,7 @@ public static void ExportAsPfx()
5255
}
5356

5457
[Fact]
58+
[ActiveIssue(1993, PlatformID.AnyUnix)]
5559
public static void ExportAsPfxWithPassword()
5660
{
5761
const string password = "Cotton";
@@ -70,6 +74,7 @@ public static void ExportAsPfxWithPassword()
7074
}
7175

7276
[Fact]
77+
[ActiveIssue(1993, PlatformID.AnyUnix)]
7378
public static void ExportAsPfxVerifyPassword()
7479
{
7580
const string password = "Cotton";

0 commit comments

Comments
 (0)