本文整理汇总了C#中System.Security.Cryptography.X509Certificates.X509Certificate2Collection.AddRange方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection.AddRange方法的具体用法?C# X509Certificate2Collection.AddRange怎么用?C# X509Certificate2Collection.AddRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.X509Certificates.X509Certificate2Collection
的用法示例。
在下文中一共展示了X509Certificate2Collection.AddRange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildDecryptorStore
private static System.Security.Cryptography.SafeCertStoreHandle BuildDecryptorStore(X509Certificate2Collection extraStore)
{
X509Certificate2Collection collection = new X509Certificate2Collection();
try
{
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.IncludeArchived | OpenFlags.OpenExistingOnly);
collection.AddRange(store.Certificates);
}
catch (SecurityException)
{
}
try
{
X509Store store2 = new X509Store("MY", StoreLocation.LocalMachine);
store2.Open(OpenFlags.IncludeArchived | OpenFlags.OpenExistingOnly);
collection.AddRange(store2.Certificates);
}
catch (SecurityException)
{
}
if (extraStore != null)
{
collection.AddRange(extraStore);
}
if (collection.Count == 0)
{
throw new CryptographicException(-2146889717);
}
return System.Security.Cryptography.X509Certificates.X509Utils.ExportToMemoryStore(collection);
}
示例2: CreateBagOfCertificates
internal static X509Certificate2Collection CreateBagOfCertificates(CmsSigner signer)
{
X509Certificate2Collection certificates = new X509Certificate2Collection();
certificates.AddRange(signer.Certificates);
if (signer.IncludeOption != X509IncludeOption.None)
{
if (signer.IncludeOption == X509IncludeOption.EndCertOnly)
{
certificates.Add(signer.Certificate);
return certificates;
}
int count = 1;
X509Chain chain = new X509Chain();
chain.Build(signer.Certificate);
if ((chain.ChainStatus.Length > 0) && ((chain.ChainStatus[0].Status & X509ChainStatusFlags.PartialChain) == X509ChainStatusFlags.PartialChain))
{
throw new CryptographicException(-2146762486);
}
if (signer.IncludeOption == X509IncludeOption.WholeChain)
{
count = chain.ChainElements.Count;
}
else if (chain.ChainElements.Count > 1)
{
count = chain.ChainElements.Count - 1;
}
for (int i = 0; i < count; i++)
{
certificates.Add(chain.ChainElements[i].Certificate);
}
}
return certificates;
}
示例3: Main
private static void Main(string[] args)
{
Task.Run(async () =>
{
Console.WriteLine("Enter PIN: ");
string pin = Console.ReadLine();
WebRequestHandler handler = new WebRequestHandler();
handler.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
using (HttpClient client = new HttpClient(handler, true))
{
client.BaseAddress = new Uri(string.Format(@"https://{0}:{1}", MachineName, RemotePort));
X509Store store = null;
try
{
var response = await client.GetAsync("certs/" + pin);
response.EnsureSuccessStatusCode();
byte[] rawCert = await response.Content.ReadAsByteArrayAsync();
X509Certificate2Collection certs = new X509Certificate2Collection();
certs.Import(rawCert, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.UserKeySet);
store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection oldCerts = new X509Certificate2Collection();
foreach (var cert in certs)
{
oldCerts.AddRange(store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, cert.Subject, false));
}
store.RemoveRange(certs);
store.AddRange(certs);
store.Close();
Console.WriteLine("Success");
}
catch (HttpRequestException e)
{
Console.WriteLine("Error communicating with vcremote. Make sure that vcremote is running in secure mode and that a new client cert has been generated.");
}
finally
{
if (store != null)
{
store.Close();
}
}
}
}).Wait();
}
示例4: Configure
public override void Configure(IOfferRemoteComposition server)
{
if (_copyCertFromFile)
{
var cert = new X509Certificate2(_certFile);
ConfigureCertInstall(server, cert);
}
else
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
try
{
var certs = new X509Certificate2Collection();
if (_certFriendlyName != null)
{
certs.AddRange(store.Certificates.Cast<X509Certificate2>().Where(cert => cert.FriendlyName == _certFriendlyName).ToArray());
}
else
{
certs.AddRange(store.Certificates.Find(_findType, _searchString, true));
}
if (certs.Count != 1)
{
if (certs.Count < 1)
throw new ConDepCertificateNotFoundException("Certificate not found");
throw new ConDepCertificateDuplicationException("More than one certificate found in search");
}
ConfigureCertInstall(server, certs[0]);
}
finally
{
store.Close();
}
}
}
示例5: FindCertificateAcrossCertStore
/// <summary>
///
/// </summary>
/// <param name="findBy"></param>
/// <param name="value"></param>
/// <returns></returns>
public static X509Certificate2 FindCertificateAcrossCertStore(X509FindType findBy, string value)
{
var certColl = new X509Certificate2Collection();
foreach (var e in (StoreName[])Enum.GetValues(typeof(StoreName)))
{
var store = new X509Store(e, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
certColl.AddRange(store.Certificates.Find(findBy, value, false));
store.Close();
if (certColl.Count != 0)
{
return certColl[0];
}
}
throw new Exception("Certificate not found: " + value);
}
示例6: initialize
//.........这里部分代码省略.........
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException(ex);
e.reason = "IceSSL: unable to load password callback class " + passwordCallbackClass;
throw e;
}
if(_passwordCallback == null)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: unable to load password callback class " + passwordCallbackClass;
throw e;
}
}
//
// If the user hasn't supplied a certificate collection, we need to examine
// the property settings.
//
if(_certs == null)
{
//
// If IceSSL.CertFile is defined, load a certificate from a file and
// add it to the collection.
//
// TODO: tracing?
_certs = new X509Certificate2Collection();
string certFile = properties.getProperty(prefix + "CertFile");
string passwordStr = properties.getProperty(prefix + "Password");
if(certFile.Length > 0)
{
if(!checkPath(ref certFile))
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: certificate file not found: " + certFile;
throw e;
}
SecureString password = null;
if(passwordStr.Length > 0)
{
password = createSecureString(passwordStr);
}
else if(_passwordCallback != null)
{
password = _passwordCallback.getPassword(certFile);
}
try
{
X509Certificate2 cert;
if(password != null)
{
cert = new X509Certificate2(certFile, password, keyStorageFlags);
}
else
{
cert = new X509Certificate2(certFile, "", keyStorageFlags);
}
_certs.Add(cert);
}
catch(CryptographicException ex)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException(ex);
e.reason = "IceSSL: error while attempting to load certificate from " + certFile;
throw e;
}
}
//
// If IceSSL.FindCert.* properties are defined, add the selected certificates
// to the collection.
//
// TODO: tracing?
const string findPrefix = prefix + "FindCert.";
Dictionary<string, string> certProps = properties.getPropertiesForPrefix(findPrefix);
if(certProps.Count > 0)
{
foreach(KeyValuePair<string, string> entry in certProps)
{
string name = entry.Key;
string val = entry.Value;
if(val.Length > 0)
{
string storeSpec = name.Substring(findPrefix.Length);
X509Certificate2Collection coll = findCertificates(name, storeSpec, val);
_certs.AddRange(coll);
}
}
if(_certs.Count == 0)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: no certificates found";
throw e;
}
}
}
_initialized = true;
}
示例7: BuildBagOfCerts
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;
}
示例8: initialize
//.........这里部分代码省略.........
try
{
X509Certificate2 cert;
X509KeyStorageFlags importFlags;
if(_useMachineContext)
{
importFlags = X509KeyStorageFlags.MachineKeySet;
}
else
{
importFlags = X509KeyStorageFlags.UserKeySet;
}
if(password != null)
{
cert = new X509Certificate2(certFile, password, importFlags);
}
else
{
cert = new X509Certificate2(certFile, "", importFlags);
}
_certs.Add(cert);
}
catch(CryptographicException ex)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException(ex);
e.reason = "IceSSL: error while attempting to load certificate from " + certFile;
throw e;
}
}
else if(findCert.Length > 0)
{
string certStore = properties.getPropertyWithDefault("IceSSL.CertStore", "My");
_certs.AddRange(findCertificates("IceSSL.FindCert", storeLocation, certStore, findCert));
if(_certs.Count == 0)
{
throw new Ice.PluginInitializationException("IceSSL: no certificates found");
}
}
else if(findCertProps.Count > 0)
{
//
// If IceSSL.FindCert.* properties are defined, add the selected certificates
// to the collection.
//
foreach(KeyValuePair<string, string> entry in findCertProps)
{
string name = entry.Key;
string val = entry.Value;
if(val.Length > 0)
{
string storeSpec = name.Substring(findPrefix.Length);
StoreLocation storeLoc = 0;
StoreName storeName = 0;
string sname = null;
parseStore(name, storeSpec, ref storeLoc, ref storeName, ref sname);
if(sname == null)
{
sname = storeName.ToString();
}
X509Certificate2Collection coll = findCertificates(name, storeLoc, sname, val);
_certs.AddRange(coll);
}
}
if(_certs.Count == 0)
{
示例9: PickCertificate
private static X509Certificate2 PickCertificate()
{
X509Certificate2Collection collection = new X509Certificate2Collection();
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
collection.AddRange(store.Certificates);
#if NO
store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
collection.AddRange(store.Certificates);
#endif
try
{
// pick a certificate from the store
X509Certificate2Collection selected =
X509Certificate2UI.SelectFromCollection(
collection,
"受信任的根证书",
"请选择一个证书:",
X509SelectionFlag.SingleSelection);
if (selected.Count == 0)
return null;
return selected[0];
/*
X509Certificate2 cert =
// show certificate details dialog
X509Certificate2UI.DisplayCertificate(cert);
* */
}
finally
{
store.Close();
}
}
示例10: BuildCertChainStore
/// <summary>
/// Build a certificate list containing all certificates needed to perform a chain validate of the {cert} param
/// </summary>
/// <param name="cert">The certificate that needs to be validated</param>
/// <param name="store">An opened X509Store to extract keys from</param>
/// <return></return>
private static X509Certificate2Collection BuildCertChainStore(X509Certificate2 cert, X509Store store)
{
if (cert == null)
throw new ArgumentNullException(nameof(cert));
if (store == null)
throw new ArgumentNullException(nameof(store));
var collection = new X509Certificate2Collection();
// Calculate which certificate issued the given {cert} param
// Do this by extracting the Authority Key Identifier and searching
// the store for certificates with an identical Subject Key Identifier
// For all matches (though there should only be one), repeat the process
// until no matches are found. This will ensure certificates for the entire
// chain are selected
var authorityKey = ExtractX509Extension(cert, OidAuthorityKeyIdentifier);
var matches = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, authorityKey.RawData, true);
foreach (var match in matches)
{
collection.Add(match);
collection.AddRange(BuildCertChainStore(match, store));
}
return collection;
}
示例11: BuildBagOfCerts
private X509Certificate2Collection BuildBagOfCerts()
{
X509Certificate2Collection certificates = new X509Certificate2Collection();
if (this.KeyInfo != null)
{
foreach (KeyInfoClause clause in this.KeyInfo)
{
KeyInfoX509Data data = clause as KeyInfoX509Data;
if (data != null)
{
certificates.AddRange(System.Security.Cryptography.Xml.Utils.BuildBagOfCerts(data, CertUsageType.Verification));
}
}
}
return certificates;
}
示例12: findAllCertificatesInAllStores
private X509Certificate2Collection findAllCertificatesInAllStores(IList<X509Store> stores, Func<X509Store, X509Certificate2Collection> searchAction)
{
X509Certificate2Collection matchingCertificates = new X509Certificate2Collection();
foreach (var store in stores)
matchingCertificates.AddRange(this.findAllCertificatesInStore(store, searchAction));
return matchingCertificates;
}
示例13: BuildOriginatorStore
private static SafeCertStoreHandle BuildOriginatorStore(X509Certificate2Collection bagOfCerts, X509Certificate2Collection extraStore)
{
X509Certificate2Collection collection = new X509Certificate2Collection();
try
{
X509Store x509Store = new X509Store("AddressBook", StoreLocation.CurrentUser);
x509Store.Open(OpenFlags.OpenExistingOnly | OpenFlags.IncludeArchived);
collection.AddRange(x509Store.Certificates);
}
catch (SecurityException ex)
{
}
try
{
X509Store x509Store = new X509Store("AddressBook", StoreLocation.LocalMachine);
x509Store.Open(OpenFlags.OpenExistingOnly | OpenFlags.IncludeArchived);
collection.AddRange(x509Store.Certificates);
}
catch (SecurityException ex)
{
}
if (bagOfCerts != null)
collection.AddRange(bagOfCerts);
if (extraStore != null)
collection.AddRange(extraStore);
if (collection.Count == 0)
throw new CryptographicException(-2146885628);
else
return X509Utils.ExportToMemoryStore(collection);
}
示例14: findCertificates
private X509Certificate2Collection findCertificates(string prop, string storeSpec, string value)
{
StoreLocation storeLoc = 0;
StoreName storeName = 0;
string storeNameStr = null;
parseStore(prop, storeSpec, ref storeLoc, ref storeName, ref storeNameStr);
//
// Open the X509 certificate store.
//
X509Store store = null;
try
{
if(storeNameStr != null)
{
store = new X509Store(storeNameStr, storeLoc);
}
else
{
store = new X509Store(storeName, storeLoc);
}
store.Open(OpenFlags.ReadOnly);
}
catch(Exception ex)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException(ex);
e.reason = "IceSSL: failure while opening store specified by " + prop;
throw e;
}
//
// Start with all of the certificates in the collection and filter as necessary.
//
// - If the value is "*", return all certificates.
// - Otherwise, search using key:value pairs. The following keys are supported:
//
// Issuer
// IssuerDN
// Serial
// Subject
// SubjectDN
// SubjectKeyId
// Thumbprint
//
// A value must be enclosed in single or double quotes if it contains whitespace.
//
X509Certificate2Collection result = new X509Certificate2Collection();
result.AddRange(store.Certificates);
try
{
if(value != "*")
{
int start = 0;
int pos;
while((pos = value.IndexOf(':', start)) != -1)
{
//
// Parse the X509FindType.
//
string field = value.Substring(start, pos - start).Trim().ToUpperInvariant();
X509FindType findType;
if(field.Equals("SUBJECT"))
{
findType = X509FindType.FindBySubjectName;
}
else if(field.Equals("SUBJECTDN"))
{
findType = X509FindType.FindBySubjectDistinguishedName;
}
else if(field.Equals("ISSUER"))
{
findType = X509FindType.FindByIssuerName;
}
else if(field.Equals("ISSUERDN"))
{
findType = X509FindType.FindByIssuerDistinguishedName;
}
else if(field.Equals("THUMBPRINT"))
{
findType = X509FindType.FindByThumbprint;
}
else if(field.Equals("SUBJECTKEYID"))
{
findType = X509FindType.FindBySubjectKeyIdentifier;
}
else if(field.Equals("SERIAL"))
{
findType = X509FindType.FindBySerialNumber;
}
else
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: unknown key in `" + value + "'";
throw e;
}
//
// Parse the argument.
//
start = pos + 1;
//.........这里部分代码省略.........
示例15: CreateBagOfCertificates
internal static X509Certificate2Collection CreateBagOfCertificates (CmsSigner signer) {
X509Certificate2Collection certificates = new X509Certificate2Collection();
//
// First add extra bag of certs.
//
certificates.AddRange(signer.Certificates);
//
// Then include chain option.
//
if (signer.IncludeOption != X509IncludeOption.None) {
if (signer.IncludeOption == X509IncludeOption.EndCertOnly) {
certificates.Add(signer.Certificate);
}
else {
int cCerts = 1;
X509Chain chain = new X509Chain();
chain.Build(signer.Certificate);
// Can't honor the option if we only have a partial chain.
if ((chain.ChainStatus.Length > 0) &&
((chain.ChainStatus[0].Status & X509ChainStatusFlags.PartialChain) == X509ChainStatusFlags.PartialChain))
throw new CryptographicException(CAPI.CERT_E_CHAINING);
if (signer.IncludeOption == X509IncludeOption.WholeChain) {
cCerts = chain.ChainElements.Count;
}
else {
// Default to ExcludeRoot.
if (chain.ChainElements.Count > 1) {
cCerts = chain.ChainElements.Count - 1;
}
}
for (int i = 0; i < cCerts; i++) {
certificates.Add(chain.ChainElements[i].Certificate);
}
}
}
return certificates;
}