internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509Certificate2Collection collection = new X509Certificate2Collection(); ArrayList decryptionIssuerSerials = (certUsageType == CertUsageType.Decryption ? new ArrayList() : null); if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate2 certificate in keyInfoX509Data.Certificates) { switch (certUsageType) { case CertUsageType.Verification: collection.Add(certificate); break; case CertUsageType.Decryption: decryptionIssuerSerials.Add(CreateX509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); break; } } } if (keyInfoX509Data.SubjectNames == null && keyInfoX509Data.IssuerSerials == null && keyInfoX509Data.SubjectKeyIds == null && decryptionIssuerSerials == null) { return(collection); } // Open LocalMachine and CurrentUser "Other People"/"My" stores. X509Store[] stores = new X509Store[2]; string storeName = (certUsageType == CertUsageType.Verification ? "AddressBook" : "My"); stores[0] = new X509Store(storeName, StoreLocation.CurrentUser); stores[1] = new X509Store(storeName, StoreLocation.LocalMachine); for (int index = 0; index < stores.Length; index++) { if (stores[index] != null) { X509Certificate2Collection filters = null; // We don't care if we can't open the store. try { stores[index].Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); filters = stores[index].Certificates; stores[index].Close(); if (keyInfoX509Data.SubjectNames != null) { foreach (string subjectName in keyInfoX509Data.SubjectNames) { filters = filters.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, false); } } if (keyInfoX509Data.IssuerSerials != null) { foreach (X509IssuerSerial issuerSerial in keyInfoX509Data.IssuerSerials) { filters = filters.Find(X509FindType.FindByIssuerDistinguishedName, issuerSerial.IssuerName, false); filters = filters.Find(X509FindType.FindBySerialNumber, issuerSerial.SerialNumber, false); } } if (keyInfoX509Data.SubjectKeyIds != null) { foreach (byte[] ski in keyInfoX509Data.SubjectKeyIds) { string hex = EncodeHexString(ski); filters = filters.Find(X509FindType.FindBySubjectKeyIdentifier, hex, false); } } if (decryptionIssuerSerials != null) { foreach (X509IssuerSerial issuerSerial in decryptionIssuerSerials) { filters = filters.Find(X509FindType.FindByIssuerDistinguishedName, issuerSerial.IssuerName, false); filters = filters.Find(X509FindType.FindBySerialNumber, issuerSerial.SerialNumber, false); } } } // Store doesn't exist, no read permissions, other system error catch (CryptographicException) { } // Opening LocalMachine stores (other than Root or CertificateAuthority) on Linux catch (PlatformNotSupportedException) { } if (filters != null) { collection.AddRange(filters); } } } return(collection); }
internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509Certificate2Collection collection = new X509Certificate2Collection(); ArrayList decryptionIssuerSerials = (certUsageType == CertUsageType.Decryption ? new ArrayList() : null); if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate2 certificate in keyInfoX509Data.Certificates) { switch (certUsageType) { case CertUsageType.Verification: collection.Add(certificate); break; case CertUsageType.Decryption: decryptionIssuerSerials.Add(new X509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); break; } } } if (keyInfoX509Data.SubjectNames == null && keyInfoX509Data.IssuerSerials == null && keyInfoX509Data.SubjectKeyIds == null && decryptionIssuerSerials == null) { return(collection); } // Open LocalMachine and CurrentUser "Other People"/"My" stores. // Assert OpenStore since we are not giving back any certificates to the user. StorePermission sp = new StorePermission(StorePermissionFlags.OpenStore); sp.Assert(); X509Store[] stores = new X509Store[2]; string storeName = (certUsageType == CertUsageType.Verification ? "AddressBook" : "My"); stores[0] = new X509Store(storeName, StoreLocation.CurrentUser); stores[1] = new X509Store(storeName, StoreLocation.LocalMachine); for (int index = 0; index < stores.Length; index++) { if (stores[index] != null) { X509Certificate2Collection filters = null; // We don't care if we can't open the store. try { stores[index].Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); filters = stores[index].Certificates; stores[index].Close(); if (keyInfoX509Data.SubjectNames != null) { foreach (string subjectName in keyInfoX509Data.SubjectNames) { filters = filters.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, false); } } if (keyInfoX509Data.IssuerSerials != null) { foreach (X509IssuerSerial issuerSerial in keyInfoX509Data.IssuerSerials) { filters = filters.Find(X509FindType.FindByIssuerDistinguishedName, issuerSerial.IssuerName, false); filters = filters.Find(X509FindType.FindBySerialNumber, issuerSerial.SerialNumber, false); } } if (keyInfoX509Data.SubjectKeyIds != null) { foreach (byte[] ski in keyInfoX509Data.SubjectKeyIds) { string hex = X509Utils.EncodeHexString(ski); filters = filters.Find(X509FindType.FindBySubjectKeyIdentifier, hex, false); } } if (decryptionIssuerSerials != null) { foreach (X509IssuerSerial issuerSerial in decryptionIssuerSerials) { filters = filters.Find(X509FindType.FindByIssuerDistinguishedName, issuerSerial.IssuerName, false); filters = filters.Find(X509FindType.FindBySerialNumber, issuerSerial.SerialNumber, false); } } } catch (CryptographicException) {} if (filters != null) { collection.AddRange(filters); } } } return(collection); }
internal static X509CertificateCollection BuildBagOfCerts(KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509CertificateCollection collection = new X509CertificateCollection(); //ArrayList decryptionIssuerSerials = (certUsageType == CertUsageType.Decryption ? new ArrayList() : null); if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate certificate in keyInfoX509Data.Certificates) { collection.Add(certificate); //switch (certUsageType) //{ // case CertUsageType.Verification: // collection.Add(certificate); // break; // case CertUsageType.Decryption: // decryptionIssuerSerials.Add(new X509IssuerSerial(certificate.IssuerName, certificate.SerialNumber)); // break; //} } } //if (keyInfoX509Data.SubjectNames == null && keyInfoX509Data.IssuerSerials == null && // keyInfoX509Data.SubjectKeyIds == null && decryptionIssuerSerials == null) return(collection); // Open LocalMachine and CurrentUser "Other People"/"My" stores. // Assert OpenStore since we are not giving back any certificates to the user. throw new NotSupportedException(); }
internal static X509Certificate2Collection BuildBagOfCerts (KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509Certificate2Collection collection = new X509Certificate2Collection(); ArrayList decryptionIssuerSerials = (certUsageType == CertUsageType.Decryption ? new ArrayList() : null); if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate2 certificate in keyInfoX509Data.Certificates) { switch (certUsageType) { case CertUsageType.Verification: collection.Add(certificate); break; case CertUsageType.Decryption: decryptionIssuerSerials.Add(new X509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); break; } } } if (keyInfoX509Data.SubjectNames == null && keyInfoX509Data.IssuerSerials == null && keyInfoX509Data.SubjectKeyIds == null && decryptionIssuerSerials == null) return collection; // Open LocalMachine and CurrentUser "Other People"/"My" stores. // Assert OpenStore since we are not giving back any certificates to the user. StorePermission sp = new StorePermission(StorePermissionFlags.OpenStore); sp.Assert(); X509Store[] stores = new X509Store[2]; string storeName = (certUsageType == CertUsageType.Verification ? "AddressBook" : "My"); stores[0] = new X509Store(storeName, StoreLocation.CurrentUser); stores[1] = new X509Store(storeName, StoreLocation.LocalMachine); for (int index=0; index < stores.Length; index++) { if (stores[index] != null) { X509Certificate2Collection filters = null; // We don't care if we can't open the store. try { stores[index].Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); filters = stores[index].Certificates; stores[index].Close(); if (keyInfoX509Data.SubjectNames != null) { foreach (string subjectName in keyInfoX509Data.SubjectNames) { filters = filters.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, false); } } if (keyInfoX509Data.IssuerSerials != null) { foreach (X509IssuerSerial issuerSerial in keyInfoX509Data.IssuerSerials) { filters = filters.Find(X509FindType.FindByIssuerDistinguishedName, issuerSerial.IssuerName, false); filters = filters.Find(X509FindType.FindBySerialNumber, issuerSerial.SerialNumber, false); } } if (keyInfoX509Data.SubjectKeyIds != null) { foreach (byte[] ski in keyInfoX509Data.SubjectKeyIds) { string hex = X509Utils.EncodeHexString(ski); filters = filters.Find(X509FindType.FindBySubjectKeyIdentifier, hex, false); } } if (decryptionIssuerSerials != null) { foreach (X509IssuerSerial issuerSerial in decryptionIssuerSerials) { filters = filters.Find(X509FindType.FindByIssuerDistinguishedName, issuerSerial.IssuerName, false); filters = filters.Find(X509FindType.FindBySerialNumber, issuerSerial.SerialNumber, false); } } } catch (CryptographicException) {} if (filters != null) collection.AddRange(filters); } } return collection; }
internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509Certificate2Collection certificates = new X509Certificate2Collection(); ArrayList list = (certUsageType == CertUsageType.Decryption) ? new ArrayList() : null; if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate2 certificate in keyInfoX509Data.Certificates) { switch (certUsageType) { case CertUsageType.Verification: certificates.Add(certificate); break; case CertUsageType.Decryption: list.Add(new X509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); break; } } } if (((keyInfoX509Data.SubjectNames != null) || (keyInfoX509Data.IssuerSerials != null)) || ((keyInfoX509Data.SubjectKeyIds != null) || (list != null))) { new StorePermission(StorePermissionFlags.OpenStore).Assert(); X509Store[] storeArray = new X509Store[2]; string storeName = (certUsageType == CertUsageType.Verification) ? "AddressBook" : "My"; storeArray[0] = new X509Store(storeName, StoreLocation.CurrentUser); storeArray[1] = new X509Store(storeName, StoreLocation.LocalMachine); for (int i = 0; i < storeArray.Length; i++) { if (storeArray[i] != null) { X509Certificate2Collection certificates2 = null; try { storeArray[i].Open(OpenFlags.OpenExistingOnly); certificates2 = storeArray[i].Certificates; storeArray[i].Close(); if (keyInfoX509Data.SubjectNames != null) { foreach (string str2 in keyInfoX509Data.SubjectNames) { certificates2 = certificates2.Find(X509FindType.FindBySubjectDistinguishedName, str2, false); } } if (keyInfoX509Data.IssuerSerials != null) { foreach (X509IssuerSerial serial in keyInfoX509Data.IssuerSerials) { certificates2 = certificates2.Find(X509FindType.FindByIssuerDistinguishedName, serial.IssuerName, false); certificates2 = certificates2.Find(X509FindType.FindBySerialNumber, serial.SerialNumber, false); } } if (keyInfoX509Data.SubjectKeyIds != null) { foreach (byte[] buffer in keyInfoX509Data.SubjectKeyIds) { string findValue = System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(buffer); certificates2 = certificates2.Find(X509FindType.FindBySubjectKeyIdentifier, findValue, false); } } if (list != null) { foreach (X509IssuerSerial serial2 in list) { certificates2 = certificates2.Find(X509FindType.FindByIssuerDistinguishedName, serial2.IssuerName, false); certificates2 = certificates2.Find(X509FindType.FindBySerialNumber, serial2.SerialNumber, false); } } } catch (CryptographicException) { } if (certificates2 != null) { certificates.AddRange(certificates2); } } } } return certificates; }
internal static X509Certificate2Collection BuildBagOfCerts(KeyInfoX509Data keyInfoX509Data, CertUsageType certUsageType) { X509Certificate2Collection certificates = new X509Certificate2Collection(); ArrayList list = (certUsageType == CertUsageType.Decryption) ? new ArrayList() : null; if (keyInfoX509Data.Certificates != null) { foreach (X509Certificate2 certificate in keyInfoX509Data.Certificates) { switch (certUsageType) { case CertUsageType.Verification: certificates.Add(certificate); break; case CertUsageType.Decryption: list.Add(new X509IssuerSerial(certificate.IssuerName.Name, certificate.SerialNumber)); break; } } } if (((keyInfoX509Data.SubjectNames != null) || (keyInfoX509Data.IssuerSerials != null)) || ((keyInfoX509Data.SubjectKeyIds != null) || (list != null))) { new StorePermission(StorePermissionFlags.OpenStore).Assert(); X509Store[] storeArray = new X509Store[2]; string storeName = (certUsageType == CertUsageType.Verification) ? "AddressBook" : "My"; storeArray[0] = new X509Store(storeName, StoreLocation.CurrentUser); storeArray[1] = new X509Store(storeName, StoreLocation.LocalMachine); for (int i = 0; i < storeArray.Length; i++) { if (storeArray[i] != null) { X509Certificate2Collection certificates2 = null; try { storeArray[i].Open(OpenFlags.OpenExistingOnly); certificates2 = storeArray[i].Certificates; storeArray[i].Close(); if (keyInfoX509Data.SubjectNames != null) { foreach (string str2 in keyInfoX509Data.SubjectNames) { certificates2 = certificates2.Find(X509FindType.FindBySubjectDistinguishedName, str2, false); } } if (keyInfoX509Data.IssuerSerials != null) { foreach (X509IssuerSerial serial in keyInfoX509Data.IssuerSerials) { certificates2 = certificates2.Find(X509FindType.FindByIssuerDistinguishedName, serial.IssuerName, false); certificates2 = certificates2.Find(X509FindType.FindBySerialNumber, serial.SerialNumber, false); } } if (keyInfoX509Data.SubjectKeyIds != null) { foreach (byte[] buffer in keyInfoX509Data.SubjectKeyIds) { string findValue = System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(buffer); certificates2 = certificates2.Find(X509FindType.FindBySubjectKeyIdentifier, findValue, false); } } if (list != null) { foreach (X509IssuerSerial serial2 in list) { certificates2 = certificates2.Find(X509FindType.FindByIssuerDistinguishedName, serial2.IssuerName, false); certificates2 = certificates2.Find(X509FindType.FindBySerialNumber, serial2.SerialNumber, false); } } } catch (CryptographicException) { } if (certificates2 != null) { certificates.AddRange(certificates2); } } } } return(certificates); }