-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathWolfSSLSocketFactory.java
More file actions
352 lines (298 loc) · 11.4 KB
/
Copy pathWolfSSLSocketFactory.java
File metadata and controls
352 lines (298 loc) · 11.4 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
/* WolfSSLSocketFactory.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 3 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 java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLSocketFactory;
import com.wolfssl.WolfSSL;
import com.wolfssl.WolfSSLDebug;
import com.wolfssl.WolfSSLException;
/**
* wolfSSL implementation of SSLSocketFactory
*
* @author wolfSSL
*/
public class WolfSSLSocketFactory extends SSLSocketFactory {
private WolfSSLAuthStore authStore = null;
private com.wolfssl.WolfSSLContext ctx = null;
private com.wolfssl.provider.jsse.WolfSSLContext jsseCtx = null;
private WolfSSLParameters params;
/* Defer creation and initialization of DEFAULT Context until used,
* to remove creation logic from constructor */
private int isDefault = 0;
private int isDefaultInitialized = 0;
/**
* Create new WolfSSLSocketFactory object with default settings
*
* This constructor is used when the JSSE calls
* SSLSocketFactory.getDefault()
*/
public WolfSSLSocketFactory() {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "created new default WolfSSLSocketFactory");
this.isDefault = 1;
this.isDefaultInitialized = 0;
/* initialize later on first use */
this.jsseCtx = null;
this.ctx = null;
this.authStore = null;
this.params = null;
}
/**
* Create new WolfSSLSocketFactory object
*
* @param ctx WolfSSLContext object to use with this factory
* @param authStore WolfSSLAuthStore object to use with this factory
* @param params WolfSSLParameters to use with this factory
*/
public WolfSSLSocketFactory(com.wolfssl.WolfSSLContext ctx,
WolfSSLAuthStore authStore, WolfSSLParameters params) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "created new WolfSSLSocketFactory");
this.ctx = ctx;
this.authStore = authStore;
this.params = params;
}
/**
* Private internal function to create and initialize default context
* and set ctx, authStore, and params from it.
* @throws WolfSSLException if default CTX is null
*/
private void initDefaultContext() throws WolfSSLException {
if (this.isDefault == 1 && this.isDefaultInitialized == 0) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "creating and initializing DEFAULT_Context");
this.jsseCtx =
new com.wolfssl.provider.jsse.WolfSSLContext.DEFAULT_Context();
this.ctx = jsseCtx.getInternalWolfSSLContext();
if (this.ctx == null) {
throw new WolfSSLException("Issue with null internal TLS CTX");
}
this.authStore = jsseCtx.getInternalAuthStore();
this.params = jsseCtx.getInternalSSLParams();
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "DEFAULT_Context created and initialized");
}
}
/**
* Returns the default cipher suite list for wolfJSSE.
*
* Filters out anon suites by default.
*
* @return default array of cipher suite Strings for wolfSSL
*/
@Override
public String[] getDefaultCipherSuites() {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered getDefaultCipherSuites()");
return WolfSSLUtil.sanitizeSuites(WolfSSL.getCiphersIana(), true);
}
/**
* Returns the supported cipher suite list for this factory.
*
* @return array of supported cipher suite Strings
*/
@Override
public String[] getSupportedCipherSuites() {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered getSupportedCipherSuites()");
return WolfSSLUtil.sanitizeSuites(WolfSSL.getCiphersIana(), false);
}
/**
* Creates a new unconnected SSLSocket
*
* @return the new Socket
* @throws IOException if socket creation fails
*/
@Override
public Socket createSocket() throws IOException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket()");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params, true);
}
/**
* Creates a new Socket connected to the specified host and port.
*
* @param host server host name for Socket to be connected to, or null
* for loopback address
* @param port server port
*
* @return the new Socket
* @throws IOException if socket creation fails
*/
@Override
public Socket createSocket(InetAddress host, int port)
throws IOException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket(InetAddress host, int port)");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params, true, host, port);
}
/**
* Creates a new Socket connected to the specified remote host and port,
* and also bound to the specified local address and port.
*
* @param address server host name for Socket to be connected to, or null
* for loopback address
* @param port server port
* @param localAddress local address that the Socket will be bound to
* @param localPort local port that the Socket will be bound to
*
* @return the new Socket
* @throws IOException if socket creation fails
*/
@Override
public Socket createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort) throws IOException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket(InetAddress host, port: " + port +
", InetAddress localAddress, localPort: " + localPort + ")");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params,
true, address, port, localAddress, localPort);
}
/**
* Creates a new Socket connected to the specified host and port.
*
* @param host server host name for Socket to be connected to, or null
* for loopback address
* @param port server port
*
* @return the new Socket
* @throws IOException if socket creation fails
*/
@Override
public Socket createSocket(String host, int port)
throws IOException, UnknownHostException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket(host: " +
WolfSSLDebug.sanitizeForLog(host) + ", port: " +
port + ")");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params, true, host, port);
}
/**
* Creates a new Socket connected to the specified remote host and port,
* and also bound to the specified local address and port.
*
* @param host server host name for Socket to be connected, or null for
* loopback address
* @param port server port
* @param localHost local address that the Socket will be bound to
* @param localPort local port that the Socket will be bound to
*
* @return the new Socket
* @throws IOException if socket creation fails.
*/
@Override
public Socket createSocket(String host, int port, InetAddress localHost,
int localPort) throws IOException, UnknownHostException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket(host: " +
WolfSSLDebug.sanitizeForLog(host) + ", port: " + port +
", InetAddress localHost, localPort: " + localPort + ")");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params,
true, host, port, localHost, localPort);
}
/**
* Creates a new SSLSocket layered over an existing Socket connected to the
* specified host and port.
*
* @param s connected Socket to host
* @param host host that the Socket is connected to
* @param port port that the Socket is connected to
* @param autoClose flag indicating if the underlying Socket should be
* closed when the SSLSocket is closed
*
* @return the new Socket
* @throws IOException if socket creation fails
*/
@Override
public Socket createSocket(Socket s, String host, int port,
boolean autoClose) throws IOException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket(Socket: " + s.getClass() +
", host: " + WolfSSLDebug.sanitizeForLog(host) +
", port: " + port + ", autoClose: " +
String.valueOf(autoClose) + ")");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params,
true, s, host, port, autoClose);
}
/**
* Creates a new SSLSocket layered over an existing connected Socket, and
* is able to read data that has already been consumed from exising
* Socket's InputStream.
*
* @param s connected Socket to host
* @param consumed consumed inbound network data that has been read off
* the existing Socket's InputStream. May be null if no
* data has been read.
* @param autoClose flag indicating if the underlying Socket should be
* closed when the SSLSocket is closed
*
* @return the new Socket
* @throws IOException if socket creation fails
*/
public Socket createSocket(Socket s, InputStream consumed,
boolean autoClose) throws IOException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
() -> "entered createSocket(Socket: " + s.getClass() +
", InputStream consumed, autoClose: "
+ String.valueOf(autoClose) + ")");
try {
initDefaultContext();
} catch (WolfSSLException e) {
throw new IOException(e);
}
return new WolfSSLSocket(ctx, authStore, params, s,
consumed, autoClose);
}
}