25
25
import javax .net .ssl .SSLEngineResult ;
26
26
import javax .net .ssl .SSLEngineResult .HandshakeStatus ;
27
27
import javax .net .ssl .SSLEngineResult .Status ;
28
+ import javax .net .ssl .SSLHandshakeException ;
28
29
29
30
import org .neo4j .driver .internal .net .BoltServerAddress ;
30
31
import org .neo4j .driver .v1 .Logger ;
@@ -70,7 +71,8 @@ public static TLSSocketChannel create( BoltServerAddress address, SecurityPlan s
70
71
return new TLSSocketChannel ( channel , logger , sslEngine );
71
72
}
72
73
73
- public TLSSocketChannel ( ByteChannel channel , Logger logger , SSLEngine sslEngine ) throws IOException
74
+ public TLSSocketChannel ( ByteChannel channel , Logger logger , SSLEngine sslEngine )
75
+ throws IOException
74
76
{
75
77
this .logger = logger ;
76
78
this .channel = channel ;
@@ -79,7 +81,21 @@ public TLSSocketChannel( ByteChannel channel, Logger logger, SSLEngine sslEngine
79
81
this .cipherIn = ByteBuffer .allocate ( sslEngine .getSession ().getPacketBufferSize () );
80
82
this .plainOut = ByteBuffer .allocate ( sslEngine .getSession ().getApplicationBufferSize () );
81
83
this .cipherOut = ByteBuffer .allocate ( sslEngine .getSession ().getPacketBufferSize () );
82
- runHandshake ();
84
+
85
+ try
86
+ {
87
+ runHandshake ();
88
+ }
89
+ catch ( SSLHandshakeException e )
90
+ {
91
+ Throwable error = e ;
92
+ if ( error .getCause () != null ) // get the real exception
93
+ {
94
+ error = e .getCause ();
95
+ }
96
+ throw new ClientException ( "Failed to establish secured connection with the server: " + error .getMessage (),
97
+ error .getCause () );
98
+ }
83
99
}
84
100
85
101
/**
@@ -164,6 +180,14 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
164
180
*/
165
181
if ( channel .read ( cipherIn ) < 0 )
166
182
{
183
+ try
184
+ {
185
+ channel .close ();
186
+ }
187
+ catch ( IOException e )
188
+ {
189
+ // best effort
190
+ }
167
191
throw new ServiceUnavailableException (
168
192
"SSL Connection terminated while receiving data. " +
169
193
"This can happen due to network instabilities, or due to restarts of the database." );
@@ -289,14 +313,19 @@ private HandshakeStatus wrap( ByteBuffer buffer ) throws IOException
289
313
// flush as much data as possible
290
314
cipherOut .flip ();
291
315
int written = channel .write ( cipherOut );
292
- if (written = = 0 )
316
+ if (written < = 0 )
293
317
{
294
- throw new ClientException (
295
- String .format (
296
- "Failed to enlarge network buffer from %s to %s. This is either because the " +
297
- "new size is however less than the old size, or because the application " +
298
- "buffer size %s is so big that the application data still cannot fit into the " +
299
- "new network buffer." , curNetSize , netSize , buffer .capacity () ) );
318
+ try
319
+ {
320
+ channel .close ();
321
+ }
322
+ catch ( IOException e )
323
+ {
324
+ // best effort
325
+ }
326
+ throw new ServiceUnavailableException (
327
+ "SSL Connection terminated while writing data. " +
328
+ "This can happen due to network instabilities, or due to restarts of the database." );
300
329
}
301
330
cipherOut .compact ();
302
331
logger .debug ( "Network output buffer couldn't be enlarged, flushing data to the channel instead." );
0 commit comments