summary refs log tree commit diff
path: root/crypto/src/asn1/tsp/TSTInfo.cs
blob: 14fac55c78262e2e4462d8aa2027f54fdf418751 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;

using Org.BouncyCastle.Asn1.X509;

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

        public static TstInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new TstInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));

        public static TstInfo GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new TstInfo(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));

        private readonly DerInteger m_version;
		private readonly DerObjectIdentifier m_policy;
		private readonly MessageImprint m_messageImprint;
		private readonly DerInteger m_serialNumber;
		private readonly Asn1GeneralizedTime m_genTime;
		private readonly Accuracy m_accuracy;
		private readonly DerBoolean m_ordering;
		private readonly DerInteger m_nonce;
		private readonly GeneralName m_tsa;
		private readonly X509Extensions m_extensions;

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

            m_version = DerInteger.GetInstance(seq[pos++]);
            m_policy = DerObjectIdentifier.GetInstance(seq[pos++]);
            m_messageImprint = MessageImprint.GetInstance(seq[pos++]);
            m_serialNumber = DerInteger.GetInstance(seq[pos++]);
            m_genTime = Asn1GeneralizedTime.GetInstance(seq[pos++]);
            m_accuracy = Asn1Utilities.ReadOptional(seq, ref pos, Accuracy.GetOptional);
            m_ordering = Asn1Utilities.ReadOptional(seq, ref pos, DerBoolean.GetOptional) ?? DerBoolean.False;
            m_nonce = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional);
            m_tsa = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, GeneralName.GetTagged); // CHOICE
            m_extensions = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, X509Extensions.GetTagged);

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

        // TODO[api] 'tsaPolicyId' => 'policy'
        public TstInfo(DerObjectIdentifier tsaPolicyId, MessageImprint messageImprint, DerInteger serialNumber,
            Asn1GeneralizedTime genTime, Accuracy accuracy, DerBoolean ordering, DerInteger nonce, GeneralName tsa,
            X509Extensions extensions)
        {
            m_version = DerInteger.One;
            m_policy = tsaPolicyId ?? throw new ArgumentNullException(nameof(tsaPolicyId));
            m_messageImprint = messageImprint ?? throw new ArgumentNullException(nameof(messageImprint));
            m_serialNumber = serialNumber ?? throw new ArgumentNullException(nameof(serialNumber));
            m_genTime = genTime ?? throw new ArgumentNullException(nameof(genTime));
            m_accuracy = accuracy;
            m_ordering = ordering ?? DerBoolean.False;
            m_nonce = nonce;
            m_tsa = tsa;
            m_extensions = extensions;
        }

        public DerInteger Version => m_version;

        public MessageImprint MessageImprint => m_messageImprint;

        public DerObjectIdentifier Policy => m_policy;

        public DerInteger SerialNumber => m_serialNumber;

        public Accuracy Accuracy => m_accuracy;

        public Asn1GeneralizedTime GenTime => m_genTime;

        public DerBoolean Ordering => m_ordering;

        public DerInteger Nonce => m_nonce;

        public GeneralName Tsa => m_tsa;

        public X509Extensions Extensions => m_extensions;

        /**
		 * <pre>
		 *
		 *     TstInfo ::= SEQUENCE  {
		 *        version                      INTEGER  { v1(1) },
		 *        policy                       TSAPolicyId,
		 *        messageImprint               MessageImprint,
		 *          -- MUST have the same value as the similar field in
		 *          -- TimeStampReq
		 *        serialNumber                 INTEGER,
		 *         -- Time-Stamping users MUST be ready to accommodate integers
		 *         -- up to 160 bits.
		 *        genTime                      GeneralizedTime,
		 *        accuracy                     Accuracy                 OPTIONAL,
		 *        ordering                     BOOLEAN             DEFAULT FALSE,
		 *        nonce                        INTEGER                  OPTIONAL,
		 *          -- MUST be present if the similar field was present
		 *          -- in TimeStampReq.  In that case it MUST have the same value.
		 *        tsa                          [0] GeneralName          OPTIONAL,
		 *        extensions                   [1] IMPLICIT Extensions   OPTIONAL  }
		 *
		 * </pre>
		 */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(10);
            v.Add(m_version, m_policy, m_messageImprint, m_serialNumber, m_genTime);
            v.AddOptional(m_accuracy);

            if (m_ordering.IsTrue)
            {
                v.Add(m_ordering);
            }

            v.AddOptional(m_nonce);
            v.AddOptionalTagged(true, 0, m_tsa); // CHOICE
            v.AddOptionalTagged(false, 1, m_extensions);
            return new DerSequence(v);
        }
    }
}