aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/JCA.java
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-14 05:12:06 +0800
committerYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-14 05:12:06 +0800
commit0bcc057e741af3fbc108f42b75f9d42f48f6a51e (patch)
treed638c81c0778554a8902efc59000e61db74060be /src/main/ui/JCA.java
parentf369da34cf9aca151df0150d90e651e6a88ee700 (diff)
downloadjca-0bcc057e741af3fbc108f42b75f9d42f48f6a51e.tar
jca-0bcc057e741af3fbc108f42b75f9d42f48f6a51e.tar.gz
jca-0bcc057e741af3fbc108f42b75f9d42f48f6a51e.tar.bz2
jca-0bcc057e741af3fbc108f42b75f9d42f48f6a51e.zip
Implement the CA
Signed-off-by: Yuuta Liang <yuutaw@students.cs.ubc.ca>
Diffstat (limited to 'src/main/ui/JCA.java')
-rw-r--r--src/main/ui/JCA.java203
1 files changed, 203 insertions, 0 deletions
diff --git a/src/main/ui/JCA.java b/src/main/ui/JCA.java
new file mode 100644
index 0000000..f9467ea
--- /dev/null
+++ b/src/main/ui/JCA.java
@@ -0,0 +1,203 @@
+package ui;
+
+import model.asn1.exceptions.ParseException;
+import model.ca.AuditLogEntry;
+import model.ca.CACertificate;
+import model.ca.Template;
+
+import java.nio.charset.StandardCharsets;
+import java.security.NoSuchAlgorithmException;
+import java.time.ZonedDateTime;
+import java.util.*;
+
+/**
+ * Main program
+ */
+public class JCA {
+ /**
+ * The current screen.
+ */
+ private UIHandler screen;
+
+ /**
+ * Instances of the five screens;
+ */
+ private final UIHandler mainScreen;
+ private final UIHandler mgmtScreen;
+ private final UIHandler issueScreen;
+ private final UIHandler templatesScreen;
+ private final UIHandler templateSetScreen;
+
+ /**
+ * Templates
+ */
+ private final List<Template> templates;
+
+ /**
+ * The CA
+ */
+ private final CACertificate ca;
+
+ /**
+ * Audit logs
+ */
+ private final List<AuditLogEntry> logs;
+
+ /**
+ * Current user
+ */
+ private final String user;
+
+ /**
+ * EFFECTS: Init with main screen, empty templates, logs, user 'yuuta', and generate a private key with no CA cert.
+ * Throws {@link NoSuchAlgorithmException} when crypto issue happens.
+ */
+ public JCA() throws NoSuchAlgorithmException {
+ this.mainScreen = new MainScreen(this);
+ this.mgmtScreen = new MgmtScreen(this);
+ this.issueScreen = new IssueScreen(this);
+ this.templatesScreen = new TemplatesScreen(this);
+ this.templateSetScreen = new TemplateSetScreen(this);
+
+ setScreen(Screen.MAIN);
+
+ this.templates = new ArrayList<>();
+ this.ca = new CACertificate();
+ this.logs = new ArrayList<>();
+ this.user = "yuuta";
+
+ this.ca.generateKey();
+ }
+
+ /**
+ * EFFECT: Checks if the CA is installed or not (according to the desired state) and print if not matching. Returns
+ * true if matching.
+ */
+ public boolean checkCA(boolean requireInstalled) {
+ if (requireInstalled && ca.getCertificate() == null) {
+ System.out.println("The CA is not installed yet");
+ return false;
+ } else if (!requireInstalled && ca.getCertificate() != null) {
+ System.out.println("The CA is already installed");
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * EFFECTS: Read PEM from stdin, matched the given tag.
+ * Throws {@link ParseException} if the input is incorrect.
+ */
+ public Byte[] handleInputPEM(String desiredTag) throws ParseException {
+ final Scanner scanner = new Scanner(System.in);
+ StringBuilder in = new StringBuilder();
+ while (true) {
+ final String line = scanner.nextLine();
+ in.append(line);
+ in.append("\n");
+ if (line.matches("-----END .*-----")) {
+ break;
+ }
+ }
+ return Utils.parsePEM(Utils.byteToByte(in.toString().getBytes(StandardCharsets.UTF_8)), desiredTag);
+ }
+
+ /**
+ * EFFECTS: Find the template based on name, or null if not found.
+ */
+ public Template findTemplate(String name, boolean requireEnabled) {
+ Optional<Template> opt = templates.stream().filter(temp -> {
+ if (requireEnabled && !temp.isEnabled()) {
+ return false;
+ }
+ return temp.getName().equals(name);
+ }).findFirst();
+ return opt.orElse(null);
+ }
+
+ /**
+ * EFFECT: Set the current screen with optional args. Exit the program when mode is null.
+ * MODIFIES: this
+ */
+ public void setScreen(Screen mode, Object... args) {
+ if (mode == null) {
+ System.exit(0);
+ }
+ switch (mode) {
+ case MAIN:
+ this.screen = mainScreen;
+ break;
+ case MGMT:
+ this.screen = mgmtScreen;
+ break;
+ case ISSUE:
+ this.screen = issueScreen;
+ break;
+ case TEMPLATES:
+ this.screen = templatesScreen;
+ break;
+ case TEMPLATE_SET:
+ this.screen = templateSetScreen;
+ break;
+ }
+ screen.enter(args);
+ }
+
+ private void handleLine(String... args) {
+ if (!args[0].isBlank()) {
+ switch (args[0]) {
+ case "help":
+ screen.help();
+ break;
+ case "show":
+ screen.show();
+ break;
+ case "commit":
+ screen.commit();
+ break;
+ case "exit":
+ setScreen(screen.exit());
+ break;
+ default:
+ screen.command(args);
+ break;
+ }
+ }
+ printPS1();
+ }
+
+ private void printPS1() {
+ System.out.printf("%s@JCA %s ", user, screen.getPS1());
+ }
+
+ /**
+ * EFFECT: Log an action to the audit log
+ * MODIFIES: this
+ */
+ public void log(String action) {
+ this.logs.add(new AuditLogEntry(user, ZonedDateTime.now(), action));
+ }
+
+ /**
+ * EFFECTS: Run the program
+ */
+ public void run() {
+ printPS1();
+ final Scanner scanner = new Scanner(System.in);
+ while (true) {
+ handleLine(scanner.nextLine().split(" "));
+ }
+ }
+
+ public List<Template> getTemplates() {
+ return templates;
+ }
+
+ public CACertificate getCa() {
+ return ca;
+ }
+
+ public List<AuditLogEntry> getLogs() {
+ return logs;
+ }
+}