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)); }
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."); }
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."); }
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."); }
/// <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; }