Пример #1
0
        /// <summary>
        /// Decrypts the <see cref="MultipartEncrypted"/> part.
        /// </summary>
        /// <remarks>
        /// Decrypts the <see cref="MultipartEncrypted"/> and extracts any digital signatures in cases
        /// where the content was also signed.
        /// </remarks>
        /// <returns>The decrypted entity.</returns>
        /// <param name="ctx">The OpenPGP cryptography context to use for decrypting.</param>
        /// <param name="signatures">A list of digital signatures if the data was both signed and encrypted.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="ctx"/> is <c>null</c>.
        /// </exception>
        /// <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">
        /// The provided <see cref="OpenPgpContext"/> does not support the protocol parameter.
        /// </exception>
        /// <exception cref="PrivateKeyNotFoundException">
        /// The private key could not be found to decrypt the encrypted data.
        /// </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(OpenPgpContext ctx, out DigitalSignatureCollection signatures)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException("ctx");
            }

            var protocol = ContentType.Parameters["protocol"];

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

            protocol = protocol.Trim().ToLowerInvariant();
            if (!ctx.Supports(protocol))
            {
                throw new NotSupportedException();
            }

            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 memory = new MemoryBlockStream()) {
                encrypted.ContentObject.DecodeTo(memory);
                memory.Position = 0;

                return(ctx.Decrypt(memory, out signatures));
            }
        }
Пример #2
0
 /// <summary>
 /// Verify the digital signatures of the specified signed data and extract the original content.
 /// </summary>
 /// <remarks>
 /// Verifies the digital signatures of the specified signed data and extracts the original content.
 /// </remarks>
 /// <returns>The extracted content stream.</returns>
 /// <param name="signedData">The signed data.</param>
 /// <param name="signatures">The digital signatures.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="signedData"/> is <c>null</c>.
 /// </exception>
 /// <exception cref="Org.BouncyCastle.Cms.CmsException">
 /// An error occurred in the cryptographic message syntax subsystem.
 /// </exception>
 /// <exception cref="System.OperationCanceledException">
 /// The operation was cancelled via the cancellation token.
 /// </exception>
 public abstract Stream Verify(Stream signedData, out DigitalSignatureCollection signatures, CancellationToken cancellationToken = default(CancellationToken));
Пример #3
0
        /// <summary>
        /// Decrypts the <see cref="MultipartEncrypted"/> part.
        /// </summary>
        /// <remarks>
        /// Decrypts the <see cref="MultipartEncrypted"/> and extracts any digital signatures in cases
        /// where the content was also signed.
        /// </remarks>
        /// <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="PrivateKeyNotFoundException">
        /// The private key could not be found to decrypt the encrypted data.
        /// </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 DigitalSignatureCollection 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 MemoryBlockStream()) {
                    var pgp = ctx as OpenPgpContext;

                    encrypted.ContentObject.DecodeTo(memory);
                    memory.Position = 0;

                    if (pgp != null)
                    {
                        return(pgp.Decrypt(memory, out signatures));
                    }

                    signatures = null;

                    return(ctx.Decrypt(memory));
                }
            }
        }
Пример #4
0
 public override Stream Verify(Stream signedData, out DigitalSignatureCollection signatures, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }