aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/ca/Template.java
blob: 44de676630f054078221a3374e3b5a6643f452e8 (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
package model.ca;

import model.asn1.ASN1Object;
import model.asn1.ObjectIdentifier;
import model.asn1.PrintableString;
import model.asn1.exceptions.ParseException;
import model.x501.AttributeTypeAndValue;
import model.x501.Name;
import model.x501.RelativeDistinguishedName;

/**
 * Represents a certificate template. Certificate templates are like policies the define part of the issued certificates
 * of what to have in common.
 */
public class Template {
    /**
     * Name of the template.
     */
    private final String name;

    /**
     * Whether the template is usable or not.
     */
    private final boolean enabled;

    /**
     * Subject of the issued certs. Null -> unspecified
     */
    private final Name subject;

    /**
     * Length of validity in days since the point of issue.
     */
    private final long validity;

    /**
     * EFFECTS: Init with all given parameters, and commonName will be converted into CN=commonName,C=CA if nonnull.
     * Throws {@link ParseException} if the commonName is invalid.
     */
    public Template(String name,
                    boolean enabled,
                    String commonName,
                    long validity) throws ParseException {
        this(name, enabled, commonName == null ? null : parseString(commonName), validity);
    }

    /**
     * EFFECTS: Init with all given parameters.
     * REQUIRES: name should be non-null; subject should be a valid X.509 subject name; validity should be > 0
     */
    public Template(String name,
                    boolean enabled,
                    Name subject,
                    long validity) {
        this.name = name;
        this.enabled = enabled;
        this.subject = subject;
        this.validity = validity;
    }

    public String getName() {
        return name;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public Name getSubject() {
        return subject;
    }

    public long getValidity() {
        return validity;
    }

    /**
     * EFFECTS: Convert the given commonName to RDN of CN=commonName,C=CA
     * Throws {@link ParseException} if the given commonName is invalid.
     */
    private static Name parseString(String commonName) throws ParseException {
        return new Name(ASN1Object.TAG_SEQUENCE, null, new RelativeDistinguishedName[]{
                new RelativeDistinguishedName(ASN1Object.TAG_SET, null, new AttributeTypeAndValue[]{
                        new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null,
                                new ObjectIdentifier(ObjectIdentifier.TAG, null,
                                        ObjectIdentifier.OID_CN),
                                new PrintableString(PrintableString.TAG, null, commonName))}),
                new RelativeDistinguishedName(ASN1Object.TAG_SET, null, new AttributeTypeAndValue[]{
                        new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null,
                                new ObjectIdentifier(ObjectIdentifier.TAG, null,
                                        ObjectIdentifier.OID_C),
                                new PrintableString(PrintableString.TAG, null, "CA"))})});
    }
}