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); }
public void TestImportSingleCertificate () { var store = new X509CertificateStore (); store.Import (GetTestDataPath (CertificateAuthorities[0])); var certificate = store.Certificates.FirstOrDefault (); var count = store.Certificates.Count (); Assert.AreEqual (1, count, "Unexpected number of certificates imported."); Assert.AreEqual ("*****@*****.**", certificate.GetSubjectEmailAddress (), "Unexpected email address for certificate."); }
/// <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; }
public void TestImportExportMultipleCertificates() { var store = new X509CertificateStore (); foreach (var authority in CertificateAuthorities) store.Import (GetTestDataPath (authority)); var count = store.Certificates.Count (); Assert.AreEqual (CertificateAuthorities.Length, count, "Unexpected number of certificates imported."); store.Export ("exported.crt"); var imported = new X509CertificateStore (); imported.Import ("exported.crt"); count = imported.Certificates.Count (); Assert.AreEqual (CertificateAuthorities.Length, count, "Unexpected number of certificates re-imported."); }
/// <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); }
public void TestImportExportPkcs12() { var store = new X509CertificateStore (); store.Import (GetTestDataPath ("smime.p12"), "no.secret"); var certificate = store.Certificates.FirstOrDefault (); var count = store.Certificates.Count (); Assert.AreEqual (1, count, "Unexpected number of certificates imported."); Assert.IsNotNull (store.GetPrivateKey (certificate), "Failed to get private key."); foreach (var authority in CertificateAuthorities) store.Import (GetTestDataPath (authority)); store.Export ("exported.p12", "no.secret"); var imported = new X509CertificateStore (); imported.Import ("exported.p12", "no.secret"); count = imported.Certificates.Count (); Assert.AreEqual (store.Certificates.Count (), count, "Unexpected number of certificates re-imported."); Assert.IsNotNull (imported.GetPrivateKey (certificate), "Failed to get private key after re-importing."); }
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; }
/// <summary> /// Initializes a new instance of the <see cref="MimeKit.Cryptography.DefaultSecureMimeContext"/> class. /// </summary> /// <param name="revokedFileName">The path to the revoked certificate lists.</param> /// <param name="addressbookFileName">The path to the addressbook certificates.</param> /// <param name="rootFileName">The path to the root certificates.</param> /// <param name="userFileName">The path to the pkcs12-formatted user certificates.</param> /// <param name="password">The password for the pkcs12 user certificates file.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="addressbookFileName"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="rootFileName"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="userFileName"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="password"/> is <c>null</c>.</para> /// </exception> /// <exception cref="System.IO.IOException"> /// An error occurred while reading the file. /// </exception> protected DefaultSecureMimeContext(string revokedFileName, string addressbookFileName, string rootFileName, string userFileName, string password) { addressbook = new X509CertificateStore (); store = new X509CertificateStore (); root = new X509CertificateStore (); crls = new HashSet<X509Crl> (); try { using (var file = File.OpenRead (revokedFileName)) { var parser = new X509CrlParser (); foreach (X509Crl crl in parser.ReadCrls (file)) crls.Add (crl); } } catch (FileNotFoundException) { } try { addressbook.Import (addressbookFileName); } catch (FileNotFoundException) { } try { store.Import (userFileName, password); } catch (FileNotFoundException) { } try { root.Import (rootFileName); } catch (FileNotFoundException) { } this.addressbookFileName = addressbookFileName; this.revokedFileName = revokedFileName; this.userFileName = userFileName; this.password = password; }