示例#1
0
        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.Dsa:
                key = new DSAImplementation.DSASecurityTransforms();
                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);
        }
示例#2
0
        public ICertificatePal CopyWithPrivateKey(DSA privateKey)
        {
            var typedKey = privateKey as DSAImplementation.DSASecurityTransforms;

            if (typedKey != null)
            {
                return(CopyWithPrivateKey(typedKey.GetKeys()));
            }

            DSAParameters dsaParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(dsaParameters.X))
                using (typedKey = new DSAImplementation.DSASecurityTransforms())
                {
                    typedKey.ImportParameters(dsaParameters);
                    return(CopyWithPrivateKey(typedKey.GetKeys()));
                }
        }