package model.ca; import annotations.Assoc; 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 */ @Assoc(partOf = true, lowerBond = 0) 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"))})}); } }