aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/pki/cert/TbsCertificate.java
blob: 6a6c19938e721be1b3a1c689f81666feb03336b5 (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package model.pki.cert;

import annotations.Assoc;
import model.asn1.ASN1Object;
import model.asn1.Int;
import model.asn1.Tag;
import model.asn1.TagClass;
import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;
import model.pki.AlgorithmIdentifier;
import model.pki.SubjectPublicKeyInfo;
import model.x501.Name;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Stream;

/**
 * Represents a X.509 certificate
 *
 * <pre>
 *  Certificate ::= SIGNED{TBSCertificate}
 *  TBSCertificate ::= SEQUENCE {
 *      version                 [0] Version DEFAULT v1,
 *      serialNumber            CertificateSerialNumber,
 *      signature               AlgorithmIdentifier{{SupportedAlgorithms}},
 *      issuer                  Name,
 *      validity                Validity,
 *      subject                 Name,
 *      subjectPublicKeyInfo    SubjectPublicKeyInfo,
 *      issuerUniqueIdentifier  [1] IMPLICIT UniqueIdentifier OPTIONAL,
 *      ...,
 *      [[2: -- if present, version shall be v2 or v3
 *      subjectUniqueIdentifier [2] IMPLICIT UniqueIdentifier OPTIONAL]],
 *      [[3: -- if present, version shall be v2 or v3
 *      extensions              [3] Extensions OPTIONAL]]
 *      -- If present, version shall be v3]]
 *  }
 *
 *  Version ::= INTEGER {v1(0), v2(1), v3(2)}
 *  CertificateSerialNumber ::= INTEGER
 *
 *  uniqueIdentifier ATTRIBUTE ::= {
 *      WITH SYNTAX UniqueIdentifier
 *      EQUALITY MATCHING RULE bitStringMatch
 *      LDAP-SYNTAX bitString.&id
 *      LDAP-NAME {"x500UniqueIdentifier"}
 *      ID id-at-uniqueIdentifier }
 *  UniqueIdentifier ::= BIT STRING
 * </pre>
 * <p>
 * NOTE that subjectUniqueIdentifier and issuerUniqueIdentifier are not supported.
 */
public class TbsCertificate extends ASN1Object {
    // Version ::= INTEGER {v1(0), v2(1), v3(2)}
    public static final int VERSION_V1 = 0;
    public static final int VERSION_V2 = 1;
    public static final int VERSION_V3 = 2;

    /**
     * The X.509 cert version. subjectUniqueIdentifier is v2 only, and extensions is v3 only.
     * <pre>
     *   [0] Version DEFAULT v1
     * </pre>
     */
    @Assoc(partOf = true, lowerBond = 0)
    private final Int version;

    /**
     * The serial number of that certificate that is unique across the CA.
     * <pre>
     *     serialNumber CertificateSerialNumber
     *     CertificateSerialNumber ::= INTEGER
     * </pre>
     */
    @Assoc(partOf = true)
    private final Int serialNumber;

    @Assoc(partOf = true)
    private final AlgorithmIdentifier signature;

    /**
     * The subject and issuer distinguished names.
     * <pre>
     *     issuer Name,
     *     subject Name
     * </pre>
     */
    @Assoc(partOf = true)
    private final Name issuer;

    /**
     * The validity period of that certificate.
     * Validity ::= SEQUENCE { notBefore Time, notAfter Time, ... }
     */
    @Assoc(partOf = true)
    private final Validity validity;

    /**
     * See the comments on issuer.
     */
    @Assoc(partOf = true)
    private final Name subject;

    /**
     * The public key of the certificate's holder.
     */
    @Assoc(partOf = true)
    private final SubjectPublicKeyInfo subjectPublicKeyInfo;

    /**
     * [3] Optional.
     */
    @Assoc(partOf = true, lowerBond = 0)
    private final Extensions extensions;

    /**
     * EFFECTS: Init with the given parameters. For tag and parentTag, see {@link ASN1Object}.
     * REQUIRES:
     * - Version must be V1, V2, or V3.
     * - {issuer,subject}UniqueIdentifier could be null.
     * - If {issuer,subject}UniqueIdentifier presents, version must be V2 or V3.
     * - Extensions could be null.
     * - If extensions presents, version must be V3.
     * - The signature should be valid.
     * - Field and Desired Tags:
     * version       CONTEXT SPECIFIC 0 (EXPLICIT), INTEGER, OPTIONAL DEFAULT v1
     * serialNumber  INTEGER
     * signature     SEQUENCE
     * issuer        SEQUENCE
     * validity      SEQUENCE
     * subject       SEQUENCE
     * subjectPublicKeyInfo  SEQUENCE
     * extensions    CONTEXT SPECIFIC 3 (EXPLICIT), SEQUENCE, OPTIONAL
     */
    public TbsCertificate(Tag tag, Tag parentTag,
                          final Int version,
                          final Int serialNumber,
                          final AlgorithmIdentifier signature,
                          final Name issuer,
                          final Validity validity,
                          final Name subject,
                          final SubjectPublicKeyInfo subjectPublicKeyInfo,
                          final Extensions extensions) {
        super(tag, parentTag);
        this.version = version;
        this.serialNumber = serialNumber;
        this.signature = signature;
        this.issuer = issuer;
        this.validity = validity;
        this.subject = subject;
        this.subjectPublicKeyInfo = subjectPublicKeyInfo;
        this.extensions = extensions;
    }

    /**
     * EFFECTS: Parse input DER.
     * Throws {@link ASN1Object} if invalid:
     * - Any fields missing
     * - Any fields having an incorrect parent / inner tag (as seen in the ASN.1 definition)
     * - Any fields with encoding instructions that violate implicit / explicit encoding rules
     * - extensions are specified, but the version is v1 or v2
     * - Other issues found during parsing the object, like early EOF (see {@link ASN1Object})
     * MODIFIES: this, encoded
     */
    public TbsCertificate(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
        int i = encoded.getIndex();
        if (encoded.detectTag(new Tag(TagClass.CONTEXT_SPECIFIC, true, 0))) {
            this.version = new Int(encoded, true);
        } else {
            this.version = null;
        }
        this.serialNumber = new Int(encoded, false);
        this.signature = new AlgorithmIdentifier(encoded, false);
        this.issuer = new Name(encoded, false);
        this.validity = new Validity(encoded, false);
        this.subject = new Name(encoded, false);
        this.subjectPublicKeyInfo = new SubjectPublicKeyInfo(encoded, false);
        if (encoded.detectTag(new Tag(TagClass.CONTEXT_SPECIFIC, true, 3))) {
            this.extensions = new Extensions(encoded, true);
        } else {
            // Enforce the extensions tag - nothing else should be here.
            if (Integer.compareUnsigned(getLength(), (encoded.getIndex() - i)) != 0) {
                throw new ParseException("Unexpected objects after.");
            }
            this.extensions = null;
        }
        enforceInput();
        enforceVersion();
    }

    /**
     * EFFECTS: Throw {@link ParseException} if any field have illegal tags.
     */
    private void enforceInput() throws ParseException {
        if (this.version != null) {
            this.version.getTag().enforce(Int.TAG);
            this.version.getParentTag().enforce(new Tag(TagClass.CONTEXT_SPECIFIC, true, 0));
        }
        this.serialNumber.getTag().enforce(Int.TAG);
        this.signature.getTag().enforce(TAG_SEQUENCE);
        this.issuer.getTag().enforce(TAG_SEQUENCE);
        this.validity.getTag().enforce(TAG_SEQUENCE);
        this.subject.getTag().enforce(TAG_SEQUENCE);
        this.subjectPublicKeyInfo.getTag().enforce(TAG_SEQUENCE);
        if (extensions != null) {
            this.extensions.getTag().enforce(TAG_SEQUENCE);
            this.extensions.getParentTag().enforce(new Tag(TagClass.CONTEXT_SPECIFIC, true, 3));
        }
    }

    /**
     * EFFECTS: Throw {@link ParseException} if the version is incorrect.
     */
    private void enforceVersion() throws ParseException {
        if (version != null
                && (version.getLong() < VERSION_V1 || version.getLong() > VERSION_V3)) {
            throw new ParseException("Illegal certificate version: " + version.getLong());
        }
        if (extensions != null && (version == null || version.getLong() != VERSION_V3)) {
            throw new ParseException("Extensions present. The version must be v3 or above.");
        }
    }

    /**
     * EFFECTS: Encode into ordered DER.
     */
    @Override
    public Byte[] encodeValueDER() {
        return Stream.of(version == null ? Collections.<Byte>emptyList() : Arrays.asList(version.encodeDER()),
                        Arrays.asList(serialNumber.encodeDER()),
                        Arrays.asList(signature.encodeDER()),
                        Arrays.asList(issuer.encodeDER()),
                        Arrays.asList(validity.encodeDER()),
                        Arrays.asList(subject.encodeDER()),
                        Arrays.asList(subjectPublicKeyInfo.encodeDER()),
                        extensions == null ? Collections.<Byte>emptyList()
                                : Arrays.asList(extensions.encodeDER()))
                .flatMap(Collection::stream)
                .toArray(Byte[]::new);
    }

    /**
     * EFFECT: Get the extension by ID. If the certificate is V1 or does not have any extensions or does not have the
     * specified extension, null is returned.
     * REQUIRES: extnId should be a valid X.509 certificate extension ID.
     */
    public Extension getExtension(Integer[] extnId) {
        if (extensions == null) {
            return null;
        }
        return Arrays.stream(extensions.getExtensions())
                .filter(extn -> Arrays.equals(extnId, extn.getExtnId().getInts()))
                .findFirst()
                .orElse(null);
    }

    public Int getVersion() {
        return version;
    }

    public Int getSerialNumber() {
        return serialNumber;
    }

    public AlgorithmIdentifier getSignature() {
        return signature;
    }

    public Name getIssuer() {
        return issuer;
    }

    public Validity getValidity() {
        return validity;
    }

    public Name getSubject() {
        return subject;
    }

    public SubjectPublicKeyInfo getSubjectPublicKeyInfo() {
        return subjectPublicKeyInfo;
    }

    public Extensions getExtensions() {
        return extensions;
    }
}