aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/JCA.java
blob: 882c5467d1826d1c2dd8baa4f17bbf8e77a8ff40 (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
package ui;

import model.asn1.exceptions.ParseException;
import model.ca.CertificationAuthority;

import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Scanner;

/**
 * Main program
 */
public class JCA {
    /**
     * 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 final CertificationAuthority ca;
    /**
     * The current screen.
     */
    private UIHandler screen;

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

    private void handleLine(String... args) {
        if (args[0].equals("log")) {
            ca.getLogs().forEach(System.out::println);
            return;
        }
        switch (args[0]) {
            case "help":
                screen.help();
                System.out.println("log\tView audit logs");
                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 ", ca.getUser(), screen.getPS1());
    }

    /**
     * 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 CertificationAuthority getCa() {
        return ca;
    }
}