protected override AsymmetricAlgorithm LoadKey(ReadOnlyMemory <byte> pkcs8) { PrivateKeyInfoAsn privateKeyInfo = PrivateKeyInfoAsn.Decode(pkcs8, AsnEncodingRules.BER); AsymmetricAlgorithm key; switch (privateKeyInfo.PrivateKeyAlgorithm.Algorithm.Value) { case Oids.Rsa: key = new RSAOpenSsl(); break; case Oids.Dsa: key = new DSAOpenSsl(); break; case Oids.EcDiffieHellman: case Oids.EcPublicKey: key = new ECDiffieHellmanOpenSsl(); break; default: throw new CryptographicException( SR.Cryptography_UnknownAlgorithmIdentifier, privateKeyInfo.PrivateKeyAlgorithm.Algorithm.Value); } key.ImportPkcs8PrivateKey(pkcs8.Span, out int bytesRead); if (bytesRead != pkcs8.Length) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } return(key); }
protected override AsymmetricAlgorithm LoadKey(ReadOnlyMemory <byte> pkcs8) { PrivateKeyInfoAsn privateKeyInfo = PrivateKeyInfoAsn.Decode(pkcs8, AsnEncodingRules.BER); AsymmetricAlgorithm key; string algorithm = privateKeyInfo.PrivateKeyAlgorithm.Algorithm; switch (algorithm) { case Oids.Rsa: key = new RSAImplementation.RSAAndroid(); break; case Oids.Dsa: // TODO: [AndroidCrypto] Handle DSA throw new NotImplementedException($"{nameof(LoadKey)} ({algorithm})"); case Oids.EcDiffieHellman: case Oids.EcPublicKey: key = new ECDsaImplementation.ECDsaAndroid(); break; default: throw new CryptographicException(SR.Cryptography_UnknownAlgorithmIdentifier, algorithm); } key.ImportPkcs8PrivateKey(pkcs8.Span, out int bytesRead); if (bytesRead != pkcs8.Length) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } return(key); }
private static unsafe AsnWriter?RewritePkcs8ECPrivateKeyWithZeroPublicKey(ReadOnlySpan <byte> source) { fixed(byte *ptr = &MemoryMarshal.GetReference(source)) { using (MemoryManager <byte> manager = new PointerMemoryManager <byte>(ptr, source.Length)) { PrivateKeyInfoAsn privateKeyInfo = PrivateKeyInfoAsn.Decode(manager.Memory, AsnEncodingRules.BER); AlgorithmIdentifierAsn privateAlgorithm = privateKeyInfo.PrivateKeyAlgorithm; if (privateAlgorithm.Algorithm != Oids.EcPublicKey) { return(null); } ECPrivateKey privateKey = ECPrivateKey.Decode(privateKeyInfo.PrivateKey, AsnEncodingRules.BER); EccKeyFormatHelper.FromECPrivateKey(privateKey, privateAlgorithm, out ECParameters ecParameters); fixed(byte *pD = ecParameters.D) { try { if (!ecParameters.Curve.IsExplicit || ecParameters.Q.X != null || ecParameters.Q.Y != null) { return(null); } byte[] zero = new byte[ecParameters.D !.Length];
public static Pkcs8PrivateKeyInfo Decode( ReadOnlyMemory <byte> source, out int bytesRead, bool skipCopy = false) { AsnReader reader = new AsnReader(source, AsnEncodingRules.BER); if (!skipCopy) { reader = new AsnReader(reader.ReadEncodedValue().ToArray(), AsnEncodingRules.BER); } int localRead = reader.PeekEncodedValue().Length; PrivateKeyInfoAsn.Decode(reader, out PrivateKeyInfoAsn privateKeyInfo); bytesRead = localRead; return(new Pkcs8PrivateKeyInfo( privateKeyInfo.PrivateKeyAlgorithm.Algorithm, privateKeyInfo.PrivateKeyAlgorithm.Parameters, privateKeyInfo.PrivateKey, SignerInfo.MakeAttributeCollection(privateKeyInfo.Attributes))); }