-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWolfSSLCertManager.java
More file actions
351 lines (299 loc) · 12 KB
/
WolfSSLCertManager.java
File metadata and controls
351 lines (299 loc) · 12 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* WolfSSLCertManager.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL 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; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL 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-1335, USA
*/
package com.wolfssl;
import java.util.Enumeration;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateEncodingException;
/**
* CertManager class which wraps the native WolfSSL embedded SSL library.
* This class contains library init and cleanup methods, general callback
* methods, as well as error codes and general wolfSSL codes.
*
* @author wolfSSL
*/
public class WolfSSLCertManager {
private boolean active = false;
private long cmPtr = 0;
/* lock around active state */
private final Object stateLock = new Object();
/* lock around native WOLFSSL_CERT_MANAGER pointer use */
private final Object cmLock = new Object();
static native long CertManagerNew();
static native void CertManagerFree(long cm);
static native int CertManagerLoadCA(long cm, String f, String d);
static native int CertManagerLoadCABuffer(long cm, byte[] in, long sz,
int format);
static native int CertManagerUnloadCAs(long cm);
static native int CertManagerVerifyBuffer(long cm, byte[] in, long sz,
int format);
static native int CertManagerCheckOCSPResponse(long cm,
byte[] response, byte[] cert, byte[] issuerCert);
/**
* Create new WolfSSLCertManager object
*
* @throws WolfSSLException if unable to create new manager
*/
public WolfSSLCertManager() throws WolfSSLException {
cmPtr = CertManagerNew();
if (cmPtr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertManager");
}
this.active = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, cmPtr, () -> "creating new WolfSSLCertManager");
}
/**
* Verifies that the current WolfSSLCertManager object is active.
*
* @throws IllegalStateException if object has been freed
*/
private synchronized void confirmObjectIsActive()
throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
throw new IllegalStateException(
"WolfSSLCertManager object has been freed");
}
}
}
/**
* Load CA into CertManager
*
* @param f X.509 certificate file to load
* @param d directory of X.509 certs to load, or null
*
* @return WolfSSL.SSL_SUCESS on success, negative on error
* @throws IllegalStateException WolfSSLContext has been freed
*/
public synchronized int CertManagerLoadCA(String f, String d)
throws IllegalStateException {
confirmObjectIsActive();
synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
() -> "entered CertManagerLoadCA(" + f + ", " + d + ")");
return CertManagerLoadCA(this.cmPtr, f, d);
}
}
/**
* Load CA into CertManager from byte array
*
* @param in byte array holding X.509 certificate to load
* @param sz size of input byte array, bytes
* @param format format of input certificate, either
* WolfSSL.SSL_FILETYPE_PEM (PEM formatted) or
* WolfSSL.SSL_FILETYPE_ASN1 (ASN.1/DER).
*
* @return WolfSSL.SSL_SUCCESS on success, negative on error
* @throws IllegalStateException WolfSSLContext has been freed
*/
public synchronized int CertManagerLoadCABuffer(
byte[] in, long sz, int format) throws IllegalStateException {
confirmObjectIsActive();
synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
() -> "entered CertManagerLoadCABuffer(sz: " + sz +
", format: " + format + ")");
return CertManagerLoadCABuffer(this.cmPtr, in, sz, format);
}
}
/**
* Loads KeyStore certificates into WolfSSLCertManager object.
*
* @param ks - input KeyStore from which to load CA certs
* @return WolfSSL.SSL_SUCCESS if at least one cert was loaded
* successfully, otherwise WolfSSL.SSL_FAILURE.
* @throws WolfSSLException on exception working with KeyStore
* @throws IllegalStateException WolfSSLContext has been freed
*/
public synchronized int CertManagerLoadCAKeyStore(KeyStore ks)
throws WolfSSLException, IllegalStateException {
int ret = 0;
int loadedCerts = 0;
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
() -> "entered CertManagerLoadCAKeyStore(" + ks + ")");
if (ks == null) {
throw new WolfSSLException("Input KeyStore is null");
}
try {
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String name = aliases.nextElement();
X509Certificate cert = null;
if (ks.isKeyEntry(name)) {
Certificate[] chain = ks.getCertificateChain(name);
if (chain != null) {
cert = (X509Certificate) chain[0];
}
} else {
cert = (X509Certificate) ks.getCertificate(name);
}
if (cert != null && (cert.getBasicConstraints() >= 0 ||
WolfSSL.trustPeerCertEnabled())) {
ret = CertManagerLoadCABuffer(cert.getEncoded(),
cert.getEncoded().length,
WolfSSL.SSL_FILETYPE_ASN1);
if (ret == WolfSSL.SSL_SUCCESS) {
loadedCerts++;
}
}
}
} catch (KeyStoreException | CertificateEncodingException ex) {
throw new WolfSSLException(ex);
}
if (loadedCerts > 0) {
return WolfSSL.SSL_SUCCESS;
} else {
return WolfSSL.SSL_FAILURE;
}
}
/**
* Unload any CAs that have been loaded into WolfSSLCertManager object.
*
* @return WolfSSL.SSL_SUCCESS on success, negative on error.
* @throws IllegalStateException WolfSSLContext has been freed
*/
public synchronized int CertManagerUnloadCAs()
throws IllegalStateException {
confirmObjectIsActive();
synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
() -> "entered CertManagerUnloadCAs()");
return CertManagerUnloadCAs(this.cmPtr);
}
}
/**
* Verify X.509 certificate held in byte array
*
* @param in input X.509 certificate as byte array
* @param sz size of input certificate array, bytes
* @param format format of input certificate, either
* WolfSSL.SSL_FILETYPE_PEM (PEM formatted) or
* WolfSSL.SSL_FILETYPE_ASN1 (ASN.1/DER).
*
* @return WolfSSL.SSL_SUCCESS on successful verification, otherwise
* negative on error.
* @throws IllegalStateException WolfSSLContext has been freed
*/
public synchronized int CertManagerVerifyBuffer(
byte[] in, long sz, int format) throws IllegalStateException {
confirmObjectIsActive();
synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
() -> "entered CertManagerVerifyBuffer(sz: " + sz +
", format: " + format + ")");
return CertManagerVerifyBuffer(this.cmPtr, in, sz, format);
}
}
/**
* Check OCSP response for revocation status.
*
* This method validates that the OCSP response is signed by a trusted
* OCSP responder, contains a status for the specified certificate (matches
* serial number and issuer), and checks that the certificate is neither
* revoked nor reported with an unknown status by the OCSP responder.
* Certificates with an unknown status in the OCSP response cause this
* method to fail and return a specific negative OCSP error code.
*
* @param response DER-encoded OCSP response data
* @param cert DER-encoded certificate to check against the OCSP response
* @param issuerCert DER-encoded issuer certificate (optional)
*
* @return WolfSSL.SSL_SUCCESS on success, or a negative error code on
* failure. This includes specific OCSP error codes indicating
* revoked or unknown certificate status as reported by the OCSP
* responder.
*
* @throws IllegalStateException if WolfSSLCertManager has been freed
* @throws IllegalArgumentException if response or cert is null/empty
* @throws WolfSSLException if OCSP is not compiled in
*/
public synchronized int CertManagerCheckOCSPResponse(
byte[] response, byte[] cert, byte[] issuerCert)
throws IllegalStateException, WolfSSLException {
int ret;
confirmObjectIsActive();
if (response == null || response.length == 0) {
throw new IllegalArgumentException(
"OCSP response data is null or invalid size");
}
if (cert == null || cert.length == 0) {
throw new IllegalArgumentException(
"Certificate data is null or invalid size");
}
synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr,
() -> "entered CertManagerCheckOCSPResponse(" +
"responseSz: " + response.length + ", certSz: " +
cert.length + ", issuerCertSz: " +
(issuerCert != null ? issuerCert.length : 0) + ")");
ret = CertManagerCheckOCSPResponse(this.cmPtr, response, cert,
issuerCert);
if (ret == WolfSSL.NOT_COMPILED_IN) {
throw new WolfSSLException(
"OCSP support not compiled into wolfSSL");
}
return ret;
}
}
/**
* Frees CertManager object
* @see WolfSSLSession#freeSSL()
*/
public synchronized void free() throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
/* already freed, just return */
return;
}
synchronized (cmLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.cmPtr, () -> "entered free()");
/* free native resources */
CertManagerFree(this.cmPtr);
/* free Java resources */
this.active = false;
this.cmPtr = 0;
}
}
}
@SuppressWarnings({"deprecation", "removal"})
@Override
protected void finalize() throws Throwable
{
try {
/* checks active state in this.free() */
this.free();
} catch (IllegalStateException e) {
/* already freed */
}
super.finalize();
}
}