forked from wolfSSL/wolfssljni
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWolfSSLInternalVerifyCb.java
More file actions
386 lines (359 loc) · 16.7 KB
/
WolfSSLInternalVerifyCb.java
File metadata and controls
386 lines (359 loc) · 16.7 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* WolfSSLInternalVerifyCb.java
*
* Copyright (C) 2006-2025 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.provider.jsse;
import com.wolfssl.WolfSSLDebug;
import com.wolfssl.WolfSSLVerifyCallback;
import com.wolfssl.WolfSSLException;
import com.wolfssl.WolfSSLJNIException;
import com.wolfssl.WolfSSLCertificate;
import com.wolfssl.WolfSSLX509StoreCtx;
import com.wolfssl.provider.jsse.WolfSSLInternalVerifyCb;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import java.io.IOException;
/**
* Internal verify callback.
* This is used when a user registers a TrustManager which is NOT
* com.wolfssl.provider.jsse.WolfSSLTrustManager. It is used to call
* TrustManager checkClientTrusted() or checkServerTrusted().
* If wolfJSSE TrustManager is used, native wolfSSL does certificate
* verification internally. Used by WolfSSLEngineHelper.
*/
public class WolfSSLInternalVerifyCb implements WolfSSLVerifyCallback {
private X509TrustManager tm = null;
private boolean clientMode;
private SSLSocket callingSocket = null;
private SSLEngine callingEngine = null;
private WolfSSLParameters params = null;
/**
* Create new WolfSSLInternalVerifyCb
*
* @param xtm X509TrustManager to use with this object
* @param client boolean representing if this is client side
* @param socket SSLSocket associated with this callback, or null
* @param engine SSLEngine associated with this callback, or null
* @param params WolfSSLParameters associated with this callback
*/
public WolfSSLInternalVerifyCb(X509TrustManager xtm, boolean client,
SSLSocket socket, SSLEngine engine, WolfSSLParameters params) {
this.tm = xtm;
this.clientMode = client;
this.callingSocket = socket;
this.callingEngine = engine;
this.params = params;
}
/**
* Reset internal variables back to null/default.
*/
protected void clearInternalVars() {
this.callingSocket = null;
this.callingEngine = null;
this.params = null;
this.tm = null;
}
/**
* Verify hostname of provided peer certificate using
* Endpoint Identification Algorithm if set in SSLParameters.
*
* Used by verifyCallback() only for case where internal native wolfSSL
* peer verification is done. Otherwise, hostname is verified as part
* of full cert validation later in verifyCallback().
*
* @param peer peer certificate to use for hostname verification
*
* @return 1 if hostname verification was successful (allows verify
* callback to proceed, otherwise 0 if verification was not
* successful (or not able to do it since Endpoitn Identification
* Algorithm has not been set).
*/
private int verifyHostnameOnly(X509Certificate peer) {
WolfSSLTrustX509 wolfTM = (WolfSSLTrustX509)tm;
try {
if (this.callingSocket != null) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "checking hostname verification using SSLSocket");
/* Throws CertificateException when verify fails */
wolfTM.verifyHostname(peer, this.callingSocket,
null, clientMode);
}
else if (this.callingEngine != null) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "checking hostname verification using SSLEngine");
/* Throws CertificateException when verify fails */
wolfTM.verifyHostname(peer, null,
this.callingEngine, clientMode);
}
else {
throw new CertificateException(
"Both SSLSocket and SSLEngine null when trying to " +
"do hostname verification");
}
} catch (CertificateException e) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "X509ExtendedTrustManager hostname verification " +
"failed: " + e.getMessage());
return 0;
}
/* Hostname verification successful */
return 1;
}
/**
* Calls registered X509TrustManager / X509ExtendedTrustManager to
* verify certificate chain.
*
* @param certs Peer certificate chain to validate
* @param authType Authentication type
*
* @return true on successful validation, otherwise false on failure
*/
private boolean VerifyCertChainWithTrustManager(X509Certificate[] certs,
String authType) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Verifying peer with X509TrustManager: " + this.tm);
if (this.tm instanceof X509ExtendedTrustManager) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "X509TrustManager of type X509ExtendedTrustManager");
}
else {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "X509TrustManager of type X509TrustManager");
}
try {
/* Call TrustManager to do cert verification, should throw
* CertificateException if verification fails */
if (this.clientMode) {
if (this.tm instanceof X509ExtendedTrustManager) {
X509ExtendedTrustManager xtm =
(X509ExtendedTrustManager)this.tm;
if (this.callingSocket != null) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Calling TrustManager.checkServerTrusted(" +
"SSLSocket)");
xtm.checkServerTrusted(certs, authType,
this.callingSocket);
}
else if (this.callingEngine != null) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Calling TrustManager.checkServerTrusted(" +
"SSLEngine)");
xtm.checkServerTrusted(certs, authType,
this.callingEngine);
}
else {
/* If we do have access to X509ExtendedTrustManager,
* but don't have SSLSocket/Engine, error out instead
* of falling back to verify without hostname. */
throw new Exception(
"SSLSocket/SSLEngine null during server peer " +
"verification, failed to verify");
}
}
else {
/* Basic X509TrustManager does not support HTTPS
* hostname verification, no SSLSocket/Engine needed */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Calling TrustManager.checkServerTrusted()");
this.tm.checkServerTrusted(certs, authType);
}
} else {
if (this.tm instanceof X509ExtendedTrustManager) {
X509ExtendedTrustManager xtm =
(X509ExtendedTrustManager)this.tm;
if (this.callingSocket != null) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Calling TrustManager.checkClientTrusted(" +
"SSLSocket)");
xtm.checkClientTrusted(certs, authType,
this.callingSocket);
}
else if (this.callingEngine != null) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Calling TrustManager.checkClientTrusted(" +
"SSLEngine)");
xtm.checkClientTrusted(certs, authType,
this.callingEngine);
}
else {
/* If we do have access to X509ExtendedTrustManager,
* but don't have SSLSocket/Engine, error out instead
* of falling back to verify without hostname. */
throw new Exception(
"SSLSocket/SSLEngine null during client peer " +
"verification, failed to verify");
}
}
else {
/* Basic X509TrustManager does not support HTTPS
* hostname verification, no SSLSocket/Engine needed */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Calling TrustManager.checkClientTrusted()");
this.tm.checkClientTrusted(certs, authType);
}
}
} catch (Exception e) {
/* TrustManager rejected certificate, not valid */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "TrustManager rejected certificates, verification " +
"failed: " + e.getMessage());
return false;
}
return true;
}
/**
* Native wolfSSL verify callback.
*
* @param preverify_ok Will be 1 if native wolfSSL verification
* procedured have passed, otherwise 0.
* @param x509StorePtr Native pointer to WOLFSSL_X509_STORE structure
*
* @return 1 if verification should be considered successful and
* SSL/TLS handshake should continue. Otherwise return 0
* to mark verification failure and stop/abort handshake.
*/
public int verifyCallback(int preverify_ok, long x509StorePtr) {
WolfSSLCertificate[] certs = null;
String authType = null;
X509Certificate[] x509certs = new X509Certificate[0];
if (preverify_ok == 1) {
/* When using WolfSSLTrustX509 implementation of
* X509TrustManager, we use internal wolfSSL verification logic
* but register this verify callback (always called) so that
* we can do additional hostname verification if user has set
* the Endpoint Identification Algorithm in SSLParameters.
* Note that certificate verification has already been done and
* passed if preverify_ok == 1, so we skip doing it again here
* later on for this case */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Native wolfSSL peer verification passed (clientMode: " +
this.clientMode + ")");
} else {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "NOTE: Native wolfSSL peer verification failed " +
"(clientMode: " + this.clientMode + ")");
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> " Continuing with X509TrustManager verification");
}
try {
/* Get WolfSSLCertificate[] from x509StorePtr, certs from
* store.getCerts() should be listed in order of peer to root */
WolfSSLX509StoreCtx store =
new WolfSSLX509StoreCtx(x509StorePtr);
certs = store.getCerts();
} catch (WolfSSLException e) {
/* failed to get certs from native, give app null array */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Failed to get certs from x509StorePtr, certs = null: " +
e.getMessage());
certs = null;
}
if (certs != null && certs.length > 0) {
try {
/* Convert WolfSSLCertificate[] to X509Certificate[] */
x509certs = new X509Certificate[certs.length];
for (int i = 0; i < certs.length; i++) {
x509certs[i] = certs[i].getX509Certificate();
final String tmpName =
x509certs[i].getSubjectDN().getName();
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Peer cert: " + tmpName);
}
} catch (CertificateException | IOException |
WolfSSLJNIException ce) {
/* failed to get cert array, give app empty array */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Failed to get X509Certificate[] array, set to " +
"empty array: " + ce.getMessage());
x509certs = new X509Certificate[0];
}
/* get authType, use first cert */
String sigType = certs[0].getSignatureType();
if (sigType.contains("RSA")) {
authType = "RSA";
} else if (sigType.contains("ECDSA")) {
authType = "ECDSA";
} else if (sigType.contains("DSA")) {
authType = "DSA";
} else if (sigType.contains("ED25519")) {
authType = "ED25519";
}
final String tmpAuthType = authType;
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Auth type: " + tmpAuthType);
/* Free native WolfSSLCertificate memory. At this
* point x509certs[] is all Java managed memory now. */
for (int i = 0; i < certs.length; i++) {
certs[i].free();
}
}
/* Case where native wolfSSL verification was already done and passed
* and we only want to do hostname verification if needed additionally.
* We do that here and return before going on to additional
* checkServerTrusted/checkClientTrusted() so that we do not
* duplicate verification. */
if ((preverify_ok == 1) && (x509certs.length > 0) &&
(tm instanceof WolfSSLTrustX509)) {
return verifyHostnameOnly(x509certs[0]);
}
/* If server-side application has explicitly disabled client
* authentication, return as success and skip X509TrustManager
* verification */
if ((!this.clientMode) && (this.params != null) &&
(!this.params.getNeedClientAuth())) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "Application has disabled client verification with " +
"setNeedClientAuth(false), skipping verification");
return 1;
}
else if ((preverify_ok == 1) && (x509certs.length == 0) &&
(!this.clientMode) && (this.params != null) &&
this.params.getWantClientAuth() &&
(!this.params.getNeedClientAuth())) {
/* If native wolfSSL verification has passed, and we have no peer
* certificates, if application has set client authentication be
* requested (wantClientAuth == true), but not fatal if no
* certificate was sent (needClientAuth == false), don't call
* TrustManager with empty certificate chain just consider
* verification successful at this point */
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "No client cert sent and client auth marked optional, " +
"not calling TrustManager for hostname verification");
}
else {
/* Poll X509TrustManager / X509ExtendedTrustManager for certificate
* verification status. Returns 0 if certificates are rejected,
* otherwise 1 on successful verification */
if (VerifyCertChainWithTrustManager(x509certs, authType) == false) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "TrustManager verification failed");
/* Abort handshake, verification failed */
return 0;
}
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "TrustManager verification successful");
/* Continue handshake, verification succeeded */
return 1;
}
}