Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

[release/3.1] Data loss - Don't delete private keys detected from SerializedCert imports #42881

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static ICertificatePal FromBlobOrFile(byte[] rawData, string fileName, S
bool loadFromFile = (fileName != null);

PfxCertStoreFlags pfxCertStoreFlags = MapKeyStorageFlags(keyStorageFlags);
bool persistKeySet = (0 != (keyStorageFlags & X509KeyStorageFlags.PersistKeySet));
bool deleteKeyContainer = false;

CertEncodingType msgAndCertEncodingType;
ContentType contentType;
Expand Down Expand Up @@ -86,9 +86,16 @@ out pCertContext
if (loadFromFile)
rawData = File.ReadAllBytes(fileName);
pCertContext = FilterPFXStore(rawData, password, pfxCertStoreFlags);

// If PersistKeySet is set we don't delete the key, so that it persists.
// If EphemeralKeySet is set we don't delete the key, because there's no file, so it's a wasteful call.
const X509KeyStorageFlags DeleteUnless =
X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.EphemeralKeySet;

deleteKeyContainer = ((keyStorageFlags & DeleteUnless) == 0);
}

CertificatePal pal = new CertificatePal(pCertContext, deleteKeyContainer: !persistKeySet);
CertificatePal pal = new CertificatePal(pCertContext, deleteKeyContainer);
pCertContext = null;
return pal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@ public static void X509Certificate2WithT61String()
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public static void SerializedCertDisposeDoesNotRemoveKeyFile()
{
using (X509Certificate2 fromPfx = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
Assert.True(fromPfx.HasPrivateKey, "fromPfx.HasPrivateKey - before");

byte[] serializedCert = fromPfx.Export(X509ContentType.SerializedCert);

using (X509Certificate2 fromSerialized = new X509Certificate2(serializedCert))
{
Assert.True(fromSerialized.HasPrivateKey, "fromSerialized.HasPrivateKey");
}

using (RSA key = fromPfx.GetRSAPrivateKey())
{
key.SignData(serializedCert, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
}

public static IEnumerable<object> StorageFlags => CollectionImportTests.StorageFlags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,35 @@ public static void X509ExtensionCollection_OidIndexer_NoMatchByFriendlyName()
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public static void SerializedCertDisposeDoesNotRemoveKeyFile()
{
using (X509Certificate2 fromPfx = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
Assert.True(fromPfx.HasPrivateKey, "fromPfx.HasPrivateKey - before");

byte[] serializedCert = fromPfx.Export(X509ContentType.SerializedCert);

X509Certificate2 fromSerialized;

using (ImportedCollection imported = Cert.Import(serializedCert))
{
fromSerialized = imported.Collection[0];
Assert.True(fromSerialized.HasPrivateKey, "fromSerialized.HasPrivateKey");
Assert.NotEqual(IntPtr.Zero, fromSerialized.Handle);
}

// The certificate got disposed by the collection holder.
Assert.Equal(IntPtr.Zero, fromSerialized.Handle);

using (RSA key = fromPfx.GetRSAPrivateKey())
{
key.SignData(serializedCert, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
}

private static void TestExportSingleCert_SecureStringPassword(X509ContentType ct)
{
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.CreatePfxDataPasswordSecureString(), Cert.EphemeralIfPossible))
Expand Down