예제 #1
0
        public void CertificateAdd(string[] args)
        {
            CertificateFileInfo certFileInfo = CertificateFileInfo.Create(0, args);
            MemoryX509Store     certStore    = certFileInfo.LoadCerts();

            PushCerts(certStore, false, certFileInfo.Status);
        }
예제 #2
0
        public void CertificateExportKey(string[] args)
        {
            CertificateFileInfo certFileInfo = CertificateFileInfo.Create(0, args);
            MemoryX509Store     certStore    = certFileInfo.LoadCerts();

            ShowKey(certStore);
        }
예제 #3
0
        internal MemoryX509Store LoadCerts(string filePath, string password, X509KeyStorageFlags flags)
        {
            MemoryX509Store certStore = new MemoryX509Store();

            LoadCerts(certStore, filePath, password, flags);
            return(certStore);
        }
예제 #4
0
 static TestCertificates()
 {
     AgentTester.EnsureStandardMachineStores();
     ChainCertsStore  = LoadCertificateFolder("Chain");
     PublicCertsStore = new MemoryX509Store(LoadAllPublic());
     AllPublicCerts   = LoadAllPublic();
 }
예제 #5
0
        internal MemoryX509Store LoadCerts(X509KeyStorageFlags flags)
        {
            MemoryX509Store certStore = new MemoryX509Store();

            LoadCerts(certStore, flags);
            return(certStore);
        }
예제 #6
0
        public static MemoryX509Store LoadCertificates(string folderPath, X509KeyStorageFlags flags)
        {
            if (string.IsNullOrEmpty(folderPath))
            {
                throw new ArgumentException("value was null or empty", "folderPath");
            }

            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException("Directory not found: " + folderPath);
            }

            MemoryX509Store certStore = new MemoryX509Store();

            string[] files = Directory.GetFiles(folderPath);
            for (int i = 0; i < files.Length; ++i)
            {
                string file = files[i];
                string ext  = Path.GetExtension(file) ?? string.Empty;
                ext = ext.ToLower();

                switch (ext)
                {
                default:
                    certStore.ImportKeyFile(file, flags);
                    break;

                case ".pfx":
                    certStore.ImportKeyFile(file, "passw0rd!", flags);
                    break;
                }
            }

            return(certStore);
        }
예제 #7
0
        MemoryX509Store LoadCertificates(string folderPath)
        {
            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException("Directory not found: " + folderPath);
            }

            MemoryX509Store certStore = new MemoryX509Store();

            try
            {
                string[] files = Directory.GetFiles(folderPath);

                if (files.IsNullOrEmpty())
                {
                    throw new ArgumentException("Empty directory");
                }

                CertificateCommands certcmd = GetCommand <CertificateCommands>();

                foreach (string file in files)
                {
                    certcmd.LoadCerts(certStore, file, "passw0rd!", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
                }

                return(certStore);
            }
            catch
            {
                certStore.Dispose();
                throw;
            }
        }
예제 #8
0
        static bool DoPrivateKeysExist(X509Store store, string path)
        {
            using (MemoryX509Store certs = LoadPrivateCerts(path, false))
            {
                foreach (X509Certificate2 cert in certs)
                {
                    X509Certificate2 found = store.Certificates.FindByThumbprint(cert.Thumbprint);
                    if (found == null)
                    {
                        return(false);
                    }

                    bool hasKey = false;
                    try
                    {
                        hasKey = (!string.IsNullOrEmpty(found.PrivateKey.ToXmlString(true)));
                    }
                    catch
                    {
                    }
                    if (!hasKey)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #9
0
        public CryptographerTests()
        {
            m_tester        = AgentTester.CreateTest();
            m_cryptographer = m_tester.AgentA.Cryptographer;
            MemoryX509Store certs = AgentTester.LoadPrivateCerts("redmond");

            m_cert = certs.First();
        }
예제 #10
0
        public static MemoryX509Store LoadCertificateFolder(string folderPath)
        {
            string          path  = FullPath(folderPath);
            MemoryX509Store store = new MemoryX509Store();

            store.ImportFolder(path, X509KeyStorageFlags.DefaultKeySet);

            return(store);
        }
예제 #11
0
        public static DirectAgent CreateAgent(string domain, string certsBasePath)
        {
            MemoryX509Store     privateCerts = LoadPrivateCerts(certsBasePath, false);
            MemoryX509Store     publicCerts  = LoadPublicCerts(certsBasePath);
            TrustAnchorResolver anchors      = new TrustAnchorResolver(
                (IX509CertificateStore)LoadIncomingAnchors(certsBasePath),
                (IX509CertificateStore)LoadOutgoingAnchors(certsBasePath));

            return(new DirectAgent(domain, privateCerts.CreateResolver(), publicCerts.CreateResolver(), anchors));
        }
예제 #12
0
        public void CertificateEnsure(string[] args)
        {
            CertificateFileInfo certFileInfo = CertificateFileInfo.Create(0, args);
            MemoryX509Store     certStore    = certFileInfo.LoadCerts();

            //
            // This checks for duplicates...(using thumbprints)
            //
            PushCerts(certStore, true, certFileInfo.Status);
        }
예제 #13
0
        public void CertificateSearchByFile(string[] args)
        {
            CertificateFileInfo certFileInfo = CertificateFileInfo.Create(0, args);
            MemoryX509Store     certs        = certFileInfo.LoadCerts();

            WriteLine(string.Empty);

            this.Search(
                x => (certs.FirstOrDefault(y => this.Match(x, y)) != null)
                );
        }
예제 #14
0
        public CryptographerTests()
        {
            m_tester        = AgentTester.CreateTest();
            m_cryptographer = m_tester.AgentA.Cryptographer as SMIMECryptographer;

            MemoryX509Store certs = AgentTester.LoadPrivateCerts("redmond");

            m_cert = certs
                     .ToList()
                     .First(c => c.HasPrivateKey);
        }
예제 #15
0
        public void TestDnsAltName()
        {
            MemoryX509Store dnsCerts = TestCertificates.LoadCertificateFolder("Dns");

            foreach (X509Certificate2 dnsCert in dnsCerts)
            {
                string dnsName = dnsCert.GetNameInfo(X509NameType.DnsName, false);
                Assert.True(dnsCert.MatchDnsName(dnsName));
                Assert.True(dnsCert.MatchDnsOrEmailOrName(dnsName));
            }
        }
예제 #16
0
 public TrustChainTests()
 {
     m_store     = TestCertificates.ChainCertsStore.Clone();
     m_resolver  = m_store.CreateResolver();
     m_validator = this.CreateValidator();
     //
     // Find the endcert and the root cert
     // We'll trust the root cert, but the intermediaries are not trusted
     //
     m_endCerts       = m_resolver.GetCertificates(new MailAddress("*****@*****.**"));
     m_trustedAnchors = m_resolver.GetCertificatesForDomain("root.xyz");
 }
예제 #17
0
 static void InstallPrivateKeys(X509Store store, MemoryX509Store certs)
 {
     foreach (X509Certificate2 cert in certs)
     {
         X509Certificate2 found = store.Certificates.FindByThumbprint(cert.Thumbprint);
         if (found != null)
         {
             store.Remove(found);
             found.Reset();
         }
         store.Add(cert);
     }
 }
예제 #18
0
        public void AnchorSearchByFile(string[] args)
        {
            string owner = args.GetRequiredValue(0);
            CertificateFileInfo certFileInfo = CertificateFileInfo.Create(1, args);
            MemoryX509Store     certs        = certFileInfo.LoadCerts();

            WriteLine(string.Empty);

            this.Search(
                owner,
                x => (certs.FirstOrDefault(y => this.CertCommands.Match(x, y)) != null)
                );
        }
예제 #19
0
        internal void LoadCerts(MemoryX509Store certStore, string filePath, string password, X509KeyStorageFlags flags)
        {
            string ext = Path.GetExtension(filePath) ?? string.Empty;

            switch (ext.ToLower())
            {
            default:
                certStore.ImportKeyFile(filePath, flags);
                break;

            case ".pfx":
                certStore.ImportKeyFile(filePath, password, flags);
                break;
            }
        }
예제 #20
0
        public void CertificateEnsureMachine(string[] args)
        {
            using (SystemX509Store store = OpenStore(args.GetRequiredValue(0)))
            {
                CertificateFileInfo certFileInfo = CertificateFileInfo.Create(1, args);
                MemoryX509Store     certs        = certFileInfo.LoadCerts(MachineKeyFlags);
                foreach (X509Certificate2 cert in certs)
                {
                    string comment = "EXISTS";
                    if (!store.Contains(cert))
                    {
                        store.Add(cert);
                        comment = "ADDED";
                    }

                    WriteLine("{0}: {1}, Thumbprint:{2}", comment, cert.Subject, cert.Thumbprint);
                }
            }
        }
예제 #21
0
        internal void LoadCerts(MemoryX509Store certStore, X509KeyStorageFlags flags)
        {
            if (!File.Exists(this.FilePath))
            {
                throw new FileNotFoundException("File does not exist", this.FilePath);
            }

            string ext = Path.GetExtension(this.FilePath) ?? string.Empty;

            switch (ext.ToLower())
            {
            default:
                certStore.ImportKeyFile(this.FilePath, flags);
                break;

            case ".pfx":
                certStore.ImportKeyFile(FilePath, this.Password, flags);
                break;
            }
        }
예제 #22
0
        public static DirectAgent CreateAgent(
            IDomainResolver domain,
            string certsBasePath,
            ISmimeCryptographer cryptographer,
            ICertPolicyResolvers certPolicyResolvers)
        {
            MemoryX509Store     privateCerts = LoadPrivateCerts(certsBasePath, false);
            MemoryX509Store     publicCerts  = LoadPublicCerts(certsBasePath);
            TrustAnchorResolver anchors      = new TrustAnchorResolver(
                (IX509CertificateStore)LoadOutgoingAnchors(certsBasePath),
                (IX509CertificateStore)LoadIncomingAnchors(certsBasePath));

            return(new DirectAgent(
                       domain,
                       privateCerts.CreateResolver(),
                       publicCerts.CreateResolver(),
                       anchors,
                       TrustModel.Default,
                       cryptographer ?? SMIMECryptographer.Default,
                       certPolicyResolvers
                       ));
        }