aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/pki/cert/Extensions.java
blob: 8c636b0aeffecd3686bbb917f82ae5c045556cb9 (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
package model.pki.cert;

import annotations.Assoc;
import model.asn1.ASN1Object;
import model.asn1.Encodable;
import model.asn1.Tag;
import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * Represents an X.509 certificate extensions list:
 * <pre>
 *     Extensions ::= SEQUENCE OF Extension
 * </pre>
 */
public class Extensions extends ASN1Object {
    /**
     * The extension array.
     */
    @Assoc(partOf = true)
    private final Extension[] extensions;

    /**
     * EFFECT: Initialize with the given tags and extensions. For tag and parentTag, consult
     * {@link ASN1Object}.
     * REQUIRES: Extensions should have SEQUENCE tag.
     */
    public Extensions(Tag tag, Tag parentTag, Extension[] extensions) {
        super(tag, parentTag);
        this.extensions = extensions;
    }

    /**
     * EFFECT: Parse the Name from input DER bytes. For details on parsing, refer to {@link ASN1Object}.
     * Throws {@link ParseException} for invalid input.
     * MODIFIES: this, encoded
     */
    public Extensions(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
        final List<Extension> list = new ArrayList<>();
        for (int i = 0; i < getLength(); ) {
            int index = encoded.getIndex();
            final Extension ext = new Extension(encoded, false);
            ext.getTag().enforce(TAG_SEQUENCE);
            list.add(ext);
            index = encoded.getIndex() - index;
            i += index;
        }
        this.extensions = list.toArray(new Extension[0]);
    }

    /**
     * EFFECTS: Encode the SEQUENCE OF into DER, keep order. RDNs will be encoded one-by-one.
     */
    @Override
    public Byte[] encodeValueDER() {
        return Stream.of(extensions)
                .map(Encodable::encodeDER)
                .flatMap(Arrays::stream)
                .toArray(Byte[]::new);
    }

    public Extension[] getExtensions() {
        return extensions;
    }
}