protected override AsymmetricAlgorithm LoadKey(ReadOnlyMemory <byte> pkcs8) { PrivateKeyInfoAsn privateKeyInfo = PrivateKeyInfoAsn.Decode(pkcs8, AsnEncodingRules.BER); AsymmetricAlgorithm key; switch (privateKeyInfo.PrivateKeyAlgorithm.Algorithm) { case Oids.Rsa: key = new RSAImplementation.RSASecurityTransforms(); break; case Oids.EcDiffieHellman: case Oids.EcPublicKey: key = new ECDsaImplementation.ECDsaSecurityTransforms(); break; default: throw new CryptographicException( SR.Cryptography_UnknownAlgorithmIdentifier, privateKeyInfo.PrivateKeyAlgorithm.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.Value != 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];
private AsnWriter WritePkcs8() { PrivateKeyInfoAsn info = new PrivateKeyInfoAsn { PrivateKeyAlgorithm = { Algorithm = AlgorithmId, }, PrivateKey = PrivateKeyBytes, }; if (AlgorithmParameters?.Length > 0) { info.PrivateKeyAlgorithm.Parameters = AlgorithmParameters; } if (Attributes.Count > 0) { info.Attributes = PkcsHelpers.NormalizeAttributeSet(CmsSigner.BuildAttributes(Attributes).ToArray()); } // Write in BER in case any of the provided fields was BER. AsnWriter writer = new AsnWriter(AsnEncodingRules.BER); info.Encode(writer); return(writer); }
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: case Oids.Dsa: // TODO: [AndroidCrypto] Handle RSA / 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); }
public static Pkcs8PrivateKeyInfo Decode( ReadOnlyMemory <byte> source, out int bytesRead, bool skipCopy = false) { if (!skipCopy) { AsnReader reader = new AsnReader(source, AsnEncodingRules.BER); source = reader.GetEncodedValue().ToArray(); } PrivateKeyInfoAsn privateKeyInfo = AsnSerializer.Deserialize <PrivateKeyInfoAsn>(source, AsnEncodingRules.BER, out bytesRead); return(new Pkcs8PrivateKeyInfo( privateKeyInfo.PrivateKeyAlgorithm.Algorithm, privateKeyInfo.PrivateKeyAlgorithm.Parameters, privateKeyInfo.PrivateKey, SignerInfo.MakeAttributeCollection(privateKeyInfo.Attributes))); }
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))); }