|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import javax.net.ssl.SSLContext; |
| 25 | +import javax.net.ssl.SSLEngine; |
| 26 | +import javax.net.ssl.SSLEngineResult; |
| 27 | +import javax.net.ssl.SSLException; |
| 28 | +import java.nio.ByteBuffer; |
| 29 | +import java.security.NoSuchAlgorithmException; |
| 30 | +import java.util.HexFormat; |
| 31 | + |
| 32 | +/* |
| 33 | + * @test |
| 34 | + * @bug 8293176 |
| 35 | + * @summary SSLEngine should close after bad key share |
| 36 | + * @run main SSLEngineDecodeBadPoint |
| 37 | + */ |
| 38 | +public class SSLEngineDecodeBadPoint { |
| 39 | + static final byte[] clientHello = HexFormat.of().parseHex( |
| 40 | + "160303013a0100013603031570" + |
| 41 | + "151d6066940aa5dfcecc99f470bfdc175eec2c6f3273b079b2f80b49" + |
| 42 | + "75c820efe3d307201492a49fcee79fac5b2f05dca26c572b65b0d90d" + |
| 43 | + "81f51fd26b49b700021302010000eb000500050100000000000a0016" + |
| 44 | + "0014001d001700180019001e01000101010201030104000d00220020" + |
| 45 | + "040305030603080708080804080508060809080a080b040105010601" + |
| 46 | + "02030201002b0003020304002d000201010032002200200403050306" + |
| 47 | + "03080708080804080508060809080a080b0401050106010203020100" + |
| 48 | + "33006b0069001d00209a4d13131f83cc4c5be46520f0b4d7a6f1d3f6" + |
| 49 | + "ca7118e6dd115125090da6e044" + |
| 50 | + // ECDHE key share, 5th byte changed from 04 to (invalid) 05 |
| 51 | + "0017004105d8c3734b9c729f6a9851" + |
| 52 | + "5049543ec5a9bb6c19b8c02ca0bdc3b20a77c44acdab226b6329b7c5" + |
| 53 | + "db9204421932c6fa1abe614c6892f5289edf9ff43cac534cad9e"); |
| 54 | + |
| 55 | + public static void main(String[] args) throws NoSuchAlgorithmException, SSLException { |
| 56 | + SSLContext ctx = SSLContext.getDefault(); |
| 57 | + SSLEngine eng = ctx.createSSLEngine(); |
| 58 | + eng.setUseClientMode(false); |
| 59 | + eng.beginHandshake(); |
| 60 | + ByteBuffer hello = ByteBuffer.wrap(clientHello); |
| 61 | + ByteBuffer emptyBuf = ByteBuffer.allocate(0); |
| 62 | + SSLEngineResult res = eng.unwrap(hello, emptyBuf); |
| 63 | + System.out.println("status after unwrap: " + res); |
| 64 | + eng.getDelegatedTask().run(); |
| 65 | + |
| 66 | + SSLEngineResult.HandshakeStatus status = eng.getHandshakeStatus(); |
| 67 | + System.out.println("status after task: " + status); |
| 68 | + if (status != SSLEngineResult.HandshakeStatus.NEED_WRAP) { |
| 69 | + throw new RuntimeException("Unexpected status after task: " + status); |
| 70 | + } |
| 71 | + ByteBuffer alert = ByteBuffer.allocate(eng.getSession().getPacketBufferSize()); |
| 72 | + |
| 73 | + try { |
| 74 | + eng.wrap(emptyBuf, alert); |
| 75 | + throw new RuntimeException("Expected wrap to throw"); |
| 76 | + } catch (SSLException e) { |
| 77 | + System.err.println("Received expected SSLException:"); |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + if (alert.position() != 0) { |
| 81 | + throw new RuntimeException("Expected no bytes written in first wrap call"); |
| 82 | + } |
| 83 | + res = eng.wrap(emptyBuf, alert); |
| 84 | + System.out.println("status after wrap: " + res); |
| 85 | + if (res.getStatus() != SSLEngineResult.Status.CLOSED || |
| 86 | + res.getHandshakeStatus() != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { |
| 87 | + throw new RuntimeException("Unexpected status after wrap: " + res); |
| 88 | + } |
| 89 | + if (!eng.isOutboundDone()) { |
| 90 | + throw new RuntimeException("Expected outbound done"); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments