aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/JCA.java
blob: 64cfaa3e0405c5a5af4bdeaf0776d9f9246db77c (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package ui;

import model.asn1.exceptions.InvalidDBException;
import model.asn1.exceptions.ParseException;
import model.ca.CertificationAuthority;
import persistence.Decoder;
import persistence.FS;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Scanner;

/**
 * Main program
 */
public class JCA {
    /**
     * Default db file (./data/ca.json)
     */
    private static final Path PATH_DEFAULT = Path.of("data", "ca.json");

    /**
     * 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 CertificationAuthority ca;
    /**
     * The current screen.
     */
    private UIHandler screen;

    /**
     * There are unsaved changes.
     */
    private boolean unsaved = false;

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

    /**
     * EFFECTS: Read the database file and replace all local states.
     * MODIFIES: this
     */
    private void load() {
        if (unsaved) {
            System.out.println("Current database is not saved yet.");
            return;
        }
        try {
            this.ca = Decoder.decodeCA(FS.read(PATH_DEFAULT));
        } catch (InvalidDBException | NoSuchAlgorithmException e) {
            System.out.println(e.getMessage());
            if (e.getCause() != null) {
                System.out.println(e.getCause().getMessage());
                e.getCause().printStackTrace();
            }
        }
    }

    /**
     * EFFECTS: Handle input line
     * MODIFIES: this
     */
    private void handleLine(String... args) {
        if (args[0].equals("log")) {
            ca.getLogs().forEach(System.out::println);
            return;
        }
        if ("help".equals(args[0])) {
            screen.help();
            System.out.println("log\tView audit logs");
            System.out.println("load\tLoad database");
            System.out.println("save\tSave database");
        } else if ("commit".equals(args[0])) {
            screen.commit();
        } else if ("show".equals(args[0])) {
            screen.show();
        } else if ("exit".equals(args[0])) {
            setScreen(screen.exit());
        } else if ("save".equals(args[0])) {
            save();
        } else if ("load".equals(args[0])) {
            load();
        } else {
            screen.command(args);
        }
        printPS1();
    }

    /**
     * EFFECTS: Print the '*user@JCA PS1' line
     */
    private void printPS1() {
        System.out.printf("%s%s@JCA %s ",
                unsaved ? "*" : "",
                ca.getUser(),
                screen.getPS1());
    }

    /**
     * EFFECTS: Save the DB
     */
    public void save() {
        try {
            FS.write(PATH_DEFAULT, Decoder.encodeCA(ca));
            unsaved = false;
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 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 void setUnsaved(boolean unsaved) {
        this.unsaved = unsaved;
    }

    public CertificationAuthority getCa() {
        return ca;
    }
}