aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/JCA.java
blob: f9467ea2f33e9d89f52f99ea0fdf0d37eec4c7b6 (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
201
202
203
package ui;

import model.asn1.exceptions.ParseException;
import model.ca.AuditLogEntry;
import model.ca.CACertificate;
import model.ca.Template;

import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * Main program
 */
public class JCA {
    /**
     * The current screen.
     */
    private UIHandler screen;

    /**
     * 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;

    /**
     * Templates
     */
    private final List<Template> templates;

    /**
     * The CA
     */
    private final CACertificate ca;

    /**
     * Audit logs
     */
    private final List<AuditLogEntry> logs;

    /**
     * Current user
     */
    private final String user;

    /**
     * EFFECTS: Init with main screen, empty templates, logs, user 'yuuta', and generate a private key with no CA cert.
     *   Throws {@link NoSuchAlgorithmException} when crypto issue happens.
     */
    public JCA() throws NoSuchAlgorithmException {
        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.templates = new ArrayList<>();
        this.ca = new CACertificate();
        this.logs = new ArrayList<>();
        this.user = "yuuta";

        this.ca.generateKey();
    }

    /**
     * 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("The CA is not installed yet");
            return false;
        } else if (!requireInstalled && ca.getCertificate() != null) {
            System.out.println("The CA is 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);
    }

    /**
     * EFFECTS: Find the template based on name, or null if not found.
     */
    public Template findTemplate(String name, boolean requireEnabled) {
        Optional<Template> opt = templates.stream().filter(temp -> {
            if (requireEnabled && !temp.isEnabled()) {
                return false;
            }
            return temp.getName().equals(name);
        }).findFirst();
        return opt.orElse(null);
    }

    /**
     * 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].isBlank()) {
            switch (args[0]) {
                case "help":
                    screen.help();
                    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 ", user, screen.getPS1());
    }

    /**
     * EFFECT: Log an action to the audit log
     * MODIFIES: this
     */
    public void log(String action) {
        this.logs.add(new AuditLogEntry(user, ZonedDateTime.now(), action));
    }

    /**
     * EFFECTS: Run the program
     */
    public void run() {
        printPS1();
        final Scanner scanner = new Scanner(System.in);
        while (true) {
            handleLine(scanner.nextLine().split(" "));
        }
    }

    public List<Template> getTemplates() {
        return templates;
    }

    public CACertificate getCa() {
        return ca;
    }

    public List<AuditLogEntry> getLogs() {
        return logs;
    }
}