Esempio n. 1
0
        /// <summary>
        ///     Extend signature to publication.
        /// </summary>
        /// <param name="calendarHashChain">extended calendar hash chain</param>
        /// <param name="publicationRecord">extended publication record</param>
        /// <param name="signatureFactory">signature factory to be used when creating extended signature</param>
        /// <returns>extended KSI signature</returns>
        public IKsiSignature Extend(CalendarHashChain calendarHashChain, PublicationRecordInSignature publicationRecord, IKsiSignatureFactory signatureFactory = null)
        {
            Logger.Debug("Extending KSI signature.");

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

            if (CalendarHashChain != null && !CalendarHashChain.AreRightLinksEqual(calendarHashChain))
            {
                throw new KsiException("Right links of signature calendar hash chain and extended calendar hash chain do not match");
            }

            if (publicationRecord == null)
            {
                publicationRecord = new PublicationRecordInSignature(false, false, calendarHashChain.PublicationData);
            }

            if (signatureFactory == null)
            {
                signatureFactory = new KsiSignatureFactory();
            }

            using (TlvWriter writer = new TlvWriter(new MemoryStream()))
            {
                foreach (ITlvTag childTag in this)
                {
                    switch (childTag.Type)
                    {
                    case Constants.CalendarHashChain.TagType:
                    case Constants.CalendarAuthenticationRecord.TagType:
                    case Constants.PublicationRecord.TagTypeInSignature:
                        break;

                    default:
                        writer.WriteTag(childTag);
                        break;
                    }
                }

                writer.WriteTag(calendarHashChain);
                writer.WriteTag(publicationRecord);

                try
                {
                    IKsiSignature signature = signatureFactory.CreateByContent(((MemoryStream)writer.BaseStream).ToArray(), InputHash);
                    Logger.Debug("Extending KSI signature successful.");

                    return(signature);
                }
                catch (TlvException e)
                {
                    Logger.Warn("Extending KSI signature failed: {0}", e);
                    throw;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Compare right links if they are equal.
        /// </summary>
        /// <param name="calendarHashChain">calendar hash chain to compare to</param>
        /// <returns>true if right links are equal and in same order</returns>
        public bool AreRightLinksEqual(CalendarHashChain calendarHashChain)
        {
            IEnumerator <Link> currentEnumerator  = GetRightLinksEnumerable(_chain).GetEnumerator();
            IEnumerator <Link> externalEnumerator = GetRightLinksEnumerable(calendarHashChain._chain).GetEnumerator();
            Link currentLink  = currentEnumerator.MoveNext() ? currentEnumerator.Current : null;
            Link externalLink = externalEnumerator.MoveNext() ? externalEnumerator.Current : null;

            while (currentLink != null || externalLink != null)
            {
                if (currentLink != externalLink)
                {
                    return(false);
                }

                currentLink  = currentEnumerator.MoveNext() ? currentEnumerator.Current : null;
                externalLink = externalEnumerator.MoveNext() ? externalEnumerator.Current : null;
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Create KSI signature instance from tlv tags
        /// </summary>
        /// <param name="aggregationHashChains">Aggregation hash chain tlv elements</param>
        /// <param name="calendarHashChain">Calendar hash chain tlv element</param>
        /// <param name="calendarAuthenticationRecord">Calendar authentication record tlv element</param>
        /// <param name="publicationRecord">Publication record tlv element</param>
        /// <param name="rfc3161Record">RFC3161 record tlv element</param>
        /// <param name="hash">Signed hash</param>
        /// <returns></returns>
        public IKsiSignature Create(ICollection <AggregationHashChain> aggregationHashChains, CalendarHashChain calendarHashChain,
                                    CalendarAuthenticationRecord calendarAuthenticationRecord, PublicationRecordInSignature publicationRecord,
                                    Rfc3161Record rfc3161Record, DataHash hash)
        {
            List <ITlvTag> childTags = new List <ITlvTag>();

            if (rfc3161Record != null)
            {
                childTags.Add(rfc3161Record);
            }

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

            foreach (AggregationHashChain childTag in aggregationHashChains)
            {
                childTags.Add(childTag);
            }

            if (calendarHashChain != null)
            {
                childTags.Add(calendarHashChain);
            }

            if (publicationRecord != null)
            {
                childTags.Add(publicationRecord);
            }

            if (calendarAuthenticationRecord != null)
            {
                childTags.Add(calendarAuthenticationRecord);
            }

            return(CreateAndVerify(childTags.ToArray(), hash));
        }
Esempio n. 4
0
 /// <summary>
 ///     Extend signature to publication.
 /// </summary>
 /// <param name="calendarHashChain">extended calendar hash chain</param>
 /// <param name="publicationRecord">extended publication record</param>
 /// <param name="signatureFactory">signature factory to be used when creating extended signature</param>
 /// <returns>extended KSI signature</returns>
 public IKsiSignature Extend(CalendarHashChain calendarHashChain, PublicationRecordInPublicationFile publicationRecord, IKsiSignatureFactory signatureFactory = null)
 {
     return(Extend(calendarHashChain, publicationRecord?.ConvertToPublicationRecordInSignature(), signatureFactory));
 }
Esempio n. 5
0
 /// <summary>
 ///     Extend KSI signature with given calendar hash chain.
 /// </summary>
 /// <param name="calendarHashChain">calendar hash chain</param>
 /// <param name="signatureFactory">signature factory to be used when creating extended signature</param>
 /// <returns>extended KSI signature</returns>
 public IKsiSignature Extend(CalendarHashChain calendarHashChain, IKsiSignatureFactory signatureFactory = null)
 {
     return(Extend(calendarHashChain, (PublicationRecordInSignature)null, signatureFactory));
 }