-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWolfSSLX509StoreCtx.java
More file actions
335 lines (289 loc) · 11.1 KB
/
WolfSSLX509StoreCtx.java
File metadata and controls
335 lines (289 loc) · 11.1 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
/* WolfSSLX509StoreCtx.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.wolfcrypt;
/**
* Wrapper class for native WOLFSSL_X509_STORE and WOLFSSL_X509_STORE_CTX.
*
* @author wolfSSL
*/
public class WolfSSLX509StoreCtx implements AutoCloseable {
private boolean active = false;
private long storePtr = 0;
/* Lock around active state */
private final Object stateLock = new Object();
/* Lock around native pointer use */
private final Object storeLock = new Object();
/* Cached result of runtime WOLFSSL_X509_STORE check_time support detection.
* Result does not change during process lifetime since it depends on the
* linked wolfSSL library version. */
private static volatile Boolean storeCheckTimeSupported = null;
/**
* Flag to skip date/time validation when adding certificates to store.
* This allows expired certificates to be added for testing or when
* date validation will be performed separately with a custom time.
*/
public static final long WOLFSSL_NO_CHECK_TIME = 0x200000L;
/** Flag to use a custom time for date validation instead of system time. */
public static final long WOLFSSL_USE_CHECK_TIME = 0x2L;
private static native long wolfSSL_X509_STORE_new();
private static native void wolfSSL_X509_STORE_free(long store);
private static native int wolfSSL_X509_STORE_set_flags(long store,
long flags);
private static native int wolfSSL_X509_STORE_set_time(long store,
long epochSeconds);
private static native int wolfSSL_X509_STORE_add_cert(long store,
byte[] certDer);
private static native byte[][] wolfSSL_X509_verify_cert_and_get_chain(
long store, byte[] targetCertDer, byte[][] intermediateCertsDer,
int maxPathLength) throws WolfCryptException;
private static native int isCertPathBuilderSupported();
private static native int isNativeStoreCheckTimeSupported();
/**
* Check if CertPathBuilder functionality is supported.
*
* CertPathBuilder requires wolfSSL version 5.8.0 or later for proper
* X509_STORE chain building support.
*
* @return true if CertPathBuilder is supported, false otherwise
*/
public static boolean isSupported() {
try {
return (isCertPathBuilderSupported() == 1);
} catch (UnsatisfiedLinkError e) {
return false;
}
}
/**
* Check if WOLFSSL_X509_STORE custom verification time (check_time) is
* supported by linked version of native wolfSSL.
*
* Custom verification time support requires a wolfSSL version that
* propagates check_time and NO_CHECK_TIME flags from WOLFSSL_X509_STORE
* to WOLFSSL_X509_STORE_CTX during initialization. This feature is needed
* for PKIXBuilderParameters.setDate() to work correctly with
* expired or not-yet-valid certificates.
*
* @return true if store check_time is supported, false otherwise
*/
public static boolean isStoreCheckTimeSupported() {
Boolean cached = null;
boolean supported = false;
cached = storeCheckTimeSupported;
if (cached != null) {
return cached.booleanValue();
}
try {
supported = (isNativeStoreCheckTimeSupported() == 1);
} catch (UnsatisfiedLinkError e) {
supported = false;
}
storeCheckTimeSupported = Boolean.valueOf(supported);
return supported;
}
/**
* Create new WolfSSLX509StoreCtx object.
*
* Requires wolfSSL version 5.8.0 or later for proper X509_STORE
* chain building support.
*
* @throws WolfCryptException if unable to create native X509_STORE,
* or if wolfSSL version is too old (requires 5.8.0+)
*/
public WolfSSLX509StoreCtx() throws WolfCryptException {
storePtr = wolfSSL_X509_STORE_new();
if (storePtr == 0) {
throw new WolfCryptException(
"Failed to create native WOLFSSL_X509_STORE. " +
"CertPathBuilder requires wolfSSL 5.8.0 or later.");
}
this.active = true;
}
/**
* Verify this object is active and not freed.
*
* @throws IllegalStateException if object has been freed or is invalid
*/
private void confirmObjectIsActive() throws IllegalStateException {
synchronized (stateLock) {
if (!this.active || this.storePtr == 0) {
throw new IllegalStateException(
"WolfSSLX509StoreCtx object has been freed or is invalid");
}
}
}
/**
* Set flags on the X509 store.
*
* Available flags:
* - WOLFSSL_NO_CHECK_TIME: Skip date validation when adding certificates.
* - WOLFSSL_USE_CHECK_TIME: Use a custom time for date validation.
*
* @param flags flags to set on the store (can OR together)
*
* @throws IllegalStateException if object has been freed
* @throws WolfCryptException if unable to set flags
*/
public void setFlags(long flags)
throws IllegalStateException, WolfCryptException {
int ret;
confirmObjectIsActive();
synchronized (storeLock) {
ret = wolfSSL_X509_STORE_set_flags(this.storePtr, flags);
if (ret != WolfCrypt.SUCCESS) {
throw new WolfCryptException(
"Failed to set store flags: " + ret);
}
}
}
/**
* Set a custom verification time for certificate date validation.
*
* When set, chain verification will validate certificate dates against
* this time instead of the current system time. This is useful for:
*
* - Testing with expired certificates
* - Verifying certificates were valid at a specific point in time
* - Reproducing validation results from a specific date
*
* @param epochSeconds the verification time as seconds since Unix epoch
* (January 1, 1970 00:00:00 UTC)
*
* @throws IllegalStateException if object has been freed
* @throws WolfCryptException if unable to set verification time
*/
public void setVerificationTime(long epochSeconds)
throws IllegalStateException, WolfCryptException {
int ret;
confirmObjectIsActive();
synchronized (storeLock) {
ret = wolfSSL_X509_STORE_set_time(this.storePtr, epochSeconds);
if (ret != WolfCrypt.SUCCESS) {
throw new WolfCryptException(
"Failed to set verification time: " + ret);
}
}
}
/**
* Add a certificate to the store.
*
* Self-signed certificates added as trust anchors. Non self-signed
* certificates added as intermediate certificates.
*
* @param certDer DER-encoded X.509 certificate
*
* @throws IllegalStateException if object has been freed
* @throws WolfCryptException if unable to add certificate
*/
public void addCertificate(byte[] certDer)
throws IllegalStateException, WolfCryptException {
int ret;
confirmObjectIsActive();
if (certDer == null || certDer.length == 0) {
throw new WolfCryptException("Certificate data is null or empty");
}
synchronized (storeLock) {
ret = wolfSSL_X509_STORE_add_cert(this.storePtr, certDer);
if (ret != WolfCrypt.SUCCESS) {
throw new WolfCryptException(
"Failed to add certificate to store: " + ret);
}
}
}
/**
* Build and verify a certificate chain for the target certificate.
*
* @param targetCertDer DER-encoded target certificate
* @param intermediateCertsDer optional array of additional intermediate
* certificates (can be null). If provided,
* each element must be non-null and non-empty.
* @param maxPathLength maximum path length constraint, or -1 for unlimited
*
* @return array of DER-encoded certificates forming the verified chain,
* ordered from target certificate to trust anchor
*
* @throws IllegalStateException if object has been freed
* @throws WolfCryptException if chain building or verification fails,
* or if any intermediate certificate is null or empty
*/
public byte[][] buildAndVerifyChain(byte[] targetCertDer,
byte[][] intermediateCertsDer, int maxPathLength)
throws IllegalStateException, WolfCryptException {
byte[][] chain;
confirmObjectIsActive();
if (targetCertDer == null || targetCertDer.length == 0) {
throw new WolfCryptException(
"Target certificate data is null or empty");
}
/* Validate intermediate certificates if provided */
if (intermediateCertsDer != null) {
for (int i = 0; i < intermediateCertsDer.length; i++) {
if (intermediateCertsDer[i] == null ||
intermediateCertsDer[i].length == 0) {
throw new WolfCryptException(
"Intermediate certificate at index " + i +
" is null or empty");
}
}
}
synchronized (storeLock) {
chain = wolfSSL_X509_verify_cert_and_get_chain(this.storePtr,
targetCertDer, intermediateCertsDer, maxPathLength);
}
if (chain == null) {
throw new WolfCryptException(
"Certificate chain verification failed: native error");
}
return chain;
}
/**
* Free native resources associated with this object.
*/
public void free() {
synchronized (stateLock) {
if (this.active) {
synchronized (storeLock) {
try {
wolfSSL_X509_STORE_free(this.storePtr);
} finally {
this.storePtr = 0;
}
}
this.active = false;
}
}
}
/**
* Implements AutoCloseable for use with try-with-resources.
*/
@Override
public void close() {
free();
}
@Override
@SuppressWarnings({"deprecation", "removal"})
protected void finalize() throws Throwable {
try {
free();
} finally {
super.finalize();
}
}
}