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

Commit 870e5a2

Browse files
committed
Add support for private keys in X509Certificate2 objects on Unix.
1 parent 8b05acb commit 870e5a2

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public List<OpenSslX509CertificateReader> ReadCertificates()
120120
OpenSslX509CertificateReader reader = new OpenSslX509CertificateReader(_x509Handle);
121121
_x509Handle = null;
122122

123+
if (_evpPkeyHandle != null && !_evpPkeyHandle.IsInvalid)
124+
{
125+
reader.SetPrivateKey(_evpPkeyHandle);
126+
_evpPkeyHandle = null;
127+
}
128+
123129
certs.Add(reader);
124130
}
125131

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal sealed class OpenSslX509CertificateReader : ICertificatePal
1818
private static DateTimeFormatInfo s_validityDateTimeFormatInfo;
1919

2020
private SafeX509Handle _cert;
21+
private SafeEvpPkeyHandle _privateKey;
2122
private X500DistinguishedName _subjectName;
2223
private X500DistinguishedName _issuerName;
2324

@@ -37,7 +38,7 @@ internal OpenSslX509CertificateReader(SafeX509Handle handle)
3738

3839
public bool HasPrivateKey
3940
{
40-
get { return false; }
41+
get { return _privateKey != null; }
4142
}
4243

4344
public IntPtr Handle
@@ -228,9 +229,22 @@ public IEnumerable<X509Extension> Extensions
228229
}
229230
}
230231

232+
internal void SetPrivateKey(SafeEvpPkeyHandle privateKey)
233+
{
234+
_privateKey = privateKey;
235+
}
236+
231237
public RSA GetRSAPrivateKey()
232238
{
233-
return null;
239+
if (_privateKey == null || _privateKey.IsInvalid)
240+
{
241+
return null;
242+
}
243+
244+
using (SafeRsaHandle rsaHandle = Interop.libcrypto.EVP_PKEY_get1_RSA(_privateKey))
245+
{
246+
return new RSAOpenSsl(rsaHandle.DangerousGetHandle());
247+
}
234248
}
235249

236250
public string GetNameInfo(X509NameType nameType, bool forIssuer)
@@ -258,10 +272,25 @@ public string GetNameInfo(X509NameType nameType, bool forIssuer)
258272

259273
public void AppendPrivateKeyInfo(StringBuilder sb)
260274
{
275+
if (!HasPrivateKey)
276+
{
277+
return;
278+
}
279+
280+
// There's nothing really to say about the key, just acknowledge there is one.
281+
sb.AppendLine();
282+
sb.AppendLine();
283+
sb.AppendLine("[Private Key]");
261284
}
262285

263286
public void Dispose()
264287
{
288+
if (_privateKey != null)
289+
{
290+
_privateKey.Dispose();
291+
_privateKey = null;
292+
}
293+
265294
if (_cert != null)
266295
{
267296
_cert.Dispose();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public static void TestRawData()
5757
}
5858

5959
[Fact]
60-
[ActiveIssue(1993, PlatformID.AnyUnix)]
6160
public static void TestPrivateKey()
6261
{
6362
using (var c = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))

0 commit comments

Comments
 (0)