public void VerifyECDecoderWithRSAKey() { byte[] data = Convert.FromBase64String(RsaPrivateKey); Exception ex = Assert.Throws <InvalidDataException>(() => LightweightPkcs8Decoder.DecodeECDsaPkcs8(data, null)); Assert.AreEqual("Invalid PKCS#8 Data", ex.Message); }
public void ECDecoderPrime256v1RequiresPublicKey() { #if NET461 Assert.Ignore("ECC is not supported before .NET Framework 4.7"); #endif byte[] data = Convert.FromBase64String(EcPrime256v1PrivateKeyImported); Exception ex = Assert.Throws <InvalidDataException>(() => LightweightPkcs8Decoder.DecodeECDsaPkcs8(data, null)); Assert.AreEqual("Unsupported PKCS#8 Data", ex.Message); }
// Not using TestCaseSource because params too long for friendly test case rendering. private static void VerifyECDecoder(string key, CertificateKeyCurveName keyCurveName, string signature, string cer = null) { #if NET461 Assert.Ignore("ECC is not supported before .NET Framework 4.7"); #endif byte[] data = Convert.FromBase64String(key); byte[] signatureBytes = Convert.FromBase64String(signature); ECDsa publicKey = null; try { if (cer != null) { byte[] publicKeyData = Convert.FromBase64String(cer); using X509Certificate2 certificate = new X509Certificate2(publicKeyData); publicKey = certificate.GetECDsaPublicKey(); } using ECDsa keyPair = LightweightPkcs8Decoder.DecodeECDsaPkcs8(data, publicKey); Assert.AreEqual(keyCurveName.GetKeySize(), keyPair.KeySize); Assert.IsTrue(keyPair.VerifyData(Encoding.UTF8.GetBytes("test"), signatureBytes, HashAlgorithmName.SHA256)); } catch (Exception ex) when( (ex is CryptographicException || (ex is TargetInvocationException && ex.InnerException is CryptographicException)) && RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && keyCurveName == CertificateKeyCurveName.P256 || keyCurveName == CertificateKeyCurveName.P256K) { Assert.Ignore("The curve is not supported by the current platform"); } finally { publicKey?.Dispose(); } }