aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/ui/IssueScreen.java28
-rw-r--r--src/main/ui/MainScreen.java35
-rw-r--r--src/main/ui/MgmtScreen.java30
-rw-r--r--src/main/ui/TemplateSetScreen.java21
-rw-r--r--src/main/ui/TemplatesScreen.java29
5 files changed, 143 insertions, 0 deletions
diff --git a/src/main/ui/IssueScreen.java b/src/main/ui/IssueScreen.java
index 93e1948..8376146 100644
--- a/src/main/ui/IssueScreen.java
+++ b/src/main/ui/IssueScreen.java
@@ -5,6 +5,9 @@ import model.ca.Template;
import model.csr.CertificationRequest;
import model.pki.cert.Certificate;
+/**
+ * The screen that accepts a CSR and template and allows user to change its properties and issue.
+ */
public class IssueScreen implements UIHandler {
private final JCA session;
@@ -29,6 +32,9 @@ public class IssueScreen implements UIHandler {
this.template = (Template) args[1];
}
+ /**
+ * EFFECTS: Print help.
+ */
@Override
public void help() {
System.out.print("show\tView the current certificate\n"
@@ -38,6 +44,9 @@ public class IssueScreen implements UIHandler {
+ "help\tPrint this message\n");
}
+ /**
+ * EFFECTS: Print pending cert info.
+ */
@Override
public void show() {
System.out.println("Requested Subject:\t" + incomingCSR.getCertificationRequestInfo().getSubject());
@@ -48,6 +57,10 @@ public class IssueScreen implements UIHandler {
System.out.println("Validity:\t" + template.getValidity() + " days");
}
+ /**
+ * EFFECTS: Issue the cert and log it.
+ * MODIFIES: session
+ */
@Override
public void commit() {
try {
@@ -60,6 +73,10 @@ public class IssueScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Set or unset the subject.
+ * MODIFIES: template
+ */
private void handleIssueSetSubject(String val) {
try {
template.setSubject(val);
@@ -68,6 +85,10 @@ public class IssueScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Set or unset the validity.
+ * MODIFIES: template
+ */
private void handleIssueSetValidity(String val) {
if (val == null) {
System.out.println("Cannot unset validity");
@@ -85,6 +106,10 @@ public class IssueScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Handle the set command.
+ * MODIFIES: template
+ */
private void handleIssueSet(String... args) {
if (args.length != 2 && args.length != 3) {
System.out.println("Usage: set <key> <value>");
@@ -128,6 +153,9 @@ public class IssueScreen implements UIHandler {
return Screen.MAIN;
}
+ /**
+ * EFFECTS: Return "/subj/ %"
+ */
@Override
public String getPS1() {
return String.format("/%s/ %%", template.getSubject() == null
diff --git a/src/main/ui/MainScreen.java b/src/main/ui/MainScreen.java
index 4d77022..b6e4372 100644
--- a/src/main/ui/MainScreen.java
+++ b/src/main/ui/MainScreen.java
@@ -18,6 +18,9 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Optional;
+/**
+ * The main screen that handles submenus (mgmt / issue / template), list certs, revoke certs, generate CRLs.
+ */
public class MainScreen implements UIHandler {
private final JCA session;
@@ -28,6 +31,9 @@ public class MainScreen implements UIHandler {
this.session = session;
}
+ /**
+ * EFFECTS: Print help
+ */
@Override
public void help() {
System.out.print("mgmt\tView and manage the CA certificate\n"
@@ -42,6 +48,9 @@ public class MainScreen implements UIHandler {
+ "help\tPrint this message\n");
}
+ /**
+ * EFFECTS: Print each issued cert in Subject Serial Status format.
+ */
@Override
public void show() {
session.getCa().getSigned().forEach(cert -> {
@@ -53,6 +62,9 @@ public class MainScreen implements UIHandler {
});
}
+ /**
+ * EFFECTS: Read the input CSR.
+ */
private CertificationRequest handleIssueInputCSR() {
try {
return new CertificationRequest(new BytesReader(session.handleInputPEM("CERTIFICATE REQUEST")),
@@ -63,6 +75,9 @@ public class MainScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Handle the issue command. Read CSR, find template, switch to issue screen.
+ */
private void handleIssue(String... args) {
if (!session.checkCA(true)) {
return;
@@ -104,6 +119,10 @@ public class MainScreen implements UIHandler {
return c.get();
}
+ /**
+ * EFFECTS: Handle the revoke command and log it.
+ * MODIFIES: session
+ */
private void handleRevoke(String... args) {
if (args.length < 3) {
System.out.println("Usage: revoke <serial> <reason>");
@@ -125,6 +144,9 @@ public class MainScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Export a cert to file
+ */
private void handleExport(String... args) {
if (args.length < 3) {
System.out.println("Usage: export <serial> <path>");
@@ -147,6 +169,10 @@ public class MainScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Issue a CRL and do audit log.
+ * MODIFIES: session
+ */
private void handleCRL() {
if (!session.checkCA(true)) {
return;
@@ -159,6 +185,9 @@ public class MainScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Handle commands
+ */
@Override
public void command(String... args) {
switch (args[0]) {
@@ -184,11 +213,17 @@ public class MainScreen implements UIHandler {
help();
}
+ /**
+ * EFFECTS: Exit the program
+ */
@Override
public Screen exit() {
return null;
}
+ /**
+ * EFFECTS: return "/ %"
+ */
@Override
public String getPS1() {
return "/ %";
diff --git a/src/main/ui/MgmtScreen.java b/src/main/ui/MgmtScreen.java
index 613aa50..1957c7e 100644
--- a/src/main/ui/MgmtScreen.java
+++ b/src/main/ui/MgmtScreen.java
@@ -16,6 +16,9 @@ import java.util.Arrays;
import java.util.Base64;
import java.util.BitSet;
+/**
+ * Manage the private key and CA certificate. It can print the public key, generate CSR, and install CA cert.
+ */
public class MgmtScreen implements UIHandler {
private final JCA session;
@@ -26,6 +29,9 @@ public class MgmtScreen implements UIHandler {
this.session = session;
}
+ /**
+ * EFFECTS: Print help
+ */
@Override
public void help() {
System.out.print("show\tView the public key and CA certificate\n"
@@ -55,6 +61,9 @@ public class MgmtScreen implements UIHandler {
.getSubjectPublicKey().getConvertedVal())));
}
+ /**
+ * EFFECT: Generate a CSR
+ */
private void handleCSR() {
if (!session.checkCA(false)) {
return;
@@ -68,6 +77,9 @@ public class MgmtScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Throw {@link ParseException} if the incoming cert is not v3.
+ */
private void validateCACertificateVersion(Certificate cert) throws ParseException {
if (cert.getCertificate().getVersion() == null
|| cert.getCertificate().getVersion().getLong() != TbsCertificate.VERSION_V3) {
@@ -75,6 +87,9 @@ public class MgmtScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Throw {@link ParseException} if the incoming cert does not have the matching public key.
+ */
private void validateCACertificatePublicKey(Certificate cert) throws ParseException {
final SubjectPublicKeyInfo expectedPKInfo = session.getCa().getCAPublicKeyInfo();
if (!Arrays.equals(cert.getCertificate().getSubjectPublicKeyInfo().getAlgorithm().getType().getInts(),
@@ -85,6 +100,9 @@ public class MgmtScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Throw {@link ParseException} if the incoming cert does not have cA = true in its basicConstraints.
+ */
private void validateCACertificateBasicConstraints(Certificate cert) throws ParseException {
final Extension basicConstraints = cert.getCertificate().getExtension(ObjectIdentifier.OID_BASIC_CONSTRAINTS);
if (basicConstraints == null
@@ -104,6 +122,9 @@ public class MgmtScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Throw {@link ParseException} if the incoming cert does not have valid key usages.
+ */
private void validateCACertificateKeyUsage(Certificate cert) throws ParseException {
final Extension keyUsage = cert.getCertificate().getExtension(ObjectIdentifier.OID_KEY_USAGE);
if (keyUsage == null
@@ -122,6 +143,9 @@ public class MgmtScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Handle the 'install' command. Read incoming certificate and validate it.
+ */
private void handleInstall() {
if (!session.checkCA(false)) {
return;
@@ -140,6 +164,9 @@ public class MgmtScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Handle commands.
+ */
@Override
public void command(String... args) {
switch (args[0]) {
@@ -163,6 +190,9 @@ public class MgmtScreen implements UIHandler {
return Screen.MAIN;
}
+ /**
+ * EFFECTS: return "/ca/ #"
+ */
@Override
public String getPS1() {
return "/ca/ #";
diff --git a/src/main/ui/TemplateSetScreen.java b/src/main/ui/TemplateSetScreen.java
index 9a31f50..42f393b 100644
--- a/src/main/ui/TemplateSetScreen.java
+++ b/src/main/ui/TemplateSetScreen.java
@@ -3,6 +3,9 @@ package ui;
import model.asn1.exceptions.ParseException;
import model.ca.Template;
+/**
+ * The screen that modifies the properties of a single template and add it to the store.
+ */
public class TemplateSetScreen implements UIHandler {
private final JCA session;
@@ -15,6 +18,9 @@ public class TemplateSetScreen implements UIHandler {
private Template template;
+ /**
+ * EFFECTS: Print help
+ */
@Override
public void help() {
System.out.println("show\tView the current template settings\n"
@@ -24,6 +30,10 @@ public class TemplateSetScreen implements UIHandler {
+ "help\tPrint this help message\n");
}
+ /**
+ * EFFECTS: Parse and set / unset the subject of the template
+ * MODIFIES: this#template
+ */
private void handleSetSubject(String val) {
try {
template.setSubject(val);
@@ -32,6 +42,10 @@ public class TemplateSetScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Set the validity of the template to the given integer
+ * MODIFIES: this#template
+ */
private void handleSetValidity(String val) {
if (val == null) {
System.out.println("Cannot unset validity");
@@ -49,6 +63,10 @@ public class TemplateSetScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Handle the `set` command.
+ * MODIFIES: this#template
+ */
private void handleSet(String... args) {
if (args.length != 2 && args.length != 3) {
System.out.println("Usage: set <key> <value>");
@@ -89,6 +107,9 @@ public class TemplateSetScreen implements UIHandler {
System.out.println("Validity:\t" + template.getValidity() + " days");
}
+ /**
+ * EFFECTS: Handle commands
+ */
@Override
public void command(String... args) {
switch (args[0]) {
diff --git a/src/main/ui/TemplatesScreen.java b/src/main/ui/TemplatesScreen.java
index 9b0bf3e..3bdbebe 100644
--- a/src/main/ui/TemplatesScreen.java
+++ b/src/main/ui/TemplatesScreen.java
@@ -2,6 +2,9 @@ package ui;
import model.ca.Template;
+/**
+ * The screen that allows users to list templates and manage them.
+ */
public class TemplatesScreen implements UIHandler {
private final JCA session;
@@ -12,6 +15,9 @@ public class TemplatesScreen implements UIHandler {
this.session = session;
}
+ /**
+ * EFFECTS: Print help.
+ */
@Override
public void help() {
System.out.println("show\tList templates\n"
@@ -23,6 +29,9 @@ public class TemplatesScreen implements UIHandler {
+ "help\tPrint this message");
}
+ /**
+ * EFFECTS: List templates in Name[ENABLED / DISABLED] Subject Validity format.
+ */
@Override
public void show() {
session.getTemplates().forEach(tem ->
@@ -33,6 +42,9 @@ public class TemplatesScreen implements UIHandler {
tem.getValidity()));
}
+ /**
+ * EFFECTS: Create a new template with the given name and switch to the template set screen.
+ */
private void handleAdd(String... args) {
if (args.length <= 1) {
System.out.println("Usage: add <name>");
@@ -47,6 +59,10 @@ public class TemplatesScreen implements UIHandler {
new Template(args[1], false, null, 30));
}
+ /**
+ * EFFECTS: Handle the enable / disable commands.
+ * MODIFIES: session
+ */
private void handleEnableDisable(boolean enable, String... args) {
if (args.length <= 1) {
System.out.printf("Usage: %s <template>\n", enable ? "enable" : "disable");
@@ -61,6 +77,10 @@ public class TemplatesScreen implements UIHandler {
session.log("A template was enabled / disabled.");
}
+ /**
+ * EFFECTS: Handle the delete command
+ * MODIFIES: session
+ */
private void handleDelete(String... args) {
if (args.length <= 1) {
System.out.println("Usage: delete <template>");
@@ -75,6 +95,9 @@ public class TemplatesScreen implements UIHandler {
session.log("A template was deleted.");
}
+ /**
+ * EFFECTS: Handle commands.
+ */
@Override
public void command(String... args) {
switch (args[0]) {
@@ -96,11 +119,17 @@ public class TemplatesScreen implements UIHandler {
}
}
+ /**
+ * EFFECTS: Go to main menu.
+ */
@Override
public Screen exit() {
return Screen.MAIN;
}
+ /**
+ * EFFECTS: Return "/templates/ %"
+ */
@Override
public String getPS1() {
return "/templates/ %";