static EncryptionAlgorithm[] DecodeEncryptionAlgorithms(byte[] rawData)
        {
            using (var memory = new MemoryStream(rawData, false)) {
                using (var asn1 = new Asn1InputStream(memory)) {
                    var algorithms = new List <EncryptionAlgorithm> ();
                    var sequence   = asn1.ReadObject() as Asn1Sequence;

                    if (sequence == null)
                    {
                        return(null);
                    }

                    for (int i = 0; i < sequence.Count; i++)
                    {
                        var identifier = AlgorithmIdentifier.GetInstance(sequence[i]);
                        EncryptionAlgorithm algorithm;

                        if (BouncyCastleSecureMimeContext.TryGetEncryptionAlgorithm(identifier, out algorithm))
                        {
                            algorithms.Add(algorithm);
                        }
                    }

                    return(algorithms.ToArray());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.Cryptography.WindowsSecureMimeDigitalSignature"/> class.
        /// </summary>
        /// <remarks>
        /// Creates a new <see cref="WindowsSecureMimeDigitalSignature"/>.
        /// </remarks>
        /// <param name="signerInfo">The information about the signer.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="signerInfo"/> is <c>null</c>.
        /// </exception>
        public WindowsSecureMimeDigitalSignature(SignerInfo signerInfo)
        {
            if (signerInfo == null)
            {
                throw new ArgumentNullException(nameof(signerInfo));
            }

            SignerInfo = signerInfo;

            var             algorithms = new List <EncryptionAlgorithm> ();
            DigestAlgorithm digestAlgo;

            if (signerInfo.SignedAttributes != null)
            {
                for (int i = 0; i < signerInfo.SignedAttributes.Count; i++)
                {
                    if (signerInfo.SignedAttributes[i].Oid.Value == CmsAttributes.SigningTime.Id)
                    {
                        var signingTime = signerInfo.SignedAttributes[i].Values[0] as Pkcs9SigningTime;

                        if (signingTime != null)
                        {
                            CreationDate = signingTime.SigningTime;
                        }
                    }
                    else if (signerInfo.SignedAttributes[i].Oid.Value == SmimeAttributes.SmimeCapabilities.Id)
                    {
                        foreach (var value in signerInfo.SignedAttributes[i].Values)
                        {
                            var sequences = (DerSequence)Asn1Object.FromByteArray(value.RawData);

                            foreach (Asn1Sequence sequence in sequences)
                            {
                                var identifier = Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier.GetInstance(sequence);
                                EncryptionAlgorithm algorithm;

                                if (BouncyCastleSecureMimeContext.TryGetEncryptionAlgorithm(identifier, out algorithm))
                                {
                                    algorithms.Add(algorithm);
                                }
                            }
                        }
                    }
                }
            }

            EncryptionAlgorithms = algorithms.ToArray();

            if (WindowsSecureMimeContext.TryGetDigestAlgorithm(signerInfo.DigestAlgorithm, out digestAlgo))
            {
                DigestAlgorithm = digestAlgo;
            }

            SignerCertificate = new WindowsSecureMimeDigitalCertificate(signerInfo.Certificate);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.Cryptography.SecureMimeDigitalSignature"/> class.
        /// </summary>
        /// <remarks>
        /// Creates a new <see cref="SecureMimeDigitalSignature"/>.
        /// </remarks>
        /// <param name="signerInfo">The information about the signer.</param>
        /// <param name="certificate">The signer's certificate.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="signerInfo"/> is <c>null</c>.
        /// </exception>
        public SecureMimeDigitalSignature(SignerInformation signerInfo, X509Certificate certificate)
        {
            if (signerInfo == null)
            {
                throw new ArgumentNullException(nameof(signerInfo));
            }

            SignerInfo = signerInfo;

            var             algorithms = new List <EncryptionAlgorithm> ();
            DigestAlgorithm digestAlgo;

            if (signerInfo.SignedAttributes != null)
            {
                Asn1EncodableVector vector = signerInfo.SignedAttributes.GetAll(CmsAttributes.SigningTime);
                foreach (Org.BouncyCastle.Asn1.Cms.Attribute attr in vector)
                {
                    var signingTime = (DerUtcTime)((DerSet)attr.AttrValues)[0];
                    CreationDate = ToAdjustedDateTime(signingTime);
                    break;
                }

                vector = signerInfo.SignedAttributes.GetAll(SmimeAttributes.SmimeCapabilities);
                foreach (Org.BouncyCastle.Asn1.Cms.Attribute attr in vector)
                {
                    foreach (Asn1Sequence sequence in attr.AttrValues)
                    {
                        for (int i = 0; i < sequence.Count; i++)
                        {
                            var identifier = AlgorithmIdentifier.GetInstance(sequence[i]);
                            EncryptionAlgorithm algorithm;

                            if (BouncyCastleSecureMimeContext.TryGetEncryptionAlgorithm(identifier, out algorithm))
                            {
                                algorithms.Add(algorithm);
                            }
                        }
                    }
                }

                EncryptionAlgorithms = algorithms.ToArray();
            }

            if (BouncyCastleSecureMimeContext.TryGetDigestAlgorithm(signerInfo.DigestAlgorithmID, out digestAlgo))
            {
                DigestAlgorithm = digestAlgo;
            }

            if (certificate != null)
            {
                SignerCertificate = new SecureMimeDigitalCertificate(certificate);
            }
        }