Make cert fixture lazy (#1412)

This commit is contained in:
Justin Kotalik 2018-09-19 12:26:14 -07:00 committed by GitHub
parent 4e57b0e1f1
commit b41f1f0cae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 40 deletions

View File

@ -10,65 +10,79 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
{ {
public class ClientCertificateFixture : IDisposable public class ClientCertificateFixture : IDisposable
{ {
public ClientCertificateFixture() private X509Certificate2 _certificate;
{
using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadWrite);
foreach (var cert in store.Certificates) public X509Certificate2 Certificate
{
get
{
if (_certificate != null)
{ {
if (cert.Issuer != "CN=IISIntegrationTest_Root") return _certificate;
{
continue;
}
Certificate = cert;
store.Close();
return;
} }
var parentKey = CreateKeyMaterial(2048); using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadWrite);
// On first run of the test, creates the certificate in the trusted root certificate authorities. foreach (var cert in store.Certificates)
var parentRequest = new CertificateRequest("CN=IISIntegrationTest_Root", parentKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); {
if (cert.Issuer != "CN=IISIntegrationTest_Root")
{
continue;
}
_certificate = cert;
store.Close();
return cert;
}
parentRequest.CertificateExtensions.Add( var parentKey = CreateKeyMaterial(2048);
new X509BasicConstraintsExtension(
certificateAuthority: true,
hasPathLengthConstraint: false,
pathLengthConstraint: 0,
critical: true));
parentRequest.CertificateExtensions.Add( // On first run of the test, creates the certificate in the trusted root certificate authorities.
new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.NonRepudiation, critical: true)); var parentRequest = new CertificateRequest("CN=IISIntegrationTest_Root", parentKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
parentRequest.CertificateExtensions.Add( parentRequest.CertificateExtensions.Add(
new X509SubjectKeyIdentifierExtension(parentRequest.PublicKey, false)); new X509BasicConstraintsExtension(
certificateAuthority: true,
hasPathLengthConstraint: false,
pathLengthConstraint: 0,
critical: true));
var notBefore = DateTimeOffset.Now.AddDays(-1); parentRequest.CertificateExtensions.Add(
var notAfter = DateTimeOffset.Now.AddYears(5); new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.NonRepudiation, critical: true));
var parentCert = parentRequest.CreateSelfSigned(notBefore, notAfter); parentRequest.CertificateExtensions.Add(
new X509SubjectKeyIdentifierExtension(parentRequest.PublicKey, false));
// Need to export/import the certificate to associate the private key with the cert. var notBefore = DateTimeOffset.Now.AddDays(-1);
var imported = parentCert; var notAfter = DateTimeOffset.Now.AddYears(5);
var export = parentCert.Export(X509ContentType.Pkcs12, ""); var parentCert = parentRequest.CreateSelfSigned(notBefore, notAfter);
imported = new X509Certificate2(export, "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
Array.Clear(export, 0, export.Length);
// Add the cert to the cert store // Need to export/import the certificate to associate the private key with the cert.
Certificate = imported; var imported = parentCert;
store.Add(certificate: imported); var export = parentCert.Export(X509ContentType.Pkcs12, "");
store.Close(); imported = new X509Certificate2(export, "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
Array.Clear(export, 0, export.Length);
// Add the cert to the cert store
_certificate = imported;
store.Add(certificate: imported);
store.Close();
return imported;
}
} }
} }
public X509Certificate2 Certificate { get; }
public void Dispose() public void Dispose()
{ {
if (_certificate == null)
{
return;
}
using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine)) using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
{ {
store.Open(OpenFlags.ReadWrite); store.Open(OpenFlags.ReadWrite);