예제 #1
0
 /// <summary>
 /// Decrypt the content.
 /// </summary>
 /// <returns>The decrypted <see cref="MimeKit.MimeEntity"/>.</returns>
 /// <exception cref="System.InvalidOperationException">
 /// The "smime-type" parameter on the Content-Type header does not support decryption.
 /// </exception>
 public MimeEntity Decrypt()
 {
     // FIXME: find out what exceptions BouncyCastle can throw...
     using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime")) {
         return(Decrypt(ctx));
     }
 }
예제 #2
0
        /// <summary>
        /// Compresses the specified entity.
        /// </summary>
        /// <remarks>
        /// <para>Compresses the specified entity using the default <see cref="SecureMimeContext"/>.</para>
        /// <note type="warning">Most mail clients, even among those that support S/MIME, do not support compression.</note>
        /// </remarks>
        /// <returns>The compressed entity.</returns>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="entity"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime Compress(MimeEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
                return(Compress(ctx, entity));
        }
예제 #3
0
        /// <summary>
        /// Decompress the compressed-data.
        /// </summary>
        /// <remarks>
        /// Decompresses the compressed-data using the default <see cref="SecureMimeContext"/>.
        /// </remarks>
        /// <returns>The decompressed <see cref="MimeKit.MimeEntity"/>.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// The "smime-type" parameter on the Content-Type header is not "compressed-data".
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public MimeEntity Decompress()
        {
            if (SecureMimeType != SecureMimeType.CompressedData && SecureMimeType != SecureMimeType.Unknown)
            {
                throw new InvalidOperationException();
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
                return(Decompress(ctx));
        }
예제 #4
0
        /// <summary>
        /// Asynchronously verify the multipart/signed part.
        /// </summary>
        /// <remarks>
        /// Verifies the multipart/signed part using the default cryptography context.
        /// </remarks>
        /// <returns>A signer info collection.</returns>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <exception cref="System.FormatException">
        /// <para>The <c>protocol</c> parameter was not specified.</para>
        /// <para>-or-</para>
        /// <para>The multipart is malformed in some way.</para>
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// A cryptography context suitable for verifying the signature could not be found.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was cancelled via the cancellation token.
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public Task <DigitalSignatureCollection> VerifyAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var protocol = ContentType.Parameters["protocol"]?.Trim();

            if (string.IsNullOrEmpty(protocol))
            {
                throw new FormatException("The multipart/signed part did not specify a protocol.");
            }

            using (var ctx = CryptographyContext.Create(protocol))
                return(VerifyAsync(ctx, cancellationToken));
        }
예제 #5
0
        /// <summary>
        /// Decrypt this instance.
        /// </summary>
        /// <returns>The decrypted entity.</returns>
        /// <param name="signatures">A list of digital signatures if the data was both signed and encrypted.</param>
        /// <exception cref="System.FormatException">
        /// <para>The <c>protocol</c> parameter was not specified.</para>
        /// <para>-or-</para>
        /// <para>The multipart is malformed in some way.</para>
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// A suitable <see cref="MimeKit.Cryptography.CryptographyContext"/> for
        /// decrypting could not be found.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The user chose to cancel the password prompt.
        /// </exception>
        /// <exception cref="System.UnauthorizedAccessException">
        /// 3 bad attempts were made to unlock the secret key.
        /// </exception>
        public MimeEntity Decrypt(out IList <IDigitalSignature> signatures)
        {
            var protocol = ContentType.Parameters["protocol"];

            if (string.IsNullOrEmpty(protocol))
            {
                throw new FormatException();
            }

            protocol = protocol.Trim().ToLowerInvariant();

            if (Count < 2)
            {
                throw new FormatException();
            }

            var version = this[0] as MimePart;

            if (version == null)
            {
                throw new FormatException();
            }

            var ctype = version.ContentType;
            var value = string.Format("{0}/{1}", ctype.MediaType, ctype.MediaSubtype);

            if (value.ToLowerInvariant() != protocol)
            {
                throw new FormatException();
            }

            var encrypted = this[1] as MimePart;

            if (encrypted == null || encrypted.ContentObject == null)
            {
                throw new FormatException();
            }

            if (!encrypted.ContentType.Matches("application", "octet-stream"))
            {
                throw new FormatException();
            }

            using (var ctx = CryptographyContext.Create(protocol)) {
                using (var memory = new MemoryStream()) {
                    encrypted.ContentObject.DecodeTo(memory);
                    memory.Position = 0;

                    return(ctx.Decrypt(memory, out signatures));
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Cryptographically signs the specified entity.
        /// </summary>
        /// <remarks>
        /// <para>Signs the entity using the supplied signer and the default <see cref="SecureMimeContext"/>.</para>
        /// <para>For better interoperability with other mail clients, you should use
        /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity)"/>
        /// instead as the multipart/signed format is supported among a much larger
        /// subset of mail client software.</para>
        /// </remarks>
        /// <returns>The signed entity.</returns>
        /// <param name="signer">The signer.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="signer"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime Sign(CmsSigner signer, MimeEntity entity)
        {
            if (signer == null)
            {
                throw new ArgumentNullException(nameof(signer));
            }

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
                return(Sign(ctx, signer, entity));
        }
예제 #7
0
        /// <summary>
        /// Encrypts the specified entity.
        /// </summary>
        /// <remarks>
        /// Encrypts the entity to the specified recipients using the default <see cref="SecureMimeContext"/>.
        /// </remarks>
        /// <returns>The encrypted entity.</returns>
        /// <param name="recipients">The recipients.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Valid certificates could not be found for one or more of the <paramref name="recipients"/>.
        /// </exception>
        /// <exception cref="CertificateNotFoundException">
        /// A certificate could not be found for one or more of the <paramref name="recipients"/>.
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime Encrypt(IEnumerable <MailboxAddress> recipients, MimeEntity entity)
        {
            if (recipients == null)
            {
                throw new ArgumentNullException(nameof(recipients));
            }

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
                return(Encrypt(ctx, recipients, entity));
        }
예제 #8
0
        /// <summary>
        /// Verifies the multipart/signed part.
        /// </summary>
        /// <remarks>
        /// Verifies the multipart/signed part using the default cryptography context.
        /// </remarks>
        /// <returns>A signer info collection.</returns>
        /// <exception cref="System.FormatException">
        /// <para>The <c>protocol</c> parameter was not specified.</para>
        /// <para>-or-</para>
        /// <para>The multipart is malformed in some way.</para>
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// A cryptography context suitable for verifying the signature could not be found.
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public DigitalSignatureCollection Verify()
        {
            var protocol = ContentType.Parameters["protocol"];

            if (string.IsNullOrEmpty(protocol))
            {
                throw new FormatException("The multipart/signed part did not specify a protocol.");
            }

            protocol = protocol.Trim().ToLowerInvariant();

            using (var ctx = CryptographyContext.Create(protocol)) {
                return(Verify(ctx));
            }
        }
예제 #9
0
        /// <summary>
        /// Cryptographically signs the specified entity.
        /// </summary>
        /// <remarks>
        /// <para>Signs the entity using the supplied signer, digest algorithm and the default
        /// <see cref="SecureMimeContext"/>.</para>
        /// <para>For better interoperability with other mail clients, you should use
        /// <see cref="MultipartSigned.Create(SecureMimeContext, CmsSigner, MimeEntity)"/>
        /// instead as the multipart/signed format is supported among a much larger
        /// subset of mail client software.</para>
        /// </remarks>
        /// <returns>The signed entity.</returns>
        /// <param name="signer">The signer.</param>
        /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="signer"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="CertificateNotFoundException">
        /// A signing certificate could not be found for <paramref name="signer"/>.
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime Sign(MailboxAddress signer, DigestAlgorithm digestAlgo, MimeEntity entity)
        {
            if (signer == null)
            {
                throw new ArgumentNullException(nameof(signer));
            }

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
                return(Sign(ctx, signer, digestAlgo, entity));
        }
예제 #10
0
        /// <summary>
        /// Encrypts the specified entity.
        /// </summary>
        /// <remarks>
        /// Encrypts the entity to the specified recipients using the default <see cref="SecureMimeContext"/>.
        /// </remarks>
        /// <returns>The encrypted entity.</returns>
        /// <param name="recipients">The recipients.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime Encrypt(CmsRecipientCollection recipients, MimeEntity entity)
        {
            if (recipients == null)
            {
                throw new ArgumentNullException("recipients");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime")) {
                return(Encrypt(ctx, recipients, entity));
            }
        }
예제 #11
0
        /// <summary>
        /// Creates a new <see cref="MultipartEncrypted"/>.
        /// </summary>
        /// <remarks>
        /// Encrypts the entity to the specified recipients, encapsulating the result in a
        /// new multipart/encrypted part.
        /// </remarks>
        /// <returns>A new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance containing
        /// the encrypted version of the specified entity.</returns>
        /// <param name="recipients">The recipients for the encrypted entity.</param>
        /// <param name="entity">The entity to sign and encrypt.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// One or more of the recipient keys cannot be used for encrypting.
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// A default <see cref="OpenPgpContext"/> has not been registered.
        /// </exception>
        public static MultipartEncrypted Create(IEnumerable <PgpPublicKey> recipients, MimeEntity entity)
        {
            if (recipients == null)
            {
                throw new ArgumentNullException("recipients");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var ctx = (OpenPgpContext)CryptographyContext.Create("application/pgp-encrypted")) {
                return(Create(ctx, recipients, entity));
            }
        }
예제 #12
0
        /// <summary>
        /// Cryptographically signs and encrypts the specified entity.
        /// </summary>
        /// <remarks>
        /// Cryptographically signs entity using the supplied signer and the default <see cref="SecureMimeContext"/>
        /// and then encrypts the result to the specified recipients.
        /// </remarks>
        /// <returns>The signed and encrypted entity.</returns>
        /// <param name="signer">The signer.</param>
        /// <param name="recipients">The recipients.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="signer"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime SignAndEncrypt(CmsSigner signer, CmsRecipientCollection recipients, MimeEntity entity)
        {
            if (signer == null)
            {
                throw new ArgumentNullException(nameof(signer));
            }

            if (recipients == null)
            {
                throw new ArgumentNullException(nameof(recipients));
            }

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
                return(SignAndEncrypt(ctx, signer, recipients, entity));
        }
예제 #13
0
        /// <summary>
        /// Cryptographically signs and encrypts the specified entity.
        /// </summary>
        /// <remarks>
        /// Cryptographically signs entity using the supplied signer and then
        /// encrypts the result to the specified recipients.
        /// </remarks>
        /// <returns>The signed and encrypted entity.</returns>
        /// <param name="signer">The signer.</param>
        /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
        /// <param name="recipients">The recipients.</param>
        /// <param name="entity">The entity.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="signer"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="CertificateNotFoundException">
        /// <para>A signing certificate could not be found for <paramref name="signer"/>.</para>
        /// <para>-or-</para>
        /// <para>A certificate could not be found for one or more of the <paramref name="recipients"/>.</para>
        /// </exception>
        /// <exception cref="Org.BouncyCastle.Cms.CmsException">
        /// An error occurred in the cryptographic message syntax subsystem.
        /// </exception>
        public static ApplicationPkcs7Mime SignAndEncrypt(MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable <MailboxAddress> recipients, MimeEntity entity)
        {
            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            if (recipients == null)
            {
                throw new ArgumentNullException("recipients");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime")) {
                return(SignAndEncrypt(ctx, signer, digestAlgo, recipients, entity));
            }
        }
예제 #14
0
        /// <summary>
        /// Creates a new <see cref="MultipartEncrypted"/>.
        /// </summary>
        /// <remarks>
        /// Signs the entity using the supplied signer and digest algorithm and then encrypts to
        /// the specified recipients, encapsulating the result in a new multipart/encrypted part.
        /// </remarks>
        /// <returns>A new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance containing
        /// the signed and encrypted version of the specified entity.</returns>
        /// <param name="signer">The signer to use to sign the entity.</param>
        /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
        /// <param name="cipherAlgo">The encryption algorithm.</param>
        /// <param name="recipients">The recipients for the encrypted entity.</param>
        /// <param name="entity">The entity to sign and encrypt.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="signer"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// <para><paramref name="signer"/> cannot be used for signing.</para>
        /// <para>-or-</para>
        /// <para>One or more of the recipient keys cannot be used for encrypting.</para>
        /// <para>-or-</para>
        /// <para>No recipients were specified.</para>
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// The <paramref name="digestAlgo"/> was out of range.
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// <para>A default <see cref="OpenPgpContext"/> has not been registered.</para>
        /// <para>-or-</para>
        /// <para>The <paramref name="digestAlgo"/> is not supported.</para>
        /// <para>-or-</para>
        /// <para>The <paramref name="cipherAlgo"/> is not supported.</para>
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The user chose to cancel the password prompt.
        /// </exception>
        /// <exception cref="System.UnauthorizedAccessException">
        /// 3 bad attempts were made to unlock the secret key.
        /// </exception>
        public static MultipartEncrypted Create(PgpSecretKey signer, DigestAlgorithm digestAlgo, EncryptionAlgorithm cipherAlgo, IEnumerable <PgpPublicKey> recipients, MimeEntity entity)
        {
            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            if (recipients == null)
            {
                throw new ArgumentNullException("recipients");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var ctx = (OpenPgpContext)CryptographyContext.Create("application/pgp-encrypted")) {
                return(Create(ctx, signer, digestAlgo, cipherAlgo, recipients, entity));
            }
        }
예제 #15
0
        /// <summary>
        /// Creates a new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance with the entity as the content.
        /// </summary>
        /// <returns>A new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance containing
        /// the signed and encrypted version of the specified entity.</returns>
        /// <param name="signer">The signer to use to sign the entity.</param>
        /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
        /// <param name="recipients">The recipients for the encrypted entity.</param>
        /// <param name="entity">The entity to sign and encrypt.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="signer"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// A default <see cref="OpenPgpContext"/> has not been registered.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The user chose to cancel the password prompt.
        /// </exception>
        /// <exception cref="System.UnauthorizedAccessException">
        /// 3 bad attempts were made to unlock the secret key.
        /// </exception>
        public static MultipartEncrypted Create(MailboxAddress signer, DigestAlgorithm digestAlgo, IEnumerable <MailboxAddress> recipients, MimeEntity entity)
        {
            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            if (recipients == null)
            {
                throw new ArgumentNullException("recipients");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var ctx = CryptographyContext.Create("application/pgp-encrypted")) {
                using (var memory = new MemoryStream()) {
                    var options = FormatOptions.Default.Clone();
                    options.NewLineFormat = NewLineFormat.Dos;

                    PrepareEntityForEncrypting(entity);
                    entity.WriteTo(options, memory);
                    memory.Position = 0;

                    var encrypted = new MultipartEncrypted();
                    encrypted.ContentType.Parameters["protocol"] = ctx.EncryptionProtocol;

                    // add the protocol version part
                    encrypted.Add(new ApplicationPgpEncrypted());

                    // add the encrypted entity as the second part
                    encrypted.Add(ctx.SignAndEncrypt(signer, digestAlgo, recipients, memory));

                    return(encrypted);
                }
            }
        }
예제 #16
0
        /// <summary>
        /// Creates a new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance with the entity as the content.
        /// </summary>
        /// <returns>A new <see cref="MimeKit.Cryptography.MultipartEncrypted"/> instance containing
        /// the encrypted version of the specified entity.</returns>
        /// <param name="recipients">The recipients for the encrypted entity.</param>
        /// <param name="entity">The entity to sign and encrypt.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="recipients"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="entity"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// A default <see cref="OpenPgpContext"/> has not been registered.
        /// </exception>
        public static MultipartEncrypted Create(IEnumerable <MailboxAddress> recipients, MimeEntity entity)
        {
            if (recipients == null)
            {
                throw new ArgumentNullException("recipients");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (var ctx = CryptographyContext.Create("application/pgp-encrypted")) {
                using (var memory = new MemoryStream()) {
                    using (var filtered = new FilteredStream(memory)) {
                        filtered.Add(new Unix2DosFilter());

                        PrepareEntityForEncrypting(entity);
                        entity.WriteTo(filtered);
                        filtered.Flush();
                    }

                    memory.Position = 0;

                    var encrypted = new MultipartEncrypted();
                    encrypted.ContentType.Parameters["protocol"] = ctx.EncryptionProtocol;

                    // add the protocol version part
                    encrypted.Add(new ApplicationPgpEncrypted());

                    // add the encrypted entity as the second part
                    encrypted.Add(ctx.Encrypt(recipients, memory));

                    return(encrypted);
                }
            }
        }
예제 #17
0
 /// <summary>
 /// Creates a new <see cref="MultipartSigned"/>.
 /// </summary>
 /// <remarks>
 /// Cryptographically signs the entity using the supplied signer in order
 /// to generate a detached signature and then adds the entity along with
 /// the detached signature data to a new multipart/signed part.
 /// </remarks>
 /// <returns>A new <see cref="MultipartSigned"/> instance.</returns>
 /// <param name="signer">The signer.</param>
 /// <param name="entity">The entity to sign.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <para><paramref name="signer"/> is <c>null</c>.</para>
 /// <para>-or-</para>
 /// <para><paramref name="entity"/> is <c>null</c>.</para>
 /// </exception>
 /// <exception cref="System.NotSupportedException">
 /// A cryptography context suitable for signing could not be found.
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Cms.CmsException">
 /// An error occurred in the cryptographic message syntax subsystem.
 /// </exception>
 public static MultipartSigned Create(CmsSigner signer, MimeEntity entity)
 {
     using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-signature"))
         return(Create(ctx, signer, entity));
 }
예제 #18
0
 /// <summary>
 /// Creates a new <see cref="MultipartSigned"/>.
 /// </summary>
 /// <remarks>
 /// Cryptographically signs the entity using the supplied signer and digest algorithm in
 /// order to generate a detached signature and then adds the entity along with the
 /// detached signature data to a new multipart/signed part.
 /// </remarks>
 /// <returns>A new <see cref="MultipartSigned"/> instance.</returns>
 /// <param name="signer">The signer.</param>
 /// <param name="digestAlgo">The digest algorithm to use for signing.</param>
 /// <param name="entity">The entity to sign.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <para><paramref name="signer"/> is <c>null</c>.</para>
 /// <para>-or-</para>
 /// <para><paramref name="entity"/> is <c>null</c>.</para>
 /// </exception>
 /// <exception cref="System.ArgumentException">
 /// <paramref name="signer"/> cannot be used for signing.
 /// </exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// The <paramref name="digestAlgo"/> was out of range.
 /// </exception>
 /// <exception cref="System.NotSupportedException">
 /// <para>A cryptography context suitable for signing could not be found.</para>
 /// <para>-or-</para>
 /// <para>The <paramref name="digestAlgo"/> is not supported.</para>
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Bcpg.OpenPgp.PgpException">
 /// An error occurred in the OpenPGP subsystem.
 /// </exception>
 public static MultipartSigned Create(PgpSecretKey signer, DigestAlgorithm digestAlgo, MimeEntity entity)
 {
     using (var ctx = (OpenPgpContext)CryptographyContext.Create("application/pgp-signature"))
         return(Create(ctx, signer, digestAlgo, entity));
 }
예제 #19
0
 /// <summary>
 /// Verifies the signed-data and returns the unencapsulated <see cref="MimeKit.MimeEntity"/>.
 /// </summary>
 /// <remarks>
 /// Verifies the signed-data and returns the unencapsulated <see cref="MimeKit.MimeEntity"/>.
 /// </remarks>
 /// <returns>The list of digital signatures.</returns>
 /// <param name="entity">The unencapsulated entity.</param>
 /// <exception cref="System.InvalidOperationException">
 /// The "smime-type" parameter on the Content-Type header is not "signed-data".
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Cms.CmsException">
 /// An error occurred in the cryptographic message syntax subsystem.
 /// </exception>
 public DigitalSignatureCollection Verify(out MimeEntity entity)
 {
     using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime")) {
         return(Verify(ctx, out entity));
     }
 }
예제 #20
0
 /// <summary>
 /// Decrypts the content.
 /// </summary>
 /// <remarks>
 /// Decrypts the content using the default <see cref="SecureMimeContext"/>.
 /// </remarks>
 /// <returns>The decrypted <see cref="MimeKit.MimeEntity"/>.</returns>
 /// <exception cref="System.InvalidOperationException">
 /// The "smime-type" parameter on the Content-Type header is not "certs-only".
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Cms.CmsException">
 /// An error occurred in the cryptographic message syntax subsystem.
 /// </exception>
 public MimeEntity Decrypt()
 {
     using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime")) {
         return(Decrypt(ctx));
     }
 }
예제 #21
0
 /// <summary>
 /// Verifies the signed-data and returns the unencapsulated <see cref="MimeKit.MimeEntity"/>.
 /// </summary>
 /// <remarks>
 /// Verifies the signed-data using the default <see cref="SecureMimeContext"/> and returns the
 /// unencapsulated <see cref="MimeKit.MimeEntity"/>.
 /// </remarks>
 /// <returns>The list of digital signatures.</returns>
 /// <param name="entity">The unencapsulated entity.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <exception cref="System.InvalidOperationException">
 /// The "smime-type" parameter on the Content-Type header is not "signed-data".
 /// </exception>
 /// <exception cref="System.OperationCanceledException">
 /// The operation was cancelled via the cancellation token.
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Cms.CmsException">
 /// An error occurred in the cryptographic message syntax subsystem.
 /// </exception>
 public DigitalSignatureCollection Verify(out MimeEntity entity, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
         return(Verify(ctx, out entity, cancellationToken));
 }
예제 #22
0
 /// <summary>
 /// Decrypt the enveloped-data.
 /// </summary>
 /// <remarks>
 /// Decrypts the enveloped-data using the default <see cref="SecureMimeContext"/>.
 /// </remarks>
 /// <returns>The decrypted <see cref="MimeKit.MimeEntity"/>.</returns>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <exception cref="System.InvalidOperationException">
 /// The "smime-type" parameter on the Content-Type header is not "certs-only".
 /// </exception>
 /// <exception cref="System.OperationCanceledException">
 /// The operation was cancelled via the cancellation token.
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Cms.CmsException">
 /// An error occurred in the cryptographic message syntax subsystem.
 /// </exception>
 public MimeEntity Decrypt(CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var ctx = (SecureMimeContext)CryptographyContext.Create("application/pkcs7-mime"))
         return(Decrypt(ctx, cancellationToken));
 }