aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/MainScreen.java
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@student.cs.ubc.ca>2023-11-28 18:19:39 -0800
committerYuuta Liang <yuutaw@student.cs.ubc.ca>2023-11-28 18:19:39 -0800
commit1073af21305360bd33903c533cdac57e9f936294 (patch)
tree2c2d9c343ffe2577286fb53e016f06f6cdc53cbf /src/main/ui/MainScreen.java
parente13adbb9a9146dd5ece890449e3cad958a502f86 (diff)
downloadjca-1073af21305360bd33903c533cdac57e9f936294.tar
jca-1073af21305360bd33903c533cdac57e9f936294.tar.gz
jca-1073af21305360bd33903c533cdac57e9f936294.tar.bz2
jca-1073af21305360bd33903c533cdac57e9f936294.zip
Move TUI and GUI into separate packages
Signed-off-by: Yuuta Liang <yuutaw@student.cs.ubc.ca>
Diffstat (limited to 'src/main/ui/MainScreen.java')
-rw-r--r--src/main/ui/MainScreen.java233
1 files changed, 0 insertions, 233 deletions
diff --git a/src/main/ui/MainScreen.java b/src/main/ui/MainScreen.java
deleted file mode 100644
index 8a85881..0000000
--- a/src/main/ui/MainScreen.java
+++ /dev/null
@@ -1,233 +0,0 @@
-package ui;
-
-import model.asn1.ASN1Object;
-import model.asn1.UtcTime;
-import model.asn1.exceptions.ParseException;
-import model.asn1.parsing.BytesReader;
-import model.ca.Template;
-import model.csr.CertificationRequest;
-import model.pki.cert.Certificate;
-import model.pki.crl.CertificateList;
-import model.pki.crl.Reason;
-import model.pki.crl.RevokedCertificate;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-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;
-
- /**
- * EFFECTS: Init with the parent session.
- */
- public MainScreen(JCA session) {
- this.session = session;
- }
-
- /**
- * EFFECTS: Print help
- */
- @Override
- public void help() {
- System.out.print("mgmt\tView and manage the CA certificate\n"
- + "issue\tIssue a certificate\n"
- + "show\tList all issued certificates\n"
- + "export\tExport a certificate to file (DER)\n"
- + "template\tManage templates\n"
- + "revoke\tRevoke a certificate\n"
- + "crl\t\tSign CRL\n"
- + "log\t\tView audit logs\n"
- + "exit\tExit\n"
- + "help\tPrint this message\n");
- }
-
- /**
- * EFFECTS: Print each issued cert in Subject Serial Status format.
- */
- @Override
- public void show() {
- session.getCa().getSigned().forEach(cert -> {
- System.out.printf("%s\t%d\t%s\n",
- cert.getCertificate().getSubject().toString(),
- cert.getCertificate().getSerialNumber().getLong(),
- session.getCa().getRevoked().stream().anyMatch(rev -> rev.getSerialNumber().getLong()
- == cert.getCertificate().getSerialNumber().getLong()) ? "REVOKED" : "OK");
- });
- }
-
- /**
- * EFFECTS: Read the input CSR.
- */
- private CertificationRequest handleIssueInputCSR() {
- try {
- return new CertificationRequest(new BytesReader(session.handleInputPEM("CERTIFICATE REQUEST")),
- false);
- } catch (ParseException e) {
- System.out.println(e.getMessage());
- return null;
- }
- }
-
- /**
- * EFFECTS: Handle the issue command. Read CSR, find template, switch to issue screen.
- */
- private void handleIssue(String... args) {
- if (!session.checkCA(true)) {
- return;
- }
- if (args.length <= 1) {
- System.out.println("Usage: issue <template>");
- return;
- }
- Template tmp = session.getCa().findTemplate(args[1], true);
- if (tmp == null) {
- System.out.println("Cannot find the template specified");
- return;
- }
- CertificationRequest req = handleIssueInputCSR();
- if (req != null) {
- session.setScreen(Screen.ISSUE, req, new Template(tmp.getName(),
- true,
- tmp.getSubject(),
- tmp.getValidity()));
- }
- }
-
- /**
- * EFFECTS: Find issued and not revoked certificate by serial. Return null if not found.
- */
- private Certificate findCertBySerial(int serial) {
- Optional<Certificate> c = session.getCa().getSigned()
- .stream()
- .filter(cert -> cert.getCertificate().getSerialNumber().getLong() == serial)
- .findFirst();
- if (c.isEmpty()) {
- System.out.println("Cannot find the certificate specified");
- return null;
- }
- if (session.getCa().getRevoked().stream().anyMatch(rev -> rev.getSerialNumber().getLong() == serial)) {
- System.out.println("The certificate has already been revoked.");
- return null;
- }
- 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>");
- return;
- }
- try {
- final Reason reason = Reason.valueOf(args[2]);
- int serial = Integer.parseInt(args[1]);
- Certificate c = findCertBySerial(serial);
- if (c == null) {
- return;
- }
- session.getCa().revoke(new RevokedCertificate(ASN1Object.TAG_SEQUENCE, null,
- c.getCertificate().getSerialNumber(),
- new UtcTime(UtcTime.TAG, null, ZonedDateTime.now(ZoneId.of("UTC"))), reason));
- session.save();
- } catch (IllegalArgumentException ignored) {
- System.out.println("Illegal serial number or reason");
- }
- }
-
- /**
- * EFFECTS: Export a cert to file
- */
- private void handleExport(String... args) {
- if (args.length < 3) {
- System.out.println("Usage: export <serial> <path>");
- return;
- }
- try {
- int serial = Integer.parseInt(args[1]);
- Certificate c = findCertBySerial(serial);
- if (c == null) {
- return;
- }
- final File fd = new File(args[2]);
- final OutputStream out = new FileOutputStream(fd);
- out.write(Utils.byteToByte(c.encodeDER()));
- out.close();
- } catch (IllegalArgumentException ignored) {
- System.out.println("Illegal serial number");
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
-
- /**
- * EFFECTS: Issue a CRL and do audit log.
- * MODIFIES: session
- */
- private void handleCRL() {
- if (!session.checkCA(true)) {
- return;
- }
- try {
- CertificateList crl = session.getCa().signCRL();
- session.save();
- System.out.println(Utils.toPEM(crl.encodeDER(), "X509 CRL"));
- } catch (Throwable e) {
- System.out.println(e.getMessage());
- }
- }
-
- /**
- * EFFECTS: Handle commands
- */
- @Override
- public void command(String... args) {
- switch (args[0]) {
- case "mgmt":
- session.setScreen(Screen.MGMT);
- return;
- case "issue":
- handleIssue(args);
- return;
- case "revoke":
- handleRevoke(args);
- return;
- case "export":
- handleExport(args);
- return;
- case "template":
- session.setScreen(Screen.TEMPLATES);
- return;
- case "crl":
- handleCRL();
- return;
- }
- help();
- }
-
- /**
- * EFFECTS: Exit the program
- */
- @Override
- public Screen exit() {
- return null;
- }
-
- /**
- * EFFECTS: return "/ %"
- */
- @Override
- public String getPS1() {
- return "/ %";
- }
-}