本文整理汇总了C#中System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Add方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection.Add方法的具体用法?C# X509Certificate2Collection.Add怎么用?C# X509Certificate2Collection.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.X509Certificates.X509Certificate2Collection
的用法示例。
在下文中一共展示了X509Certificate2Collection.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FixtureSetUp
public void FixtureSetUp ()
{
cert_empty = new X509Certificate2 ();
cert1 = new X509Certificate2 (X509Certificate2Test.farscape_pfx, "farscape", X509KeyStorageFlags.Exportable);
cert2 = new X509Certificate2 (Encoding.ASCII.GetBytes (X509Certificate2Test.base64_cert));
//empty = new X509Certificate2Collection ();
collection = new X509Certificate2Collection ();
collection.Add (cert1);
collection.Add (cert2);
}
示例2: Rc4AndCngWrappersDontMixTest
public static void Rc4AndCngWrappersDontMixTest()
{
//
// Combination of RC4 over a CAPI certificate.
//
// This works as long as the PKCS implementation opens the cert using CAPI. If he creates a CNG wrapper handle (by passing CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG),
// the test fails with a NOTSUPPORTED crypto exception inside Decrypt(). The same happens if the key is genuinely CNG.
//
byte[] content = { 6, 3, 128, 33, 44 };
AlgorithmIdentifier rc4 = new AlgorithmIdentifier(new Oid(Oids.Rc4));
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(content), rc4);
CmsRecipientCollection recipients = new CmsRecipientCollection(new CmsRecipient(Certificates.RSAKeyTransferCapi1.GetCertificate()));
ecms.Encrypt(recipients);
byte[] encodedMessage = ecms.Encode();
ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
using (X509Certificate2 cert = Certificates.RSAKeyTransferCapi1.TryGetCertificateWithPrivateKey())
{
if (cert == null)
return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can.
X509Certificate2Collection extraStore = new X509Certificate2Collection();
extraStore.Add(cert);
ecms.Decrypt(extraStore);
}
ContentInfo contentInfo = ecms.ContentInfo;
Assert.Equal<byte>(content, contentInfo.Content);
}
示例3: 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;
}
示例4: PickCertificate
public static X509Certificate2 PickCertificate()
{
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var collection = store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
var gostOnlyCollection = new X509Certificate2Collection();
foreach ( var cert in collection.Cast<X509Certificate2>()
.Where(cert => cert.SignatureAlgorithm.Value.Equals("1.2.643.2.2.3")))
gostOnlyCollection.Add(cert);
if (gostOnlyCollection.Count == 0)
throw new ApplicationException("Не найдено ни одной подписи соответствующей ГОСТ Р 34.11/34.10-2001. \n");
var found = X509Certificate2UI.SelectFromCollection(
gostOnlyCollection,
"Выберите сертификат",
"Выбранная ЭЦП будет использована при подписании файла, и является эквивалентом собственноручной подписи либо печати организации",
X509SelectionFlag.SingleSelection
);
if (found.Count == 0)
{
throw new ApplicationException("Сертификат не выбран.\n");
}
if (found.Count > 1)
{
throw new ApplicationException("Найдено больше одного сертификата.\n");
}
return found[0];
}
示例5: ReceiverCertificates
/// <summary>
/// Initialisiert eine neue Instanz der <see cref="ReceiverCertificates"/> Klasse.
/// </summary>
/// <param name="certificates">Die Empfänger-Zertifikate</param>
public ReceiverCertificates(IReadOnlyCollection<X509Certificate2> certificates)
{
var receiverCertificates = new Dictionary<string, X509Certificate2>();
var rootCertificates = new List<X509Certificate2>();
var intermediateCertificates = new X509Certificate2Collection();
foreach (var certificate in certificates)
{
var key = GetKey(certificate);
if (key == null)
{
if (certificate.SubjectName.Name == certificate.IssuerName.Name)
{
rootCertificates.Add(certificate);
}
else
{
intermediateCertificates.Add(certificate);
}
}
else
{
receiverCertificates.Add(key, certificate);
}
}
_rootCertificates = rootCertificates.ToArray();
_intermediateCertificates = intermediateCertificates;
Certificates = receiverCertificates;
}
示例6: ConnectToAPNS
public bool ConnectToAPNS(PushData objPush)
{
try
{
string p12Certificate = "WizzDevelopement.p12";
#if DEBUG
// p12Certificate = "WizzProductioncert.p12";
#else
p12Certificate = "WizzProductioncert.p12";
#endif
string p12fileName = p12Certificate; string password = "123456";
X509Certificate2Collection certs = new X509Certificate2Collection();
// Add the Apple cert to our collection
certs.Add(getServerCert(p12fileName, password));
string apsHost;
if (getServerCert(p12fileName, password).ToString().Contains("Production"))
apsHost = "gateway.push.apple.com";
// Port= Convert.ToInt32("2195")
else
apsHost = "gateway.sandbox.push.apple.com";
TcpClient tcpClient = new TcpClient(apsHost, 2195);
// Connect();
// Create a TCP socket connection to the Apple server on port 2195
// Create a new SSL stream over the connection
sslStream = new SslStream(tcpClient.GetStream());
// Authenticate using the Apple cert
sslStream.AuthenticateAsClient(apsHost, certs, SslProtocols.Default, false);
//6455903fb51593b5fb930ab8184e39c5ec83b57503e6589596d463ddb4b1d835
Int32 messa = objPush.message.Length;
if (messa < 256)
{
Boolean send = PushMessage(objPush);
return send;
}
else
{
throw new Exception("Message not exeed 256 character");
}
}
catch (Exception ex)
{
return false;
// throw new Exception(ex.Message);
}
}
示例7: SendEmptyPushNotification
public void SendEmptyPushNotification(string deviceIdentifier, string thumbprint)
{
string server = "gateway.push.apple.com";
using (TcpClient tcpClient = new TcpClient(server, 2195))
{
Trace.TraceInformation("Opening SSL Connection...");
using (SslStream sslStream = new SslStream(tcpClient.GetStream()))
{
try
{
X509Certificate2Collection certs = new X509Certificate2Collection();
Trace.TraceInformation("Adding certificate to connection...");
X509Certificate cert = GetAppleServerCert(thumbprint);
certs.Add(cert);
Trace.TraceInformation("Authenticating against the SSL stream...");
sslStream.AuthenticateAsClient(server, certs, SslProtocols.Default, false);
}
catch (AuthenticationException exp)
{
Trace.TraceError("Failed to authenticate to APNS - {0}", exp.Message);
return;
}
catch (IOException exp)
{
Trace.TraceError("Failed to connect to APNS - {0}", exp.Message);
return;
}
byte[] buf = new byte[256];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32 });
byte[] deviceToken = HexToData(deviceIdentifier);
bw.Write(deviceToken);
string msg = "{}";
bw.Write(new byte[] { 0, 2 });
bw.Write(msg.ToCharArray());
bw.Flush();
Trace.TraceInformation("Message sent. Closing stream...");
if (sslStream != null)
{
sslStream.Write(ms.ToArray());
}
sslStream.Flush();
byte[] response = new byte[6];
sslStream.Read(response, 0, 6);
}
}
}
示例8: MoveTo
public void MoveTo(X509Certificate2Collection collection)
{
Debug.Assert(collection != null);
ICertificatePal localCert = Interlocked.Exchange(ref _cert, null);
Debug.Assert(localCert != null);
collection.Add(new X509Certificate2(localCert));
}
示例9: CopyTo
public void CopyTo(X509Certificate2Collection collection)
{
Debug.Assert(collection != null);
SafeCertContextHandle pCertContext = null;
while (Interop.crypt32.CertEnumCertificatesInStore(_certStore, ref pCertContext))
{
X509Certificate2 cert = new X509Certificate2(pCertContext.DangerousGetHandle());
collection.Add(cert);
}
}
示例10: GetClientCertificates
internal static X509Certificate2Collection GetClientCertificates (ClientParameters parameters)
{
if (parameters.ClientCertificate == null)
return null;
var clientCertificateCollection = new X509Certificate2Collection ();
var certificate = (X509Certificate2)CertificateProvider.GetCertificate (parameters.ClientCertificate);
clientCertificateCollection.Add (certificate);
return clientCertificateCollection;
}
示例11: CloneTo
public void CloneTo(X509Certificate2Collection collection)
{
Debug.Assert(collection != null);
Debug.Assert(_certs != null);
foreach (X509Certificate2 cert in _certs)
{
var certPal = (OpenSslX509CertificateReader)cert.Pal;
collection.Add(new X509Certificate2(certPal.DuplicateHandles()));
}
}
示例12: FilterValidCerts
public static X509Certificate2Collection FilterValidCerts(
X509Certificate2Collection certs,
X509ChainPolicy policy,
X509ChainStatusFlags problemFlags,
Action<Exception> notification)
{
var validCerts = new X509Certificate2Collection();
if (certs == null) return null;
foreach (var cert in certs)
{
X509Chain chainBuilder = new X509Chain();
chainBuilder.ChainPolicy = policy.Clone();
try
{
// We're using the system class as a helper to merely build the chain
// However, we will review each item in the chain ourselves, because we have our own rules...
chainBuilder.Build(cert);
X509ChainElementCollection chainElements = chainBuilder.ChainElements;
// If we don't have a trust chain, then we obviously have a problem...
if (chainElements.IsNullOrEmpty())
{
notification(new Exception(string.Format("Can't find a trust chain: {0} ", cert.Subject)));
return null;
}
// walk the chain starting at the leaf and see if we hit any issues before the anchor
foreach (X509ChainElement chainElement in chainElements)
{
if (ChainElementHasProblems(chainElement, problemFlags))
{
//this.NotifyProblem(chainElement);
notification(new Exception(string.Format("Chain Element has problem {0}", Summarize(chainElement, problemFlags))));
// Whoops... problem with at least one cert in the chain. Stop immediately
return null;
}
}
}
catch (Exception ex)
{
//this.NotifyError(certificate, ex);
// just eat it and drop out to return null
notification(ex);
return null;
}
validCerts.Add(cert);
}
return validCerts;
}
示例13: CertificatesHandler
/// <summary>
/// Initializes static members of the <see cref="CertificatesHandler" /> class.
/// </summary>
static CertificatesHandler()
{
var cacert = System.Text.Encoding.Default.GetString(Payplug.Resource.cacert);
var rawPemCertificates = new Regex(
@"-----BEGIN CERTIFICATE-----((?:.*\s)*?)-----END CERTIFICATE-----",
RegexOptions.Compiled | RegexOptions.Multiline).Matches(cacert);
certificate2Collection = new X509Certificate2Collection();
foreach (Match rawPemCertificate in rawPemCertificates)
{
certificate2Collection.Add(new X509Certificate2(Convert.FromBase64String(rawPemCertificate.Groups[1].Value)));
}
}
示例14: AddAnchorsForBundle
private void AddAnchorsForBundle(Bundle bundle, X509Certificate2Collection certs)
{
try
{
X509Certificate2Collection bundleCerts = m_downloader.DownloadCertificates(bundle.Uri);
if (!bundleCerts.IsNullOrEmpty())
{
certs.Add(bundleCerts);
}
}
catch (Exception e)
{
this.NotifyError(bundle, e);
}
}
示例15: SelfSignedTest
public void SelfSignedTest()
{
var chain = new X509Chain();
var trusted = new X509Certificate2Collection();
Assert.IsFalse(chain.Build(Certificates.SelfSigned));
Assert.IsFalse(chain.VerifyWithExtraRoots(Certificates.SelfSigned, trusted));
trusted.Add(Certificates.SelfSigned);
Assert.IsTrue(chain.VerifyWithExtraRoots(Certificates.SelfSigned, trusted));
Assert.IsFalse(chain.Build(Certificates.SelfSigned));
trusted.Clear();
Assert.IsFalse(chain.VerifyWithExtraRoots(Certificates.SelfSigned, trusted));
Assert.IsFalse(chain.Build(Certificates.SelfSigned));
}