Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for exercising certificates with platform provider keys #80558

Merged
merged 4 commits into from
Jan 16, 2023
Merged
Changes from 3 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 @@ -571,6 +571,86 @@ public static void SignatureAlgorithmOidReadableForGostCertificate()
}
}

[ConditionalFact(typeof(PlatformSupport), nameof(PlatformSupport.PlatformCryptoProviderFunctional))]
[OuterLoop("Hardware backed key generation takes several seconds.")]
public static void CreateCertificate_MicrosoftPlatformCryptoProvider_EcdsaKey()
{
CngKey key = null;

try
{
CngKeyCreationParameters cngCreationParameters = new CngKeyCreationParameters
{
Provider = CngProvider.MicrosoftPlatformCryptoProvider,
KeyCreationOptions = CngKeyCreationOptions.OverwriteExistingKey,
};

key = CngKey.Create(
vcsjones marked this conversation as resolved.
Show resolved Hide resolved
CngAlgorithm.ECDsaP384,
nameof(CreateCertificate_MicrosoftPlatformCryptoProvider_EcdsaKey),
cngCreationParameters);

using (ECDsaCng ecdsa = new ECDsaCng(key))
{
CertificateRequest req = new CertificateRequest("CN=potato", ecdsa, HashAlgorithmName.SHA256);

using (X509Certificate2 cert = req.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow))
using (ECDsa certKey = cert.GetECDsaPrivateKey())
{
Assert.NotNull(certKey);
byte[] data = new byte[] { 12, 11, 02, 08, 25, 14, 11, 18, 16 };
byte[] signature = certKey.SignData(data, HashAlgorithmName.SHA256);
bool valid = ecdsa.VerifyData(data, signature, HashAlgorithmName.SHA256);
Assert.True(valid, "valid signature");
vcsjones marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
finally
{
key?.Delete();
}
}

[ConditionalFact(typeof(PlatformSupport), nameof(PlatformSupport.PlatformCryptoProviderFunctional))]
[OuterLoop("Hardware backed key generation takes several seconds.")]
public static void CreateCertificate_MicrosoftPlatformCryptoProvider_RsaKey()
{
CngKey key = null;

try
{
CngKeyCreationParameters cngCreationParameters = new CngKeyCreationParameters
{
Provider = CngProvider.MicrosoftPlatformCryptoProvider,
KeyCreationOptions = CngKeyCreationOptions.OverwriteExistingKey,
};

key = CngKey.Create(
CngAlgorithm.Rsa,
nameof(CreateCertificate_MicrosoftPlatformCryptoProvider_RsaKey),
cngCreationParameters);

using (RSACng rsa = new RSACng(key))
{
CertificateRequest req = new CertificateRequest("CN=potato", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

using (X509Certificate2 cert = req.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow))
using (RSA certKey = cert.GetRSAPrivateKey())
{
Assert.NotNull(certKey);
byte[] data = new byte[] { 12, 11, 02, 08, 25, 14, 11, 18, 16 };
byte[] signature = certKey.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
bool valid = rsa.VerifyData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
Assert.True(valid, "valid signature");
}
}
}
finally
{
key?.Delete();
}
}

public static IEnumerable<object[]> StorageFlags => CollectionImportTests.StorageFlags;
}
}