Skip to content

Commit 5846a22

Browse files
committed
Added WindowsSecureMimeDigitalCertificate.DnsNames property
Also added more unit tests.
1 parent 40b1d4d commit 5846a22

5 files changed

+97
-14
lines changed

MimeKit/Cryptography/SecureMimeDigitalCertificate.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public X509Certificate Certificate {
6969
}
7070

7171
// /// <summary>
72-
// /// Gets the chain status.
72+
// /// Get the chain status.
7373
// /// </summary>
7474
// /// <value>The chain status.</value>
7575
// public X509ChainStatusFlags ChainStatus {
@@ -79,7 +79,7 @@ public X509Certificate Certificate {
7979
#region IDigitalCertificate implementation
8080

8181
/// <summary>
82-
/// Gets the public key algorithm supported by the certificate.
82+
/// Get the public key algorithm supported by the certificate.
8383
/// </summary>
8484
/// <remarks>
8585
/// Gets the public key algorithm supported by the certificate.
@@ -90,7 +90,7 @@ public PublicKeyAlgorithm PublicKeyAlgorithm {
9090
}
9191

9292
/// <summary>
93-
/// Gets the date that the certificate was created.
93+
/// Get the date that the certificate was created.
9494
/// </summary>
9595
/// <remarks>
9696
/// Gets the date that the certificate was created.
@@ -101,7 +101,7 @@ public DateTime CreationDate {
101101
}
102102

103103
/// <summary>
104-
/// Gets the expiration date of the certificate.
104+
/// Get the expiration date of the certificate.
105105
/// </summary>
106106
/// <remarks>
107107
/// Gets the expiration date of the certificate.
@@ -112,7 +112,7 @@ public DateTime ExpirationDate {
112112
}
113113

114114
/// <summary>
115-
/// Gets the fingerprint of the certificate.
115+
/// Get the fingerprint of the certificate.
116116
/// </summary>
117117
/// <remarks>
118118
/// Gets the fingerprint of the certificate.
@@ -123,7 +123,7 @@ public string Fingerprint {
123123
}
124124

125125
/// <summary>
126-
/// Gets the email address of the owner of the certificate.
126+
/// Get the email address of the owner of the certificate.
127127
/// </summary>
128128
/// <remarks>
129129
/// Gets the email address of the owner of the certificate.
@@ -134,7 +134,7 @@ public string Email {
134134
}
135135

136136
/// <summary>
137-
/// Gets the DNS names of the owner of the certificate.
137+
/// Get the DNS names of the owner of the certificate.
138138
/// </summary>
139139
/// <remarks>
140140
/// Gets the DNS names of the owner of the certificate.
@@ -145,7 +145,7 @@ public string[] DnsNames {
145145
}
146146

147147
/// <summary>
148-
/// Gets the name of the owner of the certificate.
148+
/// Get the name of the owner of the certificate.
149149
/// </summary>
150150
/// <remarks>
151151
/// Gets the name of the owner of the certificate.

MimeKit/Cryptography/WindowsSecureMimeDigitalCertificate.cs

+17-6
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public X509Certificate2 Certificate {
7878
#region IDigitalCertificate implementation
7979

8080
/// <summary>
81-
/// Gets the public key algorithm supported by the certificate.
81+
/// Get the public key algorithm supported by the certificate.
8282
/// </summary>
8383
/// <remarks>
8484
/// Gets the public key algorithm supported by the certificate.
@@ -89,7 +89,7 @@ public PublicKeyAlgorithm PublicKeyAlgorithm {
8989
}
9090

9191
/// <summary>
92-
/// Gets the date that the certificate was created.
92+
/// Get the date that the certificate was created.
9393
/// </summary>
9494
/// <remarks>
9595
/// Gets the date that the certificate was created.
@@ -100,7 +100,7 @@ public DateTime CreationDate {
100100
}
101101

102102
/// <summary>
103-
/// Gets the expiration date of the certificate.
103+
/// Get the expiration date of the certificate.
104104
/// </summary>
105105
/// <remarks>
106106
/// Gets the expiration date of the certificate.
@@ -111,7 +111,7 @@ public DateTime ExpirationDate {
111111
}
112112

113113
/// <summary>
114-
/// Gets the fingerprint of the certificate.
114+
/// Get the fingerprint of the certificate.
115115
/// </summary>
116116
/// <remarks>
117117
/// Gets the fingerprint of the certificate.
@@ -122,7 +122,7 @@ public string Fingerprint {
122122
}
123123

124124
/// <summary>
125-
/// Gets the email address of the owner of the certificate.
125+
/// Get the email address of the owner of the certificate.
126126
/// </summary>
127127
/// <remarks>
128128
/// Gets the email address of the owner of the certificate.
@@ -133,7 +133,18 @@ public string Email {
133133
}
134134

135135
/// <summary>
136-
/// Gets the name of the owner of the certificate.
136+
/// Get the DNS names of the owner of the certificate.
137+
/// </summary>
138+
/// <remarks>
139+
/// Gets the DNS names of the owner of the certificate.
140+
/// </remarks>
141+
/// <value>The DNS name.</value>
142+
public string[] DnsNames {
143+
get { return Certificate.GetSubjectDnsNames (true); }
144+
}
145+
146+
/// <summary>
147+
/// Get the name of the owner of the certificate.
137148
/// </summary>
138149
/// <remarks>
139150
/// Gets the name of the owner of the certificate.

UnitTests/Cryptography/ApplicationPkcs7MimeTests.cs

+31
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// THE SOFTWARE.
2525
//
2626

27+
using System.Text;
2728
using System.Security.Cryptography.X509Certificates;
2829

2930
using Org.BouncyCastle.X509;
@@ -532,6 +533,33 @@ public async Task TestEncryptDnsNamesAsync ()
532533
}
533534
}
534535

536+
static string EncodeDnsNames (string[] dnsNames)
537+
{
538+
if (dnsNames == null || dnsNames.Length == 0)
539+
return string.Empty;
540+
541+
var builder = new StringBuilder ();
542+
543+
foreach (var name in dnsNames) {
544+
if (builder.Length > 0)
545+
builder.Append (", ");
546+
builder.Append (name);
547+
}
548+
549+
return builder.ToString ();
550+
}
551+
552+
static string GetDnsNames (IDigitalCertificate certificate)
553+
{
554+
if (certificate is SecureMimeDigitalCertificate smime)
555+
return EncodeDnsNames (smime.DnsNames);
556+
557+
if (certificate is WindowsSecureMimeDigitalCertificate windows)
558+
return EncodeDnsNames (windows.DnsNames);
559+
560+
return string.Empty;
561+
}
562+
535563
void AssertSignResults (SMimeCertificate certificate, SecureMimeContext ctx, ApplicationPkcs7Mime signed, TextPart entity)
536564
{
537565
var signatures = signed.Verify (ctx, out var encapsulated);
@@ -545,6 +573,7 @@ void AssertSignResults (SMimeCertificate certificate, SecureMimeContext ctx, App
545573

546574
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
547575
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
576+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
548577
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
549578
Assert.That (signature.SignerCertificate.CreationDate, Is.EqualTo (certificate.CreationDate), "CreationDate");
550579
Assert.That (signature.SignerCertificate.ExpirationDate, Is.EqualTo (certificate.ExpirationDate), "ExpirationDate");
@@ -700,6 +729,7 @@ void AssertSignAndEncryptResults (SMimeCertificate certificate, SecureMimeContex
700729

701730
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
702731
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
732+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
703733
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
704734
Assert.That (signature.SignerCertificate.CreationDate, Is.EqualTo (certificate.CreationDate), "CreationDate");
705735
Assert.That (signature.SignerCertificate.ExpirationDate, Is.EqualTo (certificate.ExpirationDate), "ExpirationDate");
@@ -749,6 +779,7 @@ async Task AssertSignAndEncryptResultsAsync (SMimeCertificate certificate, Secur
749779

750780
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
751781
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
782+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
752783
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
753784
Assert.That (signature.SignerCertificate.CreationDate, Is.EqualTo (certificate.CreationDate), "CreationDate");
754785
Assert.That (signature.SignerCertificate.ExpirationDate, Is.EqualTo (certificate.ExpirationDate), "ExpirationDate");

UnitTests/Cryptography/CertificateExtensionTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ public void TestArgumentExceptions ()
4747
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetCommonName (null));
4848
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetSubjectName (null));
4949
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetSubjectEmailAddress (null));
50+
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetSubjectDnsNames (null));
5051
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetFingerprint (null));
5152
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetKeyUsageFlags ((X509Certificate) null));
5253
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetEncryptionAlgorithms (null));
5354
Assert.Throws<ArgumentNullException> (() => BouncyCastleCertificateExtensions.GetPublicKeyAlgorithm (null));
5455

56+
Assert.Throws<ArgumentNullException> (() => X509Certificate2Extensions.GetPrivateKeyAsAsymmetricKeyParameter (null));
5557
Assert.Throws<ArgumentNullException> (() => X509Certificate2Extensions.AsBouncyCastleCertificate (null));
5658
Assert.Throws<ArgumentNullException> (() => X509Certificate2Extensions.GetEncryptionAlgorithms (null));
5759
Assert.Throws<ArgumentNullException> (() => X509Certificate2Extensions.GetPublicKeyAlgorithm (null));
60+
Assert.Throws<ArgumentNullException> (() => X509Certificate2Extensions.GetSubjectDnsNames (null));
5861
}
5962

6063
static X509KeyUsageFlags GetX509Certificate2KeyUsageFlags (X509Certificate2 certificate)

UnitTests/Cryptography/SecureMimeTests.cs

+38
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// THE SOFTWARE.
2525
//
2626

27+
using System.Text;
2728
using System.Security.Cryptography.X509Certificates;
2829

2930
using Org.BouncyCastle.Pkcs;
@@ -639,6 +640,33 @@ protected virtual EncryptionAlgorithm[] GetEncryptionAlgorithms (IDigitalSignatu
639640
return ((SecureMimeDigitalSignature) signature).EncryptionAlgorithms;
640641
}
641642

643+
static string EncodeDnsNames (string[] dnsNames)
644+
{
645+
if (dnsNames == null || dnsNames.Length == 0)
646+
return string.Empty;
647+
648+
var builder = new StringBuilder ();
649+
650+
foreach (var name in dnsNames) {
651+
if (builder.Length > 0)
652+
builder.Append (", ");
653+
builder.Append (name);
654+
}
655+
656+
return builder.ToString ();
657+
}
658+
659+
static string GetDnsNames (IDigitalCertificate certificate)
660+
{
661+
if (certificate is SecureMimeDigitalCertificate smime)
662+
return EncodeDnsNames (smime.DnsNames);
663+
664+
if (certificate is WindowsSecureMimeDigitalCertificate windows)
665+
return EncodeDnsNames (windows.DnsNames);
666+
667+
return string.Empty;
668+
}
669+
642670
[Test]
643671
public virtual void TestSecureMimeEncapsulatedSigning ()
644672
{
@@ -667,6 +695,7 @@ public virtual void TestSecureMimeEncapsulatedSigning ()
667695

668696
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
669697
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
698+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
670699
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
671700
Assert.That (signature.SignerCertificate.CreationDate, Is.EqualTo (certificate.CreationDate), "CreationDate");
672701
Assert.That (signature.SignerCertificate.ExpirationDate, Is.EqualTo (certificate.ExpirationDate), "ExpirationDate");
@@ -726,6 +755,7 @@ public virtual async Task TestSecureMimeEncapsulatedSigningAsync ()
726755

727756
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
728757
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
758+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
729759
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
730760
Assert.That (signature.SignerCertificate.CreationDate, Is.EqualTo (certificate.CreationDate), "CreationDate");
731761
Assert.That (signature.SignerCertificate.ExpirationDate, Is.EqualTo (certificate.ExpirationDate), "ExpirationDate");
@@ -1073,6 +1103,7 @@ public virtual void TestSecureMimeSigningWithCmsSigner ()
10731103
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
10741104
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
10751105
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
1106+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
10761107
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
10771108

10781109
var algorithms = GetEncryptionAlgorithms (signature);
@@ -1127,6 +1158,7 @@ public virtual async Task TestSecureMimeSigningWithCmsSignerAsync ()
11271158
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
11281159
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
11291160
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
1161+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
11301162
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
11311163

11321164
var algorithms = GetEncryptionAlgorithms (signature);
@@ -1181,6 +1213,7 @@ public virtual void TestSecureMimeSigningWithContextAndCmsSigner ()
11811213
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
11821214
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
11831215
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
1216+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
11841217
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
11851218

11861219
var algorithms = GetEncryptionAlgorithms (signature);
@@ -1252,6 +1285,7 @@ public virtual async Task TestSecureMimeSigningWithContextAndCmsSignerAsync ()
12521285
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
12531286
Assert.That (signature.SignerCertificate.Name, Is.EqualTo ("MimeKit UnitTests"));
12541287
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (certificate.EmailAddress));
1288+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
12551289
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
12561290

12571291
var algorithms = GetEncryptionAlgorithms (signature);
@@ -1451,6 +1485,7 @@ public virtual void TestSecureMimeMessageSigning ()
14511485
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
14521486
Assert.That (signature.SignerCertificate.Name, Is.EqualTo (self.Name));
14531487
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (self.Address));
1488+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
14541489
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
14551490

14561491
var algorithms = GetEncryptionAlgorithms (signature);
@@ -1532,6 +1567,7 @@ public virtual async Task TestSecureMimeMessageSigningAsync ()
15321567
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
15331568
Assert.That (signature.SignerCertificate.Name, Is.EqualTo (self.Name));
15341569
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (self.Address));
1570+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
15351571
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (certificate.Fingerprint));
15361572

15371573
var algorithms = GetEncryptionAlgorithms (signature);
@@ -2284,6 +2320,7 @@ public virtual void TestSecureMimeSignAndEncrypt ()
22842320
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
22852321
Assert.That (signature.SignerCertificate.Name, Is.EqualTo (self.Name));
22862322
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (self.Address));
2323+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
22872324
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (self.Fingerprint));
22882325

22892326
var algorithms = GetEncryptionAlgorithms (signature);
@@ -2374,6 +2411,7 @@ public virtual async Task TestSecureMimeSignAndEncryptAsync ()
23742411
if (ctx is not WindowsSecureMimeContext || Environment.OSVersion.Platform == PlatformID.Win32NT)
23752412
Assert.That (signature.SignerCertificate.Name, Is.EqualTo (self.Name));
23762413
Assert.That (signature.SignerCertificate.Email, Is.EqualTo (self.Address));
2414+
Assert.That (GetDnsNames (signature.SignerCertificate), Is.EqualTo (EncodeDnsNames (certificate.DnsNames)));
23772415
Assert.That (signature.SignerCertificate.Fingerprint.ToLowerInvariant (), Is.EqualTo (self.Fingerprint));
23782416

23792417
var algorithms = GetEncryptionAlgorithms (signature);

0 commit comments

Comments
 (0)