Example #1
0
 /// <summary>
 /// Computes an attribute containing a time-stamp token of the provided data, from the provided TSA using the
 /// provided.
 /// </summary>
 /// <remarks>
 /// Computes an attribute containing a time-stamp token of the provided data, from the provided TSA using the
 /// provided. The hashing is performed by the method using the specified algorithm and a BouncyCastle provider.
 /// </remarks>
 /// <param name="signedData"></param>
 /// <exception cref="System.Exception">System.Exception</exception>
 protected internal virtual BcCms.Attribute GetTimeStampAttribute(DerObjectIdentifier oid
                                                                  , ITspSource tsa, AlgorithmIdentifier digestAlgorithm, byte[] messageImprint)
 {
     try
     {
         //jbonilla Hack para obtener el digest del TSA
         IDigest digest        = null;
         string  algorithmName = null;
         digest        = DigestUtilities.GetDigest(DigestAlgorithm.SHA1.GetName());
         algorithmName = DigestAlgorithm.SHA1.GetName();
         digest.BlockUpdate(messageImprint, 0, messageImprint.Length);
         byte[] r = new byte[digest.GetDigestSize()];
         digest.DoFinal(r, 0);
         byte[]            toTimeStamp = r;
         TimeStampResponse tsresp      = tsa.GetTimeStampResponse(DigestAlgorithm.GetByName(algorithmName)
                                                                  , toTimeStamp);
         TimeStampToken tstoken = tsresp.TimeStampToken;
         if (tstoken == null)
         {
             throw new ArgumentNullException("The TimeStampToken returned for the signature time stamp was empty."
                                             );
         }
         BcCms.Attribute signatureTimeStamp = new BcCms.Attribute(oid, new DerSet(Asn1Object.FromByteArray
                                                                                      (tstoken.GetEncoded())));
         return(signatureTimeStamp);
     }
     catch (IOException e)
     {
         throw new RuntimeException(e);
     }
     catch (NoSuchAlgorithmException e)
     {
         throw new RuntimeException(e);
     }
 }
        /// <summary>
        /// Computes an attribute containing a time-stamp token of the provided data, from the provided TSA using the
        /// provided.
        /// </summary>
        /// <remarks>
        /// Computes an attribute containing a time-stamp token of the provided data, from the provided TSA using the
        /// provided. The hashing is performed by the method using the specified algorithm and a BouncyCastle provider.
        /// </remarks>
        /// <param name="signedData"></param>
        /// <exception cref="System.Exception">System.Exception</exception>
        protected internal virtual BcCms.Attribute GetTimeStampAttribute(DerObjectIdentifier oid
                                                                         , ITspSource tsa, AlgorithmIdentifier digestAlgorithm, byte[] messageImprint)
        {
            try
            {
                //jbonilla Hack para obtener el digest del TSA
                IDigest digest        = null;
                string  algorithmName = null;
                if (tsa is ITSAClient)
                {
                    //TODO jbonilla - ¿AlgorithmIdentifier?
                    digest        = ((ITSAClient)tsa).GetMessageDigest();
                    algorithmName = digest.AlgorithmName;
                }
                else
                {
                    digest        = DigestUtilities.GetDigest(DigestAlgorithm.SHA1.GetName());
                    algorithmName = DigestAlgorithm.SHA1.GetName();
                }
                byte[] toTimeStamp = DigestAlgorithms.Digest(digest, messageImprint);

                TimeStampResponse tsresp = tsa.GetTimeStampResponse(DigestAlgorithm.GetByName(algorithmName)
                                                                    , toTimeStamp);
                TimeStampToken tstoken = tsresp.TimeStampToken;
                if (tstoken == null)
                {
                    throw new ArgumentNullException("The TimeStampToken returned for the signature time stamp was empty."
                                                    );
                }
                BcCms.Attribute signatureTimeStamp = new BcCms.Attribute(oid, new DerSet(Asn1Object.FromByteArray
                                                                                             (tstoken.GetEncoded())));
                return(signatureTimeStamp);
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new RuntimeException(e);
            }
        }
Example #3
0
        public override IDictionary <DerObjectIdentifier, Asn1Encodable> GetSignedAttributes(SignatureParameters parameters)
        {
            var       signedAttrs = base.GetSignedAttributes(parameters);
            Attribute policy;
            SignaturePolicyIdentifier sigPolicy;

            switch (parameters.SignaturePolicy)
            {
            case SignaturePolicy.EXPLICIT:
            {
                sigPolicy = new SignaturePolicyIdentifier(
                    new SignaturePolicyId(new DerObjectIdentifier(parameters.SignaturePolicyID),
                                          new OtherHashAlgAndValue(new AlgorithmIdentifier(new DerObjectIdentifier(DigestAlgorithm.GetByName(parameters.SignaturePolicyHashAlgo).OID)),
                                                                   new DerOctetString(parameters.SignaturePolicyHashValue))));
                policy = new Attribute(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, new DerSet(sigPolicy));
                signedAttrs.Add(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, policy);
                break;
            }

            case SignaturePolicy.IMPLICIT:
            {
                sigPolicy = new SignaturePolicyIdentifier();
                policy    = new Attribute(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, new DerSet(sigPolicy));
                signedAttrs.Add(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, policy);
                break;
            }

            case SignaturePolicy.NO_POLICY:
            {
                break;
            }
            }
            return(signedAttrs);
        }
Example #4
0
        //internal override IDictionary<DerObjectIdentifier, Asn1Encodable> GetSignedAttributes
        internal override IDictionary GetSignedAttributes
            (SignatureParameters parameters)
        {
            try
            {
                //IDictionary<DerObjectIdentifier, Asn1Encodable> signedAttrs = base.GetSignedAttributes(parameters);
                IDictionary signedAttrs             = base.GetSignedAttributes(parameters);
                Attribute   policy                  = null;
                SignaturePolicyIdentifier sigPolicy = null;
                switch (parameters.SignaturePolicy)
                {
                case SignaturePolicy.EXPLICIT:
                {
                    sigPolicy = new SignaturePolicyIdentifier(new SignaturePolicyId(new DerObjectIdentifier
                                                                                        (parameters.SignaturePolicyID), new OtherHashAlgAndValue(new AlgorithmIdentifier
                                                                                                                                                     (DigestAlgorithm.GetByName(parameters.SignaturePolicyHashAlgo).GetOid()), new
                                                                                                                                                 DerOctetString(parameters.SignaturePolicyHashValue))));
                    policy = new Attribute(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, new DerSet(sigPolicy
                                                                                                ));
                    signedAttrs.Add(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, policy);
                    break;
                }

                case SignaturePolicy.IMPLICIT:
                {
                    sigPolicy = new SignaturePolicyIdentifier();
                    //sigPolicy.IsSignaturePolicyImplied(); TODO jbonilla - validar
                    policy = new Attribute(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, new DerSet(sigPolicy
                                                                                                ));
                    signedAttrs.Add(PkcsObjectIdentifiers.IdAAEtsSigPolicyID, policy);
                    break;
                }

                case SignaturePolicy.NO_POLICY:
                {
                    break;
                }
                }
                return(signedAttrs);
            }
            catch (NoSuchAlgorithmException ex)
            {
                throw new ProfileException(ex.Message);
            }
        }