|
26 | 26 | import static org.junit.Assert.assertFalse;
|
27 | 27 | import static org.junit.Assert.assertNull;
|
28 | 28 | import static org.junit.Assert.assertTrue;
|
| 29 | +import static org.junit.Assert.fail; |
29 | 30 | import static org.mockito.ArgumentMatchers.same;
|
30 | 31 | import static org.mockito.Mockito.when;
|
31 | 32 |
|
@@ -176,22 +177,166 @@ public void closingOutboundAfterHandshakeShouldOnlyCloseOutbound() throws Except
|
176 | 177 | }
|
177 | 178 |
|
178 | 179 | @Test
|
179 |
| - public void closingInboundShouldOnlyCloseInbound() throws Exception { |
| 180 | + public void closingInboundBeforeHandshakeShouldCloseAll() throws Exception { |
| 181 | + setupEngines(TestKeyStore.getClient(), TestKeyStore.getServer()); |
| 182 | + assertFalse(clientEngine.isInboundDone()); |
| 183 | + assertFalse(clientEngine.isOutboundDone()); |
| 184 | + assertFalse(serverEngine.isInboundDone()); |
| 185 | + assertFalse(serverEngine.isOutboundDone()); |
| 186 | + |
| 187 | + clientEngine.closeInbound(); |
| 188 | + serverEngine.closeInbound(); |
| 189 | + |
| 190 | + assertTrue(clientEngine.isInboundDone()); |
| 191 | + assertTrue(clientEngine.isOutboundDone()); |
| 192 | + assertTrue(serverEngine.isInboundDone()); |
| 193 | + assertTrue(serverEngine.isOutboundDone()); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void closingInboundBeforeClosingOutboundShouldFail() throws Exception { |
180 | 198 | setupEngines(TestKeyStore.getClient(), TestKeyStore.getServer());
|
181 | 199 | doHandshake(true);
|
182 | 200 |
|
| 201 | + try { |
| 202 | + clientEngine.closeInbound(); |
| 203 | + fail(); |
| 204 | + } catch (SSLException e) { |
| 205 | + // Expected |
| 206 | + } |
| 207 | + |
| 208 | + try { |
| 209 | + serverEngine.closeInbound(); |
| 210 | + fail(); |
| 211 | + } catch (SSLException e) { |
| 212 | + // Expected |
| 213 | + } |
| 214 | + |
183 | 215 | assertFalse(clientEngine.isInboundDone());
|
184 | 216 | assertFalse(clientEngine.isOutboundDone());
|
185 | 217 | assertFalse(serverEngine.isInboundDone());
|
186 | 218 | assertFalse(serverEngine.isOutboundDone());
|
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void closingInboundAfterClosingOutboundShouldSucceed_NoAlerts() throws Exception { |
| 223 | + // Client and server both close inbound after closing outbound without seeing the other's |
| 224 | + // close alert (e.g. due to the transport being closed already). |
| 225 | + setupEngines(TestKeyStore.getClient(), TestKeyStore.getServer()); |
| 226 | + doHandshake(true); |
187 | 227 |
|
| 228 | + clientEngine.closeOutbound(); |
| 229 | + serverEngine.closeOutbound(); |
| 230 | + // After closeOutbound, wrap should produce the TLS close alerts. |
| 231 | + assertTrue(wrapClosed(clientEngine).hasRemaining()); |
| 232 | + assertTrue(wrapClosed(serverEngine).hasRemaining()); |
| 233 | + |
| 234 | + assertFalse(clientEngine.isInboundDone()); |
| 235 | + assertTrue(clientEngine.isOutboundDone()); |
| 236 | + assertFalse(serverEngine.isInboundDone()); |
| 237 | + assertTrue(serverEngine.isOutboundDone()); |
| 238 | + |
| 239 | + // Closing inbound should now succeed and produce no new handshake data. |
188 | 240 | clientEngine.closeInbound();
|
189 | 241 | serverEngine.closeInbound();
|
| 242 | + assertFalse(wrapClosed(clientEngine).hasRemaining()); |
| 243 | + assertFalse(wrapClosed(serverEngine).hasRemaining()); |
190 | 244 |
|
| 245 | + // Final state, everything closed. |
191 | 246 | assertTrue(clientEngine.isInboundDone());
|
192 |
| - assertFalse(clientEngine.isOutboundDone()); |
| 247 | + assertTrue(clientEngine.isOutboundDone()); |
193 | 248 | assertTrue(serverEngine.isInboundDone());
|
194 |
| - assertFalse(serverEngine.isOutboundDone()); |
| 249 | + assertTrue(serverEngine.isOutboundDone()); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void closingInboundAfterClosingOutboundShouldSucceed_AlertsArriveBeforeCloseInbound() |
| 254 | + throws Exception { |
| 255 | + // Client and server both call closeOutbound simultaneously. The alerts produced |
| 256 | + // by this both arrive at the peer before it calls closeInbound, making it a no-op. |
| 257 | + setupEngines(TestKeyStore.getClient(), TestKeyStore.getServer()); |
| 258 | + doHandshake(true); |
| 259 | + |
| 260 | + clientEngine.closeOutbound(); |
| 261 | + // After closeOutbound, wrap should produce a single output buffer with the TLS close alert |
| 262 | + ByteBuffer clientOutput = wrapClosed(clientEngine); |
| 263 | + assertTrue(clientOutput.hasRemaining()); |
| 264 | + |
| 265 | + // Unwrapping that on the server engine should cause it to close and send its own alert |
| 266 | + // but produce no plaintext output. |
| 267 | + assertFalse(unwrapClosed(serverEngine, clientOutput).hasRemaining()); |
| 268 | + ByteBuffer serverOutput = wrapClosed(serverEngine); |
| 269 | + assertTrue(serverOutput.hasRemaining()); |
| 270 | + |
| 271 | + // At his point, server should be fully closed after processing client's alert and sending |
| 272 | + // its own. Client inbound is still open. |
| 273 | + assertFalse(clientEngine.isInboundDone()); |
| 274 | + assertTrue(clientEngine.isOutboundDone()); |
| 275 | + assertTrue(serverEngine.isInboundDone()); |
| 276 | + assertTrue(serverEngine.isOutboundDone()); |
| 277 | + |
| 278 | + // Explicitly closing the server outbound should produce no new extra handshake data |
| 279 | + serverEngine.closeOutbound(); |
| 280 | + assertFalse(wrapClosed(serverEngine).hasRemaining()); |
| 281 | + |
| 282 | + // Unwrapping the server output on the client should cause it to close as above |
| 283 | + // but produce no new handshaking data and no new plaintext. |
| 284 | + assertFalse(unwrapClosed(clientEngine, serverOutput).hasRemaining()); |
| 285 | + assertFalse(wrapClosed(clientEngine).hasRemaining()); |
| 286 | + |
| 287 | + // Everything should be fully closed by this point. |
| 288 | + assertTrue(clientEngine.isInboundDone()); |
| 289 | + assertTrue(clientEngine.isOutboundDone()); |
| 290 | + assertTrue(serverEngine.isInboundDone()); |
| 291 | + assertTrue(serverEngine.isOutboundDone()); |
| 292 | + |
| 293 | + // Closing inbounds should now be a no-op and also produce no new handshake data. |
| 294 | + clientEngine.closeInbound(); |
| 295 | + serverEngine.closeInbound(); |
| 296 | + assertFalse(wrapClosed(clientEngine).hasRemaining()); |
| 297 | + assertFalse(wrapClosed(serverEngine).hasRemaining()); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + public void closingInboundAfterClosingOutboundShouldSucceed_AlertsArriveAfterCloseInbound() |
| 302 | + throws Exception { |
| 303 | + // Client and server both call closeOutbound simultaneously. The alerts produced |
| 304 | + // by this both arrive at the peer after it calls closeInbound. Verify that |
| 305 | + // this produces no plainttext or further handshake data. |
| 306 | + setupEngines(TestKeyStore.getClient(), TestKeyStore.getServer()); |
| 307 | + doHandshake(true); |
| 308 | + |
| 309 | + // After closeOutbound, wrap should produce a single output buffer with the TLS close alert |
| 310 | + clientEngine.closeOutbound(); |
| 311 | + ByteBuffer clientOutput = wrapClosed(clientEngine); |
| 312 | + assertTrue(clientOutput.hasRemaining()); |
| 313 | + serverEngine.closeOutbound(); |
| 314 | + ByteBuffer serverOutput = wrapClosed(serverEngine); |
| 315 | + assertTrue(serverOutput.hasRemaining()); |
| 316 | + |
| 317 | + assertFalse(clientEngine.isInboundDone()); |
| 318 | + assertTrue(clientEngine.isOutboundDone()); |
| 319 | + assertFalse(serverEngine.isInboundDone()); |
| 320 | + assertTrue(serverEngine.isOutboundDone()); |
| 321 | + |
| 322 | + // Closing inbounds should now succeed and produce no new handshake data. |
| 323 | + clientEngine.closeInbound(); |
| 324 | + serverEngine.closeInbound(); |
| 325 | + assertFalse(wrapClosed(clientEngine).hasRemaining()); |
| 326 | + assertFalse(wrapClosed(serverEngine).hasRemaining()); |
| 327 | + |
| 328 | + // Everything should be fully closed by this point. |
| 329 | + assertTrue(clientEngine.isInboundDone()); |
| 330 | + assertTrue(clientEngine.isOutboundDone()); |
| 331 | + assertTrue(serverEngine.isInboundDone()); |
| 332 | + assertTrue(serverEngine.isOutboundDone()); |
| 333 | + |
| 334 | + // Unwrapping the previous handshake data should also succeed, and produce no |
| 335 | + //new plaintext or handshake data |
| 336 | + assertFalse(unwrapClosed(serverEngine, clientOutput).hasRemaining()); |
| 337 | + assertFalse(wrapClosed(serverEngine).hasRemaining()); |
| 338 | + assertFalse(unwrapClosed(clientEngine, serverOutput).hasRemaining()); |
| 339 | + assertFalse(wrapClosed(clientEngine).hasRemaining()); |
195 | 340 | }
|
196 | 341 |
|
197 | 342 | @Test
|
@@ -493,6 +638,30 @@ private List<ByteBuffer> wrap(ByteBuffer input, SSLEngine engine) throws SSLExce
|
493 | 638 | return wrapped;
|
494 | 639 | }
|
495 | 640 |
|
| 641 | + private ByteBuffer wrapClosed(SSLEngine engine) throws SSLException { |
| 642 | + // Call wrap with empty input and return any handshaking data produced, asserting |
| 643 | + // the engine is already in a closed state. |
| 644 | + ByteBuffer emptyInput = bufferType.newBuffer(0); |
| 645 | + ByteBuffer encryptedBuffer = |
| 646 | + bufferType.newBuffer(engine.getSession().getPacketBufferSize()); |
| 647 | + SSLEngineResult wrapResult = engine.wrap(emptyInput, encryptedBuffer); |
| 648 | + assertEquals(Status.CLOSED, wrapResult.getStatus()); |
| 649 | + encryptedBuffer.flip(); |
| 650 | + return encryptedBuffer; |
| 651 | + } |
| 652 | + |
| 653 | + private ByteBuffer unwrapClosed(SSLEngine engine, ByteBuffer encrypted) throws SSLException { |
| 654 | + // Unwrap a single buffer and return the plaintext, asserting the engine is already in a |
| 655 | + // closed state. |
| 656 | + final ByteBuffer decryptedBuffer |
| 657 | + = bufferType.newBuffer(engine.getSession().getPacketBufferSize()); |
| 658 | + |
| 659 | + SSLEngineResult unwrapResult = engine.unwrap(encrypted, decryptedBuffer); |
| 660 | + assertEquals(Status.CLOSED, unwrapResult.getStatus()); |
| 661 | + decryptedBuffer.flip(); |
| 662 | + return decryptedBuffer; |
| 663 | + } |
| 664 | + |
496 | 665 | private byte[] unwrap(ByteBuffer[] encryptedBuffers, SSLEngine engine) throws IOException {
|
497 | 666 | ByteArrayOutputStream cleartextStream = new ByteArrayOutputStream();
|
498 | 667 | int decryptedBufferSize = 8192;
|
|
0 commit comments