summary refs log tree commit diff
path: root/crypto/src/asn1/x509/TBSCertificateStructure.cs
blob: e1daef3ffc6594fdfc0a2047418895b63dd32f6a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.X509
{
    /**
     * The TbsCertificate object.
     * <pre>
     * TbsCertificate ::= Sequence {
     *      version          [ 0 ]  Version DEFAULT v1(0),
     *      serialNumber            CertificateSerialNumber,
     *      signature               AlgorithmIdentifier,
     *      issuer                  Name,
     *      validity                Validity,
     *      subject                 Name,
     *      subjectPublicKeyInfo    SubjectPublicKeyInfo,
     *      issuerUniqueID    [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
     *      subjectUniqueID   [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
     *      extensions        [ 3 ] Extensions OPTIONAL
     *      }
     * </pre>
     * <p>
     * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
     * will parse them, but you really shouldn't be creating new ones.</p>
     */
	public class TbsCertificateStructure
		: Asn1Encodable
	{
		internal Asn1Sequence            seq;
		internal DerInteger              version;
		internal DerInteger              serialNumber;
		internal AlgorithmIdentifier     signature;
		internal X509Name                issuer;
		internal Time                    startDate, endDate;
		internal X509Name                subject;
		internal SubjectPublicKeyInfo    subjectPublicKeyInfo;
		internal DerBitString            issuerUniqueID;
		internal DerBitString            subjectUniqueID;
		internal X509Extensions          extensions;

        public static TbsCertificateStructure GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is TbsCertificateStructure tbsCertificateStructure)
                return tbsCertificateStructure;
            return new TbsCertificateStructure(Asn1Sequence.GetInstance(obj));
        }

        public static TbsCertificateStructure GetInstance(Asn1TaggedObject obj, bool explicitly)
        {
            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
        }

        private TbsCertificateStructure(Asn1Sequence seq)
		{
			int seqStart = 0;

			this.seq = seq;

			//
			// some certificates don't include a version number - we assume v1
			//
			if (seq[0] is Asn1TaggedObject taggedObject)
			{
				version = DerInteger.GetInstance(taggedObject, true);
			}
			else
			{
				seqStart = -1;          // field 0 is missing!
				version = DerInteger.Zero;
			}

            bool isV1 = false;
            bool isV2 = false;

            if (version.HasValue(0))
            {
                isV1 = true;
            }
            else if (version.HasValue(1))
            {
                isV2 = true;
            }
            else if (!version.HasValue(2))
            {
                throw new ArgumentException("version number not recognised");
            }

			serialNumber = DerInteger.GetInstance(seq[seqStart + 1]);

			signature = AlgorithmIdentifier.GetInstance(seq[seqStart + 2]);
			issuer = X509Name.GetInstance(seq[seqStart + 3]);

			//
			// before and after dates
			//
			Asn1Sequence  dates = (Asn1Sequence)seq[seqStart + 4];

			startDate = Time.GetInstance(dates[0]);
			endDate = Time.GetInstance(dates[1]);

			subject = X509Name.GetInstance(seq[seqStart + 5]);

			//
			// public key info.
			//
			subjectPublicKeyInfo = SubjectPublicKeyInfo.GetInstance(seq[seqStart + 6]);

            int extras = seq.Count - (seqStart + 6) - 1;
            if (extras != 0 && isV1)
                throw new ArgumentException("version 1 certificate contains extra data");

            while (extras > 0)
			{
                Asn1TaggedObject extra = Asn1TaggedObject.GetInstance(seq[seqStart + 6 + extras]);
				switch (extra.TagNo)
				{
				case 1:
                {
					issuerUniqueID = DerBitString.GetInstance(extra, false);
					break;
                }
                case 2:
                {
                    subjectUniqueID = DerBitString.GetInstance(extra, false);
                    break;
                }
				case 3:
                {
                    if (isV2)
                        throw new ArgumentException("version 2 certificate cannot contain extensions");

                    extensions = X509Extensions.GetInstance(Asn1Sequence.GetInstance(extra, true));
					break;
                }
                default:
                {
                    throw new ArgumentException("Unknown tag encountered in structure: " + extra.TagNo);
                }
                }
                extras--;
			}
		}

		public int Version
		{
            get { return version.IntValueExact + 1; }
		}

		public DerInteger VersionNumber
		{
			get { return version; }
		}

		public DerInteger SerialNumber
		{
			get { return serialNumber; }
		}

		public AlgorithmIdentifier Signature
		{
			get { return signature; }
		}

		public X509Name Issuer
		{
			get { return issuer; }
		}

		public Time StartDate
		{
			get { return startDate; }
		}

		public Time EndDate
		{
			get { return endDate; }
		}

		public X509Name Subject
		{
			get { return subject; }
		}

		public SubjectPublicKeyInfo SubjectPublicKeyInfo
		{
			get { return subjectPublicKeyInfo; }
		}

		public DerBitString IssuerUniqueID
		{
			get { return issuerUniqueID; }
        }

		public DerBitString SubjectUniqueID
        {
			get { return subjectUniqueID; }
        }

		public X509Extensions Extensions
        {
			get { return extensions; }
        }

		public override Asn1Object ToAsn1Object()
        {
            string property = Platform.GetEnvironmentVariable("Org.BouncyCastle.X509.Allow_Non-DER_TBSCert");
            if (null == property || Platform.EqualsIgnoreCase("true", property))
                return seq;

            Asn1EncodableVector v = new Asn1EncodableVector(8);

            // DEFAULT Zero
            if (!version.HasValue(0))
            {
                v.Add(new DerTaggedObject(true, 0, version));
            }

            v.Add(serialNumber, signature, issuer);

			//
			// before and after dates
			//
			v.Add(new DerSequence(startDate, endDate));

            if (subject != null)
            {
                v.Add(subject);
            }
            else
            {
                v.Add(DerSequence.Empty);
            }

            v.Add(subjectPublicKeyInfo);

            // Note: implicit tag
			v.AddOptionalTagged(false, 1, issuerUniqueID);

			// Note: implicit tag
			v.AddOptionalTagged(false, 2, subjectUniqueID);

			v.AddOptionalTagged(true, 3, extensions);

            return new DerSequence(v);
        }
    }
}