aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/pki/SubjectPublicKeyInfo.java
blob: ca3b9971440a031802441b4d67bab5f6a356afe1 (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
package model.pki;

import model.asn1.ASN1Object;
import model.asn1.BitString;
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;

/**
 * Represents the following ASN.1 structure:
 * <pre>
 *     SubjectPublicKeyInfo ::= SEQUENCE {
 *          algorithm AlgorithmIdentifier{{SupportedAlgorithms}},
 *          subjectPublicKey BIT STRING,
 *     ... }
 * </pre>
 * It represents the public key of a subject, in a certificate.
 */
public class SubjectPublicKeyInfo extends ASN1Object {
    /**
     * The algorithm used.
     */
    private final AlgorithmIdentifier algorithm;

    /**
     * The public key.
     */
    private final BitString subjectPublicKey;

    /**
     * EFFECTS: Init with tags, algorithm, subjectPublicKey. For tags, see {@link ASN1Object}.
     * REQUIRES: The public key should be a valid $algorithm key. Algorithm and publicKey should have default UNIVERSAL
     * tags (SEQUENCE and BIT STRING).
     */
    public SubjectPublicKeyInfo(Tag tag, Tag parentTag,
                                final AlgorithmIdentifier algorithm,
                                final BitString subjectPublicKey) {
        super(tag, parentTag);
        this.algorithm = algorithm;
        this.subjectPublicKey = subjectPublicKey;
    }

    /**
     * EFFECTS: Parse input DER.
     * 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 SubjectPublicKeyInfo(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
        this.algorithm = new AlgorithmIdentifier(encoded, false);
        this.algorithm.getTag().enforce(TAG_SEQUENCE);

        this.subjectPublicKey = new BitString(encoded, false);
        this.subjectPublicKey.getTag().enforce(BitString.TAG);
    }

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

    public AlgorithmIdentifier getAlgorithm() {
        return algorithm;
    }

    public BitString getSubjectPublicKey() {
        return subjectPublicKey;
    }
}