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
|
package ui.tui;
import model.asn1.exceptions.ParseException;
import model.ca.Template;
import model.csr.CertificationRequest;
import model.pki.cert.Certificate;
import model.x501.Name;
import ui.Utils;
/**
* The screen that accepts a CSR and template and allows user to change its properties and issue.
*/
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];
}
/**
* EFFECTS: Print help.
*/
@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");
}
/**
* EFFECTS: Print pending cert info.
*/
@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");
}
/**
* EFFECTS: Issue the cert and log it.
* MODIFIES: session
*/
@Override
public void commit() {
try {
Certificate certificate = session.getCa().signCert(incomingCSR.getCertificationRequestInfo(), template);
session.save();
System.out.println(Utils.toPEM(certificate.encodeDER(), "CERTIFICATE"));
session.setScreen(Screen.MAIN);
} catch (Throwable e) {
System.out.println(e.getMessage());
}
}
/**
* EFFECTS: Set or unset the subject.
* MODIFIES: this
*/
private void handleIssueSetSubject(String val) {
try {
if (val == null) {
template = new Template(template.getName(), template.isEnabled(), (Name) null, template.getValidity());
} else {
template = new Template(template.getName(), template.isEnabled(), val, template.getValidity());
}
} catch (ParseException e) {
System.out.println(e.getMessage());
}
}
/**
* EFFECTS: Set or unset the validity.
* MODIFIES: this
*/
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 = new Template(template.getName(), template.isEnabled(), template.getSubject(), i);
} catch (NumberFormatException ignored) {
System.out.println("Invalid validity days");
}
}
/**
* EFFECTS: Handle the set command.
* MODIFIES: this
*/
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) {
if (args[0].equals("set")) {
handleIssueSet(args);
} else {
help();
}
}
/**
* EFFECTS: Clear the certificates and return main.
* MODIFIES: this
*/
@Override
public Screen exit() {
incomingCSR = null;
template = null;
return Screen.MAIN;
}
/**
* EFFECTS: Return "/subj/ %"
*/
@Override
public String getPS1() {
return String.format("/%s/ %%", template.getSubject() == null
? incomingCSR.getCertificationRequestInfo().getSubject()
: template.getSubject());
}
}
|