|
| 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8294985 |
| 27 | + * @library /test/lib |
| 28 | + * @summary SSLEngine throws IAE during parsing of X500Principal |
| 29 | + * @run main/othervm TestBadDNForPeerCA |
| 30 | + * @run main/othervm -Djavax.net.debug=all TestBadDNForPeerCA |
| 31 | + */ |
| 32 | + |
| 33 | +import javax.net.ssl.KeyManagerFactory; |
| 34 | +import javax.net.ssl.SSLContext; |
| 35 | +import javax.net.ssl.SSLEngine; |
| 36 | +import javax.net.ssl.SSLEngineResult; |
| 37 | +import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
| 38 | +import javax.net.ssl.SSLHandshakeException; |
| 39 | +import javax.net.ssl.TrustManagerFactory; |
| 40 | +import java.io.FileInputStream; |
| 41 | +import java.nio.ByteBuffer; |
| 42 | +import java.security.KeyStore; |
| 43 | +import java.util.Base64; |
| 44 | + |
| 45 | + |
| 46 | +public class TestBadDNForPeerCA { |
| 47 | + |
| 48 | + private static final String proto = "TLSv1.3"; |
| 49 | + |
| 50 | + private final SSLContext sslc; |
| 51 | + |
| 52 | + private SSLEngine serverEngine; // server Engine |
| 53 | + private ByteBuffer serverIn; // read side of serverEngine |
| 54 | + |
| 55 | + private ByteBuffer cTOs; // "reliable" transport client->server |
| 56 | + |
| 57 | + private static final String keyStoreFile = |
| 58 | + System.getProperty("test.src", "./") |
| 59 | + + "/../../../../javax/net/ssl/etc/keystore"; |
| 60 | + |
| 61 | + // the following ClientHello contains a certificate with an |
| 62 | + // invalid/unparseable distinguished name |
| 63 | + private static final byte[] payload = Base64.getDecoder().decode( |
| 64 | + "FgMDAcsBAAHHAwPbDfeUCIStPzVIfXuGgCu56dSJOJ6xeus1W44frG5tciDEcBfYt" |
| 65 | + + "/PN/6MFCGojEVcmPw21mVyjYInMo0UozIn4NwBiEwITARMDwCzAK8ypwDDMqMAvA" |
| 66 | + + "J/MqgCjAJ4AosAkwCjAI8AnAGsAagBnAEDALsAywC3AMcAmCgAFKsApJcDAFMAJw" |
| 67 | + + "BMAOQA4ADMAMsAFwA/ABMAOAJ0AnAA9ADwANgAvAP8BAAEcAAUABQEAAAAAAAoAF" |
| 68 | + + "gAUAB0AFwAYABkAHgEAAQEBAgEDAQQACwACAQAAEQAJAAcCAAQAAAAAABcAAAAjA" |
| 69 | + + "AAADQAsACoEAwUDBgMIBwgICAQIBQgGCAkICggLBAEFAQYBBAIDAwMBAwICAwIBA" |
| 70 | + + "gIAKwAFBAMEAwMALQACAQEAMgAsACoEAwUDBgMIBwgICAQIBQgGCAkICggLBAEFA" |
| 71 | + + "QYBBAIDAwMBAwICAwIBAgIALwBrAGkAHQAAAAARACAAZMUAADkwsiaOwcsWAwAAA" |
| 72 | + + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 73 | + + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 74 | + + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 75 | + + "AAAAAAtAAAAAAAAAAEAADAAAAA="); |
| 76 | + |
| 77 | + /* |
| 78 | + * The following is to set up the keystores. |
| 79 | + */ |
| 80 | + private static final String passwd = "passphrase"; |
| 81 | + |
| 82 | + /* |
| 83 | + * Main entry point for this demo. |
| 84 | + */ |
| 85 | + public static void main(String[] args) throws Exception { |
| 86 | + |
| 87 | + TestBadDNForPeerCA test = new TestBadDNForPeerCA(); |
| 88 | + |
| 89 | + try { |
| 90 | + test.runTest(); |
| 91 | + throw new Exception( |
| 92 | + "TEST FAILED: Didn't generate any exception"); |
| 93 | + } catch (SSLHandshakeException she) { |
| 94 | + System.out.println("TEST PASSED: Caught expected exception"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * Create an initialized SSLContext to use for this demo. |
| 100 | + */ |
| 101 | + |
| 102 | + public TestBadDNForPeerCA() throws Exception { |
| 103 | + |
| 104 | + KeyStore ks = KeyStore.getInstance("JKS"); |
| 105 | + KeyStore ts = KeyStore.getInstance("JKS"); |
| 106 | + |
| 107 | + char[] passphrase = passwd.toCharArray(); |
| 108 | + |
| 109 | + ks.load(new FileInputStream(keyStoreFile), passphrase); |
| 110 | + ts.load(new FileInputStream(keyStoreFile), passphrase); |
| 111 | + |
| 112 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 113 | + kmf.init(ks, passphrase); |
| 114 | + |
| 115 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
| 116 | + tmf.init(ts); |
| 117 | + |
| 118 | + SSLContext sslCtx = SSLContext.getInstance(proto); |
| 119 | + |
| 120 | + sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
| 121 | + |
| 122 | + sslc = sslCtx; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + private void runTest() throws Exception { |
| 127 | + |
| 128 | + createSSLEngines(); |
| 129 | + createBuffers(); |
| 130 | + |
| 131 | + cTOs = ByteBuffer.wrap(payload); |
| 132 | + |
| 133 | + System.out.println("injecting client hello"); |
| 134 | + |
| 135 | + for (int i = 0; i < 10; i++) { //retry if survived |
| 136 | + SSLEngineResult serverResult = serverEngine.unwrap(cTOs, serverIn); |
| 137 | + System.out.println("server unwrap: " + serverResult); |
| 138 | + runDelegatedTasks(serverResult, serverEngine); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void createSSLEngines() throws Exception { |
| 143 | + |
| 144 | + serverEngine = sslc.createSSLEngine(); |
| 145 | + serverEngine.setUseClientMode(false); |
| 146 | + serverEngine.setNeedClientAuth(true); |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + private void createBuffers() { |
| 152 | + |
| 153 | + serverIn = ByteBuffer.allocateDirect(65536); |
| 154 | + |
| 155 | + cTOs = ByteBuffer.allocateDirect(65536); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + private static void runDelegatedTasks(SSLEngineResult result, |
| 160 | + SSLEngine engine) throws Exception { |
| 161 | + |
| 162 | + if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
| 163 | + Runnable runnable; |
| 164 | + while ((runnable = engine.getDelegatedTask()) != null) { |
| 165 | + System.out.println("\trunning delegated task..."); |
| 166 | + runnable.run(); |
| 167 | + } |
| 168 | + |
| 169 | + HandshakeStatus hsStatus = engine.getHandshakeStatus(); |
| 170 | + if (hsStatus == HandshakeStatus.NEED_TASK) { |
| 171 | + throw new Exception("handshake shouldn't need additional " + |
| 172 | + "tasks"); |
| 173 | + } |
| 174 | + System.out.println("\tnew HandshakeStatus: " + hsStatus); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | +} |
0 commit comments