• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jstedfast / MimeKit / 4.14.0.1801

05 Oct 2025 03:45PM UTC coverage: 94.132% (+0.01%) from 94.122%
4.14.0.1801

push

coveralls.net

jstedfast
Use a bool disposed member variable rather than setting members to null

32370 of 34388 relevant lines covered (94.13%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.58
/MimeKit/Cryptography/ApplicationPkcs7Mime.cs
1
//
2
// ApplicationPkcs7Mime.cs
3
//
4
// Author: Jeffrey Stedfast <jestedfa@microsoft.com>
5
//
6
// Copyright (c) 2013-2025 .NET Foundation and Contributors
7
//
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
// of this software and associated documentation files (the "Software"), to deal
10
// in the Software without restriction, including without limitation the rights
11
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
// copies of the Software, and to permit persons to whom the Software is
13
// furnished to do so, subject to the following conditions:
14
//
15
// The above copyright notice and this permission notice shall be included in
16
// all copies or substantial portions of the Software.
17
//
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
// THE SOFTWARE.
25
//
26

27
using System;
28
using System.IO;
29
using System.Threading;
30
using System.Threading.Tasks;
31
using System.Collections.Generic;
32

33
using MimeKit.IO;
34

35
namespace MimeKit.Cryptography {
36
        /// <summary>
37
        /// An S/MIME part with a Content-Type of application/pkcs7-mime.
38
        /// </summary>
39
        /// <remarks>
40
        /// An application/pkcs7-mime is an S/MIME part and may contain encrypted,
41
        /// signed or compressed data (or any combination of the above).
42
        /// </remarks>
43
        public class ApplicationPkcs7Mime : MimePart, IApplicationPkcs7Mime
44
        {
45
                /// <summary>
46
                /// Initialize a new instance of the <see cref="ApplicationPkcs7Mime"/> class.
47
                /// </summary>
48
                /// <remarks>
49
                /// This constructor is used by <see cref="MimeParser"/>.
50
                /// </remarks>
51
                /// <param name="args">Information used by the constructor.</param>
52
                /// <exception cref="System.ArgumentNullException">
53
                /// <paramref name="args"/> is <see langword="null"/>.
54
                /// </exception>
55
                public ApplicationPkcs7Mime (MimeEntityConstructorArgs args) : base (args)
1✔
56
                {
1✔
57
                }
1✔
58

59
                /// <summary>
60
                /// Initialize a new instance of the <see cref="ApplicationPkcs7Mime"/> class.
61
                /// </summary>
62
                /// <remarks>
63
                /// <para>Creates a new MIME part with a Content-Type of application/pkcs7-mime
64
                /// and the <paramref name="stream"/> as its content.</para>
65
                /// <para>Unless you are writing your own pkcs7 implementation, you'll probably
66
                /// want to use the <see cref="Compress(MimeEntity, CancellationToken)"/>,
67
                /// <see cref="Encrypt(CmsRecipientCollection, MimeEntity, CancellationToken)"/>, and/or
68
                /// <see cref="Sign(CmsSigner, MimeEntity, CancellationToken)"/> method to create new instances
69
                /// of this class.</para>
70
                /// </remarks>
71
                /// <param name="type">The S/MIME type.</param>
72
                /// <param name="stream">The content stream.</param>
73
                /// <exception cref="System.ArgumentNullException">
74
                /// <paramref name="stream"/> is <see langword="null"/>.
75
                /// </exception>
76
                /// <exception cref="System.ArgumentOutOfRangeException">
77
                /// <paramref name="type"/> is not a valid value.
78
                /// </exception>
79
                /// <exception cref="System.ArgumentException">
80
                /// <para><paramref name="stream"/> does not support reading.</para>
81
                /// <para>-or-</para>
82
                /// <para><paramref name="stream"/> does not support seeking.</para>
83
                /// </exception>
84
                public ApplicationPkcs7Mime (SecureMimeType type, Stream stream) : base ("application", "pkcs7-mime")
1✔
85
                {
1✔
86
                        ContentDisposition = new ContentDisposition (ContentDisposition.Attachment);
1✔
87
                        ContentTransferEncoding = ContentEncoding.Base64;
1✔
88
                        Content = new MimeContent (stream);
1✔
89

90
                        switch (type) {
1✔
91
                        case SecureMimeType.CompressedData:
92
                                ContentType.Parameters["smime-type"] = "compressed-data";
1✔
93
                                ContentDisposition.FileName = "smime.p7z";
1✔
94
                                ContentType.Name = "smime.p7z";
1✔
95
                                break;
1✔
96
                        case SecureMimeType.EnvelopedData:
97
                                ContentType.Parameters["smime-type"] = "enveloped-data";
1✔
98
                                ContentDisposition.FileName = "smime.p7m";
1✔
99
                                ContentType.Name = "smime.p7m";
1✔
100
                                break;
1✔
101
                        case SecureMimeType.SignedData:
102
                                ContentType.Parameters["smime-type"] = "signed-data";
1✔
103
                                ContentDisposition.FileName = "smime.p7m";
1✔
104
                                ContentType.Name = "smime.p7m";
1✔
105
                                break;
1✔
106
                        case SecureMimeType.AuthEnvelopedData:
107
                                ContentType.Parameters["smime-type"] = "authenveloped-data";
1✔
108
                                ContentDisposition.FileName = "smime.p7m";
1✔
109
                                ContentType.Name = "smime.p7m";
1✔
110
                                break;
1✔
111
                        case SecureMimeType.CertsOnly:
112
                                ContentType.Parameters["smime-type"] = "certs-only";
1✔
113
                                ContentDisposition.FileName = "smime.p7c";
1✔
114
                                ContentType.Name = "smime.p7c";
1✔
115
                                break;
1✔
116
                        default:
117
                                throw new ArgumentOutOfRangeException (nameof (type));
1✔
118
                        }
119
                }
1✔
120

121
                void CheckDisposed ()
122
                {
1✔
123
                        CheckDisposed (nameof (ApplicationPkcs7Mime));
1✔
124
                }
1✔
125

126
                /// <summary>
127
                /// Gets the value of the "smime-type" parameter.
128
                /// </summary>
129
                /// <remarks>
130
                /// Gets the value of the "smime-type" parameter.
131
                /// </remarks>
132
                /// <value>The value of the "smime-type" parameter.</value>
133
                public SecureMimeType SecureMimeType {
134
                        get {
1✔
135
                                var type = ContentType.Parameters["smime-type"];
1✔
136

137
                                if (type == null)
1✔
138
                                        return SecureMimeType.Unknown;
1✔
139

140
                                if (type.Equals ("authenveloped-data", StringComparison.OrdinalIgnoreCase))
1✔
141
                                        return SecureMimeType.AuthEnvelopedData;
1✔
142
                                if (type.Equals ("compressed-data", StringComparison.OrdinalIgnoreCase))
1✔
143
                                        return SecureMimeType.CompressedData;
1✔
144
                                if (type.Equals ("enveloped-data", StringComparison.OrdinalIgnoreCase))
1✔
145
                                        return SecureMimeType.EnvelopedData;
1✔
146
                                if (type.Equals ("signed-data", StringComparison.OrdinalIgnoreCase))
1✔
147
                                        return SecureMimeType.SignedData;
1✔
148
                                if (type.Equals ("certs-only", StringComparison.OrdinalIgnoreCase))
1✔
149
                                        return SecureMimeType.CertsOnly;
1✔
150

151
                                return SecureMimeType.Unknown;
1✔
152
                        }
1✔
153
                }
154

155
                /// <summary>
156
                /// Dispatches to the specific visit method for this MIME entity.
157
                /// </summary>
158
                /// <remarks>
159
                /// This default implementation for <see cref="ApplicationPkcs7Mime"/> nodes
160
                /// calls <see cref="MimeVisitor.VisitApplicationPkcs7Mime"/>. Override this
161
                /// method to call into a more specific method on a derived visitor class
162
                /// of the <see cref="MimeVisitor"/> class. However, it should still
163
                /// support unknown visitors by calling
164
                /// <see cref="MimeVisitor.VisitApplicationPkcs7Mime"/>.
165
                /// </remarks>
166
                /// <param name="visitor">The visitor.</param>
167
                /// <exception cref="System.ArgumentNullException">
168
                /// <paramref name="visitor"/> is <see langword="null"/>.
169
                /// </exception>
170
                /// <exception cref="System.ObjectDisposedException">
171
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
172
                /// </exception>
173
                public override void Accept (MimeVisitor visitor)
174
                {
1✔
175
                        if (visitor == null)
1✔
176
                                throw new ArgumentNullException (nameof (visitor));
1✔
177

178
                        CheckDisposed ();
1✔
179

180
                        visitor.VisitApplicationPkcs7Mime (this);
1✔
181
                }
1✔
182

183
                /// <summary>
184
                /// Decompress the compressed-data.
185
                /// </summary>
186
                /// <remarks>
187
                /// Decompresses the compressed-data using the specified <see cref="SecureMimeContext"/>.
188
                /// </remarks>
189
                /// <returns>The decompressed <see cref="MimeEntity"/>.</returns>
190
                /// <param name="ctx">The S/MIME context to use for decompressing.</param>
191
                /// <param name="cancellationToken">The cancellation token.</param>
192
                /// <exception cref="System.ArgumentNullException">
193
                /// <paramref name="ctx"/> is <see langword="null"/>.
194
                /// </exception>
195
                /// <exception cref="System.InvalidOperationException">
196
                /// The "smime-type" parameter on the Content-Type header is not "compressed-data".
197
                /// </exception>
198
                /// <exception cref="System.ObjectDisposedException">
199
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
200
                /// </exception>
201
                /// <exception cref="System.OperationCanceledException">
202
                /// The operation was canceled via the cancellation token.
203
                /// </exception>
204
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
205
                /// An error occurred in the cryptographic message syntax subsystem.
206
                /// </exception>
207
                public MimeEntity Decompress (SecureMimeContext ctx, CancellationToken cancellationToken = default)
208
                {
1✔
209
                        if (ctx == null)
1✔
210
                                throw new ArgumentNullException (nameof (ctx));
1✔
211

212
                        CheckDisposed ();
1✔
213

214
                        if (SecureMimeType != SecureMimeType.CompressedData && SecureMimeType != SecureMimeType.Unknown)
1✔
215
                                throw new InvalidOperationException ();
1✔
216

217
                        using (var memory = new MemoryBlockStream ()) {
1✔
218
                                if (Content != null) {
1✔
219
                                        Content.DecodeTo (memory, cancellationToken);
1✔
220
                                        memory.Position = 0;
1✔
221
                                }
1✔
222

223
                                return ctx.Decompress (memory, cancellationToken);
1✔
224
                        }
225
                }
1✔
226

227
                /// <summary>
228
                /// Asynchronously decompress the compressed-data.
229
                /// </summary>
230
                /// <remarks>
231
                /// Asynchronously decompresses the compressed-data using the specified <see cref="SecureMimeContext"/>.
232
                /// </remarks>
233
                /// <returns>The decompressed <see cref="MimeEntity"/>.</returns>
234
                /// <param name="ctx">The S/MIME context to use for decompressing.</param>
235
                /// <param name="cancellationToken">The cancellation token.</param>
236
                /// <exception cref="System.ArgumentNullException">
237
                /// <paramref name="ctx"/> is <see langword="null"/>.
238
                /// </exception>
239
                /// <exception cref="System.InvalidOperationException">
240
                /// The "smime-type" parameter on the Content-Type header is not "compressed-data".
241
                /// </exception>
242
                /// <exception cref="System.ObjectDisposedException">
243
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
244
                /// </exception>
245
                /// <exception cref="System.OperationCanceledException">
246
                /// The operation was canceled via the cancellation token.
247
                /// </exception>
248
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
249
                /// An error occurred in the cryptographic message syntax subsystem.
250
                /// </exception>
251
                public async Task<MimeEntity> DecompressAsync (SecureMimeContext ctx, CancellationToken cancellationToken = default)
252
                {
1✔
253
                        if (ctx == null)
1✔
254
                                throw new ArgumentNullException (nameof (ctx));
1✔
255

256
                        CheckDisposed ();
1✔
257

258
                        if (SecureMimeType != SecureMimeType.CompressedData && SecureMimeType != SecureMimeType.Unknown)
1✔
259
                                throw new InvalidOperationException ();
1✔
260

261
                        using (var memory = new MemoryBlockStream ()) {
1✔
262
                                if (Content != null) {
1✔
263
                                        await Content.DecodeToAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
264
                                        memory.Position = 0;
1✔
265
                                }
1✔
266

267
                                return await ctx.DecompressAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
268
                        }
269
                }
1✔
270

271
                /// <summary>
272
                /// Decompress the compressed-data.
273
                /// </summary>
274
                /// <remarks>
275
                /// Decompresses the compressed-data using the default <see cref="SecureMimeContext"/>.
276
                /// </remarks>
277
                /// <returns>The decompressed <see cref="MimeEntity"/>.</returns>
278
                /// <param name="cancellationToken">The cancellation token.</param>
279
                /// <exception cref="System.InvalidOperationException">
280
                /// The "smime-type" parameter on the Content-Type header is not "compressed-data".
281
                /// </exception>
282
                /// <exception cref="System.ObjectDisposedException">
283
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
284
                /// </exception>
285
                /// <exception cref="System.OperationCanceledException">
286
                /// The operation was canceled via the cancellation token.
287
                /// </exception>
288
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
289
                /// An error occurred in the cryptographic message syntax subsystem.
290
                /// </exception>
291
                public MimeEntity Decompress (CancellationToken cancellationToken = default)
292
                {
1✔
293
                        CheckDisposed ();
1✔
294

295
                        if (SecureMimeType != SecureMimeType.CompressedData && SecureMimeType != SecureMimeType.Unknown)
1✔
296
                                throw new InvalidOperationException ();
1✔
297

298
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
299
                                return Decompress (ctx, cancellationToken);
1✔
300
                }
1✔
301

302
                /// <summary>
303
                /// Asynchronously decompress the compressed-data.
304
                /// </summary>
305
                /// <remarks>
306
                /// Asynchronously decompresses the compressed-data using the default <see cref="SecureMimeContext"/>.
307
                /// </remarks>
308
                /// <returns>The decompressed <see cref="MimeEntity"/>.</returns>
309
                /// <param name="cancellationToken">The cancellation token.</param>
310
                /// <exception cref="System.InvalidOperationException">
311
                /// The "smime-type" parameter on the Content-Type header is not "compressed-data".
312
                /// </exception>
313
                /// <exception cref="System.ObjectDisposedException">
314
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
315
                /// </exception>
316
                /// <exception cref="System.OperationCanceledException">
317
                /// The operation was canceled via the cancellation token.
318
                /// </exception>
319
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
320
                /// An error occurred in the cryptographic message syntax subsystem.
321
                /// </exception>
322
                public async Task<MimeEntity> DecompressAsync (CancellationToken cancellationToken = default)
323
                {
1✔
324
                        CheckDisposed ();
1✔
325

326
                        if (SecureMimeType != SecureMimeType.CompressedData && SecureMimeType != SecureMimeType.Unknown)
1✔
327
                                throw new InvalidOperationException ();
1✔
328

329
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
330
                                return await DecompressAsync (ctx, cancellationToken).ConfigureAwait (false);
1✔
331
                }
1✔
332

333
                /// <summary>
334
                /// Decrypt the enveloped-data.
335
                /// </summary>
336
                /// <remarks>
337
                /// Decrypts the enveloped-data using the specified <see cref="SecureMimeContext"/>.
338
                /// </remarks>
339
                /// <returns>The decrypted <see cref="MimeEntity"/>.</returns>
340
                /// <param name="ctx">The S/MIME context to use for decrypting.</param>
341
                /// <param name="cancellationToken">The cancellation token.</param>
342
                /// <exception cref="System.ArgumentNullException">
343
                /// <paramref name="ctx"/> is <see langword="null"/>.
344
                /// </exception>
345
                /// <exception cref="System.InvalidOperationException">
346
                /// The "smime-type" parameter on the Content-Type header is not "enveloped-data".
347
                /// </exception>
348
                /// <exception cref="System.ObjectDisposedException">
349
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
350
                /// </exception>
351
                /// <exception cref="System.OperationCanceledException">
352
                /// The operation was canceled via the cancellation token.
353
                /// </exception>
354
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
355
                /// An error occurred in the cryptographic message syntax subsystem.
356
                /// </exception>
357
                public MimeEntity Decrypt (SecureMimeContext ctx, CancellationToken cancellationToken = default)
358
                {
1✔
359
                        if (ctx == null)
1✔
360
                                throw new ArgumentNullException (nameof (ctx));
1✔
361

362
                        CheckDisposed ();
1✔
363

364
                        if (SecureMimeType != SecureMimeType.EnvelopedData && SecureMimeType != SecureMimeType.Unknown)
1✔
365
                                throw new InvalidOperationException ();
1✔
366

367
                        using (var memory = new MemoryBlockStream ()) {
1✔
368
                                if (Content != null) {
1✔
369
                                        Content.DecodeTo (memory, cancellationToken);
1✔
370
                                        memory.Position = 0;
1✔
371
                                }
1✔
372

373
                                return ctx.Decrypt (memory, cancellationToken);
1✔
374
                        }
375
                }
1✔
376

377
                /// <summary>
378
                /// Asynchronously decrypt the enveloped-data.
379
                /// </summary>
380
                /// <remarks>
381
                /// Asynchronously decrypts the enveloped-data using the specified <see cref="SecureMimeContext"/>.
382
                /// </remarks>
383
                /// <returns>The decrypted <see cref="MimeEntity"/>.</returns>
384
                /// <param name="ctx">The S/MIME context to use for decrypting.</param>
385
                /// <param name="cancellationToken">The cancellation token.</param>
386
                /// <exception cref="System.ArgumentNullException">
387
                /// <paramref name="ctx"/> is <see langword="null"/>.
388
                /// </exception>
389
                /// <exception cref="System.InvalidOperationException">
390
                /// The "smime-type" parameter on the Content-Type header is not "enveloped-data".
391
                /// </exception>
392
                /// <exception cref="System.ObjectDisposedException">
393
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
394
                /// </exception>
395
                /// <exception cref="System.OperationCanceledException">
396
                /// The operation was canceled via the cancellation token.
397
                /// </exception>
398
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
399
                /// An error occurred in the cryptographic message syntax subsystem.
400
                /// </exception>
401
                public async Task<MimeEntity> DecryptAsync (SecureMimeContext ctx, CancellationToken cancellationToken = default)
402
                {
1✔
403
                        if (ctx == null)
1✔
404
                                throw new ArgumentNullException (nameof (ctx));
1✔
405

406
                        CheckDisposed ();
1✔
407

408
                        if (SecureMimeType != SecureMimeType.EnvelopedData && SecureMimeType != SecureMimeType.Unknown)
1✔
409
                                throw new InvalidOperationException ();
1✔
410

411
                        using (var memory = new MemoryBlockStream ()) {
1✔
412
                                if (Content != null) {
1✔
413
                                        await Content.DecodeToAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
414
                                        memory.Position = 0;
1✔
415
                                }
1✔
416

417
                                return await ctx.DecryptAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
418
                        }
419
                }
1✔
420

421
                /// <summary>
422
                /// Decrypt the enveloped-data.
423
                /// </summary>
424
                /// <remarks>
425
                /// Decrypts the enveloped-data using the default <see cref="SecureMimeContext"/>.
426
                /// </remarks>
427
                /// <returns>The decrypted <see cref="MimeEntity"/>.</returns>
428
                /// <param name="cancellationToken">The cancellation token.</param>
429
                /// <exception cref="System.InvalidOperationException">
430
                /// The "smime-type" parameter on the Content-Type header is not "certs-only".
431
                /// </exception>
432
                /// <exception cref="System.ObjectDisposedException">
433
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
434
                /// </exception>
435
                /// <exception cref="System.OperationCanceledException">
436
                /// The operation was canceled via the cancellation token.
437
                /// </exception>
438
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
439
                /// An error occurred in the cryptographic message syntax subsystem.
440
                /// </exception>
441
                public MimeEntity Decrypt (CancellationToken cancellationToken = default)
442
                {
1✔
443
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
444
                                return Decrypt (ctx, cancellationToken);
1✔
445
                }
1✔
446

447
                /// <summary>
448
                /// Asynchronously decrypt the enveloped-data.
449
                /// </summary>
450
                /// <remarks>
451
                /// Asynchronously decrypts the enveloped-data using the default <see cref="SecureMimeContext"/>.
452
                /// </remarks>
453
                /// <returns>The decrypted <see cref="MimeEntity"/>.</returns>
454
                /// <param name="cancellationToken">The cancellation token.</param>
455
                /// <exception cref="System.InvalidOperationException">
456
                /// The "smime-type" parameter on the Content-Type header is not "certs-only".
457
                /// </exception>
458
                /// <exception cref="System.ObjectDisposedException">
459
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
460
                /// </exception>
461
                /// <exception cref="System.OperationCanceledException">
462
                /// The operation was canceled via the cancellation token.
463
                /// </exception>
464
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
465
                /// An error occurred in the cryptographic message syntax subsystem.
466
                /// </exception>
467
                public async Task<MimeEntity> DecryptAsync (CancellationToken cancellationToken = default)
468
                {
1✔
469
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
470
                                return await DecryptAsync (ctx, cancellationToken).ConfigureAwait (false);
1✔
471
                }
1✔
472

473
                /// <summary>
474
                /// Import the certificates contained in the application/pkcs7-mime content.
475
                /// </summary>
476
                /// <remarks>
477
                /// Imports the certificates contained in the application/pkcs7-mime content.
478
                /// </remarks>
479
                /// <param name="ctx">The S/MIME context to import certificates into.</param>
480
                /// <param name="cancellationToken">The cancellation token.</param>
481
                /// <exception cref="System.ArgumentNullException">
482
                /// <paramref name="ctx"/> is <see langword="null"/>.
483
                /// </exception>
484
                /// <exception cref="System.InvalidOperationException">
485
                /// The "smime-type" parameter on the Content-Type header is not "certs-only".
486
                /// </exception>
487
                /// <exception cref="System.ObjectDisposedException">
488
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
489
                /// </exception>
490
                /// <exception cref="System.OperationCanceledException">
491
                /// The operation was canceled via the cancellation token.
492
                /// </exception>
493
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
494
                /// An error occurred in the cryptographic message syntax subsystem.
495
                /// </exception>
496
                public void Import (SecureMimeContext ctx, CancellationToken cancellationToken = default)
497
                {
1✔
498
                        if (ctx == null)
1✔
499
                                throw new ArgumentNullException (nameof (ctx));
1✔
500

501
                        CheckDisposed ();
1✔
502

503
                        if (SecureMimeType != SecureMimeType.CertsOnly && SecureMimeType != SecureMimeType.Unknown)
1✔
504
                                throw new InvalidOperationException ();
1✔
505

506
                        if (Content is null)
1✔
507
                                return;
×
508

509
                        using (var memory = new MemoryBlockStream ()) {
1✔
510
                                Content.DecodeTo (memory, cancellationToken);
1✔
511
                                memory.Position = 0;
1✔
512

513
                                ctx.Import (memory, cancellationToken);
1✔
514
                        }
1✔
515
                }
1✔
516

517
                /// <summary>
518
                /// Asynchronously import the certificates contained in the application/pkcs7-mime content.
519
                /// </summary>
520
                /// <remarks>
521
                /// Asynchronously imports the certificates contained in the application/pkcs7-mime content.
522
                /// </remarks>
523
                /// <returns>An asynchronous task context.</returns>
524
                /// <param name="ctx">The S/MIME context to import certificates into.</param>
525
                /// <param name="cancellationToken">The cancellation token.</param>
526
                /// <exception cref="System.ArgumentNullException">
527
                /// <paramref name="ctx"/> is <see langword="null"/>.
528
                /// </exception>
529
                /// <exception cref="System.InvalidOperationException">
530
                /// The "smime-type" parameter on the Content-Type header is not "certs-only".
531
                /// </exception>
532
                /// <exception cref="System.ObjectDisposedException">
533
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
534
                /// </exception>
535
                /// <exception cref="System.OperationCanceledException">
536
                /// The operation was canceled via the cancellation token.
537
                /// </exception>
538
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
539
                /// An error occurred in the cryptographic message syntax subsystem.
540
                /// </exception>
541
                public async Task ImportAsync (SecureMimeContext ctx, CancellationToken cancellationToken = default)
542
                {
1✔
543
                        if (ctx == null)
1✔
544
                                throw new ArgumentNullException (nameof (ctx));
1✔
545

546
                        CheckDisposed ();
1✔
547

548
                        if (SecureMimeType != SecureMimeType.CertsOnly && SecureMimeType != SecureMimeType.Unknown)
1✔
549
                                throw new InvalidOperationException ();
1✔
550

551
                        if (Content is null)
1✔
552
                                return;
×
553

554
                        using (var memory = new MemoryBlockStream ()) {
1✔
555
                                await Content.DecodeToAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
556
                                memory.Position = 0;
1✔
557

558
                                await ctx.ImportAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
559
                        }
1✔
560
                }
1✔
561

562
                /// <summary>
563
                /// Verify the signed-data and return the unencapsulated <see cref="MimeEntity"/>.
564
                /// </summary>
565
                /// <remarks>
566
                /// Verifies the signed-data and returns the unencapsulated <see cref="MimeEntity"/>.
567
                /// </remarks>
568
                /// <returns>The list of digital signatures.</returns>
569
                /// <param name="ctx">The S/MIME context to use for verifying the signature.</param>
570
                /// <param name="entity">The unencapsulated entity.</param>
571
                /// <param name="cancellationToken">The cancellation token.</param>
572
                /// <exception cref="System.ArgumentNullException">
573
                /// <paramref name="ctx"/> is <see langword="null"/>.
574
                /// </exception>
575
                /// <exception cref="System.InvalidOperationException">
576
                /// The "smime-type" parameter on the Content-Type header is not "signed-data".
577
                /// </exception>
578
                /// <exception cref="System.FormatException">
579
                /// The extracted content could not be parsed as a MIME entity.
580
                /// </exception>
581
                /// <exception cref="System.ObjectDisposedException">
582
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
583
                /// </exception>
584
                /// <exception cref="System.OperationCanceledException">
585
                /// The operation was canceled via the cancellation token.
586
                /// </exception>
587
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
588
                /// An error occurred in the cryptographic message syntax subsystem.
589
                /// </exception>
590
                public DigitalSignatureCollection Verify (SecureMimeContext ctx, out MimeEntity entity, CancellationToken cancellationToken = default)
591
                {
1✔
592
                        if (ctx == null)
1✔
593
                                throw new ArgumentNullException (nameof (ctx));
1✔
594

595
                        CheckDisposed ();
1✔
596

597
                        if (SecureMimeType != SecureMimeType.SignedData && SecureMimeType != SecureMimeType.Unknown)
1✔
598
                                throw new InvalidOperationException ();
1✔
599

600
                        using (var memory = new MemoryBlockStream ()) {
1✔
601
                                if (Content != null) {
1✔
602
                                        Content.DecodeTo (memory, cancellationToken);
1✔
603
                                        memory.Position = 0;
1✔
604
                                }
1✔
605

606
                                return ctx.Verify (memory, out entity, cancellationToken);
1✔
607
                        }
608
                }
1✔
609

610
                /// <summary>
611
                /// Verifies the signed-data and returns the unencapsulated <see cref="MimeEntity"/>.
612
                /// </summary>
613
                /// <remarks>
614
                /// Verifies the signed-data using the default <see cref="SecureMimeContext"/> and returns the
615
                /// unencapsulated <see cref="MimeEntity"/>.
616
                /// </remarks>
617
                /// <returns>The list of digital signatures.</returns>
618
                /// <param name="entity">The unencapsulated entity.</param>
619
                /// <param name="cancellationToken">The cancellation token.</param>
620
                /// <exception cref="System.InvalidOperationException">
621
                /// The "smime-type" parameter on the Content-Type header is not "signed-data".
622
                /// </exception>
623
                /// <exception cref="System.ObjectDisposedException">
624
                /// The <see cref="ApplicationPkcs7Mime"/> has been disposed.
625
                /// </exception>
626
                /// <exception cref="System.OperationCanceledException">
627
                /// The operation was canceled via the cancellation token.
628
                /// </exception>
629
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
630
                /// An error occurred in the cryptographic message syntax subsystem.
631
                /// </exception>
632
                public DigitalSignatureCollection Verify (out MimeEntity entity, CancellationToken cancellationToken = default)
633
                {
1✔
634
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
635
                                return Verify (ctx, out entity, cancellationToken);
1✔
636
                }
1✔
637

638
                static async Task<ApplicationPkcs7Mime> CompressAsync (SecureMimeContext ctx, MimeEntity entity, bool doAsync, CancellationToken cancellationToken)
639
                {
1✔
640
                        if (ctx == null)
1✔
641
                                throw new ArgumentNullException (nameof (ctx));
1✔
642

643
                        if (entity == null)
1✔
644
                                throw new ArgumentNullException (nameof (entity));
1✔
645

646
                        using (var memory = new MemoryBlockStream ()) {
1✔
647
                                var options = FormatOptions.Default.Clone ();
1✔
648
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
649

650
                                if (doAsync)
1✔
651
                                        await entity.WriteToAsync (options, memory, cancellationToken).ConfigureAwait (false);
1✔
652
                                else
653
                                        entity.WriteTo (options, memory, cancellationToken);
1✔
654
                                memory.Position = 0;
1✔
655

656
                                if (doAsync)
1✔
657
                                        return await ctx.CompressAsync (memory, cancellationToken).ConfigureAwait (false);
1✔
658

659
                                return ctx.Compress (memory, cancellationToken);
1✔
660
                        }
661
                }
1✔
662

663
                /// <summary>
664
                /// Compress the specified entity.
665
                /// </summary>
666
                /// <remarks>
667
                /// <para>Compresses the specified entity using the specified <see cref="SecureMimeContext"/>.</para>
668
                /// <note type="warning">Most mail clients, even among those that support S/MIME, do not support compression.</note>
669
                /// </remarks>
670
                /// <returns>The compressed entity.</returns>
671
                /// <param name="ctx">The S/MIME context to use for compressing.</param>
672
                /// <param name="entity">The entity.</param>
673
                /// <param name="cancellationToken">The cancellation token.</param>
674
                /// <exception cref="System.ArgumentNullException">
675
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
676
                /// <para>-or-</para>
677
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
678
                /// </exception>
679
                /// <exception cref="System.ObjectDisposedException">
680
                /// <paramref name="entity"/> has been disposed.
681
                /// </exception>
682
                /// <exception cref="System.OperationCanceledException">
683
                /// The operation was canceled via the cancellation token.
684
                /// </exception>
685
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
686
                /// An error occurred in the cryptographic message syntax subsystem.
687
                /// </exception>
688
                public static ApplicationPkcs7Mime Compress (SecureMimeContext ctx, MimeEntity entity, CancellationToken cancellationToken = default)
689
                {
1✔
690
                        return CompressAsync (ctx, entity, false, cancellationToken).GetAwaiter ().GetResult ();
1✔
691
                }
1✔
692

693
                /// <summary>
694
                /// Asynchronously compress the specified entity.
695
                /// </summary>
696
                /// <remarks>
697
                /// <para>Asynchronously compresses the specified entity using the specified <see cref="SecureMimeContext"/>.</para>
698
                /// <note type="warning">Most mail clients, even among those that support S/MIME, do not support compression.</note>
699
                /// </remarks>
700
                /// <returns>The compressed entity.</returns>
701
                /// <param name="ctx">The S/MIME context to use for compressing.</param>
702
                /// <param name="entity">The entity.</param>
703
                /// <param name="cancellationToken">The cancellation token.</param>
704
                /// <exception cref="System.ArgumentNullException">
705
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
706
                /// <para>-or-</para>
707
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
708
                /// </exception>
709
                /// <exception cref="System.ObjectDisposedException">
710
                /// <paramref name="entity"/> has been disposed.
711
                /// </exception>
712
                /// <exception cref="System.OperationCanceledException">
713
                /// The operation was canceled via the cancellation token.
714
                /// </exception>
715
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
716
                /// An error occurred in the cryptographic message syntax subsystem.
717
                /// </exception>
718
                public static Task<ApplicationPkcs7Mime> CompressAsync (SecureMimeContext ctx, MimeEntity entity, CancellationToken cancellationToken = default)
719
                {
1✔
720
                        return CompressAsync (ctx, entity, true, cancellationToken);
1✔
721
                }
1✔
722

723
                /// <summary>
724
                /// Compress the specified entity.
725
                /// </summary>
726
                /// <remarks>
727
                /// <para>Compresses the specified entity using the default <see cref="SecureMimeContext"/>.</para>
728
                /// <note type="warning">Most mail clients, even among those that support S/MIME, do not support compression.</note>
729
                /// </remarks>
730
                /// <returns>The compressed entity.</returns>
731
                /// <param name="entity">The entity.</param>
732
                /// <param name="cancellationToken">The cancellation token.</param>
733
                /// <exception cref="System.ArgumentNullException">
734
                /// <paramref name="entity"/> is <see langword="null"/>.
735
                /// </exception>
736
                /// <exception cref="System.ObjectDisposedException">
737
                /// <paramref name="entity"/> has been disposed.
738
                /// </exception>
739
                /// <exception cref="System.OperationCanceledException">
740
                /// The operation was canceled via the cancellation token.
741
                /// </exception>
742
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
743
                /// An error occurred in the cryptographic message syntax subsystem.
744
                /// </exception>
745
                public static ApplicationPkcs7Mime Compress (MimeEntity entity, CancellationToken cancellationToken = default)
746
                {
1✔
747
                        if (entity == null)
1✔
748
                                throw new ArgumentNullException (nameof (entity));
1✔
749

750
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
751
                                return Compress (ctx, entity, cancellationToken);
1✔
752
                }
1✔
753

754
                /// <summary>
755
                /// Asynchronously compress the specified entity.
756
                /// </summary>
757
                /// <remarks>
758
                /// <para>Asynchronously compresses the specified entity using the default <see cref="SecureMimeContext"/>.</para>
759
                /// <note type="warning">Most mail clients, even among those that support S/MIME, do not support compression.</note>
760
                /// </remarks>
761
                /// <returns>The compressed entity.</returns>
762
                /// <param name="entity">The entity.</param>
763
                /// <param name="cancellationToken">The cancellation token.</param>
764
                /// <exception cref="System.ArgumentNullException">
765
                /// <paramref name="entity"/> is <see langword="null"/>.
766
                /// </exception>
767
                /// <exception cref="System.ObjectDisposedException">
768
                /// <paramref name="entity"/> has been disposed.
769
                /// </exception>
770
                /// <exception cref="System.OperationCanceledException">
771
                /// The operation was canceled via the cancellation token.
772
                /// </exception>
773
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
774
                /// An error occurred in the cryptographic message syntax subsystem.
775
                /// </exception>
776
                public static async Task<ApplicationPkcs7Mime> CompressAsync (MimeEntity entity, CancellationToken cancellationToken = default)
777
                {
1✔
778
                        if (entity == null)
1✔
779
                                throw new ArgumentNullException (nameof (entity));
1✔
780

781
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
782
                                return await CompressAsync (ctx, entity, cancellationToken).ConfigureAwait (false);
1✔
783
                }
1✔
784

785
                /// <summary>
786
                /// Encrypt the specified entity.
787
                /// </summary>
788
                /// <remarks>
789
                /// Encrypts the entity to the specified recipients using the supplied <see cref="SecureMimeContext"/>.
790
                /// </remarks>
791
                /// <returns>The encrypted entity.</returns>
792
                /// <param name="ctx">The S/MIME context to use for encrypting.</param>
793
                /// <param name="recipients">The recipients.</param>
794
                /// <param name="entity">The entity.</param>
795
                /// <param name="cancellationToken">The cancellation token.</param>
796
                /// <exception cref="System.ArgumentNullException">
797
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
798
                /// <para>-or-</para>
799
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
800
                /// <para>-or-</para>
801
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
802
                /// </exception>
803
                /// <exception cref="System.ObjectDisposedException">
804
                /// <paramref name="entity"/> has been disposed.
805
                /// </exception>
806
                /// <exception cref="System.OperationCanceledException">
807
                /// The operation was canceled via the cancellation token.
808
                /// </exception>
809
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
810
                /// An error occurred in the cryptographic message syntax subsystem.
811
                /// </exception>
812
                public static ApplicationPkcs7Mime Encrypt (SecureMimeContext ctx, CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
813
                {
1✔
814
                        if (ctx == null)
1✔
815
                                throw new ArgumentNullException (nameof (ctx));
1✔
816

817
                        if (recipients == null)
1✔
818
                                throw new ArgumentNullException (nameof (recipients));
1✔
819

820
                        if (entity == null)
1✔
821
                                throw new ArgumentNullException (nameof (entity));
1✔
822

823
                        using (var memory = new MemoryBlockStream ()) {
1✔
824
                                var options = FormatOptions.Default.Clone ();
1✔
825
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
826

827
                                entity.WriteTo (options, memory, cancellationToken);
1✔
828
                                memory.Position = 0;
1✔
829

830
                                return ctx.Encrypt (recipients, memory, cancellationToken);
1✔
831
                        }
832
                }
1✔
833

834
                /// <summary>
835
                /// Asynchronously encrypt the specified entity.
836
                /// </summary>
837
                /// <remarks>
838
                /// Asynchronously encrypts the entity to the specified recipients using the supplied <see cref="SecureMimeContext"/>.
839
                /// </remarks>
840
                /// <returns>The encrypted entity.</returns>
841
                /// <param name="ctx">The S/MIME context to use for encrypting.</param>
842
                /// <param name="recipients">The recipients.</param>
843
                /// <param name="entity">The entity.</param>
844
                /// <param name="cancellationToken">The cancellation token.</param>
845
                /// <exception cref="System.ArgumentNullException">
846
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
847
                /// <para>-or-</para>
848
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
849
                /// <para>-or-</para>
850
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
851
                /// </exception>
852
                /// <exception cref="System.ObjectDisposedException">
853
                /// <paramref name="entity"/> has been disposed.
854
                /// </exception>
855
                /// <exception cref="System.OperationCanceledException">
856
                /// The operation was canceled via the cancellation token.
857
                /// </exception>
858
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
859
                /// An error occurred in the cryptographic message syntax subsystem.
860
                /// </exception>
861
                public static async Task<ApplicationPkcs7Mime> EncryptAsync (SecureMimeContext ctx, CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
862
                {
1✔
863
                        if (ctx == null)
1✔
864
                                throw new ArgumentNullException (nameof (ctx));
1✔
865

866
                        if (recipients == null)
1✔
867
                                throw new ArgumentNullException (nameof (recipients));
1✔
868

869
                        if (entity == null)
1✔
870
                                throw new ArgumentNullException (nameof (entity));
1✔
871

872
                        using (var memory = new MemoryBlockStream ()) {
1✔
873
                                var options = FormatOptions.Default.Clone ();
1✔
874
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
875

876
                                await entity.WriteToAsync (options, memory, cancellationToken).ConfigureAwait (false);
1✔
877
                                memory.Position = 0;
1✔
878

879
                                return await ctx.EncryptAsync (recipients, memory, cancellationToken).ConfigureAwait (false);
1✔
880
                        }
881
                }
1✔
882

883
                /// <summary>
884
                /// Encrypt the specified entity.
885
                /// </summary>
886
                /// <remarks>
887
                /// Encrypts the entity to the specified recipients using the default <see cref="SecureMimeContext"/>.
888
                /// </remarks>
889
                /// <returns>The encrypted entity.</returns>
890
                /// <param name="recipients">The recipients.</param>
891
                /// <param name="entity">The entity.</param>
892
                /// <param name="cancellationToken">The cancellation token.</param>
893
                /// <exception cref="System.ArgumentNullException">
894
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
895
                /// <para>-or-</para>
896
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
897
                /// </exception>
898
                /// <exception cref="System.ObjectDisposedException">
899
                /// <paramref name="entity"/> has been disposed.
900
                /// </exception>
901
                /// <exception cref="System.OperationCanceledException">
902
                /// The operation was canceled via the cancellation token.
903
                /// </exception>
904
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
905
                /// An error occurred in the cryptographic message syntax subsystem.
906
                /// </exception>
907
                public static ApplicationPkcs7Mime Encrypt (CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
908
                {
1✔
909
                        if (recipients == null)
1✔
910
                                throw new ArgumentNullException (nameof (recipients));
1✔
911

912
                        if (entity == null)
1✔
913
                                throw new ArgumentNullException (nameof (entity));
1✔
914

915
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
916
                                return Encrypt (ctx, recipients, entity, cancellationToken);
1✔
917
                }
1✔
918

919
                /// <summary>
920
                /// Asynchronously encrypt the specified entity.
921
                /// </summary>
922
                /// <remarks>
923
                /// Asynchronously encrypts the entity to the specified recipients using the default <see cref="SecureMimeContext"/>.
924
                /// </remarks>
925
                /// <returns>The encrypted entity.</returns>
926
                /// <param name="recipients">The recipients.</param>
927
                /// <param name="entity">The entity.</param>
928
                /// <param name="cancellationToken">The cancellation token.</param>
929
                /// <exception cref="System.ArgumentNullException">
930
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
931
                /// <para>-or-</para>
932
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
933
                /// </exception>
934
                /// <exception cref="System.ObjectDisposedException">
935
                /// <paramref name="entity"/> has been disposed.
936
                /// </exception>
937
                /// <exception cref="System.OperationCanceledException">
938
                /// The operation was canceled via the cancellation token.
939
                /// </exception>
940
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
941
                /// An error occurred in the cryptographic message syntax subsystem.
942
                /// </exception>
943
                public static async Task<ApplicationPkcs7Mime> EncryptAsync (CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
944
                {
1✔
945
                        if (recipients == null)
1✔
946
                                throw new ArgumentNullException (nameof (recipients));
1✔
947

948
                        if (entity == null)
1✔
949
                                throw new ArgumentNullException (nameof (entity));
1✔
950

951
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
952
                                return await EncryptAsync (ctx, recipients, entity, cancellationToken).ConfigureAwait (false);
1✔
953
                }
1✔
954

955
                /// <summary>
956
                /// Encrypt the specified entity.
957
                /// </summary>
958
                /// <remarks>
959
                /// Encrypts the entity to the specified recipients using the supplied <see cref="SecureMimeContext"/>.
960
                /// </remarks>
961
                /// <returns>The encrypted entity.</returns>
962
                /// <param name="ctx">The S/MIME context to use for encrypting.</param>
963
                /// <param name="recipients">The recipients.</param>
964
                /// <param name="entity">The entity.</param>
965
                /// <param name="cancellationToken">The cancellation token.</param>
966
                /// <exception cref="System.ArgumentNullException">
967
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
968
                /// <para>-or-</para>
969
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
970
                /// <para>-or-</para>
971
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
972
                /// </exception>
973
                /// <exception cref="System.ArgumentException">
974
                /// Valid certificates could not be found for one or more of the <paramref name="recipients"/>.
975
                /// </exception>
976
                /// <exception cref="System.ObjectDisposedException">
977
                /// <paramref name="entity"/> has been disposed.
978
                /// </exception>
979
                /// <exception cref="System.OperationCanceledException">
980
                /// The operation was canceled via the cancellation token.
981
                /// </exception>
982
                /// <exception cref="CertificateNotFoundException">
983
                /// A certificate could not be found for one or more of the <paramref name="recipients"/>.
984
                /// </exception>
985
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
986
                /// An error occurred in the cryptographic message syntax subsystem.
987
                /// </exception>
988
                public static ApplicationPkcs7Mime Encrypt (SecureMimeContext ctx, IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
989
                {
1✔
990
                        if (ctx == null)
1✔
991
                                throw new ArgumentNullException (nameof (ctx));
1✔
992

993
                        if (recipients == null)
1✔
994
                                throw new ArgumentNullException (nameof (recipients));
1✔
995

996
                        if (entity == null)
1✔
997
                                throw new ArgumentNullException (nameof (entity));
1✔
998

999
                        using (var memory = new MemoryBlockStream ()) {
1✔
1000
                                var options = FormatOptions.Default.Clone ();
1✔
1001
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
1002

1003
                                entity.WriteTo (options, memory, cancellationToken);
1✔
1004
                                memory.Position = 0;
1✔
1005

1006
                                return (ApplicationPkcs7Mime) ctx.Encrypt (recipients, memory, cancellationToken);
1✔
1007
                        }
1008
                }
1✔
1009

1010
                /// <summary>
1011
                /// Asynchronously encrypt the specified entity.
1012
                /// </summary>
1013
                /// <remarks>
1014
                /// Asynchronously encrypts the entity to the specified recipients using the supplied <see cref="SecureMimeContext"/>.
1015
                /// </remarks>
1016
                /// <returns>The encrypted entity.</returns>
1017
                /// <param name="ctx">The S/MIME context to use for encrypting.</param>
1018
                /// <param name="recipients">The recipients.</param>
1019
                /// <param name="entity">The entity.</param>
1020
                /// <param name="cancellationToken">The cancellation token.</param>
1021
                /// <exception cref="System.ArgumentNullException">
1022
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1023
                /// <para>-or-</para>
1024
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1025
                /// <para>-or-</para>
1026
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1027
                /// </exception>
1028
                /// <exception cref="System.ArgumentException">
1029
                /// Valid certificates could not be found for one or more of the <paramref name="recipients"/>.
1030
                /// </exception>
1031
                /// <exception cref="System.ObjectDisposedException">
1032
                /// <paramref name="entity"/> has been disposed.
1033
                /// </exception>
1034
                /// <exception cref="System.OperationCanceledException">
1035
                /// The operation was canceled via the cancellation token.
1036
                /// </exception>
1037
                /// <exception cref="CertificateNotFoundException">
1038
                /// A certificate could not be found for one or more of the <paramref name="recipients"/>.
1039
                /// </exception>
1040
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1041
                /// An error occurred in the cryptographic message syntax subsystem.
1042
                /// </exception>
1043
                public static async Task<ApplicationPkcs7Mime> EncryptAsync (SecureMimeContext ctx, IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1044
                {
1✔
1045
                        if (ctx == null)
1✔
1046
                                throw new ArgumentNullException (nameof (ctx));
1✔
1047

1048
                        if (recipients == null)
1✔
1049
                                throw new ArgumentNullException (nameof (recipients));
1✔
1050

1051
                        if (entity == null)
1✔
1052
                                throw new ArgumentNullException (nameof (entity));
1✔
1053

1054
                        using (var memory = new MemoryBlockStream ()) {
1✔
1055
                                var options = FormatOptions.Default.Clone ();
1✔
1056
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
1057

1058
                                await entity.WriteToAsync (options, memory, cancellationToken).ConfigureAwait (false);
1✔
1059
                                memory.Position = 0;
1✔
1060

1061
                                return (ApplicationPkcs7Mime) await ctx.EncryptAsync (recipients, memory, cancellationToken).ConfigureAwait (false);
1✔
1062
                        }
1063
                }
1✔
1064

1065
                /// <summary>
1066
                /// Encrypt the specified entity.
1067
                /// </summary>
1068
                /// <remarks>
1069
                /// Encrypts the entity to the specified recipients using the default <see cref="SecureMimeContext"/>.
1070
                /// </remarks>
1071
                /// <returns>The encrypted entity.</returns>
1072
                /// <param name="recipients">The recipients.</param>
1073
                /// <param name="entity">The entity.</param>
1074
                /// <param name="cancellationToken">The cancellation token.</param>
1075
                /// <exception cref="System.ArgumentNullException">
1076
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1077
                /// <para>-or-</para>
1078
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1079
                /// </exception>
1080
                /// <exception cref="System.ArgumentException">
1081
                /// Valid certificates could not be found for one or more of the <paramref name="recipients"/>.
1082
                /// </exception>
1083
                /// <exception cref="System.ObjectDisposedException">
1084
                /// <paramref name="entity"/> has been disposed.
1085
                /// </exception>
1086
                /// <exception cref="System.OperationCanceledException">
1087
                /// The operation was canceled via the cancellation token.
1088
                /// </exception>
1089
                /// <exception cref="CertificateNotFoundException">
1090
                /// A certificate could not be found for one or more of the <paramref name="recipients"/>.
1091
                /// </exception>
1092
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1093
                /// An error occurred in the cryptographic message syntax subsystem.
1094
                /// </exception>
1095
                public static ApplicationPkcs7Mime Encrypt (IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1096
                {
1✔
1097
                        if (recipients == null)
1✔
1098
                                throw new ArgumentNullException (nameof (recipients));
1✔
1099

1100
                        if (entity == null)
1✔
1101
                                throw new ArgumentNullException (nameof (entity));
1✔
1102

1103
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1104
                                return Encrypt (ctx, recipients, entity, cancellationToken);
1✔
1105
                }
1✔
1106

1107
                /// <summary>
1108
                /// Asynchronously encrypt the specified entity.
1109
                /// </summary>
1110
                /// <remarks>
1111
                /// Asynchronously encrypts the entity to the specified recipients using the default <see cref="SecureMimeContext"/>.
1112
                /// </remarks>
1113
                /// <returns>The encrypted entity.</returns>
1114
                /// <param name="recipients">The recipients.</param>
1115
                /// <param name="entity">The entity.</param>
1116
                /// <param name="cancellationToken">The cancellation token.</param>
1117
                /// <exception cref="System.ArgumentNullException">
1118
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1119
                /// <para>-or-</para>
1120
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1121
                /// </exception>
1122
                /// <exception cref="System.ArgumentException">
1123
                /// Valid certificates could not be found for one or more of the <paramref name="recipients"/>.
1124
                /// </exception>
1125
                /// <exception cref="System.ObjectDisposedException">
1126
                /// <paramref name="entity"/> has been disposed.
1127
                /// </exception>
1128
                /// <exception cref="System.OperationCanceledException">
1129
                /// The operation was canceled via the cancellation token.
1130
                /// </exception>
1131
                /// <exception cref="CertificateNotFoundException">
1132
                /// A certificate could not be found for one or more of the <paramref name="recipients"/>.
1133
                /// </exception>
1134
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1135
                /// An error occurred in the cryptographic message syntax subsystem.
1136
                /// </exception>
1137
                public static async Task<ApplicationPkcs7Mime> EncryptAsync (IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1138
                {
1✔
1139
                        if (recipients == null)
1✔
1140
                                throw new ArgumentNullException (nameof (recipients));
1✔
1141

1142
                        if (entity == null)
1✔
1143
                                throw new ArgumentNullException (nameof (entity));
1✔
1144

1145
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1146
                                return await EncryptAsync (ctx, recipients, entity, cancellationToken).ConfigureAwait (false);
1✔
1147
                }
1✔
1148

1149
                /// <summary>
1150
                /// Sign the specified entity.
1151
                /// </summary>
1152
                /// <remarks>
1153
                /// <para>Signs the entity using the supplied signer and <see cref="SecureMimeContext"/>.</para>
1154
                /// <para>For better interoperability with other mail clients, you should use
1155
                /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1156
                /// instead as the multipart/signed format is supported among a much larger
1157
                /// subset of mail client software.</para>
1158
                /// </remarks>
1159
                /// <returns>The signed entity.</returns>
1160
                /// <param name="ctx">The S/MIME context to use for signing.</param>
1161
                /// <param name="signer">The signer.</param>
1162
                /// <param name="entity">The entity.</param>
1163
                /// <param name="cancellationToken">The cancellation token.</param>
1164
                /// <exception cref="System.ArgumentNullException">
1165
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1166
                /// <para>-or-</para>
1167
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1168
                /// <para>-or-</para>
1169
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1170
                /// </exception>
1171
                /// <exception cref="System.ObjectDisposedException">
1172
                /// <paramref name="entity"/> has been disposed.
1173
                /// </exception>
1174
                /// <exception cref="System.OperationCanceledException">
1175
                /// The operation was canceled via the cancellation token.
1176
                /// </exception>
1177
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1178
                /// An error occurred in the cryptographic message syntax subsystem.
1179
                /// </exception>
1180
                public static ApplicationPkcs7Mime Sign (SecureMimeContext ctx, CmsSigner signer, MimeEntity entity, CancellationToken cancellationToken = default)
1181
                {
1✔
1182
                        if (ctx == null)
1✔
1183
                                throw new ArgumentNullException (nameof (ctx));
1✔
1184

1185
                        if (signer == null)
1✔
1186
                                throw new ArgumentNullException (nameof (signer));
1✔
1187

1188
                        if (entity == null)
1✔
1189
                                throw new ArgumentNullException (nameof (entity));
1✔
1190

1191
                        using (var memory = new MemoryBlockStream ()) {
1✔
1192
                                var options = FormatOptions.Default.Clone ();
1✔
1193
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
1194

1195
                                entity.WriteTo (options, memory, cancellationToken);
1✔
1196
                                memory.Position = 0;
1✔
1197

1198
                                return ctx.EncapsulatedSign (signer, memory, cancellationToken);
1✔
1199
                        }
1200
                }
1✔
1201

1202
                /// <summary>
1203
                /// Asynchronously sign the specified entity.
1204
                /// </summary>
1205
                /// <remarks>
1206
                /// <para>Asynchronously signs the entity using the supplied signer and <see cref="SecureMimeContext"/>.</para>
1207
                /// <para>For better interoperability with other mail clients, you should use
1208
                /// <see cref="MultipartSigned.CreateAsync(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1209
                /// instead as the multipart/signed format is supported among a much larger
1210
                /// subset of mail client software.</para>
1211
                /// </remarks>
1212
                /// <returns>The signed entity.</returns>
1213
                /// <param name="ctx">The S/MIME context to use for signing.</param>
1214
                /// <param name="signer">The signer.</param>
1215
                /// <param name="entity">The entity.</param>
1216
                /// <param name="cancellationToken">The cancellation token.</param>
1217
                /// <exception cref="System.ArgumentNullException">
1218
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1219
                /// <para>-or-</para>
1220
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1221
                /// <para>-or-</para>
1222
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1223
                /// </exception>
1224
                /// <exception cref="System.ObjectDisposedException">
1225
                /// <paramref name="entity"/> has been disposed.
1226
                /// </exception>
1227
                /// <exception cref="System.OperationCanceledException">
1228
                /// The operation was canceled via the cancellation token.
1229
                /// </exception>
1230
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1231
                /// An error occurred in the cryptographic message syntax subsystem.
1232
                /// </exception>
1233
                public static async Task<ApplicationPkcs7Mime> SignAsync (SecureMimeContext ctx, CmsSigner signer, MimeEntity entity, CancellationToken cancellationToken = default)
1234
                {
1✔
1235
                        if (ctx == null)
1✔
1236
                                throw new ArgumentNullException (nameof (ctx));
1✔
1237

1238
                        if (signer == null)
1✔
1239
                                throw new ArgumentNullException (nameof (signer));
1✔
1240

1241
                        if (entity == null)
1✔
1242
                                throw new ArgumentNullException (nameof (entity));
1✔
1243

1244
                        using (var memory = new MemoryBlockStream ()) {
1✔
1245
                                var options = FormatOptions.Default.Clone ();
1✔
1246
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
1247

1248
                                await entity.WriteToAsync (options, memory, cancellationToken).ConfigureAwait (false);
1✔
1249
                                memory.Position = 0;
1✔
1250

1251
                                return await ctx.EncapsulatedSignAsync (signer, memory, cancellationToken).ConfigureAwait (false);
1✔
1252
                        }
1253
                }
1✔
1254

1255
                /// <summary>
1256
                /// Sign the specified entity.
1257
                /// </summary>
1258
                /// <remarks>
1259
                /// <para>Signs the entity using the supplied signer and the default <see cref="SecureMimeContext"/>.</para>
1260
                /// <para>For better interoperability with other mail clients, you should use
1261
                /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1262
                /// instead as the multipart/signed format is supported among a much larger
1263
                /// subset of mail client software.</para>
1264
                /// </remarks>
1265
                /// <returns>The signed entity.</returns>
1266
                /// <param name="signer">The signer.</param>
1267
                /// <param name="entity">The entity.</param>
1268
                /// <param name="cancellationToken">The cancellation token.</param>
1269
                /// <exception cref="System.ArgumentNullException">
1270
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1271
                /// <para>-or-</para>
1272
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1273
                /// </exception>
1274
                /// <exception cref="System.ObjectDisposedException">
1275
                /// <paramref name="entity"/> has been disposed.
1276
                /// </exception>
1277
                /// <exception cref="System.OperationCanceledException">
1278
                /// The operation was canceled via the cancellation token.
1279
                /// </exception>
1280
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1281
                /// An error occurred in the cryptographic message syntax subsystem.
1282
                /// </exception>
1283
                public static ApplicationPkcs7Mime Sign (CmsSigner signer, MimeEntity entity, CancellationToken cancellationToken = default)
1284
                {
1✔
1285
                        if (signer == null)
1✔
1286
                                throw new ArgumentNullException (nameof (signer));
1✔
1287

1288
                        if (entity == null)
1✔
1289
                                throw new ArgumentNullException (nameof (entity));
1✔
1290

1291
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1292
                                return Sign (ctx, signer, entity, cancellationToken);
1✔
1293
                }
1✔
1294

1295
                /// <summary>
1296
                /// Asynchronously sign the specified entity.
1297
                /// </summary>
1298
                /// <remarks>
1299
                /// <para>Asynchronously signs the entity using the supplied signer and the default <see cref="SecureMimeContext"/>.</para>
1300
                /// <para>For better interoperability with other mail clients, you should use
1301
                /// <see cref="MultipartSigned.CreateAsync(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1302
                /// instead as the multipart/signed format is supported among a much larger
1303
                /// subset of mail client software.</para>
1304
                /// </remarks>
1305
                /// <returns>The signed entity.</returns>
1306
                /// <param name="signer">The signer.</param>
1307
                /// <param name="entity">The entity.</param>
1308
                /// <param name="cancellationToken">The cancellation token.</param>
1309
                /// <exception cref="System.ArgumentNullException">
1310
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1311
                /// <para>-or-</para>
1312
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1313
                /// </exception>
1314
                /// <exception cref="System.ObjectDisposedException">
1315
                /// <paramref name="entity"/> has been disposed.
1316
                /// </exception>
1317
                /// <exception cref="System.OperationCanceledException">
1318
                /// The operation was canceled via the cancellation token.
1319
                /// </exception>
1320
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1321
                /// An error occurred in the cryptographic message syntax subsystem.
1322
                /// </exception>
1323
                public static async Task<ApplicationPkcs7Mime> SignAsync (CmsSigner signer, MimeEntity entity, CancellationToken cancellationToken = default)
1324
                {
1✔
1325
                        if (signer == null)
1✔
1326
                                throw new ArgumentNullException (nameof (signer));
1✔
1327

1328
                        if (entity == null)
1✔
1329
                                throw new ArgumentNullException (nameof (entity));
1✔
1330

1331
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1332
                                return await SignAsync (ctx, signer, entity, cancellationToken).ConfigureAwait (false);
1✔
1333
                }
1✔
1334

1335
                /// <summary>
1336
                /// Sign the specified entity.
1337
                /// </summary>
1338
                /// <remarks>
1339
                /// <para>Signs the entity using the supplied signer, digest algorithm and <see cref="SecureMimeContext"/>.</para>
1340
                /// <para>For better interoperability with other mail clients, you should use
1341
                /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1342
                /// instead as the multipart/signed format is supported among a much larger
1343
                /// subset of mail client software.</para>
1344
                /// </remarks>
1345
                /// <returns>The signed entity.</returns>
1346
                /// <param name="ctx">The S/MIME context to use for signing.</param>
1347
                /// <param name="signer">The signer.</param>
1348
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1349
                /// <param name="entity">The entity.</param>
1350
                /// <param name="cancellationToken">The cancellation token.</param>
1351
                /// <exception cref="System.ArgumentNullException">
1352
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1353
                /// <para>-or-</para>
1354
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1355
                /// <para>-or-</para>
1356
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1357
                /// </exception>
1358
                /// <exception cref="System.ObjectDisposedException">
1359
                /// <paramref name="entity"/> has been disposed.
1360
                /// </exception>
1361
                /// <exception cref="System.OperationCanceledException">
1362
                /// The operation was canceled via the cancellation token.
1363
                /// </exception>
1364
                /// <exception cref="CertificateNotFoundException">
1365
                /// A signing certificate could not be found for <paramref name="signer"/>.
1366
                /// </exception>
1367
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1368
                /// An error occurred in the cryptographic message syntax subsystem.
1369
                /// </exception>
1370
                public static ApplicationPkcs7Mime Sign (SecureMimeContext ctx, MailboxAddress signer, DigestAlgorithm digestAlgo, MimeEntity entity, CancellationToken cancellationToken = default)
1371
                {
1✔
1372
                        if (ctx == null)
1✔
1373
                                throw new ArgumentNullException (nameof (ctx));
1✔
1374

1375
                        if (signer == null)
1✔
1376
                                throw new ArgumentNullException (nameof (signer));
1✔
1377

1378
                        if (entity == null)
1✔
1379
                                throw new ArgumentNullException (nameof (entity));
1✔
1380

1381
                        using (var memory = new MemoryBlockStream ()) {
1✔
1382
                                var options = FormatOptions.Default.Clone ();
1✔
1383
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
1384

1385
                                entity.WriteTo (options, memory, cancellationToken);
1✔
1386
                                memory.Position = 0;
1✔
1387

1388
                                return ctx.EncapsulatedSign (signer, digestAlgo, memory, cancellationToken);
1✔
1389
                        }
1390
                }
1✔
1391

1392
                /// <summary>
1393
                /// Asynchronously sign the specified entity.
1394
                /// </summary>
1395
                /// <remarks>
1396
                /// <para>Asynchronously signs the entity using the supplied signer, digest algorithm and <see cref="SecureMimeContext"/>.</para>
1397
                /// <para>For better interoperability with other mail clients, you should use
1398
                /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1399
                /// instead as the multipart/signed format is supported among a much larger
1400
                /// subset of mail client software.</para>
1401
                /// </remarks>
1402
                /// <returns>The signed entity.</returns>
1403
                /// <param name="ctx">The S/MIME context to use for signing.</param>
1404
                /// <param name="signer">The signer.</param>
1405
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1406
                /// <param name="entity">The entity.</param>
1407
                /// <param name="cancellationToken">The cancellation token.</param>
1408
                /// <exception cref="System.ArgumentNullException">
1409
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1410
                /// <para>-or-</para>
1411
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1412
                /// <para>-or-</para>
1413
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1414
                /// </exception>
1415
                /// <exception cref="System.ObjectDisposedException">
1416
                /// <paramref name="entity"/> has been disposed.
1417
                /// </exception>
1418
                /// <exception cref="System.OperationCanceledException">
1419
                /// The operation was canceled via the cancellation token.
1420
                /// </exception>
1421
                /// <exception cref="CertificateNotFoundException">
1422
                /// A signing certificate could not be found for <paramref name="signer"/>.
1423
                /// </exception>
1424
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1425
                /// An error occurred in the cryptographic message syntax subsystem.
1426
                /// </exception>
1427
                public static async Task<ApplicationPkcs7Mime> SignAsync (SecureMimeContext ctx, MailboxAddress signer, DigestAlgorithm digestAlgo, MimeEntity entity, CancellationToken cancellationToken = default)
1428
                {
1✔
1429
                        if (ctx == null)
1✔
1430
                                throw new ArgumentNullException (nameof (ctx));
1✔
1431

1432
                        if (signer == null)
1✔
1433
                                throw new ArgumentNullException (nameof (signer));
1✔
1434

1435
                        if (entity == null)
1✔
1436
                                throw new ArgumentNullException (nameof (entity));
1✔
1437

1438
                        using (var memory = new MemoryBlockStream ()) {
1✔
1439
                                var options = FormatOptions.Default.Clone ();
1✔
1440
                                options.NewLineFormat = NewLineFormat.Dos;
1✔
1441

1442
                                await entity.WriteToAsync (options, memory, cancellationToken).ConfigureAwait (false);
1✔
1443
                                memory.Position = 0;
1✔
1444

1445
                                return await ctx.EncapsulatedSignAsync (signer, digestAlgo, memory, cancellationToken).ConfigureAwait (false);
1✔
1446
                        }
1447
                }
1✔
1448

1449
                /// <summary>
1450
                /// Sign the specified entity.
1451
                /// </summary>
1452
                /// <remarks>
1453
                /// <para>Signs the entity using the supplied signer, digest algorithm and the default
1454
                /// <see cref="SecureMimeContext"/>.</para>
1455
                /// <para>For better interoperability with other mail clients, you should use
1456
                /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1457
                /// instead as the multipart/signed format is supported among a much larger
1458
                /// subset of mail client software.</para>
1459
                /// </remarks>
1460
                /// <returns>The signed entity.</returns>
1461
                /// <param name="signer">The signer.</param>
1462
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1463
                /// <param name="entity">The entity.</param>
1464
                /// <param name="cancellationToken">The cancellation token.</param>
1465
                /// <exception cref="System.ArgumentNullException">
1466
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1467
                /// <para>-or-</para>
1468
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1469
                /// </exception>
1470
                /// <exception cref="System.ObjectDisposedException">
1471
                /// <paramref name="entity"/> has been disposed.
1472
                /// </exception>
1473
                /// <exception cref="System.OperationCanceledException">
1474
                /// The operation was canceled via the cancellation token.
1475
                /// </exception>
1476
                /// <exception cref="CertificateNotFoundException">
1477
                /// A signing certificate could not be found for <paramref name="signer"/>.
1478
                /// </exception>
1479
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1480
                /// An error occurred in the cryptographic message syntax subsystem.
1481
                /// </exception>
1482
                public static ApplicationPkcs7Mime Sign (MailboxAddress signer, DigestAlgorithm digestAlgo, MimeEntity entity, CancellationToken cancellationToken = default)
1483
                {
1✔
1484
                        if (signer == null)
1✔
1485
                                throw new ArgumentNullException (nameof (signer));
1✔
1486

1487
                        if (entity == null)
1✔
1488
                                throw new ArgumentNullException (nameof (entity));
1✔
1489

1490
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1491
                                return Sign (ctx, signer, digestAlgo, entity, cancellationToken);
1✔
1492
                }
1✔
1493

1494
                /// <summary>
1495
                /// Asynchronously sign the specified entity.
1496
                /// </summary>
1497
                /// <remarks>
1498
                /// <para>Asynchronously signs the entity using the supplied signer, digest algorithm and the default
1499
                /// <see cref="SecureMimeContext"/>.</para>
1500
                /// <para>For better interoperability with other mail clients, you should use
1501
                /// <see cref="MultipartSigned.CreateAsync(SecureMimeContext, CmsSigner, MimeEntity,CancellationToken)"/>
1502
                /// instead as the multipart/signed format is supported among a much larger
1503
                /// subset of mail client software.</para>
1504
                /// </remarks>
1505
                /// <returns>The signed entity.</returns>
1506
                /// <param name="signer">The signer.</param>
1507
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1508
                /// <param name="entity">The entity.</param>
1509
                /// <param name="cancellationToken">The cancellation token.</param>
1510
                /// <exception cref="System.ArgumentNullException">
1511
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1512
                /// <para>-or-</para>
1513
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1514
                /// </exception>
1515
                /// <exception cref="System.ObjectDisposedException">
1516
                /// <paramref name="entity"/> has been disposed.
1517
                /// </exception>
1518
                /// <exception cref="System.OperationCanceledException">
1519
                /// The operation was canceled via the cancellation token.
1520
                /// </exception>
1521
                /// <exception cref="CertificateNotFoundException">
1522
                /// A signing certificate could not be found for <paramref name="signer"/>.
1523
                /// </exception>
1524
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1525
                /// An error occurred in the cryptographic message syntax subsystem.
1526
                /// </exception>
1527
                public static async Task<ApplicationPkcs7Mime> SignAsync (MailboxAddress signer, DigestAlgorithm digestAlgo, MimeEntity entity, CancellationToken cancellationToken = default)
1528
                {
1✔
1529
                        if (signer == null)
1✔
1530
                                throw new ArgumentNullException (nameof (signer));
1✔
1531

1532
                        if (entity == null)
1✔
1533
                                throw new ArgumentNullException (nameof (entity));
1✔
1534

1535
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1536
                                return await SignAsync (ctx, signer, digestAlgo, entity, cancellationToken).ConfigureAwait (false);
1✔
1537
                }
1✔
1538

1539
                /// <summary>
1540
                /// Sign and encrypt the specified entity.
1541
                /// </summary>
1542
                /// <remarks>
1543
                /// Signs entity using the supplied signer and then encrypts the result to the specified recipients.
1544
                /// </remarks>
1545
                /// <returns>The signed and encrypted entity.</returns>
1546
                /// <param name="ctx">The S/MIME context to use for signing and encrypting.</param>
1547
                /// <param name="signer">The signer.</param>
1548
                /// <param name="recipients">The recipients.</param>
1549
                /// <param name="entity">The entity.</param>
1550
                /// <param name="cancellationToken">The cancellation token.</param>
1551
                /// <exception cref="System.ArgumentNullException">
1552
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1553
                /// <para>-or-</para>
1554
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1555
                /// <para>-or-</para>
1556
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1557
                /// <para>-or-</para>
1558
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1559
                /// </exception>
1560
                /// <exception cref="System.ObjectDisposedException">
1561
                /// <paramref name="entity"/> has been disposed.
1562
                /// </exception>
1563
                /// <exception cref="System.OperationCanceledException">
1564
                /// The operation was canceled via the cancellation token.
1565
                /// </exception>
1566
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1567
                /// An error occurred in the cryptographic message syntax subsystem.
1568
                /// </exception>
1569
                public static ApplicationPkcs7Mime SignAndEncrypt (SecureMimeContext ctx, CmsSigner signer, CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1570
                {
1✔
1571
                        if (ctx == null)
1✔
1572
                                throw new ArgumentNullException (nameof (ctx));
1✔
1573

1574
                        if (signer == null)
1✔
1575
                                throw new ArgumentNullException (nameof (signer));
1✔
1576

1577
                        if (recipients == null)
1✔
1578
                                throw new ArgumentNullException (nameof (recipients));
1✔
1579

1580
                        if (entity == null)
1✔
1581
                                throw new ArgumentNullException (nameof (entity));
1✔
1582

1583
                        var signed = MultipartSigned.Create (ctx, signer, entity, cancellationToken);
1✔
1584

1585
                        return Encrypt (ctx, recipients, signed, cancellationToken);
1✔
1586
                }
1✔
1587

1588
                /// <summary>
1589
                /// Asynchronously sign and encrypt the specified entity.
1590
                /// </summary>
1591
                /// <remarks>
1592
                /// Asynchronously signs entity using the supplied signer and then encrypts the result to the specified recipients.
1593
                /// </remarks>
1594
                /// <returns>The signed and encrypted entity.</returns>
1595
                /// <param name="ctx">The S/MIME context to use for signing and encrypting.</param>
1596
                /// <param name="signer">The signer.</param>
1597
                /// <param name="recipients">The recipients.</param>
1598
                /// <param name="entity">The entity.</param>
1599
                /// <param name="cancellationToken">The cancellation token.</param>
1600
                /// <exception cref="System.ArgumentNullException">
1601
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1602
                /// <para>-or-</para>
1603
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1604
                /// <para>-or-</para>
1605
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1606
                /// <para>-or-</para>
1607
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1608
                /// </exception>
1609
                /// <exception cref="System.ObjectDisposedException">
1610
                /// <paramref name="entity"/> has been disposed.
1611
                /// </exception>
1612
                /// <exception cref="System.OperationCanceledException">
1613
                /// The operation was canceled via the cancellation token.
1614
                /// </exception>
1615
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1616
                /// An error occurred in the cryptographic message syntax subsystem.
1617
                /// </exception>
1618
                public static async Task<ApplicationPkcs7Mime> SignAndEncryptAsync (SecureMimeContext ctx, CmsSigner signer, CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1619
                {
1✔
1620
                        if (ctx == null)
1✔
1621
                                throw new ArgumentNullException (nameof (ctx));
1✔
1622

1623
                        if (signer == null)
1✔
1624
                                throw new ArgumentNullException (nameof (signer));
1✔
1625

1626
                        if (recipients == null)
1✔
1627
                                throw new ArgumentNullException (nameof (recipients));
1✔
1628

1629
                        if (entity == null)
1✔
1630
                                throw new ArgumentNullException (nameof (entity));
1✔
1631

1632
                        var signed = await MultipartSigned.CreateAsync (ctx, signer, entity, cancellationToken).ConfigureAwait (false);
1✔
1633

1634
                        return await EncryptAsync (ctx, recipients, signed, cancellationToken).ConfigureAwait (false);
1✔
1635
                }
1✔
1636

1637
                /// <summary>
1638
                /// Sign and encrypt the specified entity.
1639
                /// </summary>
1640
                /// <remarks>
1641
                /// Signs entity using the supplied signer and the default <see cref="SecureMimeContext"/>
1642
                /// and then encrypts the result to the specified recipients.
1643
                /// </remarks>
1644
                /// <returns>The signed and encrypted entity.</returns>
1645
                /// <param name="signer">The signer.</param>
1646
                /// <param name="recipients">The recipients.</param>
1647
                /// <param name="entity">The entity.</param>
1648
                /// <param name="cancellationToken">The cancellation token.</param>
1649
                /// <exception cref="System.ArgumentNullException">
1650
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1651
                /// <para>-or-</para>
1652
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1653
                /// <para>-or-</para>
1654
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1655
                /// </exception>
1656
                /// <exception cref="System.ObjectDisposedException">
1657
                /// <paramref name="entity"/> has been disposed.
1658
                /// </exception>
1659
                /// <exception cref="System.OperationCanceledException">
1660
                /// The operation was canceled via the cancellation token.
1661
                /// </exception>
1662
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1663
                /// An error occurred in the cryptographic message syntax subsystem.
1664
                /// </exception>
1665
                public static ApplicationPkcs7Mime SignAndEncrypt (CmsSigner signer, CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1666
                {
1✔
1667
                        if (signer == null)
1✔
1668
                                throw new ArgumentNullException (nameof (signer));
1✔
1669

1670
                        if (recipients == null)
1✔
1671
                                throw new ArgumentNullException (nameof (recipients));
1✔
1672

1673
                        if (entity == null)
1✔
1674
                                throw new ArgumentNullException (nameof (entity));
1✔
1675

1676
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1677
                                return SignAndEncrypt (ctx, signer, recipients, entity, cancellationToken);
1✔
1678
                }
1✔
1679

1680
                /// <summary>
1681
                /// Asynchroinously sign and encrypt the specified entity.
1682
                /// </summary>
1683
                /// <remarks>
1684
                /// Asynchronously signs entity using the supplied signer and the default <see cref="SecureMimeContext"/>
1685
                /// and then encrypts the result to the specified recipients.
1686
                /// </remarks>
1687
                /// <returns>The signed and encrypted entity.</returns>
1688
                /// <param name="signer">The signer.</param>
1689
                /// <param name="recipients">The recipients.</param>
1690
                /// <param name="entity">The entity.</param>
1691
                /// <param name="cancellationToken">The cancellation token.</param>
1692
                /// <exception cref="System.ArgumentNullException">
1693
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1694
                /// <para>-or-</para>
1695
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1696
                /// <para>-or-</para>
1697
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1698
                /// </exception>
1699
                /// <exception cref="System.ObjectDisposedException">
1700
                /// <paramref name="entity"/> has been disposed.
1701
                /// </exception>
1702
                /// <exception cref="System.OperationCanceledException">
1703
                /// The operation was canceled via the cancellation token.
1704
                /// </exception>
1705
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1706
                /// An error occurred in the cryptographic message syntax subsystem.
1707
                /// </exception>
1708
                public static async Task<ApplicationPkcs7Mime> SignAndEncryptAsync (CmsSigner signer, CmsRecipientCollection recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1709
                {
1✔
1710
                        if (signer == null)
1✔
1711
                                throw new ArgumentNullException (nameof (signer));
1✔
1712

1713
                        if (recipients == null)
1✔
1714
                                throw new ArgumentNullException (nameof (recipients));
1✔
1715

1716
                        if (entity == null)
1✔
1717
                                throw new ArgumentNullException (nameof (entity));
1✔
1718

1719
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1720
                                return await SignAndEncryptAsync (ctx, signer, recipients, entity, cancellationToken).ConfigureAwait (false);
1✔
1721
                }
1✔
1722

1723
                /// <summary>
1724
                /// Sign and encrypt the specified entity.
1725
                /// </summary>
1726
                /// <remarks>
1727
                /// Signs entity using the supplied signer and then encrypts the result to the specified recipients.
1728
                /// </remarks>
1729
                /// <returns>The signed and encrypted entity.</returns>
1730
                /// <param name="ctx">The S/MIME context to use for signing and encrypting.</param>
1731
                /// <param name="signer">The signer.</param>
1732
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1733
                /// <param name="recipients">The recipients.</param>
1734
                /// <param name="entity">The entity.</param>
1735
                /// <param name="cancellationToken">The cancellation token.</param>
1736
                /// <exception cref="System.ArgumentNullException">
1737
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1738
                /// <para>-or-</para>
1739
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1740
                /// <para>-or-</para>
1741
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1742
                /// <para>-or-</para>
1743
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1744
                /// </exception>
1745
                /// <exception cref="System.ObjectDisposedException">
1746
                /// <paramref name="entity"/> has been disposed.
1747
                /// </exception>
1748
                /// <exception cref="System.OperationCanceledException">
1749
                /// The operation was canceled via the cancellation token.
1750
                /// </exception>
1751
                /// <exception cref="CertificateNotFoundException">
1752
                /// <para>A signing certificate could not be found for <paramref name="signer"/>.</para>
1753
                /// <para>-or-</para>
1754
                /// <para>A certificate could not be found for one or more of the <paramref name="recipients"/>.</para>
1755
                /// </exception>
1756
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1757
                /// An error occurred in the cryptographic message syntax subsystem.
1758
                /// </exception>
1759
                public static ApplicationPkcs7Mime SignAndEncrypt (SecureMimeContext ctx, MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1760
                {
1✔
1761
                        if (ctx == null)
1✔
1762
                                throw new ArgumentNullException (nameof (ctx));
1✔
1763

1764
                        if (signer == null)
1✔
1765
                                throw new ArgumentNullException (nameof (signer));
1✔
1766

1767
                        if (recipients == null)
1✔
1768
                                throw new ArgumentNullException (nameof (recipients));
1✔
1769

1770
                        if (entity == null)
1✔
1771
                                throw new ArgumentNullException (nameof (entity));
1✔
1772

1773
                        var signed = MultipartSigned.Create (ctx, signer, digestAlgo, entity, cancellationToken);
1✔
1774

1775
                        return Encrypt (ctx, recipients, signed, cancellationToken);
1✔
1776
                }
1✔
1777

1778
                /// <summary>
1779
                /// Asynchronously sign and encrypt the specified entity.
1780
                /// </summary>
1781
                /// <remarks>
1782
                /// Asynchronously signs entity using the supplied signer and then encrypts the result to the specified recipients.
1783
                /// </remarks>
1784
                /// <returns>The signed and encrypted entity.</returns>
1785
                /// <param name="ctx">The S/MIME context to use for signing and encrypting.</param>
1786
                /// <param name="signer">The signer.</param>
1787
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1788
                /// <param name="recipients">The recipients.</param>
1789
                /// <param name="entity">The entity.</param>
1790
                /// <param name="cancellationToken">The cancellation token.</param>
1791
                /// <exception cref="System.ArgumentNullException">
1792
                /// <para><paramref name="ctx"/> is <see langword="null"/>.</para>
1793
                /// <para>-or-</para>
1794
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1795
                /// <para>-or-</para>
1796
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1797
                /// <para>-or-</para>
1798
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1799
                /// </exception>
1800
                /// <exception cref="System.ObjectDisposedException">
1801
                /// <paramref name="entity"/> has been disposed.
1802
                /// </exception>
1803
                /// <exception cref="System.OperationCanceledException">
1804
                /// The operation was canceled via the cancellation token.
1805
                /// </exception>
1806
                /// <exception cref="CertificateNotFoundException">
1807
                /// <para>A signing certificate could not be found for <paramref name="signer"/>.</para>
1808
                /// <para>-or-</para>
1809
                /// <para>A certificate could not be found for one or more of the <paramref name="recipients"/>.</para>
1810
                /// </exception>
1811
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1812
                /// An error occurred in the cryptographic message syntax subsystem.
1813
                /// </exception>
1814
                public static async Task<ApplicationPkcs7Mime> SignAndEncryptAsync (SecureMimeContext ctx, MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1815
                {
1✔
1816
                        if (ctx == null)
1✔
1817
                                throw new ArgumentNullException (nameof (ctx));
1✔
1818

1819
                        if (signer == null)
1✔
1820
                                throw new ArgumentNullException (nameof (signer));
1✔
1821

1822
                        if (recipients == null)
1✔
1823
                                throw new ArgumentNullException (nameof (recipients));
1✔
1824

1825
                        if (entity == null)
1✔
1826
                                throw new ArgumentNullException (nameof (entity));
1✔
1827

1828
                        var signed = await MultipartSigned.CreateAsync (ctx, signer, digestAlgo, entity, cancellationToken).ConfigureAwait (false);
1✔
1829

1830
                        return await EncryptAsync (ctx, recipients, signed, cancellationToken).ConfigureAwait (false);
1✔
1831
                }
1✔
1832

1833
                /// <summary>
1834
                /// Sign and encrypt the specified entity.
1835
                /// </summary>
1836
                /// <remarks>
1837
                /// Signs entity using the supplied signer and the default <see cref="SecureMimeContext"/>
1838
                /// and then encrypts the result to the specified recipients.
1839
                /// </remarks>
1840
                /// <returns>The signed and encrypted entity.</returns>
1841
                /// <param name="signer">The signer.</param>
1842
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1843
                /// <param name="recipients">The recipients.</param>
1844
                /// <param name="entity">The entity.</param>
1845
                /// <param name="cancellationToken">The cancellation token.</param>
1846
                /// <exception cref="System.ArgumentNullException">
1847
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1848
                /// <para>-or-</para>
1849
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1850
                /// <para>-or-</para>
1851
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1852
                /// </exception>
1853
                /// <exception cref="System.ObjectDisposedException">
1854
                /// <paramref name="entity"/> has been disposed.
1855
                /// </exception>
1856
                /// <exception cref="System.OperationCanceledException">
1857
                /// The operation was canceled via the cancellation token.
1858
                /// </exception>
1859
                /// <exception cref="CertificateNotFoundException">
1860
                /// <para>A signing certificate could not be found for <paramref name="signer"/>.</para>
1861
                /// <para>-or-</para>
1862
                /// <para>A certificate could not be found for one or more of the <paramref name="recipients"/>.</para>
1863
                /// </exception>
1864
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1865
                /// An error occurred in the cryptographic message syntax subsystem.
1866
                /// </exception>
1867
                public static ApplicationPkcs7Mime SignAndEncrypt (MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1868
                {
1✔
1869
                        if (signer == null)
1✔
1870
                                throw new ArgumentNullException (nameof (signer));
1✔
1871

1872
                        if (recipients == null)
1✔
1873
                                throw new ArgumentNullException (nameof (recipients));
1✔
1874

1875
                        if (entity == null)
1✔
1876
                                throw new ArgumentNullException (nameof (entity));
1✔
1877

1878
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1879
                                return SignAndEncrypt (ctx, signer, digestAlgo, recipients, entity, cancellationToken);
1✔
1880
                }
1✔
1881

1882
                /// <summary>
1883
                /// Asynchronously sign and encrypt the specified entity.
1884
                /// </summary>
1885
                /// <remarks>
1886
                /// Asynchronously signs entity using the supplied signer and the default <see cref="SecureMimeContext"/>
1887
                /// and then encrypts the result to the specified recipients.
1888
                /// </remarks>
1889
                /// <returns>The signed and encrypted entity.</returns>
1890
                /// <param name="signer">The signer.</param>
1891
                /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
1892
                /// <param name="recipients">The recipients.</param>
1893
                /// <param name="entity">The entity.</param>
1894
                /// <param name="cancellationToken">The cancellation token.</param>
1895
                /// <exception cref="System.ArgumentNullException">
1896
                /// <para><paramref name="signer"/> is <see langword="null"/>.</para>
1897
                /// <para>-or-</para>
1898
                /// <para><paramref name="recipients"/> is <see langword="null"/>.</para>
1899
                /// <para>-or-</para>
1900
                /// <para><paramref name="entity"/> is <see langword="null"/>.</para>
1901
                /// </exception>
1902
                /// <exception cref="System.ObjectDisposedException">
1903
                /// <paramref name="entity"/> has been disposed.
1904
                /// </exception>
1905
                /// <exception cref="System.OperationCanceledException">
1906
                /// The operation was canceled via the cancellation token.
1907
                /// </exception>
1908
                /// <exception cref="CertificateNotFoundException">
1909
                /// <para>A signing certificate could not be found for <paramref name="signer"/>.</para>
1910
                /// <para>-or-</para>
1911
                /// <para>A certificate could not be found for one or more of the <paramref name="recipients"/>.</para>
1912
                /// </exception>
1913
                /// <exception cref="Org.BouncyCastle.Cms.CmsException">
1914
                /// An error occurred in the cryptographic message syntax subsystem.
1915
                /// </exception>
1916
                public static async Task<ApplicationPkcs7Mime> SignAndEncryptAsync (MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable<MailboxAddress> recipients, MimeEntity entity, CancellationToken cancellationToken = default)
1917
                {
1✔
1918
                        if (signer == null)
1✔
1919
                                throw new ArgumentNullException (nameof (signer));
1✔
1920

1921
                        if (recipients == null)
1✔
1922
                                throw new ArgumentNullException (nameof (recipients));
1✔
1923

1924
                        if (entity == null)
1✔
1925
                                throw new ArgumentNullException (nameof (entity));
1✔
1926

1927
                        using (var ctx = (SecureMimeContext) CryptographyContext.Create ("application/pkcs7-mime"))
1✔
1928
                                return await SignAndEncryptAsync (ctx, signer, digestAlgo, recipients, entity, cancellationToken).ConfigureAwait (false);
1✔
1929
                }
1✔
1930
        }
1931
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc