|
30 | 30 | import java.security.cert.*; |
31 | 31 | import java.util.*; |
32 | 32 | import java.util.concurrent.locks.ReentrantLock; |
33 | | - |
34 | 33 | import javax.net.ssl.*; |
35 | | - |
36 | 34 | import sun.security.ssl.SSLAlgorithmConstraints.SIGNATURE_CONSTRAINTS_MODE; |
37 | 35 | import sun.security.util.AnchorCertificates; |
38 | 36 | import sun.security.util.HostnameChecker; |
39 | 37 | import sun.security.validator.*; |
40 | 38 |
|
41 | 39 | /** |
42 | 40 | * This class implements the SunJSSE X.509 trust manager using the internal |
43 | | - * validator API in J2SE core. The logic in this class is minimal.<p> |
| 41 | + * validator API in J2SE core. The logic in this class is minimal. |
44 | 42 | * <p> |
45 | 43 | * This class supports both the Simple validation algorithm from previous |
46 | | - * JSSE versions and PKIX validation. Currently, it is not possible for the |
47 | | - * application to specify PKIX parameters other than trust anchors. This will |
48 | | - * be fixed in a future release using new APIs. When that happens, it may also |
49 | | - * make sense to separate the Simple and PKIX trust managers into separate |
50 | | - * classes. |
| 44 | + * JSSE versions and PKIX validation. |
51 | 45 | * |
52 | 46 | * @author Andreas Sterbenz |
53 | 47 | */ |
54 | | -final class X509TrustManagerImpl extends X509ExtendedTrustManager |
55 | | - implements X509TrustManager { |
| 48 | +final class X509TrustManagerImpl extends X509ExtendedTrustManager { |
56 | 49 |
|
57 | 50 | private final String validatorType; |
58 | 51 |
|
@@ -208,120 +201,90 @@ private Validator checkTrustedInit(X509Certificate[] chain, |
208 | 201 | private void checkTrusted(X509Certificate[] chain, |
209 | 202 | String authType, Socket socket, |
210 | 203 | boolean checkClientTrusted) throws CertificateException { |
211 | | - Validator v = checkTrustedInit(chain, authType, checkClientTrusted); |
212 | 204 |
|
213 | | - X509Certificate[] trustedChain; |
| 205 | + SSLSession session = null; |
| 206 | + AlgorithmConstraints constraints = null; |
| 207 | + String identityAlg = null; |
| 208 | + |
214 | 209 | if (socket instanceof SSLSocket sslSocket && sslSocket.isConnected()) { |
| 210 | + session = sslSocket.getHandshakeSession(); |
| 211 | + constraints = SSLAlgorithmConstraints.forSocket( |
| 212 | + sslSocket, SIGNATURE_CONSTRAINTS_MODE.LOCAL, false); |
| 213 | + identityAlg = sslSocket.getSSLParameters(). |
| 214 | + getEndpointIdentificationAlgorithm(); |
| 215 | + } |
215 | 216 |
|
216 | | - SSLSession session = sslSocket.getHandshakeSession(); |
217 | | - if (session == null) { |
218 | | - throw new CertificateException("No handshake session"); |
219 | | - } |
| 217 | + findTrustedCertificate(chain, authType, |
| 218 | + session, constraints, identityAlg, checkClientTrusted); |
| 219 | + } |
220 | 220 |
|
221 | | - AlgorithmConstraints constraints = SSLAlgorithmConstraints.forSocket( |
222 | | - sslSocket, SIGNATURE_CONSTRAINTS_MODE.LOCAL, false); |
| 221 | + private void checkTrusted(X509Certificate[] chain, |
| 222 | + String authType, SSLEngine engine, |
| 223 | + boolean checkClientTrusted) throws CertificateException { |
223 | 224 |
|
224 | | - // Grab any stapled OCSP responses for use in validation |
225 | | - List<byte[]> responseList = Collections.emptyList(); |
226 | | - if (!checkClientTrusted && session instanceof ExtendedSSLSession) { |
227 | | - responseList = |
228 | | - ((ExtendedSSLSession)session).getStatusResponses(); |
229 | | - } |
230 | | - trustedChain = v.validate(chain, null, responseList, |
231 | | - constraints, checkClientTrusted ? null : authType); |
| 225 | + SSLSession session = null; |
| 226 | + AlgorithmConstraints constraints = null; |
| 227 | + String identityAlg = null; |
232 | 228 |
|
233 | | - // check endpoint identity |
234 | | - String identityAlg = sslSocket.getSSLParameters(). |
| 229 | + if (engine != null) { |
| 230 | + session = engine.getHandshakeSession(); |
| 231 | + constraints = SSLAlgorithmConstraints.forEngine( |
| 232 | + engine, SIGNATURE_CONSTRAINTS_MODE.LOCAL, false); |
| 233 | + identityAlg = engine.getSSLParameters(). |
235 | 234 | getEndpointIdentificationAlgorithm(); |
236 | | - if (identityAlg != null && !identityAlg.isEmpty()) { |
237 | | - checkIdentity(session, |
238 | | - trustedChain, identityAlg, checkClientTrusted); |
239 | | - } |
240 | | - } else { |
241 | | - trustedChain = v.validate(chain, null, Collections.emptyList(), |
242 | | - null, checkClientTrusted ? null : authType); |
243 | 235 | } |
244 | 236 |
|
245 | | - if (SSLLogger.isOn() && SSLLogger.isOn("ssl,trustmanager")) { |
246 | | - SSLLogger.fine("Found trusted certificate", |
247 | | - trustedChain[trustedChain.length - 1]); |
248 | | - } |
| 237 | + findTrustedCertificate(chain, authType, |
| 238 | + session, constraints, identityAlg, checkClientTrusted); |
249 | 239 | } |
250 | 240 |
|
251 | 241 | private void checkTrusted(X509Certificate[] chain, |
252 | 242 | String authType, QuicTLSEngineImpl quicTLSEngine, |
253 | 243 | boolean checkClientTrusted) throws CertificateException { |
254 | | - Validator v = checkTrustedInit(chain, authType, checkClientTrusted); |
255 | 244 |
|
256 | | - final X509Certificate[] trustedChain; |
257 | | - if (quicTLSEngine != null) { |
| 245 | + SSLSession session = null; |
| 246 | + AlgorithmConstraints constraints = null; |
| 247 | + String identityAlg = null; |
258 | 248 |
|
259 | | - final SSLSession session = quicTLSEngine.getHandshakeSession(); |
260 | | - if (session == null) { |
261 | | - throw new CertificateException("No handshake session"); |
262 | | - } |
263 | | - |
264 | | - // create the algorithm constraints |
265 | | - final AlgorithmConstraints constraints = SSLAlgorithmConstraints.forQUIC( |
| 249 | + if (quicTLSEngine != null) { |
| 250 | + session = quicTLSEngine.getHandshakeSession(); |
| 251 | + constraints = SSLAlgorithmConstraints.forQUIC( |
266 | 252 | quicTLSEngine, SIGNATURE_CONSTRAINTS_MODE.LOCAL, false); |
267 | | - final List<byte[]> responseList; |
268 | | - // grab any stapled OCSP responses for use in validation |
269 | | - if (!checkClientTrusted && |
270 | | - session instanceof ExtendedSSLSession extSession) { |
271 | | - responseList = extSession.getStatusResponses(); |
272 | | - } else { |
273 | | - responseList = Collections.emptyList(); |
274 | | - } |
275 | | - // do the certificate chain validation |
276 | | - trustedChain = v.validate(chain, null, responseList, |
277 | | - constraints, checkClientTrusted ? null : authType); |
278 | | - |
279 | | - // check endpoint identity |
280 | | - String identityAlg = quicTLSEngine.getSSLParameters(). |
| 253 | + identityAlg = quicTLSEngine.getSSLParameters(). |
281 | 254 | getEndpointIdentificationAlgorithm(); |
282 | | - if (identityAlg != null && !identityAlg.isEmpty()) { |
283 | | - checkIdentity(session, trustedChain, |
284 | | - identityAlg, checkClientTrusted); |
285 | | - } |
286 | | - } else { |
287 | | - trustedChain = v.validate(chain, null, Collections.emptyList(), |
288 | | - null, checkClientTrusted ? null : authType); |
289 | 255 | } |
290 | 256 |
|
291 | | - if (SSLLogger.isOn() && SSLLogger.isOn("ssl,trustmanager")) { |
292 | | - SSLLogger.fine("Found trusted certificate", |
293 | | - trustedChain[trustedChain.length - 1]); |
294 | | - } |
| 257 | + findTrustedCertificate(chain, authType, |
| 258 | + session, constraints, identityAlg, checkClientTrusted); |
295 | 259 | } |
296 | 260 |
|
297 | | - private void checkTrusted(X509Certificate[] chain, |
298 | | - String authType, SSLEngine engine, |
| 261 | + private void findTrustedCertificate(X509Certificate[] chain, |
| 262 | + String authType, SSLSession session, |
| 263 | + AlgorithmConstraints constraints, String identityAlg, |
299 | 264 | boolean checkClientTrusted) throws CertificateException { |
300 | | - Validator v = checkTrustedInit(chain, authType, checkClientTrusted); |
301 | 265 |
|
302 | | - X509Certificate[] trustedChain; |
303 | | - if (engine != null) { |
| 266 | + final X509Certificate[] trustedChain; |
| 267 | + Validator v = checkTrustedInit(chain, authType, checkClientTrusted); |
304 | 268 |
|
305 | | - SSLSession session = engine.getHandshakeSession(); |
| 269 | + if (constraints != null) { |
306 | 270 | if (session == null) { |
307 | 271 | throw new CertificateException("No handshake session"); |
308 | 272 | } |
309 | 273 |
|
310 | | - AlgorithmConstraints constraints = SSLAlgorithmConstraints.forEngine( |
311 | | - engine, SIGNATURE_CONSTRAINTS_MODE.LOCAL, false); |
312 | | - |
313 | 274 | // Grab any stapled OCSP responses for use in validation |
314 | | - List<byte[]> responseList = Collections.emptyList(); |
315 | | - if (!checkClientTrusted && session instanceof ExtendedSSLSession) { |
316 | | - responseList = |
317 | | - ((ExtendedSSLSession)session).getStatusResponses(); |
| 275 | + final List<byte[]> responseList; |
| 276 | + if (!checkClientTrusted && |
| 277 | + session instanceof ExtendedSSLSession extSession) { |
| 278 | + responseList = extSession.getStatusResponses(); |
| 279 | + } else { |
| 280 | + responseList = Collections.emptyList(); |
318 | 281 | } |
| 282 | + |
| 283 | + // Certificate chain validation |
319 | 284 | trustedChain = v.validate(chain, null, responseList, |
320 | 285 | constraints, checkClientTrusted ? null : authType); |
321 | 286 |
|
322 | | - // check endpoint identity |
323 | | - String identityAlg = engine.getSSLParameters(). |
324 | | - getEndpointIdentificationAlgorithm(); |
| 287 | + // Check endpoint identity |
325 | 288 | if (identityAlg != null && !identityAlg.isEmpty()) { |
326 | 289 | checkIdentity(session, trustedChain, |
327 | 290 | identityAlg, checkClientTrusted); |
|
0 commit comments