Skip to content

Commit a4fb946

Browse files
authored
Avoid TrustManagerFacotry.init(ManagerFactoryParameters var1) if no OSCP has been configured (#1157) (#1168)
1 parent 34035cf commit a4fb946

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

driver/src/main/java/org/neo4j/driver/internal/security/SecurityPlanImpl.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.security.GeneralSecurityException;
24+
import java.security.InvalidAlgorithmParameterException;
2425
import java.security.KeyStore;
26+
import java.security.KeyStoreException;
2527
import java.security.Security;
2628
import java.security.cert.CertificateException;
2729
import java.security.cert.PKIXBuilderParameters;
@@ -86,14 +88,37 @@ private static SSLContext configureSSLContext( List<File> customCertFiles, Revoc
8688
loadSystemCertificates( trustedKeyStore );
8789
}
8890

89-
// Configure certificate revocation checking (X509CertSelector() selects all certificates)
90-
PKIXBuilderParameters pkixBuilderParameters = new PKIXBuilderParameters( trustedKeyStore, new X509CertSelector() );
91+
PKIXBuilderParameters pkixBuilderParameters = configurePKIXBuilderParameters( trustedKeyStore, revocationStrategy );
9192

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

95114
if ( requiresRevocationChecking( revocationStrategy ) )
96115
{
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+
97122
// enables status_request extension in client hello
98123
System.setProperty( "jdk.tls.client.enableStatusRequestExtension", "true" );
99124

@@ -103,14 +128,7 @@ private static SSLContext configureSSLContext( List<File> customCertFiles, Revoc
103128
Security.setProperty( "ocsp.enable", "true" );
104129
}
105130
}
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;
114132
}
115133

116134
private static void loadSystemCertificates( KeyStore trustedKeyStore ) throws GeneralSecurityException, IOException

0 commit comments

Comments
 (0)