Skip to content

Commit a99f340

Browse files
committed
8371721: Refactor checkTrusted methods in X509TrustManagerImpl
Reviewed-by: coffeys, djelinski
1 parent d854a04 commit a99f340

File tree

1 file changed

+53
-90
lines changed

1 file changed

+53
-90
lines changed

src/java.base/share/classes/sun/security/ssl/X509TrustManagerImpl.java

Lines changed: 53 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,22 @@
3030
import java.security.cert.*;
3131
import java.util.*;
3232
import java.util.concurrent.locks.ReentrantLock;
33-
3433
import javax.net.ssl.*;
35-
3634
import sun.security.ssl.SSLAlgorithmConstraints.SIGNATURE_CONSTRAINTS_MODE;
3735
import sun.security.util.AnchorCertificates;
3836
import sun.security.util.HostnameChecker;
3937
import sun.security.validator.*;
4038

4139
/**
4240
* 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.
4442
* <p>
4543
* 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.
5145
*
5246
* @author Andreas Sterbenz
5347
*/
54-
final class X509TrustManagerImpl extends X509ExtendedTrustManager
55-
implements X509TrustManager {
48+
final class X509TrustManagerImpl extends X509ExtendedTrustManager {
5649

5750
private final String validatorType;
5851

@@ -208,120 +201,90 @@ private Validator checkTrustedInit(X509Certificate[] chain,
208201
private void checkTrusted(X509Certificate[] chain,
209202
String authType, Socket socket,
210203
boolean checkClientTrusted) throws CertificateException {
211-
Validator v = checkTrustedInit(chain, authType, checkClientTrusted);
212204

213-
X509Certificate[] trustedChain;
205+
SSLSession session = null;
206+
AlgorithmConstraints constraints = null;
207+
String identityAlg = null;
208+
214209
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+
}
215216

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+
}
220220

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 {
223224

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;
232228

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().
235234
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);
243235
}
244236

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);
249239
}
250240

251241
private void checkTrusted(X509Certificate[] chain,
252242
String authType, QuicTLSEngineImpl quicTLSEngine,
253243
boolean checkClientTrusted) throws CertificateException {
254-
Validator v = checkTrustedInit(chain, authType, checkClientTrusted);
255244

256-
final X509Certificate[] trustedChain;
257-
if (quicTLSEngine != null) {
245+
SSLSession session = null;
246+
AlgorithmConstraints constraints = null;
247+
String identityAlg = null;
258248

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(
266252
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().
281254
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);
289255
}
290256

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);
295259
}
296260

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,
299264
boolean checkClientTrusted) throws CertificateException {
300-
Validator v = checkTrustedInit(chain, authType, checkClientTrusted);
301265

302-
X509Certificate[] trustedChain;
303-
if (engine != null) {
266+
final X509Certificate[] trustedChain;
267+
Validator v = checkTrustedInit(chain, authType, checkClientTrusted);
304268

305-
SSLSession session = engine.getHandshakeSession();
269+
if (constraints != null) {
306270
if (session == null) {
307271
throw new CertificateException("No handshake session");
308272
}
309273

310-
AlgorithmConstraints constraints = SSLAlgorithmConstraints.forEngine(
311-
engine, SIGNATURE_CONSTRAINTS_MODE.LOCAL, false);
312-
313274
// 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();
318281
}
282+
283+
// Certificate chain validation
319284
trustedChain = v.validate(chain, null, responseList,
320285
constraints, checkClientTrusted ? null : authType);
321286

322-
// check endpoint identity
323-
String identityAlg = engine.getSSLParameters().
324-
getEndpointIdentificationAlgorithm();
287+
// Check endpoint identity
325288
if (identityAlg != null && !identityAlg.isEmpty()) {
326289
checkIdentity(session, trustedChain,
327290
identityAlg, checkClientTrusted);

0 commit comments

Comments
 (0)