Пример #1
0
        public void ServerTest()
        {
            var rootStore = new X509TestStore();
            var myStore   = new X509TestStore();
            var store     = new CertificateStore
            {
                StoreFactory = name => name == StoreName.Root ? rootStore
                                            : name == StoreName.My ? myStore
                                            : null
            };
            var factory = new BouncyCastleCertificateFactory();

            var issuer = "hoge";
            var root   = factory.CreateRootCertificate(issuer);

            var server1 = factory.CreateServerCertificate("host1", root);

            store.InstallToPersonalStore(server1);
            store.FindServerCertificate("host1", root).Is(server1);

            var server2 = factory.CreateServerCertificate("host2", root);

            store.InstallToPersonalStore(server2);
            store.FindServerCertificate("host2", root).Is(server2);
            myStore.Certificates.Count.Is(2);

            var server3 = factory.CreateServerCertificate("host3", root);

            store.InstallToPersonalStore(server3);
            store.FindServerCertificate("host3", root).Is(server3);
            myStore.Certificates.Count.Is(3);

            store.UninstallFromPersonalStore(server2);
            myStore.Certificates.Count.Is(2);
            store.FindServerCertificate("host1", root).Is(server1);
            store.FindServerCertificate("host2", root).IsNull();
            store.FindServerCertificate("host3", root).Is(server3);

            store.UninstallAllServerCertificatesByIssuer(issuer);
            myStore.Certificates.Count.Is(0);
            store.FindServerCertificate("host1", root).IsNull();
            store.FindServerCertificate("host2", root).IsNull();
            store.FindServerCertificate("host3", root).IsNull();
        }
        public void CreateCertificateTest()
        {
            var issuerName = "CN=DO_NOT_TRUST_NekoxyRoot";
            var factory    = new BouncyCastleCertificateFactory();
            var rootCert   = factory.CreateRootCertificate(issuerName);

            rootCert.Issuer.Is(issuerName);
            rootCert.Subject.Is(issuerName);
            rootCert.Extensions.Count.Is(1);
            rootCert.Extensions[0].GetType().Is(typeof(X509BasicConstraintsExtension));
            var rootExt = rootCert.Extensions[0] as X509BasicConstraintsExtension;

            rootExt.CertificateAuthority.IsTrue();
            rootExt.Critical.IsTrue();
            rootCert.HasPrivateKey.IsTrue();

            var serverCert = factory.CreateServerCertificate("*.example.com", rootCert);

            serverCert.Issuer.Is(issuerName);
            serverCert.Subject.Is("CN=*.example.com");
            // X509Extensionsの確認は面倒なのでスキップ……
            serverCert.HasPrivateKey.IsTrue();
        }
Пример #3
0
        public void RootTest()
        {
            var rootStore = new X509TestStore();
            var myStore   = new X509TestStore();
            var store     = new CertificateStore
            {
                StoreFactory = name => name == StoreName.Root ? rootStore
                                            : name == StoreName.My ? myStore
                                            : null
            };
            var factory = new BouncyCastleCertificateFactory();

            var issuer = "hoge";
            var cert   = factory.CreateRootCertificate(issuer);

            store.InstallToRootStore(cert);
            store.FindRootCertificate(issuer).Is(cert);
            rootStore.Certificates.Count.Is(1);
            rootStore.Certificates[0].Is(cert);

            store.UninstallFromRootStore(cert);
            store.FindRootCertificate(issuer).IsNull();
            rootStore.Certificates.Count.Is(0);


            var cert2 = factory.CreateRootCertificate(issuer);

            store.InstallToRootStore(cert);
            store.FindRootCertificate(issuer).Is(cert);
            rootStore.Certificates.Count.Is(1);
            rootStore.Certificates[0].Is(cert);

            store.UninstallRootCertificates(issuer);
            store.FindRootCertificate(issuer).IsNull();
            rootStore.Certificates.Count.Is(0);
        }