package ui; import model.asn1.exceptions.ParseException; import model.ca.CertificationAuthority; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.Scanner; /** * Main program */ public class JCA { /** * 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; /** * The CA */ private final CertificationAuthority ca; /** * The current screen. */ private UIHandler screen; /** * EFFECTS: Init with main screen and empty CA. No private key and no CA cert. * Throws {@link NoSuchAlgorithmException} when crypto issue happens. */ public JCA() throws NoSuchAlgorithmException, InvalidKeySpecException { 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.ca = new CertificationAuthority(); } /** * 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("No CA installed"); return false; } else if (!requireInstalled && ca.getCertificate() != null) { System.out.println("CA 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); } /** * 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].equals("log")) { ca.getLogs().forEach(System.out::println); return; } switch (args[0]) { case "help": screen.help(); System.out.println("log\tView audit logs"); 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 ", ca.getUser(), screen.getPS1()); } /** * EFFECTS: Run the program */ public void run() { printPS1(); final Scanner scanner = new Scanner(System.in); while (true) { String[] args = scanner.nextLine().split(" "); if (args.length <= 0 || args[0].isBlank()) { printPS1(); continue; } handleLine(args); } } public CertificationAuthority getCa() { return ca; } }