Skip to content

Commit 994fb0a

Browse files
authored
Avoid TrustManagerFacotry.init(ManagerFactoryParameters var1) if no OSCP has been configured (neo4j#1157)
1 parent 0d8bd6e commit 994fb0a

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;
@@ -84,14 +86,37 @@ private static SSLContext configureSSLContext( File customCertFile, RevocationSt
8486
loadSystemCertificates( trustedKeyStore );
8587
}
8688

87-
// Configure certificate revocation checking (X509CertSelector() selects all certificates)
88-
PKIXBuilderParameters pkixBuilderParameters = new PKIXBuilderParameters( trustedKeyStore, new X509CertSelector() );
89+
PKIXBuilderParameters pkixBuilderParameters = configurePKIXBuilderParameters( trustedKeyStore, revocationStrategy );
8990

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

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

@@ -101,14 +126,7 @@ private static SSLContext configureSSLContext( File customCertFile, RevocationSt
101126
Security.setProperty( "ocsp.enable", "true" );
102127
}
103128
}
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;
112130
}
113131

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

0 commit comments

Comments
 (0)