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