public void TestArgumentExceptions () { var store = new X509CertificateStore (); Assert.Throws<ArgumentNullException> (() => store.Add (null)); Assert.Throws<ArgumentNullException> (() => store.AddRange (null)); Assert.Throws<ArgumentNullException> (() => store.Export ((Stream) null, "password")); Assert.Throws<ArgumentNullException> (() => store.Export ((string) null, "password")); Assert.Throws<ArgumentNullException> (() => store.Export (Stream.Null, null)); Assert.Throws<ArgumentNullException> (() => store.Export ("fileName", null)); Assert.Throws<ArgumentNullException> (() => store.Export ((Stream) null)); Assert.Throws<ArgumentNullException> (() => store.Export ((string) null)); Assert.Throws<ArgumentNullException> (() => store.GetPrivateKey (null)); Assert.Throws<ArgumentNullException> (() => store.Import ((Stream) null, "password")); Assert.Throws<ArgumentNullException> (() => store.Import ((string) null, "password")); Assert.Throws<ArgumentNullException> (() => store.Import ((byte[]) null, "password")); Assert.Throws<ArgumentNullException> (() => store.Import (Stream.Null, null)); Assert.Throws<ArgumentNullException> (() => store.Import (GetTestDataPath ("smime.p12"), null)); Assert.Throws<ArgumentNullException> (() => store.Import (new byte[0], null)); Assert.Throws<ArgumentNullException> (() => store.Import ((Stream) null)); Assert.Throws<ArgumentNullException> (() => store.Import ((string) null)); Assert.Throws<ArgumentNullException> (() => store.Import ((byte[]) null)); Assert.Throws<ArgumentNullException> (() => store.Remove (null)); Assert.Throws<ArgumentNullException> (() => store.RemoveRange (null)); }
/// <summary> /// Exports the certificates for the specified mailboxes. /// </summary> /// <returns>A new <see cref="MimeKit.Cryptography.ApplicationPkcs7Mime"/> instance containing /// the exported keys.</returns> /// <param name="mailboxes">The mailboxes.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="mailboxes"/> is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentException"> /// A certificate for one or more of the <paramref name="mailboxes"/> could not be found. /// </exception> public override MimePart Export(IEnumerable <MailboxAddress> mailboxes) { // FIXME: find out what exceptions BouncyCastle can throw... if (mailboxes == null) { throw new ArgumentNullException("mailboxes"); } var certificates = new X509CertificateStore(); int count = 0; foreach (var mailbox in mailboxes) { var recipient = GetCmsRecipient(mailbox); certificates.Add(recipient.Certificate); count++; } if (count == 0) { throw new ArgumentException("No mailboxes specified.", "mailboxes"); } var cms = new CmsSignedDataGenerator(); cms.AddCertificates(certificates); var signedData = cms.Generate(new CmsProcessableByteArray(new byte[0]), false); var memory = new MemoryStream(signedData.GetEncoded(), false); return(new ApplicationPkcs7Mime(SecureMimeType.CertsOnly, memory)); }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <remarks> /// An intermediate certificate is any certificate that exists between the root /// certificate issued by a Certificate Authority (CA) and the certificate at /// the end of the chain. /// </remarks> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates() { var store = new X509CertificateStore(); foreach (var certificate in certificates) { store.Add(certificate); } return(store); }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates() { var store = new X509CertificateStore(); foreach (var certificate in keychain.GetCertificates((CssmKeyUse)0)) { store.Add(certificate); } return(store); }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <remarks> /// An intermediate certificate is any certificate that exists between the root /// certificate issued by a Certificate Authority (CA) and the certificate at /// the end of the chain. /// </remarks> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates() { var intermediates = new X509CertificateStore(); foreach (var certificate in certificates) { var keyUsage = certificate.GetKeyUsage(); if (keyUsage != null && keyUsage[(int)X509KeyUsageBits.KeyCertSign] && !certificate.IsSelfSigned()) { intermediates.Add(certificate); } } return(intermediates); }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <remarks> /// Gets the intermediate certificates. /// </remarks> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates () { var storeNames = new [] { StoreName.My, StoreName.AddressBook, StoreName.TrustedPeople, StoreName.Root }; var intermediate = new X509CertificateStore (); foreach (var storeName in storeNames) { var store = new X509Store (storeName, StoreLocation); store.Open (OpenFlags.ReadOnly); foreach (var certificate in store.Certificates) { var cert = DotNetUtilities.FromX509Certificate (certificate); intermediate.Add (cert); } store.Close (); } return intermediate; }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <remarks> /// Gets the intermediate certificates. /// </remarks> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates() { var storeNames = new [] { StoreName.AuthRoot, StoreName.CertificateAuthority, StoreName.TrustedPeople, StoreName.TrustedPublisher }; var intermediate = new X509CertificateStore(); foreach (var storeName in storeNames) { var store = new X509Store(storeName, StoreLocation); store.Open(OpenFlags.ReadOnly); foreach (var certificate in store.Certificates) { var cert = GetBouncyCastleCertificate(certificate); intermediate.Add(cert); } store.Close(); } return(intermediate); }
PkixCertPath BuildCertPath(HashSet anchors, IX509Store certificates, IX509Store crls, X509Certificate certificate, DateTime? signingTime) { var intermediate = new X509CertificateStore (); foreach (X509Certificate cert in certificates.GetMatches (null)) intermediate.Add (cert); var selector = new X509CertStoreSelector (); selector.Certificate = certificate; var parameters = new PkixBuilderParameters (anchors, selector); parameters.AddStore (GetIntermediateCertificates ()); parameters.AddStore (intermediate); var localCrls = GetCertificateRevocationLists (); parameters.AddStore (localCrls); parameters.AddStore (crls); // Note: we disable revocation unless we actually have non-empty revocation lists parameters.IsRevocationEnabled = localCrls.GetMatches (null).Count > 0; parameters.ValidityModel = PkixParameters.ChainValidityModel; if (signingTime.HasValue) parameters.Date = new DateTimeObject (signingTime.Value); var result = new PkixCertPathBuilder ().Build (parameters); return result.CertPath; }
/// <summary> /// Exports the certificates for the specified mailboxes. /// </summary> /// <returns>A new <see cref="MimeKit.Cryptography.ApplicationPkcs7Mime"/> instance containing /// the exported keys.</returns> /// <param name="mailboxes">The mailboxes.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="mailboxes"/> is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentException"> /// No mailboxes were specified. /// </exception> /// <exception cref="CertificateNotFoundException"> /// A certificate for one or more of the <paramref name="mailboxes"/> could not be found. /// </exception> /// <exception cref="Org.BouncyCastle.Cms.CmsException"> /// An error occurred in the cryptographic message syntax subsystem. /// </exception> public override MimePart Export(IEnumerable<MailboxAddress> mailboxes) { if (mailboxes == null) throw new ArgumentNullException ("mailboxes"); var certificates = new X509CertificateStore (); int count = 0; foreach (var mailbox in mailboxes) { var recipient = GetCmsRecipient (mailbox); certificates.Add (recipient.Certificate); count++; } if (count == 0) throw new ArgumentException ("No mailboxes specified.", "mailboxes"); var cms = new CmsSignedDataStreamGenerator (); var memory = new MemoryStream (); cms.AddCertificates (certificates); cms.Open (memory).Close (); memory.Position = 0; return new ApplicationPkcs7Mime (SecureMimeType.CertsOnly, memory); }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates () { var store = new X509CertificateStore (); foreach (var certificate in certificates) { store.Add (certificate); } return store; }
/// <summary> /// Gets the intermediate certificates. /// </summary> /// <returns>The intermediate certificates.</returns> protected override IX509Store GetIntermediateCertificates() { var store = new X509CertificateStore (); foreach (var certificate in keychain.GetCertificates ((CssmKeyUse) 0)) { store.Add (certificate); } return store; }