Example #1
0
        public ICertificatePal CopyWithPrivateKey(DSA privateKey)
        {
            DSAOpenSsl typedKey = privateKey as DSAOpenSsl;

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

            DSAParameters dsaParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(dsaParameters.X))
                using (typedKey = new DSAOpenSsl(dsaParameters))
                {
                    return(CopyWithPrivateKey((SafeEvpPKeyHandle)typedKey.DuplicateKeyHandle()));
                }
        }
Example #2
0
        public ICertificatePal CopyWithPrivateKey(RSA privateKey)
        {
            var typedKey = privateKey as RSAImplementation.RSASecurityTransforms;

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

            byte[] rsaPrivateKey = privateKey.ExportRSAPrivateKey();

            using (PinAndClear.Track(rsaPrivateKey))
                using (SafeSecKeyRefHandle privateSecKey = Interop.AppleCrypto.ImportEphemeralKey(rsaPrivateKey, true))
                {
                    return(CopyWithPrivateKey(privateSecKey));
                }
        }
Example #3
0
        public ICertificatePal CopyWithPrivateKey(ECDiffieHellman privateKey)
        {
            var typedKey = privateKey as ECDiffieHellmanImplementation.ECDiffieHellmanSecurityTransforms;

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

            byte[] ecPrivateKey = privateKey.ExportECPrivateKey();

            using (PinAndClear.Track(ecPrivateKey))
                using (SafeSecKeyRefHandle privateSecKey = Interop.AppleCrypto.ImportEphemeralKey(ecPrivateKey, true))
                {
                    return(CopyWithPrivateKey(privateSecKey));
                }
        }
Example #4
0
        public ICertificatePal CopyWithPrivateKey(ECDsa privateKey)
        {
            var typedKey = privateKey as ECDsaImplementation.ECDsaSecurityTransforms;

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

            ECParameters ecParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(ecParameters.D))
                using (typedKey = new ECDsaImplementation.ECDsaSecurityTransforms())
                {
                    typedKey.ImportParameters(ecParameters);
                    return(CopyWithPrivateKey(typedKey.GetKeys()));
                }
        }
Example #5
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()));
                }
        }
Example #6
0
        public ICertificatePal CopyWithPrivateKey(ECDsa privateKey)
        {
            ECDsaOpenSsl typedKey = privateKey as ECDsaOpenSsl;

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

            ECParameters ecParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(ecParameters.D))
                using (typedKey = new ECDsaOpenSsl())
                {
                    typedKey.ImportParameters(ecParameters);

                    return(CopyWithPrivateKey((SafeEvpPKeyHandle)typedKey.DuplicateKeyHandle()));
                }
        }
        public ICertificatePal CopyWithPrivateKey(RSA rsa)
        {
            RSACng          rsaCng = rsa as RSACng;
            ICertificatePal clone  = null;

            if (rsaCng != null)
            {
                clone = CopyWithPersistedCngKey(rsaCng.Key);

                if (clone != null)
                {
                    return(clone);
                }
            }

            RSACryptoServiceProvider rsaCsp = rsa as RSACryptoServiceProvider;

            if (rsaCsp != null)
            {
                clone = CopyWithPersistedCapiKey(rsaCsp.CspKeyContainerInfo);

                if (clone != null)
                {
                    return(clone);
                }
            }

            RSAParameters privateParameters = rsa.ExportParameters(true);

            using (PinAndClear.Track(privateParameters.D))
                using (PinAndClear.Track(privateParameters.P))
                    using (PinAndClear.Track(privateParameters.Q))
                        using (PinAndClear.Track(privateParameters.DP))
                            using (PinAndClear.Track(privateParameters.DQ))
                                using (PinAndClear.Track(privateParameters.InverseQ))
                                    using (RSACng clonedKey = new RSACng())
                                    {
                                        clonedKey.ImportParameters(privateParameters);

                                        return(CopyWithEphemeralKey(clonedKey.Key));
                                    }
        }
Example #8
0
        public ICertificatePal CopyWithPrivateKey(RSA privateKey)
        {
            RSAOpenSsl typedKey = privateKey as RSAOpenSsl;

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

            RSAParameters rsaParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(rsaParameters.D))
                using (PinAndClear.Track(rsaParameters.P))
                    using (PinAndClear.Track(rsaParameters.Q))
                        using (PinAndClear.Track(rsaParameters.DP))
                            using (PinAndClear.Track(rsaParameters.DQ))
                                using (PinAndClear.Track(rsaParameters.InverseQ))
                                    using (typedKey = new RSAOpenSsl(rsaParameters))
                                    {
                                        return(CopyWithPrivateKey((SafeEvpPKeyHandle)typedKey.DuplicateKeyHandle()));
                                    }
        }
Example #9
0
        public ICertificatePal CopyWithPrivateKey(RSA privateKey)
        {
            var typedKey = privateKey as RSAImplementation.RSASecurityTransforms;

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

            RSAParameters rsaParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(rsaParameters.D))
                using (PinAndClear.Track(rsaParameters.P))
                    using (PinAndClear.Track(rsaParameters.Q))
                        using (PinAndClear.Track(rsaParameters.DP))
                            using (PinAndClear.Track(rsaParameters.DQ))
                                using (PinAndClear.Track(rsaParameters.InverseQ))
                                    using (typedKey = new RSAImplementation.RSASecurityTransforms())
                                    {
                                        typedKey.ImportParameters(rsaParameters);
                                        return(CopyWithPrivateKey(typedKey.GetKeys()));
                                    }
        }
Example #10
0
        public ICertificatePal CopyWithPrivateKey(DSA dsa)
        {
            DSACng          dsaCng = dsa as DSACng;
            ICertificatePal clone  = null;

            if (dsaCng != null)
            {
                clone = CopyWithPersistedCngKey(dsaCng.Key);

                if (clone != null)
                {
                    return(clone);
                }
            }

            DSACryptoServiceProvider dsaCsp = dsa as DSACryptoServiceProvider;

            if (dsaCsp != null)
            {
                clone = CopyWithPersistedCapiKey(dsaCsp.CspKeyContainerInfo);

                if (clone != null)
                {
                    return(clone);
                }
            }

            DSAParameters privateParameters = dsa.ExportParameters(true);

            using (PinAndClear.Track(privateParameters.X))
                using (DSACng clonedKey = new DSACng())
                {
                    clonedKey.ImportParameters(privateParameters);

                    return(CopyWithEphemeralKey(clonedKey.Key));
                }
        }
Example #11
0
        public ICertificatePal CopyWithPrivateKey(ECDsa ecdsa)
        {
            ECDsaCng ecdsaCng = ecdsa as ECDsaCng;

            if (ecdsaCng != null)
            {
                ICertificatePal clone = CopyWithPersistedCngKey(ecdsaCng.Key);

                if (clone != null)
                {
                    return(clone);
                }
            }

            ECParameters privateParameters = ecdsa.ExportParameters(true);

            using (PinAndClear.Track(privateParameters.D))
                using (ECDsaCng clonedKey = new ECDsaCng())
                {
                    clonedKey.ImportParameters(privateParameters);

                    return(CopyWithEphemeralKey(clonedKey.Key));
                }
        }
        internal static SafeSecKeyRefHandle?GetPrivateKey(AsymmetricAlgorithm?key)
        {
            if (key == null)
            {
                return(null);
            }

            if (key is RSAImplementation.RSASecurityTransforms rsa)
            {
                // Convert data key to legacy CSSM key that can be imported into keychain
                byte[] rsaPrivateKey = rsa.ExportRSAPrivateKey();
                using (PinAndClear.Track(rsaPrivateKey))
                {
                    return(Interop.AppleCrypto.ImportEphemeralKey(rsaPrivateKey, true));
                }
            }

            if (key is DSAImplementation.DSASecurityTransforms dsa)
            {
                // DSA always uses legacy CSSM keys do no need to convert
                return(dsa.GetKeys().PrivateKey);
            }

            if (key is ECDsaImplementation.ECDsaSecurityTransforms ecdsa)
            {
                // Convert data key to legacy CSSM key that can be imported into keychain
                byte[] ecdsaPrivateKey = ecdsa.ExportECPrivateKey();
                using (PinAndClear.Track(ecdsaPrivateKey))
                {
                    return(Interop.AppleCrypto.ImportEphemeralKey(ecdsaPrivateKey, true));
                }
            }

            Debug.Fail("Invalid key implementation");
            return(null);
        }