aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/IssueScreen.java
blob: 93e1948df7bd3daefe6ad3f1a331d9b4ce27e5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ui;

import model.asn1.exceptions.ParseException;
import model.ca.Template;
import model.csr.CertificationRequest;
import model.pki.cert.Certificate;

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];
    }

    @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");
    }

    @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");
    }

    @Override
    public void commit() {
        try {
            Certificate certificate = session.getCa().signCert(incomingCSR.getCertificationRequestInfo(), template);
            System.out.println(Utils.toPEM(certificate.encodeDER(), "CERTIFICATE"));
            session.log("A certificate was issued.");
            session.setScreen(Screen.MAIN);
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        }
    }

    private void handleIssueSetSubject(String val) {
        try {
            template.setSubject(val);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
    }

    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.setValidity(i);
        } catch (NumberFormatException ignored) {
            System.out.println("Invalid validity days");
        }
    }

    private void handleIssueSet(String... args) {
        if (args.length != 2 && args.length != 3) {
            System.out.println("Usage: set <key> <value>");
            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) {
        switch (args[0]) {
            case "set":
                handleIssueSet(args);
                break;
            default:
                help();
                break;
        }
    }

    /**
     * EFFECTS: Clear the certificates and return main.
     * MODIFIES: this
     */
    @Override
    public Screen exit() {
        incomingCSR = null;
        template = null;
        return Screen.MAIN;
    }

    @Override
    public String getPS1() {
        return String.format("/%s/ %%", template.getSubject() == null
                ? incomingCSR.getCertificationRequestInfo().getSubject()
                : template.getSubject());
    }
}