Example #1
0
        private static SafeSslHandle CreateSslContext(SafeFreeSslCredentials credential)
        {
            if (credential.CertificateContext == null)
            {
                return(Interop.AndroidCrypto.SSLStreamCreate());
            }

            SslStreamCertificateContext context = credential.CertificateContext;
            X509Certificate2            cert    = context.Certificate;

            Debug.Assert(context.Certificate.HasPrivateKey);

            PAL_KeyAlgorithm algorithm;

            byte[] keyBytes;
            using (AsymmetricAlgorithm key = GetPrivateKeyAlgorithm(cert, out algorithm))
            {
                keyBytes = key.ExportPkcs8PrivateKey();
            }
            IntPtr[] ptrs = new IntPtr[context.IntermediateCertificates.Length + 1];
            ptrs[0] = cert.Handle;
            for (int i = 0; i < context.IntermediateCertificates.Length; i++)
            {
                ptrs[i + 1] = context.IntermediateCertificates[i].Handle;
            }

            return(Interop.AndroidCrypto.SSLStreamCreateWithCertificates(keyBytes, algorithm, ptrs));
        }
Example #2
0
        public static Pkcs8PrivateKeyInfo Create(AsymmetricAlgorithm privateKey)
        {
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }

            byte[] pkcs8 = privateKey.ExportPkcs8PrivateKey();
            return(Decode(pkcs8, out _, skipCopy: true));
        }
Example #3
0
        public static string GeneratePEMWithPrivateKeyAsString(X509Certificate2 certificate)
        {
            var sb = new StringBuilder();
            AsymmetricAlgorithm key = certificate.GetRSAPrivateKey();

            byte[] privKeyBytes   = key.ExportPkcs8PrivateKey();
            char[] privKeyPem     = PemEncoding.Write("PRIVATE KEY", privKeyBytes);
            char[] certificatePem = PemEncoding.Write("CERTIFICATE", certificate.GetRawCertData());
            sb.AppendLine(new string(privKeyPem));
            sb.AppendLine();
            sb.AppendLine(new string(certificatePem));
            return(sb.ToString());
        }
Example #4
0
        public Pkcs12KeyBag AddKeyUnencrypted(AsymmetricAlgorithm key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (IsReadOnly)
            {
                throw new InvalidOperationException(SR.Cryptography_Pkcs12_SafeContentsIsReadOnly);
            }

            byte[]       pkcs8PrivateKey = key.ExportPkcs8PrivateKey();
            Pkcs12KeyBag bag             = new Pkcs12KeyBag(pkcs8PrivateKey, skipCopy: true);

            AddSafeBag(bag);
            return(bag);
        }