Skip to content

Commit a0857fd

Browse files
author
Zhen Li
committed
Add back trust all certificates
1 parent a7fb60b commit a0857fd

File tree

7 files changed

+60
-77
lines changed

7 files changed

+60
-77
lines changed

driver/src/main/java/org/neo4j/driver/Config.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.neo4j.driver.util.Immutable;
3535
import org.neo4j.driver.util.Resource;
3636

37+
import static org.neo4j.driver.Config.TrustStrategy.trustSystemCertificates;
3738
import static org.neo4j.driver.Logging.javaUtilLogging;
3839

3940
/**
@@ -249,7 +250,7 @@ public static class ConfigBuilder
249250
private long maxConnectionLifetimeMillis = PoolSettings.DEFAULT_MAX_CONNECTION_LIFETIME;
250251
private long connectionAcquisitionTimeoutMillis = PoolSettings.DEFAULT_CONNECTION_ACQUISITION_TIMEOUT;
251252
private boolean encrypted = false;
252-
private TrustStrategy trustStrategy = TrustStrategy.trustSystemCertificates();
253+
private TrustStrategy trustStrategy = trustSystemCertificates();
253254
private int routingFailureLimit = RoutingSettings.DEFAULT.maxRoutingFailures();
254255
private long routingRetryDelayMillis = RoutingSettings.DEFAULT.retryTimeoutDelay();
255256
private long routingTablePurgeDelayMillis = RoutingSettings.DEFAULT.routingTablePurgeDelayMs();
@@ -687,6 +688,7 @@ public static class TrustStrategy
687688
*/
688689
public enum Strategy
689690
{
691+
TRUST_ALL_CERTIFICATES,
690692
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES,
691693
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
692694
}
@@ -782,5 +784,16 @@ public static TrustStrategy trustSystemCertificates()
782784
{
783785
return new TrustStrategy( Strategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES );
784786
}
787+
788+
/**
789+
* Trust strategy for certificates that trust all certificates blindly. Suggested to only use this in tests.
790+
*
791+
* @return an authentication config
792+
* @since 1.1
793+
*/
794+
public static TrustStrategy trustAllCertificates()
795+
{
796+
return new TrustStrategy( Strategy.TRUST_ALL_CERTIFICATES );
797+
}
785798
}
786799
}

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ private static SecurityPlan createSecurityPlanImpl( Config config )
306306
return SecurityPlan.forCustomCASignedCertificates( trustStrategy.certFile(), hostnameVerificationEnabled );
307307
case TRUST_SYSTEM_CA_SIGNED_CERTIFICATES:
308308
return SecurityPlan.forSystemCASignedCertificates( hostnameVerificationEnabled );
309+
case TRUST_ALL_CERTIFICATES:
310+
return SecurityPlan.forAllCertificates( hostnameVerificationEnabled );
309311
default:
310312
throw new ClientException(
311313
"Unknown TLS authentication strategy: " + trustStrategy.strategy().name() );

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
import java.security.GeneralSecurityException;
2424
import java.security.KeyStore;
2525
import java.security.NoSuchAlgorithmException;
26+
import java.security.cert.CertificateException;
27+
import java.security.cert.X509Certificate;
2628
import javax.net.ssl.KeyManager;
2729
import javax.net.ssl.SSLContext;
30+
import javax.net.ssl.TrustManager;
2831
import javax.net.ssl.TrustManagerFactory;
32+
import javax.net.ssl.X509TrustManager;
2933

3034
import static org.neo4j.driver.internal.util.CertificateTool.loadX509Cert;
3135

@@ -34,6 +38,14 @@
3438
*/
3539
public class SecurityPlan
3640
{
41+
public static SecurityPlan forAllCertificates( boolean requiresHostnameVerification ) throws GeneralSecurityException
42+
{
43+
SSLContext sslContext = SSLContext.getInstance( "TLS" );
44+
sslContext.init( new KeyManager[0], new TrustManager[]{new TrustAllTrustManager()}, null );
45+
46+
return new SecurityPlan( true, sslContext, requiresHostnameVerification );
47+
}
48+
3749
public static SecurityPlan forCustomCASignedCertificates( File certFile, boolean requiresHostnameVerification )
3850
throws GeneralSecurityException, IOException
3951
{
@@ -90,4 +102,22 @@ public boolean requiresHostnameVerification()
90102
{
91103
return requiresHostnameVerification;
92104
}
105+
106+
private static class TrustAllTrustManager implements X509TrustManager
107+
{
108+
public void checkClientTrusted( X509Certificate[] chain, String authType ) throws CertificateException
109+
{
110+
throw new CertificateException( "All client connections to this client are forbidden." );
111+
}
112+
113+
public void checkServerTrusted( X509Certificate[] chain, String authType ) throws CertificateException
114+
{
115+
// all fine, pass through
116+
}
117+
118+
public X509Certificate[] getAcceptedIssuers()
119+
{
120+
return new X509Certificate[0];
121+
}
122+
}
93123
}

driver/src/test/java/org/neo4j/driver/integration/ChannelConnectorImplIT.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.UncheckedIOException;
3333
import java.net.ServerSocket;
3434
import java.net.Socket;
35+
import java.security.GeneralSecurityException;
3536
import java.util.concurrent.ExecutionException;
3637
import java.util.concurrent.TimeUnit;
3738

@@ -61,7 +62,6 @@
6162
import static org.junit.jupiter.api.Assertions.assertThrows;
6263
import static org.junit.jupiter.api.Assertions.assertTrue;
6364
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
64-
import static org.neo4j.driver.util.SecurityUtil.trustAllCertificates;
6565
import static org.neo4j.driver.util.TestUtil.await;
6666

6767
@ParallelizableIT
@@ -232,4 +232,9 @@ private ChannelConnectorImpl newConnector( AuthToken authToken, SecurityPlan sec
232232
ConnectionSettings settings = new ConnectionSettings( authToken, connectTimeoutMillis );
233233
return new ChannelConnectorImpl( settings, securityPlan, DEV_NULL_LOGGING, new FakeClock() );
234234
}
235+
236+
private static SecurityPlan trustAllCertificates() throws GeneralSecurityException
237+
{
238+
return SecurityPlan.forAllCertificates( false );
239+
}
235240
}

driver/src/test/java/org/neo4j/driver/integration/EncryptionIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import static org.hamcrest.Matchers.instanceOf;
3939
import static org.hamcrest.junit.MatcherAssert.assertThat;
4040
import static org.junit.jupiter.api.Assertions.assertThrows;
41-
import static org.neo4j.driver.Config.TrustStrategy.trustCustomCertificateSignedBy;
41+
import static org.neo4j.driver.Config.TrustStrategy.trustAllCertificates;
4242

4343
@ParallelizableIT
4444
class EncryptionIT
@@ -121,7 +121,7 @@ private static Config newConfig( boolean withEncryption )
121121

122122
private static Config configWithEncryption()
123123
{
124-
return Config.builder().withEncryption().withTrustStrategy( trustCustomCertificateSignedBy( neo4j.tlsCertFile() ) ).build();
124+
return Config.builder().withEncryption().withTrustStrategy( trustAllCertificates() ).build();
125125
}
126126

127127
private static Config configWithoutEncryption()

driver/src/test/java/org/neo4j/driver/internal/async/connection/NettyChannelInitializerTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void tearDown()
6363
@Test
6464
void shouldAddSslHandlerWhenRequiresEncryption() throws Exception
6565
{
66-
SecurityPlan security = secure();
66+
SecurityPlan security = trustAllCertificates();
6767
NettyChannelInitializer initializer = newInitializer( security );
6868

6969
initializer.initChannel( channel );
@@ -86,7 +86,7 @@ void shouldNotAddSslHandlerWhenDoesNotRequireEncryption()
8686
void shouldAddSslHandlerWithHandshakeTimeout() throws Exception
8787
{
8888
int timeoutMillis = 424242;
89-
SecurityPlan security = secure();
89+
SecurityPlan security = trustAllCertificates();
9090
NettyChannelInitializer initializer = newInitializer( security, timeoutMillis );
9191

9292
initializer.initChannel( channel );
@@ -115,7 +115,7 @@ void shouldUpdateChannelAttributes()
115115
void shouldIncludeSniHostName() throws Exception
116116
{
117117
BoltServerAddress address = new BoltServerAddress( "database.neo4j.com", 8989 );
118-
NettyChannelInitializer initializer = new NettyChannelInitializer( address, secure(), 10000, Clock.SYSTEM, DEV_NULL_LOGGING );
118+
NettyChannelInitializer initializer = new NettyChannelInitializer( address, trustAllCertificates(), 10000, Clock.SYSTEM, DEV_NULL_LOGGING );
119119

120120
initializer.initChannel( channel );
121121

@@ -142,7 +142,7 @@ void shouldNotEnableHostnameVerificationWhenNotConfigured() throws Exception
142142

143143
private void testHostnameVerificationSetting( boolean enabled, String expectedValue ) throws Exception
144144
{
145-
NettyChannelInitializer initializer = newInitializer( SecurityPlan.forSystemCASignedCertificates( enabled ) );
145+
NettyChannelInitializer initializer = newInitializer( SecurityPlan.forAllCertificates( enabled ) );
146146

147147
initializer.initChannel( channel );
148148

@@ -169,9 +169,8 @@ private static NettyChannelInitializer newInitializer( SecurityPlan securityPlan
169169
DEV_NULL_LOGGING );
170170
}
171171

172-
private static SecurityPlan secure() throws GeneralSecurityException
172+
private static SecurityPlan trustAllCertificates() throws GeneralSecurityException
173173
{
174-
// Any secure security plan
175-
return SecurityPlan.forSystemCASignedCertificates( false );
174+
return SecurityPlan.forAllCertificates( false );
176175
}
177176
}

driver/src/test/java/org/neo4j/driver/util/SecurityUtil.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)