public void TestImportExportPkcs12()
        {
            var store = new X509CertificateStore();

            store.Import(GetTestDataPath("smime.p12"), "no.secret");
            var certificate = store.Certificates.FirstOrDefault();
            var count       = store.Certificates.Count();

            Assert.AreEqual(1, count, "Unexpected number of certificates imported.");
            Assert.IsNotNull(store.GetPrivateKey(certificate), "Failed to get private key.");

            foreach (var authority in CertificateAuthorities)
            {
                store.Import(GetTestDataPath(authority));
            }

            store.Export("exported.p12", "no.secret");

            var imported = new X509CertificateStore();

            imported.Import("exported.p12", "no.secret");

            count = imported.Certificates.Count();

            Assert.AreEqual(store.Certificates.Count(), count, "Unexpected number of certificates re-imported.");
            Assert.IsNotNull(imported.GetPrivateKey(certificate), "Failed to get private key after re-importing.");
        }
        public void TestArgumentExceptions()
        {
            var store = new X509CertificateStore();

            Assert.Throws <ArgumentNullException> (() => store.Add(null));
            Assert.Throws <ArgumentNullException> (() => store.AddRange(null));
            Assert.Throws <ArgumentNullException> (() => store.Export((Stream)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Export((string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Export(Stream.Null, null));
            Assert.Throws <ArgumentNullException> (() => store.Export("fileName", null));
            Assert.Throws <ArgumentNullException> (() => store.Export((Stream)null));
            Assert.Throws <ArgumentNullException> (() => store.Export((string)null));
            Assert.Throws <ArgumentNullException> (() => store.GetPrivateKey(null));
            Assert.Throws <ArgumentNullException> (() => store.Import((Stream)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Import((string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Import((byte[])null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Import(Stream.Null, null));
            Assert.Throws <ArgumentNullException> (() => store.Import(GetTestDataPath("smime.p12"), null));
            Assert.Throws <ArgumentNullException> (() => store.Import(new byte[0], null));
            Assert.Throws <ArgumentNullException> (() => store.Import((Stream)null));
            Assert.Throws <ArgumentNullException> (() => store.Import((string)null));
            Assert.Throws <ArgumentNullException> (() => store.Import((byte[])null));
            Assert.Throws <ArgumentNullException> (() => store.Remove(null));
            Assert.Throws <ArgumentNullException> (() => store.RemoveRange(null));
        }
Exemple #3
0
        public void AddCertificate()
        {
            FileInfo[] files = this.getAllCerts();

            foreach (var file in files)
            {
                System.Security.Cryptography.X509Certificates.X509Certificate2 cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(file.FullName);

                byte[] hashBytes = cert.GetRawCertData();
                X509CertificateStore certstore = new X509CertificateStore();
                certstore.Import(hashBytes);

                IEnumerable <X509Certificate> ix509Cert = certstore.Certificates;
                foreach (var item in ix509Cert)
                {
                    X509Certificate cert2 = item;

                    X509CertificateRecord certrecord = _database.Find(cert2, X509CertificateRecordFields.Certificate);
                    if (certrecord == null)
                    {
                        certrecord = new X509CertificateRecord(cert2);
                        _database.Add(certrecord);
                    }
                }
            }
        }
        public void TestImportSingleCertificate()
        {
            var store = new X509CertificateStore();

            store.Import(GetTestDataPath(CertificateAuthorities[0]));
            var certificate = store.Certificates.FirstOrDefault();
            var count       = store.Certificates.Count();

            Assert.AreEqual(1, count, "Unexpected number of certificates imported.");
            Assert.AreEqual("*****@*****.**", certificate.GetSubjectEmailAddress(), "Unexpected email address for certificate.");
        }
        public void TestImportSingleCertificate()
        {
            var store = new X509CertificateStore();

            store.Import(GetTestDataPath(CertificateAuthorities[0]));
            var certificate = store.Certificates.FirstOrDefault();
            var count       = store.Certificates.Count();

            Assert.AreEqual(1, count, "Unexpected number of certificates imported.");
            Assert.AreEqual("StartCom Certification Authority", certificate.GetCommonName(), "Unexpected CN for certificate.");
        }
Exemple #6
0
        public static void TestParser()
        {
            string pfxLocation = @"D:\lol\certificate.pfx";

            pfxLocation = @"D:\username\Desktop\DesktopArchiv\20180329_Desktop\CORMailService\CORMailService\CORMailService\CORMailService_TemporaryKey.pfx";



            System.Security.Cryptography.X509Certificates.X509Certificate2 certificate =
                new System.Security.Cryptography.X509Certificates.X509Certificate2(pfxLocation);


            // Private Key
            if (!certificate.HasPrivateKey)
            {
                throw new System.IO.InvalidDataException("no private key in pfx file.");
            }

            System.Security.Cryptography.RSACng rsa = (System.Security.Cryptography.RSACng)certificate.PrivateKey;



            X509CertificateStore xs = new X509CertificateStore();

            xs.Import(pfxLocation);
            foreach (X509Certificate thisCert in xs.Certificates)
            {
                System.Console.WriteLine(thisCert);
                thisCert.GetPublicKey();

                // var signer = Org.BouncyCastle.Security.SignerUtilities.GetSigner(Sdk.SIGNATURE_ALGORITHM);
            }


            X509CertificateParser certParser = new X509CertificateParser();

            using (var fs = System.IO.File.OpenRead(pfxLocation))
            {
                certParser.ReadCertificate(fs);
            }

            System.Console.WriteLine(certParser);
        }
        public void TestImportExportMultipleCertificates()
        {
            var store = new X509CertificateStore();

            foreach (var authority in CertificateAuthorities)
            {
                store.Import(GetTestDataPath(authority));
            }

            var count = store.Certificates.Count();

            Assert.AreEqual(CertificateAuthorities.Length, count, "Unexpected number of certificates imported.");

            store.Export("exported.crt");

            var imported = new X509CertificateStore();

            imported.Import("exported.crt");

            count = imported.Certificates.Count();

            Assert.AreEqual(CertificateAuthorities.Length, count, "Unexpected number of certificates re-imported.");
        }