Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.Cryptography.CmsSigner"/> class.
        /// </summary>
        /// <remarks>
        /// <para>The initial value of the <see cref="MimeKit.Cryptography.DigestAlgorithm"/> will
        /// be set to <see cref="MimeKit.Cryptography.DigestAlgorithm.Sha1"/> and both the
        /// <see cref="SignedAttributes"/> and <see cref="UnsignedAttributes"/> properties will be
        /// initialized to empty tables.</para>
        /// </remarks>
        /// <param name="certificate">The signer's certificate.</param>
        /// <param name="key">The signer's private key.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="certificate"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="key"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// <para><paramref name="certificate"/> cannot be used for signing.</para>
        /// <para>-or-</para>
        /// <para><paramref name="key"/> is not a private key.</para>
        /// </exception>
        public CmsSigner(X509Certificate certificate, AsymmetricKeyParameter key) : this()
        {
            if (certificate == null)
            {
                throw new ArgumentNullException(nameof(certificate));
            }

            CheckCertificateCanBeUsedForSigning(certificate);

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

            if (!key.IsPrivate)
            {
                throw new ArgumentException("The key must be a private key.", nameof(key));
            }

            CertificateChain = new X509CertificateChain();
            CertificateChain.Add(certificate);
            Certificate = certificate;
            PrivateKey  = key;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.Cryptography.CmsSigner"/> class.
        /// </summary>
        /// <remarks>
        /// <para>The initial value of the <see cref="DigestAlgorithm"/> will be set to
        /// <see cref="MimeKit.Cryptography.DigestAlgorithm.Sha1"/> and both the
        /// <see cref="SignedAttributes"/> and <see cref="UnsignedAttributes"/> properties
        /// will be initialized to empty tables.</para>
        /// </remarks>
        /// <param name="chain">The chain of certificates starting with the signer's certificate back to the root.</param>
        /// <param name="key">The signer's private key.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="chain"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="key"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// <para><paramref name="chain"/> did not contain any certificates.</para>
        /// <para>-or-</para>
        /// <para>The certificate cannot be used for signing.</para>
        /// <para>-or-</para>
        /// <para><paramref name="key"/> is not a private key.</para>
        /// </exception>
        public CmsSigner(IEnumerable <X509CertificateEntry> chain, AsymmetricKeyParameter key) : this()
        {
            if (chain == null)
            {
                throw new ArgumentNullException("chain");
            }

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

            CertificateChain = new X509CertificateChain();
            foreach (var entry in chain)
            {
                CertificateChain.Add(entry.Certificate);
                if (Certificate == null)
                {
                    Certificate = entry.Certificate;
                }
            }

            if (CertificateChain.Count == 0)
            {
                throw new ArgumentException("The certificate chain was empty.", "chain");
            }

            CheckCertificateCanBeUsedForSigning(Certificate);

            if (!key.IsPrivate)
            {
                throw new ArgumentException("The key must be a private key.", "key");
            }

            PrivateKey = key;
        }