aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/csr/Attribute.java
blob: 26c327140cbd10620d814e076b576767e5eae150 (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
package model.csr;

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

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

/**
 * Implements the following:
 * <pre>
 *   Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
 *       type   ATTRIBUTE.&id({IOSet}),
 *       values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{@type})
 *   }
 * </pre>
 * <p>
 * Represents a key - values pair in the CSR attribute.
 */
public class Attribute extends ASN1Object {
    /**
     * The type of that attribute. For example, <pre>2.5.29.14</pre> is subjectKeyIdentifier.
     * It determines the format of the value.
     */
    @Assoc(partOf = true)
    private final ObjectIdentifier type;

    /**
     * Value set.
     */
    @Assoc(partOf = true)
    private final Values values;

    /**
     * EFFECT: Init the object with tag, parentTag, type, and values. For tag and parentTag, see {@link ASN1Object}.
     * REQUIRES: The values must match the type. Type tag should be UNIVERSAL OID, and values should be SET OF.
     */
    public Attribute(Tag tag, Tag parentTag,
                     ObjectIdentifier type, Values values) {
        super(tag, parentTag);
        this.type = type;
        this.values = values;
    }

    /**
     * EFFECTS: Parse input DER. Value is not checked against the type.
     * Throws {@link ASN1Object} if invalid:
     * - Any fields missing (info, algorithm, signature)
     * - Any fields having an incorrect tag (as seen in the ASN.1 definition)
     * - Any fields with encoding instructions that violate implicit / explicit encoding rules
     * - Other issues found during parsing the object, like early EOF (see {@link ASN1Object})
     * MODIFIES: this, encoded
     */
    public Attribute(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
        this.type = new ObjectIdentifier(encoded, false);
        this.type.getTag().enforce(ObjectIdentifier.TAG);

        this.values = new Values(encoded, false);
        this.values.getTag().enforce(TAG_SET);
    }

    /**
     * EFFECTS: Encode the fields into DER, in the order.
     */
    @Override
    public Byte[] encodeValueDER() {
        return Stream.of(Arrays.asList(type.encodeDER()),
                        Arrays.asList(values.encodeDER()))
                .flatMap(Collection::stream)
                .toArray(Byte[]::new);
    }

    public ObjectIdentifier getType() {
        return type;
    }

    public Values getValues() {
        return values;
    }
}