As part of our CI process we have a number of unit tests running. As part of our testing suite, we generate a self signed certificate and use that to sign a mail. This works on my Windows 10 dev machine and our Windows 2016 build server. We recently switched to Windows 2019 domain joined build servers and now our tests start failing when trying to sign messages with the error:
Unable to export private key in order to use a more capable algorithm
We are using 2018R4.
Code to generate the test certificate:
public static X509Certificate2 CreateTestCertificate()
{
// Generate asymmetric key pair.
var rsa = RSA.Create(2048);
// Set up the certificate properties.
var request = new CertificateRequest("CN=doctor@who.com, O=WHO, OU=LAB, L=SEA, S=xxx, C=US, E=doctor@who.com", rsa, HashAlgorithmName.SHA512, RSASignaturePadding.Pss);
request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
// Set up the subject key identifier.
request.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(request.PublicKey, false));
// Set up the enhanced key usage field.
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection
{
new Oid("1.3.6.1.5.5.7.3.2"), // Client Authentication
new Oid("1.3.6.1.5.5.7.3.4") // Secure Email
},
true));
// Set up the SAN.
var sanBuilder = new SubjectAlternativeNameBuilder();
sanBuilder.AddEmailAddress("doctor@who.com");
request.CertificateExtensions.Add(sanBuilder.Build());
// Set up validity dates.
var notBefore = new DateTimeOffset(DateTime.UtcNow.AddDays(-1));
var notAfter = new DateTimeOffset(DateTime.UtcNow.AddYears(1));
// Generate the certificate.
var certificate = request.CreateSelfSigned(notBefore, notAfter);
certificate.FriendlyName = "doctor@who.com";
return new X509Certificate2(certificate.Export(X509ContentType.Pfx, "SuperSecret"), "SuperSecret", X509KeyStorageFlags.MachineKeySet);
}