21
21
import java .io .File ;
22
22
import java .io .IOException ;
23
23
import java .security .GeneralSecurityException ;
24
+ import java .security .InvalidAlgorithmParameterException ;
24
25
import java .security .KeyStore ;
26
+ import java .security .KeyStoreException ;
25
27
import java .security .Security ;
26
28
import java .security .cert .CertificateException ;
27
29
import java .security .cert .PKIXBuilderParameters ;
@@ -86,14 +88,37 @@ private static SSLContext configureSSLContext( List<File> customCertFiles, Revoc
86
88
loadSystemCertificates ( trustedKeyStore );
87
89
}
88
90
89
- // Configure certificate revocation checking (X509CertSelector() selects all certificates)
90
- PKIXBuilderParameters pkixBuilderParameters = new PKIXBuilderParameters ( trustedKeyStore , new X509CertSelector () );
91
+ PKIXBuilderParameters pkixBuilderParameters = configurePKIXBuilderParameters ( trustedKeyStore , revocationStrategy );
91
92
92
- // sets checking of stapled ocsp response
93
- pkixBuilderParameters .setRevocationEnabled ( requiresRevocationChecking ( revocationStrategy ) );
93
+ SSLContext sslContext = SSLContext .getInstance ( "TLS" );
94
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance ( TrustManagerFactory .getDefaultAlgorithm () );
95
+
96
+ if ( pkixBuilderParameters == null )
97
+ {
98
+ trustManagerFactory .init ( trustedKeyStore );
99
+ }
100
+ else
101
+ {
102
+ trustManagerFactory .init ( new CertPathTrustManagerParameters ( pkixBuilderParameters ) );
103
+ }
104
+
105
+ sslContext .init ( new KeyManager [0 ], trustManagerFactory .getTrustManagers (), null );
106
+
107
+ return sslContext ;
108
+ }
109
+
110
+ private static PKIXBuilderParameters configurePKIXBuilderParameters ( KeyStore trustedKeyStore , RevocationStrategy revocationStrategy ) throws InvalidAlgorithmParameterException , KeyStoreException
111
+ {
112
+ PKIXBuilderParameters pkixBuilderParameters = null ;
94
113
95
114
if ( requiresRevocationChecking ( revocationStrategy ) )
96
115
{
116
+ // Configure certificate revocation checking (X509CertSelector() selects all certificates)
117
+ pkixBuilderParameters = new PKIXBuilderParameters ( trustedKeyStore , new X509CertSelector () );
118
+
119
+ // sets checking of stapled ocsp response
120
+ pkixBuilderParameters .setRevocationEnabled ( true );
121
+
97
122
// enables status_request extension in client hello
98
123
System .setProperty ( "jdk.tls.client.enableStatusRequestExtension" , "true" );
99
124
@@ -103,14 +128,7 @@ private static SSLContext configureSSLContext( List<File> customCertFiles, Revoc
103
128
Security .setProperty ( "ocsp.enable" , "true" );
104
129
}
105
130
}
106
-
107
- SSLContext sslContext = SSLContext .getInstance ( "TLS" );
108
-
109
- TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance ( TrustManagerFactory .getDefaultAlgorithm () );
110
- trustManagerFactory .init ( new CertPathTrustManagerParameters ( pkixBuilderParameters ) );
111
- sslContext .init ( new KeyManager [0 ], trustManagerFactory .getTrustManagers (), null );
112
-
113
- return sslContext ;
131
+ return pkixBuilderParameters ;
114
132
}
115
133
116
134
private static void loadSystemCertificates ( KeyStore trustedKeyStore ) throws GeneralSecurityException , IOException
0 commit comments