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
|
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2015 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import org.mozilla.jss.crypto.X509Certificate;
import org.mozilla.jss.ssl.SSLCertificateApprovalCallback;
public class PKICertificateApprovalCallback implements SSLCertificateApprovalCallback {
public PKIClient client;
public PKICertificateApprovalCallback(PKIClient client) {
this.client = client;
}
// NOTE: The following helper method defined as
// 'public String displayReason(int reason)'
// should be moved into the JSS class called
// 'org.mozilla.jss.ssl.SSLCertificateApprovalCallback'
// under its nested subclass called 'ValidityStatus'.
// While all reason values should be unique, this method has been
// written to return the name of the first defined reason that is
// encountered which contains the requested value, or null if no
// reason containing the requested value is encountered.
public String displayReason(int reason) {
for (Field f : ValidityStatus.class.getDeclaredFields()) {
int mod = f.getModifiers();
if (Modifier.isStatic(mod) &&
Modifier.isPublic(mod) &&
Modifier.isFinal(mod)) {
try {
int value = f.getInt(null);
if (value == reason) {
return f.getName();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return null;
}
public String getMessage(X509Certificate serverCert, int reason) {
if (reason == SSLCertificateApprovalCallback.ValidityStatus.BAD_CERT_DOMAIN) {
return "BAD_CERT_DOMAIN encountered on '"+serverCert.getSubjectDN()+"' indicates a common-name mismatch";
}
if (reason == SSLCertificateApprovalCallback.ValidityStatus.UNTRUSTED_ISSUER) {
return "UNTRUSTED ISSUER encountered on '" +
serverCert.getSubjectDN() + "' indicates a non-trusted CA cert '" +
serverCert.getIssuerDN() + "'";
}
if (reason == SSLCertificateApprovalCallback.ValidityStatus.CA_CERT_INVALID) {
return "CA_CERT_INVALID encountered on '"+serverCert.getSubjectDN()+"' results in a denied SSL server cert!";
}
String reasonName = displayReason(reason);
if (reasonName != null) {
return reasonName+" encountered on '"+serverCert.getSubjectDN()+"' results in a denied SSL server cert!";
}
return "Unknown/undefined reason "+reason+" encountered on '"+serverCert.getSubjectDN()+"' results in a denied SSL server cert!";
}
public boolean handleUntrustedIssuer(X509Certificate serverCert) {
try {
System.out.print("Import CA certificate (Y/n)? ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine().trim();
if (!line.equals("") && !line.equalsIgnoreCase("Y"))
return false;
String caServerURI = "http://" + client.getConfig().getServerURI().getHost() + ":8080/ca";
System.out.print("CA server URI [" + caServerURI + "]: ");
System.out.flush();
line = reader.readLine().trim();
if (!line.equals("")) {
caServerURI = line;
}
if (client.verbose) System.out.println("Downloading CA certificate chain from " + caServerURI + ".");
byte[] bytes = client.downloadCACertChain(caServerURI);
if (client.verbose) System.out.println("Importing CA certificate chain.");
client.importCACertPackage(bytes);
if (client.verbose) System.out.println("Imported CA certificate.");
return true;
} catch (Exception e) {
System.err.println("ERROR: "+e);
return false;
}
}
// Callback to approve or deny returned SSL server cert.
// Right now, simply approve the cert.
public boolean approve(X509Certificate serverCert,
SSLCertificateApprovalCallback.ValidityStatus status) {
boolean approval = true;
if (client.verbose) System.out.println("Server certificate: "+serverCert.getSubjectDN());
SSLCertificateApprovalCallback.ValidityItem item;
// If there are no items in the Enumeration returned by
// getReasons(), you can assume that the certificate is
// trustworthy, and return true to allow the connection to
// continue, or you can continue to make further tests of
// your own to determine trustworthiness.
Enumeration<?> errors = status.getReasons();
while (errors.hasMoreElements()) {
item = (SSLCertificateApprovalCallback.ValidityItem) errors.nextElement();
int reason = item.getReason();
if (client.isRejected(reason)) {
if (!client.statuses.contains(reason))
System.err.println("ERROR: " + getMessage(serverCert, reason));
approval = false;
} else if (client.isIgnored(reason)) {
// Ignore validity status
} else if (reason == SSLCertificateApprovalCallback.ValidityStatus.UNTRUSTED_ISSUER) {
// Issue a WARNING, but allow this process
// to continue since we haven't installed a trusted CA
// cert for this operation.
if (!client.statuses.contains(reason)) {
System.err.println("WARNING: " + getMessage(serverCert, reason));
handleUntrustedIssuer(serverCert);
}
} else if (reason == SSLCertificateApprovalCallback.ValidityStatus.BAD_CERT_DOMAIN) {
// Issue a WARNING, but allow this process to continue on
// common-name mismatches.
if (!client.statuses.contains(reason))
System.err.println("WARNING: " + getMessage(serverCert, reason));
} else if (reason == SSLCertificateApprovalCallback.ValidityStatus.CA_CERT_INVALID) {
// Set approval false to deny this
// certificate so that the connection is terminated.
// (Expect an IOException on the outstanding
// read()/write() on the socket).
if (!client.statuses.contains(reason))
System.err.println("ERROR: " + getMessage(serverCert, reason));
approval = false;
} else {
// Set approval false to deny this certificate so that
// the connection is terminated. (Expect an IOException
// on the outstanding read()/write() on the socket).
if (!client.statuses.contains(reason))
System.err.println("ERROR: " + getMessage(serverCert, reason));
approval = false;
}
client.statuses.add(reason);
}
return approval;
}
}
|