27
27
using System ;
28
28
using System . IO ;
29
29
using System . Net ;
30
+ using System . Linq ;
30
31
using System . Net . Http ;
31
32
using System . Threading ;
32
33
using System . Threading . Tasks ;
56
57
using IssuerAndSerialNumber = Org . BouncyCastle . Asn1 . Cms . IssuerAndSerialNumber ;
57
58
58
59
using MimeKit . IO ;
59
- using System . Linq ;
60
- using Org . BouncyCastle . Tls ;
61
60
62
61
namespace MimeKit . Cryptography {
63
62
/// <summary>
@@ -68,6 +67,7 @@ namespace MimeKit.Cryptography {
68
67
/// </remarks>
69
68
public abstract class BouncyCastleSecureMimeContext : SecureMimeContext
70
69
{
70
+ static readonly X509CertStoreSelector MatchAllCertificates = new X509CertStoreSelector ( ) ;
71
71
static readonly string RsassaPssOid = PkcsObjectIdentifiers . IdRsassaPss . Id ;
72
72
static readonly HttpClient SharedHttpClient = new HttpClient ( ) ;
73
73
@@ -171,9 +171,9 @@ protected virtual HttpClient HttpClient {
171
171
/// generally issued by a certificate authority (CA).</para>
172
172
/// <para>This method is used to build a certificate chain while verifying
173
173
/// signed content.</para>
174
- /// <para>It is critical to always load the designated trust anchors
175
- /// and not the anchor in the end certificate when building a certificate chain
176
- /// to validated trust.</para>
174
+ /// <para>It is critical to always load the designated trust anchors,
175
+ /// and not the anchor in the end certificate, when building a certificate chain
176
+ /// when validating trust.</para>
177
177
/// </remarks>
178
178
/// <returns>The trusted anchors.</returns>
179
179
protected abstract ISet < TrustAnchor > GetTrustedAnchors ( ) ;
@@ -348,7 +348,7 @@ Stream Sign (CmsSigner signer, Stream content, bool encapsulate, CancellationTok
348
348
async Task < Stream > SignAsync ( CmsSigner signer , Stream content , bool encapsulate , CancellationToken cancellationToken )
349
349
{
350
350
if ( CheckCertificateRevocation )
351
- await ValidateCertificateChainAsync ( signer . CertificateChain , DateTime . UtcNow , cancellationToken ) ;
351
+ await ValidateCertificateChainAsync ( signer . CertificateChain , DateTime . UtcNow , cancellationToken ) . ConfigureAwait ( false ) ;
352
352
353
353
var signedData = CreateSignedDataGenerator ( signer ) ;
354
354
var memory = new MemoryBlockStream ( ) ;
@@ -713,9 +713,8 @@ protected IList<X509Certificate> BuildCertificateChain (X509Certificate certific
713
713
var issuerStore = GetTrustedAnchors ( ) ;
714
714
var anchorStore = new X509CertificateStore ( ) ;
715
715
716
- foreach ( var anchor in issuerStore ) {
716
+ foreach ( var anchor in issuerStore )
717
717
anchorStore . Add ( anchor . TrustedCert ) ;
718
- }
719
718
720
719
var parameters = new PkixBuilderParameters ( issuerStore , selector ) {
721
720
ValidityModel = PkixParameters . PkixValidityModel ,
@@ -726,7 +725,7 @@ protected IList<X509Certificate> BuildCertificateChain (X509Certificate certific
726
725
727
726
var intermediateStore = GetIntermediateCertificates ( ) ;
728
727
729
- foreach ( var intermediate in intermediateStore . EnumerateMatches ( new X509CertStoreSelector ( ) ) )
728
+ foreach ( var intermediate in intermediateStore . EnumerateMatches ( MatchAllCertificates ) )
730
729
anchorStore . Add ( intermediate ) ;
731
730
732
731
parameters . AddStoreCert ( anchorStore ) ;
@@ -761,7 +760,7 @@ protected IList<X509Certificate> BuildCertificateChain (X509Certificate certific
761
760
/// <exception cref="ArgumentException">
762
761
/// <paramref name="chain"/> is empty or contains a <see langword="null"/> certificate.
763
762
/// </exception>
764
- public bool ValidateCertificateChain ( X509CertificateChain chain , DateTime dateTime , CancellationToken cancellationToken = default )
763
+ bool ValidateCertificateChain ( X509CertificateChain chain , DateTime dateTime , CancellationToken cancellationToken = default )
765
764
{
766
765
if ( chain == null )
767
766
throw new ArgumentNullException ( nameof ( chain ) ) ;
@@ -780,9 +779,8 @@ public bool ValidateCertificateChain (X509CertificateChain chain, DateTime dateT
780
779
var issuerStore = GetTrustedAnchors ( ) ;
781
780
var anchorStore = new X509CertificateStore ( ) ;
782
781
783
- foreach ( var anchor in issuerStore ) {
782
+ foreach ( var anchor in issuerStore )
784
783
anchorStore . Add ( anchor . TrustedCert ) ;
785
- }
786
784
787
785
var parameters = new PkixBuilderParameters ( issuerStore , selector ) {
788
786
ValidityModel = PkixParameters . PkixValidityModel ,
@@ -798,7 +796,7 @@ public bool ValidateCertificateChain (X509CertificateChain chain, DateTime dateT
798
796
799
797
var intermediateStore = GetIntermediateCertificates ( ) ;
800
798
801
- foreach ( var intermediate in intermediateStore . EnumerateMatches ( new X509CertStoreSelector ( ) ) ) {
799
+ foreach ( var intermediate in intermediateStore . EnumerateMatches ( MatchAllCertificates ) ) {
802
800
anchorStore . Add ( intermediate ) ;
803
801
if ( CheckCertificateRevocation )
804
802
DownloadCrls ( intermediate , cancellationToken ) ;
@@ -837,7 +835,7 @@ public bool ValidateCertificateChain (X509CertificateChain chain, DateTime dateT
837
835
/// <exception cref="ArgumentException">
838
836
/// <paramref name="chain"/> is empty or contains a <see langword="null"/> certificate.
839
837
/// </exception>
840
- public async Task < bool > ValidateCertificateChainAsync ( X509CertificateChain chain , DateTime dateTime , CancellationToken cancellationToken = default )
838
+ async Task < bool > ValidateCertificateChainAsync ( X509CertificateChain chain , DateTime dateTime , CancellationToken cancellationToken = default )
841
839
{
842
840
if ( chain == null )
843
841
throw new ArgumentNullException ( nameof ( chain ) ) ;
@@ -856,9 +854,8 @@ public async Task<bool> ValidateCertificateChainAsync (X509CertificateChain chai
856
854
var issuerStore = GetTrustedAnchors ( ) ;
857
855
var anchorStore = new X509CertificateStore ( ) ;
858
856
859
- foreach ( var anchor in issuerStore ) {
857
+ foreach ( var anchor in issuerStore )
860
858
anchorStore . Add ( anchor . TrustedCert ) ;
861
- }
862
859
863
860
var parameters = new PkixBuilderParameters ( issuerStore , selector ) {
864
861
ValidityModel = PkixParameters . PkixValidityModel ,
@@ -874,7 +871,7 @@ public async Task<bool> ValidateCertificateChainAsync (X509CertificateChain chai
874
871
875
872
var intermediateStore = GetIntermediateCertificates ( ) ;
876
873
877
- foreach ( var intermediate in intermediateStore . EnumerateMatches ( new X509CertStoreSelector ( ) ) ) {
874
+ foreach ( var intermediate in intermediateStore . EnumerateMatches ( MatchAllCertificates ) ) {
878
875
anchorStore . Add ( intermediate ) ;
879
876
if ( CheckCertificateRevocation )
880
877
await DownloadCrlsAsync ( intermediate , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -1169,7 +1166,7 @@ static IEnumerable<string> EnumerateCrlDistributionPointUrls (X509Certificate ce
1169
1166
}
1170
1167
}
1171
1168
1172
- void DownloadCrls ( X509Certificate certificate , CancellationToken cancellationToken = default )
1169
+ void DownloadCrls ( X509Certificate certificate , CancellationToken cancellationToken )
1173
1170
{
1174
1171
var nextUpdate = GetNextCertificateRevocationListUpdate ( certificate . IssuerDN ) ;
1175
1172
var now = DateTime . UtcNow ;
@@ -1305,9 +1302,8 @@ DigitalSignatureCollection GetDigitalSignatures (CmsSignedDataParser parser, Can
1305
1302
foreach ( var anchor in anchors )
1306
1303
DownloadCrls ( anchor . TrustedCert , cancellationToken ) ;
1307
1304
1308
- foreach ( X509Certificate intermediate in intermediates . EnumerateMatches ( new X509CertStoreSelector ( ) ) ) {
1305
+ foreach ( var intermediate in intermediates . EnumerateMatches ( MatchAllCertificates ) )
1309
1306
DownloadCrls ( intermediate , cancellationToken ) ;
1310
- }
1311
1307
}
1312
1308
1313
1309
try {
@@ -1364,9 +1360,8 @@ async Task<DigitalSignatureCollection> GetDigitalSignaturesAsync (CmsSignedDataP
1364
1360
foreach ( var anchor in anchors )
1365
1361
await DownloadCrlsAsync ( anchor . TrustedCert , cancellationToken ) . ConfigureAwait ( false ) ;
1366
1362
1367
- foreach ( X509Certificate intermediate in intermediates . EnumerateMatches ( new X509CertStoreSelector ( ) ) ) {
1368
- await DownloadCrlsAsync ( intermediate , cancellationToken ) ;
1369
- }
1363
+ foreach ( var intermediate in intermediates . EnumerateMatches ( MatchAllCertificates ) )
1364
+ await DownloadCrlsAsync ( intermediate , cancellationToken ) . ConfigureAwait ( false ) ;
1370
1365
}
1371
1366
1372
1367
try {
0 commit comments