<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />

ArchiveTimeStamp

using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using System; namespace Org.BouncyCastle.Asn1.Tsp { public class ArchiveTimeStamp : Asn1Encodable { private readonly AlgorithmIdentifier m_digestAlgorithm; private readonly Attributes m_attributes; private readonly Asn1Sequence m_reducedHashTree; private readonly Org.BouncyCastle.Asn1.Cms.ContentInfo m_timeStamp; public virtual Org.BouncyCastle.Asn1.Cms.ContentInfo TimeStamp => m_timeStamp; public static ArchiveTimeStamp GetInstance(object obj) { if (obj == null) return null; ArchiveTimeStamp archiveTimeStamp = obj as ArchiveTimeStamp; if (archiveTimeStamp != null) return archiveTimeStamp; return new ArchiveTimeStamp(Asn1Sequence.GetInstance(obj)); } public static ArchiveTimeStamp GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { return new ArchiveTimeStamp(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } public static ArchiveTimeStamp GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) { return new ArchiveTimeStamp(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); } private ArchiveTimeStamp(Asn1Sequence seq) { int count = seq.Count; int sequencePosition = 0; if (count < 1 || count > 4) throw new ArgumentException("Bad sequence size: " + count.ToString(), "seq"); m_digestAlgorithm = Asn1Utilities.ReadOptionalContextTagged(seq, ref sequencePosition, 0, false, AlgorithmIdentifier.GetTagged); m_attributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref sequencePosition, 1, false, Attributes.GetTagged); m_reducedHashTree = Asn1Utilities.ReadOptionalContextTagged(seq, ref sequencePosition, 2, false, Asn1Sequence.GetTagged); m_timeStamp = Org.BouncyCastle.Asn1.Cms.ContentInfo.GetInstance(seq[sequencePosition++]); if (sequencePosition != count) throw new ArgumentException("Unexpected elements in sequence", "seq"); } public ArchiveTimeStamp(AlgorithmIdentifier digestAlgorithm, PartialHashtree[] reducedHashTree, Org.BouncyCastle.Asn1.Cms.ContentInfo timeStamp) : this(digestAlgorithm, null, reducedHashTree, timeStamp) { } public ArchiveTimeStamp(Org.BouncyCastle.Asn1.Cms.ContentInfo timeStamp) : this(null, null, null, timeStamp) { } public ArchiveTimeStamp(AlgorithmIdentifier digestAlgorithm, Attributes attributes, PartialHashtree[] reducedHashTree, Org.BouncyCastle.Asn1.Cms.ContentInfo timeStamp) { m_digestAlgorithm = digestAlgorithm; m_attributes = attributes; m_reducedHashTree = ((reducedHashTree == null) ? null : new DerSequence(reducedHashTree)); if (timeStamp == null) throw new ArgumentNullException("timeStamp"); m_timeStamp = timeStamp; } public virtual AlgorithmIdentifier GetDigestAlgorithmIdentifier() { return m_digestAlgorithm ?? GetTimeStampInfo().MessageImprint.HashAlgorithm; } public virtual byte[] GetTimeStampDigestValue() { return GetTimeStampInfo().MessageImprint.GetHashedMessage(); } private TstInfo GetTimeStampInfo() { if (!CmsObjectIdentifiers.SignedData.Equals(m_timeStamp.ContentType)) throw new InvalidOperationException("cannot identify algorithm identifier for digest"); Org.BouncyCastle.Asn1.Cms.ContentInfo encapContentInfo = Org.BouncyCastle.Asn1.Cms.SignedData.GetInstance(m_timeStamp.Content).EncapContentInfo; if (!PkcsObjectIdentifiers.IdCTTstInfo.Equals(encapContentInfo.ContentType)) throw new InvalidOperationException("cannot parse time stamp"); return TstInfo.GetInstance(Asn1OctetString.GetInstance(encapContentInfo.Content).GetOctets()); } public virtual AlgorithmIdentifier DigestAlgorithm() { return m_digestAlgorithm; } public virtual PartialHashtree GetHashTreeLeaf() { if (m_reducedHashTree == null) return null; return PartialHashtree.GetInstance(m_reducedHashTree[0]); } public virtual PartialHashtree[] GetReducedHashTree() { return m_reducedHashTree?.MapElements(PartialHashtree.GetInstance); } public override Asn1Object ToAsn1Object() { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(4); asn1EncodableVector.AddOptionalTagged(false, 0, m_digestAlgorithm); asn1EncodableVector.AddOptionalTagged(false, 1, m_attributes); asn1EncodableVector.AddOptionalTagged(false, 2, m_reducedHashTree); asn1EncodableVector.Add(m_timeStamp); return new DerSequence(asn1EncodableVector); } } }