Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/Basics.java
41152 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 449574226* @summary Add non-blocking SSL/TLS functionality, usable with any27* I/O abstraction28* @ignore JSSE supported cipher suites are changed with CR 6916074,29* need to update this test case in JDK 7 soon30*31* This is intended to test many of the basic API calls to the SSLEngine32* interface. This doesn't really exercise much of the SSL code.33*34* @author Brad Wetmore35*/3637import java.security.*;38import java.io.*;39import java.nio.*;40import javax.net.ssl.*;41import javax.net.ssl.SSLEngineResult.*;4243public class Basics {4445private static String pathToStores = "../etc";46private static String keyStoreFile = "keystore";47private static String trustStoreFile = "truststore";48private static String passwd = "passphrase";4950private static String keyFilename =51System.getProperty("test.src", "./") + "/" + pathToStores +52"/" + keyStoreFile;53private static String trustFilename =54System.getProperty("test.src", "./") + "/" + pathToStores +55"/" + trustStoreFile;5657public static void main(String args[]) throws Exception {5859KeyStore ks = KeyStore.getInstance("JKS");60KeyStore ts = KeyStore.getInstance("JKS");61char[] passphrase = "passphrase".toCharArray();6263ks.load(new FileInputStream(keyFilename), passphrase);64ts.load(new FileInputStream(trustFilename), passphrase);6566KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");67kmf.init(ks, passphrase);6869TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");70tmf.init(ks);7172SSLContext sslCtx = SSLContext.getInstance("TLS");7374sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);7576SSLEngine ssle = sslCtx.createSSLEngine();7778System.out.println(ssle);7980String [] suites = ssle.getSupportedCipherSuites();81String secondSuite = suites[1];82String [] oneSuites = new String [] { secondSuite };8384printStrings("Supported Ciphersuites", suites);85printStrings("Enabled Ciphersuites", ssle.getEnabledCipherSuites());86ssle.setEnabledCipherSuites(oneSuites);87printStrings("Set Ciphersuites", ssle.getEnabledCipherSuites());8889suites = ssle.getEnabledCipherSuites();90if ((ssle.getEnabledCipherSuites().length != 1) ||91!(suites[0].equals(secondSuite))) {92throw new Exception("set ciphers not what was expected");93}9495System.out.println();9697String [] protocols = ssle.getSupportedProtocols();98String secondProtocol = protocols[1];99String [] oneProtocols = new String [] { protocols[1] };100101printStrings("Supported Protocols", protocols);102printStrings("Enabled Protocols", ssle.getEnabledProtocols());103ssle.setEnabledProtocols(oneProtocols);104printStrings("Set Protocols", ssle.getEnabledProtocols());105106protocols = ssle.getEnabledProtocols();107if ((ssle.getEnabledProtocols().length != 1) ||108!(protocols[0].equals(secondProtocol))) {109throw new Exception("set protocols not what was expected");110}111112System.out.println("Checking get/setUseClientMode");113114ssle.setUseClientMode(true);115if (ssle.getUseClientMode() != true) {116throw new Exception("set/getUseClientMode false");117}118119ssle.setUseClientMode(false);120if (ssle.getUseClientMode() != false) {121throw new Exception("set/getUseClientMode true");122}123124125System.out.println("Checking get/setClientAuth");126127ssle.setNeedClientAuth(false);128if (ssle.getNeedClientAuth() != false) {129throw new Exception("set/getNeedClientAuth true");130}131132ssle.setNeedClientAuth(true);133if (ssle.getNeedClientAuth() != true) {134throw new Exception("set/getNeedClientAuth false");135}136137ssle.setWantClientAuth(true);138139if (ssle.getNeedClientAuth() == true) {140throw new Exception("set/getWantClientAuth need = true");141}142143if (ssle.getWantClientAuth() != true) {144throw new Exception("set/getNeedClientAuth false");145}146147ssle.setWantClientAuth(false);148if (ssle.getWantClientAuth() != false) {149throw new Exception("set/getNeedClientAuth true");150}151152/*153* Reset back to client mode154*/155ssle.setUseClientMode(true);156157System.out.println("checking session creation");158159ssle.setEnableSessionCreation(false);160if (ssle.getEnableSessionCreation() != false) {161throw new Exception("set/getSessionCreation true");162}163164ssle.setEnableSessionCreation(true);165if (ssle.getEnableSessionCreation() != true) {166throw new Exception("set/getSessionCreation false");167}168169/* Checking for overflow wrap/unwrap() */170ByteBuffer smallBB = ByteBuffer.allocate(10);171172if (ssle.wrap(smallBB, smallBB).getStatus() !=173Status.BUFFER_OVERFLOW) {174throw new Exception("wrap should have overflowed");175}176177// For unwrap(), the BUFFER_OVERFLOW will not be generated178// until received SSL/TLS application data.179// Test test/jdk/javax/net/ssl/SSLEngine/LargePacket.java will check180// BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap().181//182//if (ssle.unwrap(smallBB, smallBB).getStatus() !=183// Status.BUFFER_OVERFLOW) {184// throw new Exception("unwrap should have overflowed");185//}186187SSLSession ssls = ssle.getSession();188189ByteBuffer appBB =190ByteBuffer.allocate(ssls.getApplicationBufferSize());191ByteBuffer netBB =192ByteBuffer.allocate(ssls.getPacketBufferSize());193appBB.position(10);194195/*196* start handshake, drain buffer197*/198if (ssle.wrap(appBB, netBB).getHandshakeStatus() !=199HandshakeStatus.NEED_UNWRAP) {200throw new Exception("initial client hello needs unwrap");201}202203/* Checking for overflow wrap/unwrap() */204205if (ssle.wrap(appBB, netBB).getStatus() !=206Status.BUFFER_OVERFLOW) {207throw new Exception("unwrap should have overflowed");208}209210ByteBuffer ro = appBB.asReadOnlyBuffer();211212System.out.println("checking for wrap/unwrap on RO Buffers");213try {214ssle.wrap(netBB, ro);215throw new Exception("wrap wasn't ReadOnlyBufferException");216} catch (ReadOnlyBufferException e) {217System.out.println("Caught the ReadOnlyBuffer: " + e);218}219220try {221ssle.unwrap(netBB, ro);222throw new Exception("unwrap wasn't ReadOnlyBufferException");223} catch (ReadOnlyBufferException e) {224System.out.println("Caught the ReadOnlyBuffer: " + e);225}226227appBB.position(0);228System.out.println("Check various UNDERFLOW conditions");229230SSLEngineResult sslER;231232if ((sslER =233ssle.unwrap(ByteBuffer.wrap(smallSSLHeader),234appBB)).getStatus() !=235Status.BUFFER_UNDERFLOW) {236System.out.println(sslER);237throw new Exception("unwrap should underflow");238}239240if ((sslER =241ssle.unwrap(ByteBuffer.wrap(incompleteSSLHeader),242appBB)).getStatus() !=243Status.BUFFER_UNDERFLOW) {244System.out.println(sslER);245throw new Exception("unwrap should underflow");246}247248if ((sslER =249ssle.unwrap(ByteBuffer.wrap(smallv2Header),250appBB)).getStatus() !=251Status.BUFFER_UNDERFLOW) {252System.out.println(sslER);253throw new Exception("unwrap should underflow");254}255256// junk inbound message257try {258ssle.unwrap(ByteBuffer.wrap(gobblydegook), appBB);259throw new Exception("Didn't catch the nasty SSLException");260} catch (SSLException e) {261System.out.println("caught the nasty SSLException: " + e);262}263264System.out.println("Test PASSED");265266}267268static byte [] smallSSLHeader = new byte [] {269(byte) 0x16, (byte) 0x03, (byte) 0x01,270(byte) 0x05 };271272static byte [] incompleteSSLHeader = new byte [] {273(byte) 0x16, (byte) 0x03, (byte) 0x01,274(byte) 0x00, (byte) 0x5, // 5 bytes275(byte) 0x16, (byte) 0x03, (byte) 0x01, (byte) 0x00 };276277static byte [] smallv2Header = new byte [] {278(byte) 0x80, (byte) 0x03, (byte) 0x01,279(byte) 0x00 };280281static byte [] gobblydegook = new byte [] {282// "HELLO HELLO"283(byte) 0x48, (byte) 0x45, (byte) 0x4C, (byte) 0x4C, (byte) 0x20,284(byte) 0x48, (byte) 0x45, (byte) 0x4C, (byte) 0x4C };285286static void printStrings(String label, String [] strs) {287System.out.println(label);288289for (int i = 0; i < strs.length; i++) {290System.out.println(" " + strs[i]);291}292}293}294295296