1 /* 2 * Copyright (c) 2004, 2007, 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 5019096 27 * @summary Add scatter/gather APIs for SSLEngine 28 * 29 * Check to see if the args are being parsed properly. 30 * 31 */ 32 33 import javax.net.ssl.*; 34 import javax.net.ssl.SSLEngineResult.*; 35 import java.io.*; 36 import java.security.*; 37 import java.nio.*; 38 39 public class ArgCheck { 40 41 private static boolean debug = false; 42 43 private static String pathToStores = "../etc"; 44 private static String keyStoreFile = "keystore"; 45 private static String trustStoreFile = "truststore"; 46 private static String passwd = "passphrase"; 47 48 private static String keyFilename = 49 System.getProperty("test.src", "./") + "/" + pathToStores + 50 "/" + keyStoreFile; 51 private static String trustFilename = 52 System.getProperty("test.src", "./") + "/" + pathToStores + 53 "/" + trustStoreFile; 54 tryNull(SSLEngine ssle, ByteBuffer appData, ByteBuffer netData)55 private static void tryNull(SSLEngine ssle, ByteBuffer appData, 56 ByteBuffer netData) throws Exception { 57 try { 58 ssle.wrap(appData, netData); 59 throw new Exception(); 60 } catch (IllegalArgumentException e) { 61 System.out.println("Caught right exception"); 62 } 63 64 try { 65 ssle.unwrap(netData, appData); 66 throw new Exception(); 67 } catch (IllegalArgumentException e) { 68 System.out.println("Caught right exception"); 69 } 70 } 71 tryNullArray(SSLEngine ssle, ByteBuffer [] appData, ByteBuffer netData)72 private static void tryNullArray(SSLEngine ssle, ByteBuffer [] appData, 73 ByteBuffer netData) throws Exception { 74 try { 75 ssle.wrap(appData, netData); 76 throw new Exception(); 77 } catch (IllegalArgumentException e) { 78 System.out.println("Caught right exception"); 79 } 80 81 try { 82 ssle.unwrap(netData, appData); 83 throw new Exception(); 84 } catch (IllegalArgumentException e) { 85 System.out.println("Caught right exception"); 86 } 87 } 88 tryNullArrayLen(SSLEngine ssle, ByteBuffer [] appData, int offset, int len, ByteBuffer netData)89 private static void tryNullArrayLen(SSLEngine ssle, ByteBuffer [] appData, 90 int offset, int len, ByteBuffer netData) throws Exception { 91 try { 92 ssle.wrap(appData, offset, len, netData); 93 throw new Exception(); 94 } catch (IllegalArgumentException e) { 95 System.out.println("Caught right exception"); 96 } 97 98 try { 99 ssle.unwrap(netData, appData, offset, len); 100 throw new Exception(); 101 } catch (IllegalArgumentException e) { 102 System.out.println("Caught right exception"); 103 } 104 } 105 tryReadOnly(SSLEngine ssle, ByteBuffer [] appData, int offset, int len, ByteBuffer netData)106 private static void tryReadOnly(SSLEngine ssle, ByteBuffer [] appData, 107 int offset, int len, ByteBuffer netData) throws Exception { 108 try { 109 if (netData.isReadOnly()) { 110 ssle.wrap(appData, offset, len, netData); 111 throw new Exception(); 112 } 113 } catch (ReadOnlyBufferException e) { 114 System.out.println("Caught right exception"); 115 } 116 117 try { 118 if (!netData.isReadOnly()) { 119 ssle.unwrap(netData, appData, offset, len); 120 throw new Exception(); 121 } 122 } catch (ReadOnlyBufferException e) { 123 System.out.println("Caught right exception"); 124 } 125 } 126 tryOutOfBounds(SSLEngine ssle, ByteBuffer [] appData, int offset, int len, ByteBuffer netData)127 private static void tryOutOfBounds(SSLEngine ssle, ByteBuffer [] appData, 128 int offset, int len, ByteBuffer netData) throws Exception { 129 try { 130 ssle.wrap(appData, offset, len, netData); 131 throw new Exception(); 132 } catch (IndexOutOfBoundsException e) { 133 System.out.println("Caught right exception"); 134 } 135 136 try { 137 ssle.unwrap(netData, appData, offset, len); 138 throw new Exception(); 139 } catch (IndexOutOfBoundsException e) { 140 System.out.println("Caught right exception"); 141 } 142 } 143 trySmallBufs(SSLEngine ssle, ByteBuffer appBB, ByteBuffer smallNetBB, ByteBuffer smallAppBB, ByteBuffer netBB)144 private static void trySmallBufs(SSLEngine ssle, 145 ByteBuffer appBB, ByteBuffer smallNetBB, 146 ByteBuffer smallAppBB, ByteBuffer netBB) throws Exception { 147 148 SSLEngineResult res = ssle.wrap(appBB, smallNetBB); 149 if (res.getStatus() != Status.BUFFER_OVERFLOW) { 150 throw new Exception(); 151 } 152 153 // For unwrap(), the BUFFER_OVERFLOW will not be generated 154 // until received SSL/TLS application data. 155 // Test test/javax/net/ssl/NewAPIs/SSLEngine/LargePacket will check 156 // BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap(). 157 // 158 //res = ssle.unwrap(netBB, smallAppBB); 159 //if (res.getStatus() != Status.BUFFER_OVERFLOW) { 160 // throw new Exception(); 161 //} 162 } 163 trySmallBufsArray(SSLEngine ssle, ByteBuffer [] appBB, ByteBuffer smallNetBB, ByteBuffer [] smallAppBB, ByteBuffer netBB)164 private static void trySmallBufsArray(SSLEngine ssle, 165 ByteBuffer [] appBB, ByteBuffer smallNetBB, 166 ByteBuffer [] smallAppBB, ByteBuffer netBB) throws Exception { 167 168 SSLEngineResult res = ssle.wrap(appBB, 0, appBB.length, smallNetBB); 169 if (res.getStatus() != Status.BUFFER_OVERFLOW) { 170 throw new Exception(); 171 } 172 173 // For unwrap(), the BUFFER_OVERFLOW will not be generated 174 // until received SSL/TLS application data. 175 // Test test/javax/net/ssl/NewAPIs/SSLEngine/LargePacket will check 176 // BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap(). 177 // 178 //res = ssle.unwrap(netBB, smallAppBB, 0, appBB.length); 179 //if (res.getStatus() != Status.BUFFER_OVERFLOW) { 180 // throw new Exception(); 181 //} 182 } 183 runTest(SSLEngine ssle)184 private static void runTest(SSLEngine ssle) throws Exception { 185 ByteBuffer [] bufs; 186 187 ByteBuffer roBB = ByteBuffer.allocate(40).asReadOnlyBuffer(); 188 189 ByteBuffer bb1K = ByteBuffer.allocate(1024); 190 ByteBuffer bb2K = ByteBuffer.allocate(2048); 191 ByteBuffer bb4K = ByteBuffer.allocate(5096); 192 ByteBuffer bb8K = ByteBuffer.allocate(10192); 193 194 SSLSession ssls = ssle.getSession(); 195 196 ByteBuffer netBBMinus1 = ByteBuffer.allocate( 197 ssls.getPacketBufferSize() - 1); 198 ByteBuffer appBBMinus1 = ByteBuffer.allocate( 199 ssls.getApplicationBufferSize() - 1); 200 201 ByteBuffer bbNet = ByteBuffer.allocate( 202 ssls.getPacketBufferSize()); 203 ByteBuffer bbApp = ByteBuffer.allocate( 204 ssls.getApplicationBufferSize()); 205 206 bufs = new ByteBuffer [] { bb1K, bb2K, bb4K, bb8K}; 207 208 tryNull(ssle, null, null); 209 tryNull(ssle, bb1K, null); 210 tryNull(ssle, null, bb1K); 211 212 tryNullArray(ssle, null, null); 213 tryNullArray(ssle, bufs, null); 214 tryNullArray(ssle, null, bb1K); 215 216 tryNullArrayLen(ssle, null, 0, 4, null); 217 tryNullArrayLen(ssle, bufs, 0, 4, null); 218 tryNullArrayLen(ssle, null, 0, 4, bb1K); 219 220 bufs[2] = null; 221 tryNullArray(ssle, bufs, bb1K); 222 223 bufs[2] = bb4K; 224 tryReadOnly(ssle, bufs, 0, 4, roBB); 225 226 bufs[2] = roBB; 227 tryReadOnly(ssle, bufs, 0, 4, bb4K); 228 229 bufs[2] = bb4K; 230 tryOutOfBounds(ssle, bufs, -1, 0, bb1K); 231 tryOutOfBounds(ssle, bufs, 0, -1, bb1K); 232 tryOutOfBounds(ssle, bufs, 0, bufs.length + 1, bb1K); 233 tryOutOfBounds(ssle, bufs, bufs.length, 1, bb1K); 234 235 bufs[3].position(bufs[3].limit()); 236 trySmallBufs(ssle, bb1K, netBBMinus1, appBBMinus1, bb1K); 237 trySmallBufsArray(ssle, bufs, netBBMinus1, bufs, bb1K); 238 bufs[3].rewind(); 239 240 } 241 main(String args[])242 public static void main(String args[]) throws Exception { 243 244 SSLEngine ssle = createSSLEngine(keyFilename, trustFilename); 245 runTest(ssle); 246 247 System.out.println("Test Passed."); 248 } 249 250 /* 251 * Create an initialized SSLContext to use for this test. 252 */ createSSLEngine(String keyFile, String trustFile)253 static private SSLEngine createSSLEngine(String keyFile, String trustFile) 254 throws Exception { 255 256 SSLEngine ssle; 257 258 KeyStore ks = KeyStore.getInstance("JKS"); 259 KeyStore ts = KeyStore.getInstance("JKS"); 260 261 char[] passphrase = "passphrase".toCharArray(); 262 263 ks.load(new FileInputStream(keyFile), passphrase); 264 ts.load(new FileInputStream(trustFile), passphrase); 265 266 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 267 kmf.init(ks, passphrase); 268 269 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 270 tmf.init(ts); 271 272 SSLContext sslCtx = SSLContext.getInstance("TLS"); 273 274 sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 275 276 ssle = sslCtx.createSSLEngine("client", 1001); 277 ssle.setUseClientMode(true); 278 279 return ssle; 280 } 281 } 282