Esempio n. 1
0
        /// <summary>
        ///     Encodes and signs the content using the signer object used in
        /// </summary>
        /// <returns>
        ///     An instance of <see cref="PkcsSignerInfo"/> class.
        /// </returns>
        /// <remarks>
        ///     Before signing, the method adds two authenticated attributes: content type and message digest. Authenticated attributes are then
        ///     signed with signer's private key.
        /// </remarks>
        public PkcsSignerInfo Encode()
        {
            if (_authAttributes.All(x => x.Oid.Value != MESSAGE_DIGEST))
            {
                throw new InvalidOperationException();
            }
            // version
            var builder = new Asn1Builder().AddInteger(Version);

            // signerIdentifier
            builder.AddDerData(signerCert.Encode());
            // digestAlgorithm
            builder.AddDerData(hashAlgId.RawData);
            // authenticatedAttributes
            if (_authAttributes.Any())
            {
                builder.AddExplicit(0, _authAttributes.Encode(), false);
            }
            // digestEncryptionAlgorithm
            builder.AddDerData(pubKeyAlgId.RawData);
            // encryptedDigest
            builder.AddOctetString(hashValue);
            // unauthenticatedAttributes
            if (_unauthAttributes.Any())
            {
                builder.AddExplicit(1, UnauthenticatedAttributes.Encode(), false);
            }

            // wrap
            return(new PkcsSignerInfo(builder.GetEncoded()));
        }
Esempio n. 2
0
        Byte[] encodeSignedData()
        {
            var builder = new Asn1Builder()
                          .AddInteger(Version)
                          .AddDerData(DigestAlgorithms.Encode())
                          .AddDerData(encodeContentInfo());

            // certificates
            if (Certificates.Count > 0)
            {
                builder.AddExplicit(0, Certificates.Encode(), false);
            }
            // CRLs
            if (RevocationLists.Count > 0)
            {
                var crlBytes = new List <Byte>();
                foreach (X509CRL2 crl in RevocationLists)
                {
                    crlBytes.AddRange(crl.RawData);
                }
                builder.AddExplicit(1, crlBytes.ToArray(), false);
            }
            builder.AddDerData(SignerInfos.Encode());
            return(builder.GetEncoded());
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override Byte[] Encode()
        {
            var builder = new Asn1Builder()
                          .AddInteger(Version)
                          .AddDerData(RequestMessage.Encode());

            if (PolicyID != null)
            {
                builder.AddObjectIdentifier(PolicyID);
            }
            if (UseNonce)
            {
                nonce = Guid.NewGuid().ToByteArray();
                builder.AddInteger(new BigInteger(nonce));
            }
            else
            {
                nonce = default;
            }
            if (RequestCertificates)
            {
                builder.AddBoolean(RequestCertificates);
            }
            if (_extensions.Any())
            {
                builder.AddExplicit(0, Extensions.Encode(), false);
            }

            return(builder.GetEncoded());
        }
Esempio n. 4
0
        Byte[] encodeContentInfo()
        {
            var builder = new Asn1Builder()
                          .AddObjectIdentifier(_contentType);

            if (_content != null)
            {
                switch (ContentType.Value)
                {
                case CMC_DATA:     // CMC Data. For CMC: content [0] EXPLICIT OCTET STRING OPTIONAL
                    builder.AddExplicit(0, x => x.AddOctetString(_content));
                    break;

                default:     // everything else. Suggested: content [0] EXPLICIT SEQUENCE OF ANY OPTIONAL
                    builder.AddExplicit(0, x => x.AddSequence(_content));
                    break;
                }
            }
            return(builder.GetEncoded());
        }