summary refs log tree commit diff
path: root/crypto/src/asn1/cms/TimeStampedData.cs
blob: 53a8334ecb99f662e184c1e9408e570ec3294f14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;

namespace Org.BouncyCastle.Asn1.Cms
{
	public class TimeStampedData
		: Asn1Encodable
	{
        public static TimeStampedData GetInstance(object obj)
        {
			if (obj == null)
				return null;
			if (obj is TimeStampedData timeStampedData)
				return timeStampedData;
            return new TimeStampedData(Asn1Sequence.GetInstance(obj));
        }

        public static TimeStampedData GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return new TimeStampedData(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

        private readonly DerInteger m_version;
        private readonly DerIA5String m_dataUri;
        private readonly MetaData m_metaData;
        private readonly Asn1OctetString m_content;
        private readonly Evidence m_temporalEvidence;

        public TimeStampedData(DerIA5String dataUri, MetaData metaData, Asn1OctetString content,
            Evidence temporalEvidence)
        {
            m_version = DerInteger.One;
            m_dataUri = dataUri;
            m_metaData = metaData;
            m_content = content;
            m_temporalEvidence = temporalEvidence ?? throw new ArgumentNullException(nameof(temporalEvidence));
        }

        private TimeStampedData(Asn1Sequence seq)
		{
            int count = seq.Count, pos = 0;
            if (count < 2 || count > 5)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            m_version = DerInteger.GetInstance(seq[pos++]);
            m_dataUri = Asn1Utilities.ReadOptional(seq, ref pos, DerIA5String.GetOptional);
            m_metaData = Asn1Utilities.ReadOptional(seq, ref pos, MetaData.GetOptional);
            m_content = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional);
            m_temporalEvidence = Evidence.GetInstance(seq[pos++]);

            if (pos != count)
                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
        }

        public virtual DerIA5String DataUri => m_dataUri;

        public MetaData MetaData => m_metaData;

        public Asn1OctetString Content => m_content;

        public Evidence TemporalEvidence => m_temporalEvidence;

		/**
		 * <pre>
		 * TimeStampedData ::= SEQUENCE {
		 *   version              INTEGER { v1(1) },
		 *   dataUri              IA5String OPTIONAL,
		 *   metaData             MetaData OPTIONAL,
		 *   content              OCTET STRING OPTIONAL,
		 *   temporalEvidence     Evidence
		 * }
		 * </pre>
		 * @return
		 */
		public override Asn1Object ToAsn1Object()
		{
            Asn1EncodableVector v = new Asn1EncodableVector(5);
            v.Add(m_version);
			v.AddOptional(m_dataUri, m_metaData, m_content);
			v.Add(m_temporalEvidence);
			return new BerSequence(v);
		}
	}
}