@@ -517,15 +517,26 @@ static async Task<byte[]> ReadAllBytesAsync (Stream stream, CancellationToken ca
517
517
}
518
518
}
519
519
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 )
521
521
{
522
- ContentInfo contentInfo ;
522
+ var contentInfo = new ContentInfo ( ReadAllBytes ( content ) ) ;
523
+ var signed = new SignedCms ( contentInfo , detach ) ;
523
524
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 ( ) ;
528
533
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 ) ) ;
529
540
var signed = new SignedCms ( contentInfo , detach ) ;
530
541
531
542
try {
@@ -571,7 +582,7 @@ public override ApplicationPkcs7Mime EncapsulatedSign (CmsSigner signer, Stream
571
582
throw new ArgumentNullException ( nameof ( content ) ) ;
572
583
573
584
var real = GetRealCmsSigner ( signer ) ;
574
- var signedData = SignAsync ( real , content , false , false , cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
585
+ var signedData = Sign ( real , content , false , cancellationToken ) ;
575
586
576
587
return new ApplicationPkcs7Mime ( SecureMimeType . SignedData , signedData ) ;
577
588
}
@@ -607,7 +618,7 @@ public override async Task<ApplicationPkcs7Mime> EncapsulatedSignAsync (CmsSigne
607
618
throw new ArgumentNullException ( nameof ( content ) ) ;
608
619
609
620
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 ) ;
611
622
612
623
return new ApplicationPkcs7Mime ( SecureMimeType . SignedData , signedData ) ;
613
624
}
@@ -653,7 +664,7 @@ public override ApplicationPkcs7Mime EncapsulatedSign (MailboxAddress signer, Di
653
664
throw new ArgumentNullException ( nameof ( content ) ) ;
654
665
655
666
var real = GetCmsSigner ( signer , digestAlgo ) ;
656
- var signedData = SignAsync ( real , content , false , false , cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
667
+ var signedData = Sign ( real , content , false , cancellationToken ) ;
657
668
658
669
return new ApplicationPkcs7Mime ( SecureMimeType . SignedData , signedData ) ;
659
670
}
@@ -699,7 +710,7 @@ public override async Task<ApplicationPkcs7Mime> EncapsulatedSignAsync (MailboxA
699
710
throw new ArgumentNullException ( nameof ( content ) ) ;
700
711
701
712
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 ) ;
703
714
704
715
return new ApplicationPkcs7Mime ( SecureMimeType . SignedData , signedData ) ;
705
716
}
@@ -735,7 +746,7 @@ public override ApplicationPkcs7Signature Sign (CmsSigner signer, Stream content
735
746
throw new ArgumentNullException ( nameof ( content ) ) ;
736
747
737
748
var real = GetRealCmsSigner ( signer ) ;
738
- var signature = SignAsync ( real , content , true , false , cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
749
+ var signature = Sign ( real , content , true , cancellationToken ) ;
739
750
740
751
return new ApplicationPkcs7Signature ( signature ) ;
741
752
}
@@ -771,7 +782,7 @@ public override async Task<ApplicationPkcs7Signature> SignAsync (CmsSigner signe
771
782
throw new ArgumentNullException ( nameof ( content ) ) ;
772
783
773
784
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 ) ;
775
786
776
787
return new ApplicationPkcs7Signature ( signature ) ;
777
788
}
@@ -817,7 +828,7 @@ public override MimePart Sign (MailboxAddress signer, DigestAlgorithm digestAlgo
817
828
throw new ArgumentNullException ( nameof ( content ) ) ;
818
829
819
830
var cmsSigner = GetCmsSigner ( signer , digestAlgo ) ;
820
- var signature = SignAsync ( cmsSigner , content , true , false , cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
831
+ var signature = Sign ( cmsSigner , content , true , cancellationToken ) ;
821
832
822
833
return new ApplicationPkcs7Signature ( signature ) ;
823
834
}
@@ -863,7 +874,7 @@ public override async Task<MimePart> SignAsync (MailboxAddress signer, DigestAlg
863
874
throw new ArgumentNullException ( nameof ( content ) ) ;
864
875
865
876
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 ) ;
867
878
868
879
return new ApplicationPkcs7Signature ( signature ) ;
869
880
}
@@ -1145,15 +1156,20 @@ internal RealAlgorithmIdentifier GetAlgorithmIdentifier (EncryptionAlgorithm alg
1145
1156
}
1146
1157
}
1147
1158
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 )
1149
1160
{
1150
- ContentInfo contentInfo ;
1161
+ var contentInfo = new ContentInfo ( ReadAllBytes ( content ) ) ;
1162
+ var algorithm = GetAlgorithmIdentifier ( encryptionAlgorithm ) ;
1163
+ var envelopedData = new EnvelopedCms ( contentInfo , algorithm ) ;
1151
1164
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 ) ;
1156
1166
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 ) ) ;
1157
1173
var algorithm = GetAlgorithmIdentifier ( encryptionAlgorithm ) ;
1158
1174
var envelopedData = new EnvelopedCms ( contentInfo , algorithm ) ;
1159
1175
@@ -1162,18 +1178,32 @@ async Task<Stream> EnvelopeAsync (RealCmsRecipientCollection recipients, Stream
1162
1178
return new MemoryStream ( envelopedData . Encode ( ) , false ) ;
1163
1179
}
1164
1180
1165
- Task < Stream > EnvelopeAsync ( RealCmsRecipientCollection recipients , Stream content , bool doAsync , CancellationToken cancellationToken )
1181
+ Stream Envelope ( RealCmsRecipientCollection recipients , Stream content , CancellationToken cancellationToken )
1166
1182
{
1167
1183
var algorithm = GetPreferredEncryptionAlgorithm ( recipients ) ;
1168
1184
1169
- return EnvelopeAsync ( recipients , content , algorithm , doAsync , cancellationToken ) ;
1185
+ return Envelope ( recipients , content , algorithm , cancellationToken ) ;
1170
1186
}
1171
1187
1172
- Task < Stream > EnvelopeAsync ( CmsRecipientCollection recipients , Stream content , bool doAsync , CancellationToken cancellationToken )
1188
+ Task < Stream > EnvelopeAsync ( RealCmsRecipientCollection recipients , Stream content , CancellationToken cancellationToken )
1173
1189
{
1174
1190
var algorithm = GetPreferredEncryptionAlgorithm ( recipients ) ;
1175
1191
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 ) ;
1177
1207
}
1178
1208
1179
1209
/// <summary>
@@ -1206,7 +1236,7 @@ public override ApplicationPkcs7Mime Encrypt (CmsRecipientCollection recipients,
1206
1236
if ( content == null )
1207
1237
throw new ArgumentNullException ( nameof ( content ) ) ;
1208
1238
1209
- var envelopedData = EnvelopeAsync ( recipients , content , false , cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
1239
+ var envelopedData = Envelope ( recipients , content , cancellationToken ) ;
1210
1240
1211
1241
return new ApplicationPkcs7Mime ( SecureMimeType . EnvelopedData , envelopedData ) ;
1212
1242
}
@@ -1241,7 +1271,7 @@ public override async Task<ApplicationPkcs7Mime> EncryptAsync (CmsRecipientColle
1241
1271
if ( content == null )
1242
1272
throw new ArgumentNullException ( nameof ( content ) ) ;
1243
1273
1244
- var envelopedData = await EnvelopeAsync ( recipients , content , true , cancellationToken ) . ConfigureAwait ( false ) ;
1274
+ var envelopedData = await EnvelopeAsync ( recipients , content , cancellationToken ) . ConfigureAwait ( false ) ;
1245
1275
1246
1276
return new ApplicationPkcs7Mime ( SecureMimeType . EnvelopedData , envelopedData ) ;
1247
1277
}
@@ -1283,7 +1313,7 @@ public override MimePart Encrypt (IEnumerable<MailboxAddress> recipients, Stream
1283
1313
throw new ArgumentNullException ( nameof ( content ) ) ;
1284
1314
1285
1315
var real = GetCmsRecipients ( recipients ) ;
1286
- var envelopedData = EnvelopeAsync ( real , content , false , cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
1316
+ var envelopedData = Envelope ( real , content , cancellationToken ) ;
1287
1317
1288
1318
return new ApplicationPkcs7Mime ( SecureMimeType . EnvelopedData , envelopedData ) ;
1289
1319
}
@@ -1325,45 +1355,18 @@ public override async Task<MimePart> EncryptAsync (IEnumerable<MailboxAddress> r
1325
1355
throw new ArgumentNullException ( nameof ( content ) ) ;
1326
1356
1327
1357
var real = GetCmsRecipients ( recipients ) ;
1328
- var envelopedData = await EnvelopeAsync ( real , content , true , cancellationToken ) ;
1358
+ var envelopedData = await EnvelopeAsync ( real , content , cancellationToken ) . ConfigureAwait ( false ) ;
1329
1359
1330
1360
return new ApplicationPkcs7Mime ( SecureMimeType . EnvelopedData , envelopedData ) ;
1331
1361
}
1332
1362
1333
- static async Task < MimeEntity > DecryptAsync ( Stream encryptedData , bool doAsync , CancellationToken cancellationToken )
1363
+ static byte [ ] Decrypt ( byte [ ] content )
1334
1364
{
1335
- if ( encryptedData == null )
1336
- throw new ArgumentNullException ( nameof ( encryptedData ) ) ;
1337
-
1338
1365
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
-
1347
1366
enveloped . Decode ( content ) ;
1367
+ enveloped . Decrypt ( ) ;
1348
1368
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 ( ) ;
1367
1370
}
1368
1371
1369
1372
/// <summary>
@@ -1386,7 +1389,15 @@ static async Task<MimeEntity> DecryptAsync (Stream encryptedData, bool doAsync,
1386
1389
/// </exception>
1387
1390
public override MimeEntity Decrypt ( Stream encryptedData , CancellationToken cancellationToken = default )
1388
1391
{
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 ) ;
1390
1401
}
1391
1402
1392
1403
/// <summary>
@@ -1407,36 +1418,17 @@ public override MimeEntity Decrypt (Stream encryptedData, CancellationToken canc
1407
1418
/// <exception cref="System.Security.Cryptography.CryptographicException">
1408
1419
/// An error occurred in the cryptographic message syntax subsystem.
1409
1420
/// </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 )
1416
1422
{
1417
1423
if ( encryptedData == null )
1418
1424
throw new ArgumentNullException ( nameof ( encryptedData ) ) ;
1419
1425
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 ) ;
1433
1428
1434
- var encoded = enveloped . Encode ( ) ;
1429
+ var memory = new MemoryStream ( decrypted , false ) ;
1435
1430
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 ) ;
1440
1432
}
1441
1433
1442
1434
/// <summary>
@@ -1461,7 +1453,16 @@ static async Task DecryptToAsync (Stream encryptedData, Stream decryptedData, bo
1461
1453
/// </exception>
1462
1454
public override void DecryptTo ( Stream encryptedData , Stream decryptedData , CancellationToken cancellationToken = default )
1463
1455
{
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 ) ;
1465
1466
}
1466
1467
1467
1468
/// <summary>
@@ -1485,9 +1486,18 @@ public override void DecryptTo (Stream encryptedData, Stream decryptedData, Canc
1485
1486
/// <exception cref="System.Security.Cryptography.CryptographicException">
1486
1487
/// An error occurred in the cryptographic message syntax subsystem.
1487
1488
/// </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 )
1489
1490
{
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 ) ;
1491
1501
}
1492
1502
1493
1503
/// <summary>
0 commit comments