Skip to content

Commit 33cdce6

Browse files
committed
Removed doAsync logic from WindowsSecureMimeContext
1 parent a2a0730 commit 33cdce6

File tree

1 file changed

+96
-86
lines changed

1 file changed

+96
-86
lines changed

MimeKit/Cryptography/WindowsSecureMimeContext.cs

+96-86
Original file line numberDiff line numberDiff line change
@@ -517,15 +517,26 @@ static async Task<byte[]> ReadAllBytesAsync (Stream stream, CancellationToken ca
517517
}
518518
}
519519

520-
static async Task<Stream> SignAsync (RealCmsSigner signer, Stream content, bool detach, bool doAsync, CancellationToken cancellationToken = default)
520+
static Stream Sign (RealCmsSigner signer, Stream content, bool detach, CancellationToken cancellationToken = default)
521521
{
522-
ContentInfo contentInfo;
522+
var contentInfo = new ContentInfo (ReadAllBytes (content));
523+
var signed = new SignedCms (contentInfo, detach);
523524

524-
if (doAsync)
525-
contentInfo = new ContentInfo (await ReadAllBytesAsync (content, cancellationToken).ConfigureAwait (false));
526-
else
527-
contentInfo = new ContentInfo (ReadAllBytes (content));
525+
try {
526+
signed.ComputeSignature (signer, false);
527+
} catch (CryptographicException) {
528+
signer.IncludeOption = X509IncludeOption.EndCertOnly;
529+
signed.ComputeSignature (signer, false);
530+
}
531+
532+
var signedData = signed.Encode ();
528533

534+
return new MemoryStream (signedData, false);
535+
}
536+
537+
static async Task<Stream> SignAsync (RealCmsSigner signer, Stream content, bool detach, CancellationToken cancellationToken = default)
538+
{
539+
var contentInfo = new ContentInfo (await ReadAllBytesAsync (content, cancellationToken).ConfigureAwait (false));
529540
var signed = new SignedCms (contentInfo, detach);
530541

531542
try {
@@ -571,7 +582,7 @@ public override ApplicationPkcs7Mime EncapsulatedSign (CmsSigner signer, Stream
571582
throw new ArgumentNullException (nameof (content));
572583

573584
var real = GetRealCmsSigner (signer);
574-
var signedData = SignAsync (real, content, false, false, cancellationToken).GetAwaiter ().GetResult ();
585+
var signedData = Sign (real, content, false, cancellationToken);
575586

576587
return new ApplicationPkcs7Mime (SecureMimeType.SignedData, signedData);
577588
}
@@ -607,7 +618,7 @@ public override async Task<ApplicationPkcs7Mime> EncapsulatedSignAsync (CmsSigne
607618
throw new ArgumentNullException (nameof (content));
608619

609620
var real = GetRealCmsSigner (signer);
610-
var signedData = await SignAsync (real, content, false, true, cancellationToken).ConfigureAwait (false);
621+
var signedData = await SignAsync (real, content, false, cancellationToken).ConfigureAwait (false);
611622

612623
return new ApplicationPkcs7Mime (SecureMimeType.SignedData, signedData);
613624
}
@@ -653,7 +664,7 @@ public override ApplicationPkcs7Mime EncapsulatedSign (MailboxAddress signer, Di
653664
throw new ArgumentNullException (nameof (content));
654665

655666
var real = GetCmsSigner (signer, digestAlgo);
656-
var signedData = SignAsync (real, content, false, false, cancellationToken).GetAwaiter ().GetResult ();
667+
var signedData = Sign (real, content, false, cancellationToken);
657668

658669
return new ApplicationPkcs7Mime (SecureMimeType.SignedData, signedData);
659670
}
@@ -699,7 +710,7 @@ public override async Task<ApplicationPkcs7Mime> EncapsulatedSignAsync (MailboxA
699710
throw new ArgumentNullException (nameof (content));
700711

701712
var real = GetCmsSigner (signer, digestAlgo);
702-
var signedData = await SignAsync (real, content, false, true, cancellationToken).ConfigureAwait (false);
713+
var signedData = await SignAsync (real, content, false, cancellationToken).ConfigureAwait (false);
703714

704715
return new ApplicationPkcs7Mime (SecureMimeType.SignedData, signedData);
705716
}
@@ -735,7 +746,7 @@ public override ApplicationPkcs7Signature Sign (CmsSigner signer, Stream content
735746
throw new ArgumentNullException (nameof (content));
736747

737748
var real = GetRealCmsSigner (signer);
738-
var signature = SignAsync (real, content, true, false, cancellationToken).GetAwaiter ().GetResult ();
749+
var signature = Sign (real, content, true, cancellationToken);
739750

740751
return new ApplicationPkcs7Signature (signature);
741752
}
@@ -771,7 +782,7 @@ public override async Task<ApplicationPkcs7Signature> SignAsync (CmsSigner signe
771782
throw new ArgumentNullException (nameof (content));
772783

773784
var real = GetRealCmsSigner (signer);
774-
var signature = await SignAsync (real, content, true, true, cancellationToken).ConfigureAwait (false);
785+
var signature = await SignAsync (real, content, true, cancellationToken).ConfigureAwait (false);
775786

776787
return new ApplicationPkcs7Signature (signature);
777788
}
@@ -817,7 +828,7 @@ public override MimePart Sign (MailboxAddress signer, DigestAlgorithm digestAlgo
817828
throw new ArgumentNullException (nameof (content));
818829

819830
var cmsSigner = GetCmsSigner (signer, digestAlgo);
820-
var signature = SignAsync (cmsSigner, content, true, false, cancellationToken).GetAwaiter ().GetResult ();
831+
var signature = Sign (cmsSigner, content, true, cancellationToken);
821832

822833
return new ApplicationPkcs7Signature (signature);
823834
}
@@ -863,7 +874,7 @@ public override async Task<MimePart> SignAsync (MailboxAddress signer, DigestAlg
863874
throw new ArgumentNullException (nameof (content));
864875

865876
var cmsSigner = GetCmsSigner (signer, digestAlgo);
866-
var signature = await SignAsync (cmsSigner, content, true, true, cancellationToken).ConfigureAwait (false);
877+
var signature = await SignAsync (cmsSigner, content, true, cancellationToken).ConfigureAwait (false);
867878

868879
return new ApplicationPkcs7Signature (signature);
869880
}
@@ -1145,15 +1156,20 @@ internal RealAlgorithmIdentifier GetAlgorithmIdentifier (EncryptionAlgorithm alg
11451156
}
11461157
}
11471158

1148-
async Task<Stream> EnvelopeAsync (RealCmsRecipientCollection recipients, Stream content, EncryptionAlgorithm encryptionAlgorithm, bool doAsync, CancellationToken cancellationToken)
1159+
Stream Envelope (RealCmsRecipientCollection recipients, Stream content, EncryptionAlgorithm encryptionAlgorithm, CancellationToken cancellationToken)
11491160
{
1150-
ContentInfo contentInfo;
1161+
var contentInfo = new ContentInfo (ReadAllBytes (content));
1162+
var algorithm = GetAlgorithmIdentifier (encryptionAlgorithm);
1163+
var envelopedData = new EnvelopedCms (contentInfo, algorithm);
11511164

1152-
if (doAsync)
1153-
contentInfo = new ContentInfo (await ReadAllBytesAsync (content, cancellationToken).ConfigureAwait (false));
1154-
else
1155-
contentInfo = new ContentInfo (ReadAllBytes (content));
1165+
envelopedData.Encrypt (recipients);
11561166

1167+
return new MemoryStream (envelopedData.Encode (), false);
1168+
}
1169+
1170+
async Task<Stream> EnvelopeAsync (RealCmsRecipientCollection recipients, Stream content, EncryptionAlgorithm encryptionAlgorithm, CancellationToken cancellationToken)
1171+
{
1172+
var contentInfo = new ContentInfo (await ReadAllBytesAsync (content, cancellationToken).ConfigureAwait (false));
11571173
var algorithm = GetAlgorithmIdentifier (encryptionAlgorithm);
11581174
var envelopedData = new EnvelopedCms (contentInfo, algorithm);
11591175

@@ -1162,18 +1178,32 @@ async Task<Stream> EnvelopeAsync (RealCmsRecipientCollection recipients, Stream
11621178
return new MemoryStream (envelopedData.Encode (), false);
11631179
}
11641180

1165-
Task<Stream> EnvelopeAsync (RealCmsRecipientCollection recipients, Stream content, bool doAsync, CancellationToken cancellationToken)
1181+
Stream Envelope (RealCmsRecipientCollection recipients, Stream content, CancellationToken cancellationToken)
11661182
{
11671183
var algorithm = GetPreferredEncryptionAlgorithm (recipients);
11681184

1169-
return EnvelopeAsync (recipients, content, algorithm, doAsync, cancellationToken);
1185+
return Envelope (recipients, content, algorithm, cancellationToken);
11701186
}
11711187

1172-
Task<Stream> EnvelopeAsync (CmsRecipientCollection recipients, Stream content, bool doAsync, CancellationToken cancellationToken)
1188+
Task<Stream> EnvelopeAsync (RealCmsRecipientCollection recipients, Stream content, CancellationToken cancellationToken)
11731189
{
11741190
var algorithm = GetPreferredEncryptionAlgorithm (recipients);
11751191

1176-
return EnvelopeAsync (GetCmsRecipients (recipients), content, algorithm, doAsync, cancellationToken);
1192+
return EnvelopeAsync (recipients, content, algorithm, cancellationToken);
1193+
}
1194+
1195+
Stream Envelope (CmsRecipientCollection recipients, Stream content, CancellationToken cancellationToken)
1196+
{
1197+
var algorithm = GetPreferredEncryptionAlgorithm (recipients);
1198+
1199+
return Envelope (GetCmsRecipients (recipients), content, algorithm, cancellationToken);
1200+
}
1201+
1202+
Task<Stream> EnvelopeAsync (CmsRecipientCollection recipients, Stream content, CancellationToken cancellationToken)
1203+
{
1204+
var algorithm = GetPreferredEncryptionAlgorithm (recipients);
1205+
1206+
return EnvelopeAsync (GetCmsRecipients (recipients), content, algorithm, cancellationToken);
11771207
}
11781208

11791209
/// <summary>
@@ -1206,7 +1236,7 @@ public override ApplicationPkcs7Mime Encrypt (CmsRecipientCollection recipients,
12061236
if (content == null)
12071237
throw new ArgumentNullException (nameof (content));
12081238

1209-
var envelopedData = EnvelopeAsync (recipients, content, false, cancellationToken).GetAwaiter ().GetResult ();
1239+
var envelopedData = Envelope (recipients, content, cancellationToken);
12101240

12111241
return new ApplicationPkcs7Mime (SecureMimeType.EnvelopedData, envelopedData);
12121242
}
@@ -1241,7 +1271,7 @@ public override async Task<ApplicationPkcs7Mime> EncryptAsync (CmsRecipientColle
12411271
if (content == null)
12421272
throw new ArgumentNullException (nameof (content));
12431273

1244-
var envelopedData = await EnvelopeAsync (recipients, content, true, cancellationToken).ConfigureAwait (false);
1274+
var envelopedData = await EnvelopeAsync (recipients, content, cancellationToken).ConfigureAwait (false);
12451275

12461276
return new ApplicationPkcs7Mime (SecureMimeType.EnvelopedData, envelopedData);
12471277
}
@@ -1283,7 +1313,7 @@ public override MimePart Encrypt (IEnumerable<MailboxAddress> recipients, Stream
12831313
throw new ArgumentNullException (nameof (content));
12841314

12851315
var real = GetCmsRecipients (recipients);
1286-
var envelopedData = EnvelopeAsync (real, content, false, cancellationToken).GetAwaiter ().GetResult ();
1316+
var envelopedData = Envelope (real, content, cancellationToken);
12871317

12881318
return new ApplicationPkcs7Mime (SecureMimeType.EnvelopedData, envelopedData);
12891319
}
@@ -1325,45 +1355,18 @@ public override async Task<MimePart> EncryptAsync (IEnumerable<MailboxAddress> r
13251355
throw new ArgumentNullException (nameof (content));
13261356

13271357
var real = GetCmsRecipients (recipients);
1328-
var envelopedData = await EnvelopeAsync (real, content, true, cancellationToken);
1358+
var envelopedData = await EnvelopeAsync (real, content, cancellationToken).ConfigureAwait (false);
13291359

13301360
return new ApplicationPkcs7Mime (SecureMimeType.EnvelopedData, envelopedData);
13311361
}
13321362

1333-
static async Task<MimeEntity> DecryptAsync (Stream encryptedData, bool doAsync, CancellationToken cancellationToken)
1363+
static byte[] Decrypt (byte[] content)
13341364
{
1335-
if (encryptedData == null)
1336-
throw new ArgumentNullException (nameof (encryptedData));
1337-
13381365
var enveloped = new EnvelopedCms ();
1339-
CryptographicException ce = null;
1340-
byte[] content;
1341-
1342-
if (doAsync)
1343-
content = await ReadAllBytesAsync (encryptedData, cancellationToken).ConfigureAwait (false);
1344-
else
1345-
content = ReadAllBytes (encryptedData);
1346-
13471366
enveloped.Decode (content);
1367+
enveloped.Decrypt ();
13481368

1349-
foreach (var recipient in enveloped.RecipientInfos) {
1350-
try {
1351-
enveloped.Decrypt (recipient);
1352-
ce = null;
1353-
break;
1354-
} catch (CryptographicException ex) {
1355-
ce = ex;
1356-
}
1357-
}
1358-
1359-
if (ce != null)
1360-
throw ce;
1361-
1362-
var decryptedData = enveloped.Encode ();
1363-
1364-
var memory = new MemoryStream (decryptedData, false);
1365-
1366-
return MimeEntity.Load (memory, true, cancellationToken);
1369+
return enveloped.Encode ();
13671370
}
13681371

13691372
/// <summary>
@@ -1386,7 +1389,15 @@ static async Task<MimeEntity> DecryptAsync (Stream encryptedData, bool doAsync,
13861389
/// </exception>
13871390
public override MimeEntity Decrypt (Stream encryptedData, CancellationToken cancellationToken = default)
13881391
{
1389-
return DecryptAsync (encryptedData, false, cancellationToken).GetAwaiter ().GetResult ();
1392+
if (encryptedData == null)
1393+
throw new ArgumentNullException (nameof (encryptedData));
1394+
1395+
var content = ReadAllBytes (encryptedData);
1396+
var decrypted = Decrypt (content);
1397+
1398+
var memory = new MemoryStream (decrypted, false);
1399+
1400+
return MimeEntity.Load (memory, true, cancellationToken);
13901401
}
13911402

13921403
/// <summary>
@@ -1407,36 +1418,17 @@ public override MimeEntity Decrypt (Stream encryptedData, CancellationToken canc
14071418
/// <exception cref="System.Security.Cryptography.CryptographicException">
14081419
/// An error occurred in the cryptographic message syntax subsystem.
14091420
/// </exception>
1410-
public override Task<MimeEntity> DecryptAsync (Stream encryptedData, CancellationToken cancellationToken = default)
1411-
{
1412-
return DecryptAsync (encryptedData, true, cancellationToken);
1413-
}
1414-
1415-
static async Task DecryptToAsync (Stream encryptedData, Stream decryptedData, bool doAsync, CancellationToken cancellationToken)
1421+
public override async Task<MimeEntity> DecryptAsync (Stream encryptedData, CancellationToken cancellationToken = default)
14161422
{
14171423
if (encryptedData == null)
14181424
throw new ArgumentNullException (nameof (encryptedData));
14191425

1420-
if (decryptedData == null)
1421-
throw new ArgumentNullException (nameof (decryptedData));
1422-
1423-
var enveloped = new EnvelopedCms ();
1424-
byte[] content;
1425-
1426-
if (doAsync)
1427-
content = await ReadAllBytesAsync (encryptedData, cancellationToken).ConfigureAwait (false);
1428-
else
1429-
content = ReadAllBytes (encryptedData);
1430-
1431-
enveloped.Decode (content);
1432-
enveloped.Decrypt ();
1426+
var content = await ReadAllBytesAsync (encryptedData, cancellationToken).ConfigureAwait (false);
1427+
var decrypted = Decrypt (content);
14331428

1434-
var encoded = enveloped.Encode ();
1429+
var memory = new MemoryStream (decrypted, false);
14351430

1436-
if (doAsync)
1437-
await decryptedData.WriteAsync (encoded, 0, encoded.Length, cancellationToken).ConfigureAwait (false);
1438-
else
1439-
decryptedData.Write (encoded, 0, encoded.Length);
1431+
return await MimeEntity.LoadAsync (memory, true, cancellationToken).ConfigureAwait (false);
14401432
}
14411433

14421434
/// <summary>
@@ -1461,7 +1453,16 @@ static async Task DecryptToAsync (Stream encryptedData, Stream decryptedData, bo
14611453
/// </exception>
14621454
public override void DecryptTo (Stream encryptedData, Stream decryptedData, CancellationToken cancellationToken = default)
14631455
{
1464-
DecryptToAsync (encryptedData, decryptedData, false, cancellationToken).GetAwaiter ().GetResult ();
1456+
if (encryptedData == null)
1457+
throw new ArgumentNullException (nameof (encryptedData));
1458+
1459+
if (decryptedData == null)
1460+
throw new ArgumentNullException (nameof (decryptedData));
1461+
1462+
var content = ReadAllBytes (encryptedData);
1463+
var decrypted = Decrypt (content);
1464+
1465+
decryptedData.Write (decrypted, 0, decrypted.Length);
14651466
}
14661467

14671468
/// <summary>
@@ -1485,9 +1486,18 @@ public override void DecryptTo (Stream encryptedData, Stream decryptedData, Canc
14851486
/// <exception cref="System.Security.Cryptography.CryptographicException">
14861487
/// An error occurred in the cryptographic message syntax subsystem.
14871488
/// </exception>
1488-
public override Task DecryptToAsync (Stream encryptedData, Stream decryptedData, CancellationToken cancellationToken = default)
1489+
public override async Task DecryptToAsync (Stream encryptedData, Stream decryptedData, CancellationToken cancellationToken = default)
14891490
{
1490-
return DecryptToAsync (encryptedData, decryptedData, false, cancellationToken);
1491+
if (encryptedData == null)
1492+
throw new ArgumentNullException (nameof (encryptedData));
1493+
1494+
if (decryptedData == null)
1495+
throw new ArgumentNullException (nameof (decryptedData));
1496+
1497+
var content = await ReadAllBytesAsync (encryptedData, cancellationToken).ConfigureAwait (false);
1498+
var decrypted = Decrypt (content);
1499+
1500+
await decryptedData.WriteAsync (decrypted, 0, decrypted.Length, cancellationToken).ConfigureAwait (false);
14911501
}
14921502

14931503
/// <summary>

0 commit comments

Comments
 (0)