aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/MainScreen.java
blob: 69cb32c2d6f3f8cbc37cee2172dbe543f89f2fcc (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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.Reason;
import model.pki.crl.RevokedCertificate;

import java.io.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Optional;

public class MainScreen implements UIHandler {
    private final JCA session;

    /**
     * EFFECTS: Init with the parent session.
     */
    public MainScreen(JCA session) {
        this.session = session;
    }

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

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

    private CertificationRequest handleIssueInputCSR() {
        try {
            return new CertificationRequest(new BytesReader(session.handleInputPEM("CERTIFICATE REQUEST")),
                    false);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    private void handleIssue(String... args) {
        if (!session.checkCA(true)) {
            return;
        }
        if (args.length <= 1) {
            System.out.println("Usage: issue <template>");
            return;
        }
        Template tmp = session.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();
    }

    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.log("A certificate has been revoked.");
        } catch (IllegalArgumentException ignored) {
            System.out.println("Illegal serial number or reason");
        }
    }

    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 or reason");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    private void handleCRL() {
        if (!session.checkCA(true)) {
            return;
        }
        try {
            System.out.println(Utils.toPEM(session.getCa().signCRL().encodeDER(), "X509 CRL"));
            session.log("A CRL was signed");
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        }
    }

    private void handleLog() {
        session.getLogs().forEach(System.out::println);
    }

    @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;
            case "log":
                handleLog();
                return;
        }
        help();
    }

    @Override
    public Screen exit() {
        return null;
    }

    @Override
    public String getPS1() {
        return "/ %";
    }
}