|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +// SunJSSE does not support dynamic system properties, no way to re-use |
| 25 | +// system properties in samevm/agentvm mode. |
| 26 | + |
| 27 | +/* |
| 28 | + * @test |
| 29 | + * @bug 8331682 |
| 30 | + * @summary Slow networks/Impatient clients can potentially send |
| 31 | + * unencrypted TLSv1.3 alerts that won't parse on the server. |
| 32 | + * @library /javax/net/ssl/templates /test/lib |
| 33 | + * @run main/othervm SSLEngineNoServerHelloClientShutdown |
| 34 | + */ |
| 35 | + |
| 36 | +import static jdk.test.lib.Asserts.assertEquals; |
| 37 | +import static jdk.test.lib.Asserts.fail; |
| 38 | +import static jdk.test.lib.security.SecurityUtils.inspectTlsBuffer; |
| 39 | + |
| 40 | +import java.nio.ByteBuffer; |
| 41 | +import java.security.GeneralSecurityException; |
| 42 | + |
| 43 | +import javax.net.ssl.SSLEngine; |
| 44 | +import javax.net.ssl.SSLEngineResult; |
| 45 | +import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
| 46 | +import javax.net.ssl.SSLProtocolException; |
| 47 | +import javax.net.ssl.SSLSession; |
| 48 | + |
| 49 | +/** |
| 50 | + * A SSLEngine usage example which simplifies the presentation |
| 51 | + * by removing the I/O and multi-threading concerns. |
| 52 | + * <p> |
| 53 | + * The test creates two SSLEngines, simulating a client and server. |
| 54 | + * The "transport" layer consists two byte buffers: think of them |
| 55 | + * as directly connected pipes. |
| 56 | + * <p> |
| 57 | + * When this application runs, notice that several messages |
| 58 | + * (wrap/unwrap) pass before any application data is consumed or |
| 59 | + * produced. |
| 60 | + */ |
| 61 | +public class SSLEngineNoServerHelloClientShutdown extends SSLContextTemplate { |
| 62 | + |
| 63 | + protected static final String EXCEPTION_MSG = |
| 64 | + "Unexpected plaintext alert received: " + |
| 65 | + "Level: warning; Alert: user_canceled"; |
| 66 | + |
| 67 | + protected SSLEngine clientEngine; // client Engine |
| 68 | + protected ByteBuffer clientOut; // write side of clientEngine |
| 69 | + protected final ByteBuffer clientIn; // read side of clientEngine |
| 70 | + |
| 71 | + protected final SSLEngine serverEngine; // server Engine |
| 72 | + protected final ByteBuffer serverOut; // write side of serverEngine |
| 73 | + protected final ByteBuffer serverIn; // read side of serverEngine |
| 74 | + |
| 75 | + protected ByteBuffer cTOs; // "reliable" transport client->server |
| 76 | + protected final ByteBuffer sTOc; // "reliable" transport server->client |
| 77 | + |
| 78 | + protected SSLEngineNoServerHelloClientShutdown() throws Exception { |
| 79 | + serverEngine = configureServerEngine( |
| 80 | + createServerSSLContext().createSSLEngine()); |
| 81 | + |
| 82 | + clientEngine = configureClientEngine( |
| 83 | + createClientSSLContext().createSSLEngine()); |
| 84 | + |
| 85 | + // We'll assume the buffer sizes are the same between client and server. |
| 86 | + SSLSession session = clientEngine.getSession(); |
| 87 | + int appBufferMax = session.getApplicationBufferSize(); |
| 88 | + int netBufferMax = session.getPacketBufferSize(); |
| 89 | + |
| 90 | + // We'll make the input buffers a bit bigger than the max needed |
| 91 | + // size, so that unwrap()s following a successful data transfer |
| 92 | + // won't generate BUFFER_OVERFLOWS. |
| 93 | + clientIn = ByteBuffer.allocate(appBufferMax + 50); |
| 94 | + serverIn = ByteBuffer.allocate(appBufferMax + 50); |
| 95 | + |
| 96 | + cTOs = ByteBuffer.allocateDirect(netBufferMax * 2); |
| 97 | + // Make it larger so subsequent server wraps won't generate |
| 98 | + // BUFFER_OVERFLOWS |
| 99 | + sTOc = ByteBuffer.allocateDirect(netBufferMax * 2); |
| 100 | + |
| 101 | + clientOut = createClientOutputBuffer(); |
| 102 | + serverOut = createServerOutputBuffer(); |
| 103 | + } |
| 104 | + |
| 105 | + protected ByteBuffer createServerOutputBuffer() { |
| 106 | + return ByteBuffer.wrap("Hello Client, I'm Server".getBytes()); |
| 107 | + } |
| 108 | + |
| 109 | + protected ByteBuffer createClientOutputBuffer() { |
| 110 | + return ByteBuffer.wrap("Hi Server, I'm Client".getBytes()); |
| 111 | + } |
| 112 | + |
| 113 | + /* |
| 114 | + * Configure the client side engine. |
| 115 | + */ |
| 116 | + protected SSLEngine configureClientEngine(SSLEngine clientEngine) { |
| 117 | + clientEngine.setUseClientMode(true); |
| 118 | + clientEngine.setEnabledProtocols(new String[] {"TLSv1.3"}); |
| 119 | + return clientEngine; |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * Configure the server side engine. |
| 124 | + */ |
| 125 | + protected SSLEngine configureServerEngine(SSLEngine serverEngine) { |
| 126 | + serverEngine.setUseClientMode(false); |
| 127 | + serverEngine.setNeedClientAuth(true); |
| 128 | + return serverEngine; |
| 129 | + } |
| 130 | + |
| 131 | + public static void main(String[] args) throws Exception { |
| 132 | + new SSLEngineNoServerHelloClientShutdown().runTestUserCancelled(); |
| 133 | + } |
| 134 | + |
| 135 | + // |
| 136 | + // Private methods that used to build the common part of the test. |
| 137 | + // |
| 138 | + |
| 139 | + private void runTestUserCancelled() throws Exception { |
| 140 | + SSLEngineResult clientResult; |
| 141 | + SSLEngineResult serverResult; |
| 142 | + |
| 143 | + log("================="); |
| 144 | + |
| 145 | + // Client: produce client_hello |
| 146 | + log("---Client Wrap client_hello---"); |
| 147 | + clientResult = clientEngine.wrap(clientOut, cTOs); |
| 148 | + logEngineStatus(clientEngine, clientResult); |
| 149 | + runDelegatedTasks(clientEngine); |
| 150 | + |
| 151 | + cTOs.flip(); |
| 152 | + |
| 153 | + // Server: consume client_hello |
| 154 | + log("---Server Unwrap client_hello---"); |
| 155 | + serverResult = serverEngine.unwrap(cTOs, serverIn); |
| 156 | + logEngineStatus(serverEngine, serverResult); |
| 157 | + runDelegatedTasks(serverEngine); |
| 158 | + |
| 159 | + cTOs.compact(); |
| 160 | + |
| 161 | + // Server: produce server_hello |
| 162 | + log("---Server Wrap server_hello---"); |
| 163 | + serverResult = serverEngine.wrap(serverOut, sTOc); |
| 164 | + logEngineStatus(serverEngine, serverResult); |
| 165 | + runDelegatedTasks(serverEngine); |
| 166 | + // SH packet went missing. Timeout on Client. |
| 167 | + |
| 168 | + // Server: produce other outbound messages |
| 169 | + log("---Server Wrap---"); |
| 170 | + serverResult = serverEngine.wrap(serverOut, sTOc); |
| 171 | + logEngineStatus(serverEngine, serverResult); |
| 172 | + runDelegatedTasks(serverEngine); |
| 173 | + // CCS packet went missing. Timeout on Client. |
| 174 | + |
| 175 | + // Server: produce other outbound messages |
| 176 | + log("---Server Wrap---"); |
| 177 | + serverResult = serverEngine.wrap(serverOut, sTOc); |
| 178 | + logEngineStatus(serverEngine, serverResult); |
| 179 | + runDelegatedTasks(serverEngine); |
| 180 | + // EE/etc. packet went missing. Timeout on Client. |
| 181 | + |
| 182 | + // Shutdown client |
| 183 | + log("---Client closeOutbound---"); |
| 184 | + clientEngine.closeOutbound(); |
| 185 | + |
| 186 | + // Client: produce an unencrypted user_canceled |
| 187 | + log("---Client Wrap user_canceled---"); |
| 188 | + clientResult = clientEngine.wrap(clientOut, cTOs); |
| 189 | + logEngineStatus(clientEngine, clientResult); |
| 190 | + runDelegatedTasks(clientEngine); |
| 191 | + |
| 192 | + cTOs.flip(); |
| 193 | + inspectTlsBuffer(cTOs); |
| 194 | + |
| 195 | + // Server unwrap should throw a proper exception when receiving an |
| 196 | + // unencrypted 2 byte packet user_canceled alert. |
| 197 | + log("---Server Unwrap user_canceled alert---"); |
| 198 | + try { |
| 199 | + serverEngine.unwrap(cTOs, serverIn); |
| 200 | + } catch (SSLProtocolException e) { |
| 201 | + assertEquals( |
| 202 | + GeneralSecurityException.class, e.getCause().getClass()); |
| 203 | + assertEquals(EXCEPTION_MSG, e.getCause().getMessage()); |
| 204 | + return; |
| 205 | + } |
| 206 | + fail("Server should have thrown SSLProtocolException"); |
| 207 | + } |
| 208 | + |
| 209 | + protected static void logEngineStatus(SSLEngine engine) { |
| 210 | + log("\tCurrent HS State: " + engine.getHandshakeStatus()); |
| 211 | + log("\tisInboundDone() : " + engine.isInboundDone()); |
| 212 | + log("\tisOutboundDone(): " + engine.isOutboundDone()); |
| 213 | + } |
| 214 | + |
| 215 | + protected static void logEngineStatus( |
| 216 | + SSLEngine engine, SSLEngineResult result) { |
| 217 | + log("\tResult Status : " + result.getStatus()); |
| 218 | + log("\tResult HS Status : " + result.getHandshakeStatus()); |
| 219 | + log("\tEngine HS Status : " + engine.getHandshakeStatus()); |
| 220 | + log("\tisInboundDone() : " + engine.isInboundDone()); |
| 221 | + log("\tisOutboundDone() : " + engine.isOutboundDone()); |
| 222 | + log("\tMore Result : " + result); |
| 223 | + } |
| 224 | + |
| 225 | + protected static void log(String message) { |
| 226 | + System.err.println(message); |
| 227 | + } |
| 228 | + |
| 229 | + // If the result indicates that we have outstanding tasks to do, |
| 230 | + // go ahead and run them in this thread. |
| 231 | + protected static void runDelegatedTasks(SSLEngine engine) |
| 232 | + throws Exception { |
| 233 | + |
| 234 | + if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
| 235 | + Runnable runnable; |
| 236 | + while ((runnable = engine.getDelegatedTask()) != null) { |
| 237 | + log(" running delegated task..."); |
| 238 | + runnable.run(); |
| 239 | + } |
| 240 | + HandshakeStatus hsStatus = engine.getHandshakeStatus(); |
| 241 | + if (hsStatus == HandshakeStatus.NEED_TASK) { |
| 242 | + throw new Exception( |
| 243 | + "handshake shouldn't need additional tasks"); |
| 244 | + } |
| 245 | + logEngineStatus(engine); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments