package ui; import model.asn1.exceptions.ParseException; import model.ca.Template; import model.csr.CertificationRequest; import model.pki.cert.Certificate; import model.x501.Name; /** * 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; private Template template; private CertificationRequest incomingCSR; /** * EFFECTS: Init with the session. */ public IssueScreen(JCA session) { this.session = session; } /** * EFFECTS: Set current template and CSR in use by args. * REQUIRES: args.length = 2, args[0] instanceof CertificateRequest, args[1] instanceof Template * MODIFIES: args[1] */ @Override public void enter(Object... args) { this.incomingCSR = (CertificationRequest) args[0]; this.template = (Template) args[1]; } /** * EFFECTS: Print help. */ @Override public void help() { System.out.print("show\tView the current certificate\n" + "set\tSet properties or template\n" + "commit\tIssue the certificate\n" + "exit\tDiscard and go to main menu\n" + "help\tPrint this message\n"); } /** * EFFECTS: Print pending cert info. */ @Override public void show() { System.out.println("Requested Subject:\t" + incomingCSR.getCertificationRequestInfo().getSubject()); System.out.println("Subject:\t" + (template.getSubject() == null ? incomingCSR.getCertificationRequestInfo().getSubject() : template.getSubject())); System.out.println("Template:\t" + template.getName()); System.out.println("Validity:\t" + template.getValidity() + " days"); } /** * EFFECTS: Issue the cert and log it. * MODIFIES: session */ @Override public void commit() { try { Certificate certificate = session.getCa().signCert(incomingCSR.getCertificationRequestInfo(), template); session.save(); System.out.println(Utils.toPEM(certificate.encodeDER(), "CERTIFICATE")); session.setScreen(Screen.MAIN); } catch (Throwable e) { System.out.println(e.getMessage()); } } /** * EFFECTS: Set or unset the subject. * MODIFIES: this */ private void handleIssueSetSubject(String val) { try { if (val == null) { template = new Template(template.getName(), template.isEnabled(), (Name) null, template.getValidity()); } else { template = new Template(template.getName(), template.isEnabled(), val, template.getValidity()); } } catch (ParseException e) { System.out.println(e.getMessage()); } } /** * EFFECTS: Set or unset the validity. * MODIFIES: this */ private void handleIssueSetValidity(String val) { if (val == null) { System.out.println("Cannot unset validity"); return; } try { long i = Long.parseLong(val); if (i <= 0) { System.out.println("Invalid validity days"); return; } template = new Template(template.getName(), template.isEnabled(), template.getSubject(), i); } catch (NumberFormatException ignored) { System.out.println("Invalid validity days"); } } /** * EFFECTS: Handle the set command. * MODIFIES: this */ private void handleIssueSet(String... args) { if (args.length != 2 && args.length != 3) { System.out.println("Usage: set "); System.out.println("Supported keys: subject validity"); return; } String val = args.length == 3 ? args[2] : null; switch (args[1]) { case "subject": handleIssueSetSubject(val); break; case "validity": handleIssueSetValidity(val); break; default: System.out.println("Unknown key"); break; } } @Override public void command(String... args) { if (args[0].equals("set")) { handleIssueSet(args); } else { help(); } } /** * EFFECTS: Clear the certificates and return main. * MODIFIES: this */ @Override public Screen exit() { incomingCSR = null; template = null; return Screen.MAIN; } /** * EFFECTS: Return "/subj/ %" */ @Override public String getPS1() { return String.format("/%s/ %%", template.getSubject() == null ? incomingCSR.getCertificationRequestInfo().getSubject() : template.getSubject()); } }