Example #1
0
        protected override byte[] ExportPkcs8(ICertificatePalCore certificatePal, ReadOnlySpan <char> password)
        {
            if (_privateKey != null)
            {
                return(_privateKey.ExportEncryptedPkcs8PrivateKey(password, s_windowsPbe));
            }

            Debug.Assert(certificatePal.HasPrivateKey);
            ICertificatePal     pal = (ICertificatePal)certificatePal;
            AsymmetricAlgorithm algorithm;

            switch (pal.KeyAlgorithm)
            {
            case Oids.Rsa:
                algorithm = pal.GetRSAPrivateKey() !;
                break;

            case Oids.EcPublicKey:
                algorithm = pal.GetECDsaPrivateKey() !;
                break;

            case Oids.Dsa:
            default:
                throw new CryptographicException(SR.Format(SR.Cryptography_UnknownKeyAlgorithm, pal.KeyAlgorithm));
            }
            ;

            using (algorithm)
            {
                return(algorithm.ExportEncryptedPkcs8PrivateKey(password, s_windowsPbe));
            }
        }
Example #2
0
 private static byte[] GetCertHash(HashAlgorithmName hashAlgorithm, ICertificatePalCore certPal)
 {
     using (IncrementalHash hasher = IncrementalHash.CreateHash(hashAlgorithm))
     {
         hasher.AppendData(certPal.RawData);
         return(hasher.GetHashAndReset());
     }
 }
Example #3
0
        private void BuildBags(
            ICertificatePalCore certPal,
            ReadOnlySpan <char> passwordSpan,
            AsnWriter tmpWriter,
            CertBagAsn[] certBags,
            AttributeAsn[] certAttrs,
            SafeBagAsn[] keyBags,
            ref int certIdx,
            ref int keyIdx)
        {
            tmpWriter.WriteOctetString(certPal.RawData);

            certBags[certIdx] = new CertBagAsn
            {
                CertId    = Oids.Pkcs12X509CertBagType,
                CertValue = tmpWriter.Encode(),
            };

            tmpWriter.Reset();

            if (certPal.HasPrivateKey)
            {
                byte[] attrBytes = new byte[6];
                attrBytes[0] = (byte)UniversalTagNumber.OctetString;
                attrBytes[1] = sizeof(int);
                MemoryMarshal.Write(attrBytes.AsSpan(2), ref keyIdx);

                keyBags[keyIdx] = new SafeBagAsn
                {
                    BagId         = Oids.Pkcs12ShroudedKeyBag,
                    BagValue      = ExportPkcs8(certPal, passwordSpan),
                    BagAttributes = new[]
                    {
                        new AttributeAsn
                        {
                            AttrType   = new Oid(Oids.LocalKeyId, null),
                            AttrValues = new ReadOnlyMemory <byte>[]
                            {
                                attrBytes,
                            }
                        }
                    }
                };

                // Reuse the attribute between the cert and the key.
                certAttrs[certIdx] = keyBags[keyIdx].BagAttributes[0];
                keyIdx++;
            }

            certIdx++;
        }
Example #4
0
        public virtual void Reset()
        {
            _lazyCertHash               = null;
            _lazyIssuer                 = null;
            _lazySubject                = null;
            _lazySerialNumber           = null;
            _lazyKeyAlgorithm           = null;
            _lazyKeyAlgorithmParameters = null;
            _lazyPublicKey              = null;
            _lazyNotBefore              = DateTime.MinValue;
            _lazyNotAfter               = DateTime.MinValue;

            ICertificatePalCore pal = Pal;

            if (pal != null)
            {
                Pal = null;
                pal.Dispose();
            }
        }
Example #5
0
        public static IExportPal FromCertificate(ICertificatePalCore cert)
        {
            CertificatePal certificatePal = (CertificatePal)cert;

            SafeCertStoreHandle certStore = Interop.crypt32.CertOpenStore(
                CertStoreProvider.CERT_STORE_PROV_MEMORY,
                CertEncodingType.All,
                IntPtr.Zero,
                CertStoreFlags.CERT_STORE_ENUM_ARCHIVED_FLAG | CertStoreFlags.CERT_STORE_CREATE_NEW_FLAG | CertStoreFlags.CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG,
                null);

            if (certStore.IsInvalid)
            {
                throw Marshal.GetHRForLastWin32Error().ToCryptographicException();
            }
            if (!Interop.crypt32.CertAddCertificateLinkToStore(certStore, certificatePal.CertContext, CertStoreAddDisposition.CERT_STORE_ADD_ALWAYS, IntPtr.Zero))
            {
                throw Marshal.GetHRForLastWin32Error().ToCryptographicException();
            }
            return(new StorePal(certStore));
        }
        protected override byte[] ExportPkcs8(
            ICertificatePalCore certificatePal,
            ReadOnlySpan <char> password)
        {
            AsymmetricAlgorithm?alg        = null;
            SafeEvpPKeyHandle?  privateKey = ((OpenSslX509CertificateReader)certificatePal).PrivateKeyHandle;

            try
            {
                alg = new RSAOpenSsl(privateKey !);
            }
            catch (CryptographicException)
            {
            }

            if (alg == null)
            {
                try
                {
                    alg = new ECDsaOpenSsl(privateKey !);
                }
                catch (CryptographicException)
                {
                }
            }

            if (alg == null)
            {
                try
                {
                    alg = new DSAOpenSsl(privateKey !);
                }
                catch (CryptographicException)
                {
                }
            }

            Debug.Assert(alg != null);
            return(alg.ExportEncryptedPkcs8PrivateKey(password, s_windowsPbe));
        }
        internal static partial IExportPal FromCertificate(ICertificatePalCore cert)
        {
            CertificatePal certificatePal = (CertificatePal)cert;

            SafeCertStoreHandle certStore = Interop.crypt32.CertOpenStore(
                CertStoreProvider.CERT_STORE_PROV_MEMORY,
                Interop.Crypt32.CertEncodingType.All,
                IntPtr.Zero,
                Interop.Crypt32.CertStoreFlags.CERT_STORE_ENUM_ARCHIVED_FLAG | Interop.Crypt32.CertStoreFlags.CERT_STORE_CREATE_NEW_FLAG | Interop.Crypt32.CertStoreFlags.CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG,
                null);

            using (SafeCertContextHandle certContext = certificatePal.GetCertContext())
            {
                if (certStore.IsInvalid ||
                    !Interop.Crypt32.CertAddCertificateLinkToStore(certStore, certContext, Interop.Crypt32.CertStoreAddDisposition.CERT_STORE_ADD_ALWAYS, IntPtr.Zero))
                {
                    Exception e = Marshal.GetHRForLastWin32Error().ToCryptographicException();
                    certStore.Dispose();
                    throw e;
                }
            }

            return new StorePal(certStore);
        }
Example #8
0
 public AndroidExportProvider(ICertificatePalCore cert)
     : base(cert)
 {
 }
Example #9
0
 public static IExportPal FromCertificate(ICertificatePalCore cert)
 {
     throw new NotImplementedException(nameof(FromCertificate));
 }
Example #10
0
 private static byte[] GetCertHash(HashAlgorithmName hashAlgorithm, ICertificatePalCore certPal)
 {
     return(HashOneShotHelpers.HashData(hashAlgorithm, certPal.RawData));
 }
Example #11
0
 public AppleCertificateExporter(ICertificatePalCore cert, AsymmetricAlgorithm privateKey)
     : base(cert)
 {
     _privateKey = privateKey;
 }
Example #12
0
 public void Dispose()
 {
     // Don't dispose any of the resources, they're still owned by the caller.
     _singleCertPal = null;
     _certs         = null;
 }
Example #13
0
 internal Pkcs12SmallExport(ICertificatePalCore cert, SafeSecKeyRefHandle privateKey)
     : base(cert)
 {
     Debug.Assert(!privateKey.IsInvalid);
     _privateKey = privateKey;
 }
 public static IExportPal FromCertificate(ICertificatePalCore cert)
 {
     return(new AppleCertificateExporter(cert));
 }
Example #15
0
 internal static partial IExportPal FromCertificate(ICertificatePalCore cert);
Example #16
0
 internal static partial IExportPal FromCertificate(ICertificatePalCore cert)
 {
     return(new AndroidExportProvider(cert));
 }
Example #17
0
 internal static partial IExportPal FromCertificate(ICertificatePalCore cert)
 {
     throw new PlatformNotSupportedException(SR.SystemSecurityCryptographyX509Certificates_PlatformNotSupported);
 }
Example #18
0
 protected abstract byte[] ExportPkcs8(ICertificatePalCore certificatePal, ReadOnlySpan <char> password);
Example #19
0
 internal UnixExportProvider(ICertificatePalCore singleCertPal)
 {
     _singleCertPal = singleCertPal;
 }
Example #20
0
 public AppleCertificateExporter(ICertificatePalCore cert)
     : base(cert)
 {
 }
Example #21
0
 protected override byte[] ExportPkcs8(ICertificatePalCore certificatePal, ReadOnlySpan <char> password)
 {
     return(AppleCertificatePal.ExportPkcs8(_privateKey, password));
 }
Example #22
0
 public AppleCertificateExporter(ICertificatePalCore cert)
 {
     _singleCertPal = cert;
 }
Example #23
0
 public static IExportPal FromCertificate(ICertificatePalCore cert)
 {
     return(new ExportProvider(cert));
 }
Example #24
0
            protected override byte[] ExportPkcs8(ICertificatePalCore certificatePal, ReadOnlySpan <char> password)
            {
                AppleCertificatePal pal = (AppleCertificatePal)certificatePal;

                return(pal.ExportPkcs8(password));
            }
 internal OpenSslExportProvider(ICertificatePalCore singleCertPal)
     : base(singleCertPal)
 {
 }
Example #26
0
 internal static string GetCertHashString(HashAlgorithmName hashAlgorithm, ICertificatePalCore certPal)
 {
     return(GetCertHash(hashAlgorithm, certPal).ToHexStringUpper());
 }
Example #27
0
 internal X509Certificate(ICertificatePalCore pal)
 {
     Debug.Assert(pal != null);
     Pal = pal;
 }
Example #28
0
 internal static partial IExportPal FromCertificate(ICertificatePalCore cert)
 {
     return(new AppleCertificateExporter(cert));
 }