-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWolfSSLJDK8Helper.java
More file actions
250 lines (224 loc) · 9.31 KB
/
WolfSSLJDK8Helper.java
File metadata and controls
250 lines (224 loc) · 9.31 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
/* WolfSSLJDK8Helper.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.provider.jsse;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.security.PrivilegedAction;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SNIServerName;
import javax.net.ssl.SNIHostName;
/**
* This class contains functionality that was added as of JDK 1.8, and is
* isolated in this class as to more easily avoid pre-JDK 1.8 execution from
* loading this class. Otherwise, importing the classes that did not exist
* on older JDKs would fail.
*
* Execution should be prevented from calling functions in this class on
* JDK version less than 1.8.
*/
public class WolfSSLJDK8Helper
{
/** Default WolfSSLJDK8Helper constructor */
public WolfSSLJDK8Helper() { }
/**
* Call SSLParameters.setServerNames() to set SNI server names from
* WolfSSLParameters into SSLParameters.
*
* @param out SSLParameters object to set SNI names into
* @param m Java method to call to set SNI server names
* @param in WolfSSLParameters to set SNI names from
*/
@SuppressWarnings("removal")
protected static void setServerNames(final SSLParameters out,
final Method m, WolfSSLParameters in) {
if (out == null || m == null || in == null) {
throw new NullPointerException("input arguments to " +
"WolfSSLJDK8Helper.setServerNames() cannot be null");
}
List<WolfSSLSNIServerName> wsni = in.getWolfSSLServerNames();
if (wsni != null) {
/* convert WolfSSLSNIServerName list to SNIServerName */
final ArrayList<SNIServerName> sni =
new ArrayList<SNIServerName>(wsni.size());
for (WolfSSLSNIServerName name : wsni) {
sni.add(new SNIHostName(name.getEncoded()));
}
/* call SSLParameters.setServerNames().
* AccessController used via FQN to avoid import-line removal
* warning on JDK 17+. */
java.security.AccessController
.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
try {
m.invoke(out, sni);
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
});
}
}
/**
* Call SSLParameters.getServerNames() to set SNI server names from
* SSLParameters into WolfSSLParameters.
*
* @param in input SSLParameters to read SNI names from
* @param out WolfSSLParameters to store SNI names into
*/
protected static void getServerNames(final SSLParameters in,
WolfSSLParameters out) {
if (out == null || in == null) {
throw new NullPointerException("input arguments to " +
"WolfSSLJDK8Helper.getServerNames() cannot be null");
}
List<SNIServerName> sni = in.getServerNames();
if (sni != null) {
/* convert SNIServerName list to WolfSSLSNIServerName */
final ArrayList<WolfSSLSNIServerName> wsni =
new ArrayList<WolfSSLSNIServerName>(sni.size());
for (SNIServerName name : sni) {
wsni.add(new WolfSSLGenericHostName(name.getType(),
name.getEncoded()));
}
/* call WolfSSLParameters.setWolfSSLServerNames() */
out.setWolfSSLServerNames(wsni);
}
}
/**
* Call SSLParameters.setApplicationProtocols() to set ALPN protocols from
* WolfSSLParameters into SSLParameters.
*
* @param out output SSLParameters to store ALPN protocols into
* @param m method to invoke to set protocols
* @param in input WolfSSLParameters to read ALPN protocols from
*/
@SuppressWarnings("removal")
protected static void setApplicationProtocols(final SSLParameters out,
final Method m, WolfSSLParameters in) {
if (out == null || m == null || in == null) {
throw new NullPointerException("input arguments to " +
"WolfSSLJDK8Helper.setApplicationProtocols() cannot be null");
}
final String[] appProtos = in.getApplicationProtocols();
final Object[] appProtoArr = {appProtos};
if (appProtos != null) {
/* call SSLParameters.setApplicationProtocols() */
java.security.AccessController
.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
try {
m.invoke(out, appProtoArr);
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
});
}
}
/**
* Call SSLParameters.getApplicationProtocols() to get ALPN protocols from
* SSLParameters into WolfSSLParameters.
*
* @param in input SSLParameters to read ALPN protocols from
* @param out output WolfSSLParameters to store ALPN protocols into
*/
protected static void getApplicationProtocols(final SSLParameters in,
WolfSSLParameters out) {
if (out == null || in == null) {
throw new NullPointerException("input arguments to " +
"WolfSSLJDK8Helper.getApplicationProtocols() cannot be null");
}
try {
/* Android API < 29 does not support SSLParameters
* getApplicationProtocols(). Use reflection here to conditionally
* call it if available */
Method meth = SSLParameters.class.getMethod(
"getApplicationProtocols");
String[] appProtos = (String[])meth.invoke(in);
if (appProtos != null) {
/* call WolfSSLParameters.setApplicationProtocols() */
out.setApplicationProtocols(appProtos);
}
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
/* getApplicationProtocols() not available, just return */
return;
}
}
/**
* Call SSLParameters.setEndpointIdentificationAlgorithm() to set
* Endpoint Identification algo from WolfSSLParameters into
* SSLParameters.
*
* @param out output SSLParameters to store endpoint ID algo into
* @param m method to invoke to set endpoint ID algo
* @param in input WolfSSLParameters to read endpoint ID algo from
*/
@SuppressWarnings("removal")
protected static void setEndpointIdentificationAlgorithm(
final SSLParameters out, final Method m, WolfSSLParameters in) {
if (out == null || m == null || in == null) {
throw new NullPointerException("input arguments to " +
"WolfSSLJDK8Helper.setEndpointIdentificationAlgorithm() " +
"cannot be null");
}
final String idAlgo = in.getEndpointIdentificationAlgorithm();
if (idAlgo != null) {
/* call SSLParameters.setEndpointIdentificationAlgorithm() */
java.security.AccessController
.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
try {
m.invoke(out, idAlgo);
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
});
}
}
/**
* Call SSLParameters.getEndpointIdentificationAlgorithm() to get
* Endpoint Identification Algorithm from SSLParameters into
* WolfSSLParameters.
*
* @param in input SSLParameters to read Endpoint ID algo from
* @param out output WolfSSLParameters to store Endpoint ID algo into
*/
protected static void getEndpointIdentificationAlgorithm(
final SSLParameters in, WolfSSLParameters out) {
if (out == null || in == null) {
throw new NullPointerException("input arguments to " +
"WolfSSLJDK8Helper.getEndpointIdentificationAlgorithm() " +
"cannot be null");
}
String idAlgo = in.getEndpointIdentificationAlgorithm();
if (idAlgo != null) {
/* call WolfSSLParameters.setEndpointIdentificationAlgorithm() */
out.setEndpointIdentificationAlgorithm(idAlgo);
}
}
}