From d7ff9d5e217873609d79efe279f2634e3a3dd8b4 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Wed, 25 Oct 2023 03:30:45 +0800 Subject: Refactor: move all logics into CertificationAuthority Signed-off-by: Yuuta Liang --- src/main/model/ca/Template.java | 53 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 31 deletions(-) (limited to 'src/main/model/ca/Template.java') diff --git a/src/main/model/ca/Template.java b/src/main/model/ca/Template.java index af751dc..84e639e 100644 --- a/src/main/model/ca/Template.java +++ b/src/main/model/ca/Template.java @@ -16,22 +16,33 @@ public class Template { /** * Name of the template. */ - private String name; + private final String name; /** * Whether the template is usable or not. */ - private boolean enabled; + private final boolean enabled; /** * Subject of the issued certs. Null -> unspecified */ - private Name subject; + private final Name subject; /** * Length of validity in days since the point of issue. */ - private long validity; + private final long validity; + + /** + * EFFECTS: Init with all given parameters, and commonName will be converted into CN=commonName,C=CA. + * Throws {@link ParseException} if the commonName is invalid. + */ + public Template(String name, + boolean enabled, + String commonName, + long validity) throws ParseException { + this(name, enabled, parseString(commonName), validity); + } /** * EFFECTS: Init with all given parameters. @@ -51,36 +62,24 @@ public class Template { return name; } - public void setName(String name) { - this.name = name; - } - public boolean isEnabled() { return enabled; } - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - public Name getSubject() { return subject; } - public void setSubject(Name subject) { - this.subject = subject; + public long getValidity() { + return validity; } /** - * EFFECTS: Set the subject to CN=commonName,C=CA - * Throws {@link ParseException} if commonName is not a valid PrintableString + * EFFECTS: Convert the given commonName to RDN of CN=commonName,C=CA + * Throws {@link ParseException} if the given commonName is invalid. */ - public void setSubject(String commonName) throws ParseException { - if (commonName == null) { - this.subject = null; - return; - } - setSubject(new Name(ASN1Object.TAG_SEQUENCE, null, new RelativeDistinguishedName[]{ + 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, @@ -90,14 +89,6 @@ public class Template { new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null, new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_C), - new PrintableString(PrintableString.TAG, null, "CA"))})})); - } - - public long getValidity() { - return validity; - } - - public void setValidity(long validity) { - this.validity = validity; + new PrintableString(PrintableString.TAG, null, "CA"))})}); } } -- cgit v1.2.3