|
36 | 36 | import javax.crypto.spec.SecretKeySpec; |
37 | 37 | import javax.net.ssl.SSLEngine; |
38 | 38 | import javax.net.ssl.SSLEngineResult; |
| 39 | +import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
39 | 40 | import javax.net.ssl.SSLException; |
40 | 41 | import javax.net.ssl.SSLParameters; |
41 | 42 | import java.nio.ByteBuffer; |
|
61 | 62 | import static org.junit.Assert.assertArrayEquals; |
62 | 63 | import static org.junit.Assert.assertEquals; |
63 | 64 | import static org.junit.Assert.assertFalse; |
| 65 | +import static org.junit.Assert.assertNotEquals; |
64 | 66 | import static org.junit.Assert.assertNull; |
65 | 67 | import static org.junit.Assert.assertSame; |
66 | 68 | import static org.junit.Assert.assertTrue; |
@@ -1131,6 +1133,19 @@ public boolean permits( |
1131 | 1133 | } |
1132 | 1134 | } |
1133 | 1135 |
|
| 1136 | + private static void runTasksIfNeeded(SSLEngine engine) { |
| 1137 | + if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
| 1138 | + for (;;) { |
| 1139 | + Runnable task = engine.getDelegatedTask(); |
| 1140 | + if (task == null) { |
| 1141 | + assertNotEquals(HandshakeStatus.NEED_TASK, engine.getHandshakeStatus()); |
| 1142 | + break; |
| 1143 | + } |
| 1144 | + task.run(); |
| 1145 | + } |
| 1146 | + } |
| 1147 | + } |
| 1148 | + |
1134 | 1149 | @Test |
1135 | 1150 | public void testExtractMasterkeyWorksCorrectly() throws Exception { |
1136 | 1151 | SelfSignedCertificate cert = new SelfSignedCertificate(); |
@@ -1199,17 +1214,23 @@ public void testExtractMasterkeyWorksCorrectly() throws Exception { |
1199 | 1214 | cTOs.flip(); |
1200 | 1215 | sTOc.flip(); |
1201 | 1216 |
|
| 1217 | + runTasksIfNeeded(clientEngine); |
| 1218 | + runTasksIfNeeded(serverEngine); |
| 1219 | + |
1202 | 1220 | clientEngine.unwrap(sTOc, clientIn); |
1203 | 1221 | serverEngine.unwrap(cTOs, serverIn); |
1204 | 1222 |
|
| 1223 | + runTasksIfNeeded(clientEngine); |
| 1224 | + runTasksIfNeeded(serverEngine); |
| 1225 | + |
1205 | 1226 | // check when the application data has fully been consumed and sent |
1206 | 1227 | // for both the client and server |
1207 | 1228 | if ((clientOut.limit() == serverIn.position()) && |
1208 | 1229 | (serverOut.limit() == clientIn.position())) { |
1209 | | - byte[] serverRandom = SSL.getServerRandom(((OpenSslEngine) serverEngine).sslPointer()); |
1210 | | - byte[] clientRandom = SSL.getClientRandom(((OpenSslEngine) clientEngine).sslPointer()); |
1211 | | - byte[] serverMasterKey = SSL.getMasterKey(((OpenSslEngine) serverEngine).sslPointer()); |
1212 | | - byte[] clientMasterKey = SSL.getMasterKey(((OpenSslEngine) clientEngine).sslPointer()); |
| 1230 | + byte[] serverRandom = SSL.getServerRandom(unwrapEngine(serverEngine).sslPointer()); |
| 1231 | + byte[] clientRandom = SSL.getClientRandom(unwrapEngine(clientEngine).sslPointer()); |
| 1232 | + byte[] serverMasterKey = SSL.getMasterKey(unwrapEngine(serverEngine).sslPointer()); |
| 1233 | + byte[] clientMasterKey = SSL.getMasterKey(unwrapEngine(clientEngine).sslPointer()); |
1213 | 1234 |
|
1214 | 1235 | asserted = true; |
1215 | 1236 | assertArrayEquals(serverMasterKey, clientMasterKey); |
@@ -1318,7 +1339,9 @@ ReferenceCountedOpenSslEngine unwrapEngine(SSLEngine engine) { |
1318 | 1339 |
|
1319 | 1340 | @Override |
1320 | 1341 | protected SslContext wrapContext(SslContext context) { |
1321 | | - ((OpenSslContext) context).setUseTasks(useTasks); |
| 1342 | + if (context instanceof OpenSslContext) { |
| 1343 | + ((OpenSslContext) context).setUseTasks(useTasks); |
| 1344 | + } |
1322 | 1345 | return context; |
1323 | 1346 | } |
1324 | 1347 | } |
0 commit comments