Skip to content

Commit 53383f2

Browse files
author
Alexander Dawes
committed
CSHARP-1914 Remove cloning of the certificates, as this was problematic as the private key is not included in the RawData of a cert and a cert cannot always be cloned in a deep manner (if it is non exportable)
1 parent 1ac46bd commit 53383f2

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

src/MongoDB.Driver/SslSettings.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public bool CheckCertificateRevocation
6363
/// </summary>
6464
public IEnumerable<X509Certificate> ClientCertificates
6565
{
66-
get { return (_clientCertificateCollection == null) ? null : ((IEnumerable)_clientCertificateCollection).Cast<X509Certificate>().Select(c => CloneCertificate(c)); }
66+
get { return (_clientCertificateCollection == null) ? null : ((IEnumerable)_clientCertificateCollection).Cast<X509Certificate>(); }
6767
set
6868
{
6969
if (_isFrozen) { throw new InvalidOperationException("SslSettings is frozen."); }
70-
_clientCertificateCollection = (value == null) ? null : new X509CertificateCollection(value.Select(c => CloneCertificate(c)).ToArray());
70+
_clientCertificateCollection = (value == null) ? null : new X509CertificateCollection(value.ToArray());
7171
}
7272
}
7373

@@ -250,21 +250,7 @@ public override string ToString()
250250

251251
return string.Format("{{{0}}}", string.Join(",", parts.ToArray()));
252252
}
253-
254-
// private methods
255-
private X509Certificate CloneCertificate(X509Certificate certificate)
256-
{
257-
var certificate2 = certificate as X509Certificate2;
258-
if (certificate2 != null)
259-
{
260-
return new X509Certificate2(certificate2.RawData);
261-
}
262-
else
263-
{
264-
return new X509Certificate(certificate.Export(X509ContentType.Cert));
265-
}
266-
}
267-
253+
268254
// nested classes
269255
private class X509CertificateCollectionEqualityComparer : IEqualityComparer<X509CertificateCollection>
270256
{

tests/MongoDB.Driver.Tests/SslSettingsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public void TestClientCertificates()
6969

7070
var certificateFileName = GetTestCertificateFileName();
7171
var clientCertificates = new[] { new X509Certificate2(certificateFileName, "password"), new X509Certificate2(certificateFileName, "password") };
72+
Assert.True(clientCertificates.All(cert => cert.HasPrivateKey));
7273
settings.ClientCertificates = clientCertificates;
7374
Assert.True(clientCertificates.SequenceEqual(settings.ClientCertificates));
74-
Assert.NotSame(clientCertificates[0], settings.ClientCertificates.ElementAt(0));
75-
Assert.NotSame(clientCertificates[1], settings.ClientCertificates.ElementAt(1));
76-
75+
Assert.True(settings.ClientCertificates.Cast<X509Certificate2>().All(cert => cert.HasPrivateKey));
76+
7777
settings.Freeze();
7878
Assert.True(clientCertificates.SequenceEqual(settings.ClientCertificates));
7979
Assert.Throws<InvalidOperationException>(() => { settings.ClientCertificates = clientCertificates; });

0 commit comments

Comments
 (0)