Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapCBPropertiesTest.java
41153 views
/*1* Copyright (c) 2020, Azul Systems, Inc. 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 824552726* @library lib/ /test/lib27* @run main/othervm LdapCBPropertiesTest true true com.sun.jndi.ldap.tls.cbtype tls-server-end-point28* @run main/othervm LdapCBPropertiesTest false false com.sun.jndi.ldap.tls.cbtype tls-server-end-point29* @run main/othervm LdapCBPropertiesTest true true com.sun.jndi.ldap.tls.cbtype tls-server-end-point com.sun.jndi.ldap.connect.timeout 200030* @run main/othervm LdapCBPropertiesTest false false com.sun.jndi.ldap.tls.cbtype tls-server-end-point com.sun.jndi.ldap.connect.timeout 200031* @run main/othervm LdapCBPropertiesTest false true com.sun.jndi.ldap.tls.cbtype tls-unknown32* @run main/othervm LdapCBPropertiesTest false true jdk.internal.sasl.tlschannelbinding value33* @summary test new JNDI property to control the Channel Binding data34*/3536import javax.naming.AuthenticationException;37import javax.naming.CommunicationException;38import javax.naming.Context;39import javax.naming.NamingException;40import javax.naming.directory.DirContext;41import javax.naming.directory.InitialDirContext;42import java.net.InetAddress;43import java.net.URI;44import java.util.Hashtable;4546import org.ietf.jgss.GSSException;4748import javax.net.ssl.SSLException;49import javax.net.ssl.SSLServerSocket;50import javax.net.ssl.SSLServerSocketFactory;51import javax.security.sasl.SaslException;5253import jdk.test.lib.net.URIBuilder;5455public class LdapCBPropertiesTest {56/*57* Where do we find the keystores?58*/59static String pathToStores = "../../../../javax/net/ssl/etc";60static String keyStoreFile = "keystore";61static String trustStoreFile = "truststore";62static String passwd = "passphrase";6364static boolean debug = false;6566public static void main(String[] args) throws Exception {67String keyFilename =68System.getProperty("test.src", "./") + "/" + pathToStores +69"/" + keyStoreFile;70String trustFilename =71System.getProperty("test.src", "./") + "/" + pathToStores +72"/" + trustStoreFile;7374System.setProperty("javax.net.ssl.keyStore", keyFilename);75System.setProperty("javax.net.ssl.keyStorePassword", passwd);76System.setProperty("javax.net.ssl.trustStore", trustFilename);77System.setProperty("javax.net.ssl.trustStorePassword", passwd);7879if (debug)80System.setProperty("javax.net.debug", "all");8182/*83* Start the tests.84*/85new LdapCBPropertiesTest(args);86}8788/*89* Primary constructor, used to drive remainder of the test.90*/91LdapCBPropertiesTest(String[] args) throws Exception {92InetAddress loopback = InetAddress.getLoopbackAddress();93SSLServerSocketFactory sslssf =94(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();95SSLServerSocket sslServerSocket =96(SSLServerSocket) sslssf.createServerSocket(0, 0, loopback);97int serverPort = sslServerSocket.getLocalPort();9899try (var ignore = new BaseLdapServer(sslServerSocket).start()) {100doClientSide(serverPort, args);101}102}103104/*105* Define the client side of the test.106*107* The server should start at this time already108*/109void doClientSide(int serverPort, String[] args) throws Exception {110boolean passed = false;111boolean shouldPass = Boolean.parseBoolean(args[0]);112boolean shouldConnect = Boolean.parseBoolean(args[1]);113// set disableEndpointIdentification to disable hostname verification114if (shouldConnect) {115System.setProperty(116"com.sun.jndi.ldap.object.disableEndpointIdentification", "true");117}118119// Set up the environment for creating the initial context120Hashtable env = new Hashtable();121URI uri = URIBuilder.newBuilder()122.scheme("ldaps")123.loopback()124.port(serverPort)125.build();126env.put(Context.PROVIDER_URL, uri.toString());127env.put(Context.INITIAL_CONTEXT_FACTORY,128"com.sun.jndi.ldap.LdapCtxFactory");129env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");130131// read properties132for (int i = 2; i < args.length; i += 2) {133env.put(args[i], args[i + 1]);134if (debug)135System.out.println("Env=" + args[i] + "=" + args[i + 1]);136}137138try {139DirContext ctx = new InitialDirContext(env);140passed = shouldPass;141ctx.close();142} catch (NamingException ne) {143// only NamingException is allowed144if (debug)145System.out.println("Exception=" + ne + " cause=" + ne.getRootCause());146passed = handleNamingException(ne, shouldPass, shouldConnect);147} catch(Exception e) {148System.err.println("Failed: caught an unexpected Exception - " + e);149throw e;150} finally {151// test if internal property accessible to application152if(shouldPass &&153env.get("jdk.internal.sasl.tlschannelbinding") != null) {154throw new Exception(155"Test FAILED: jdk.internal.sasl.tlschannelbinding should not be accessible");156}157}158if (!passed) {159throw new Exception(160"Test FAILED: NamingException exception should be thrown");161}162System.out.println("Test PASSED");163}164165private static boolean handleNamingException(NamingException ne, boolean shouldPass, boolean shouldConnect)166throws NamingException {167if (ne instanceof AuthenticationException &&168ne.getRootCause() instanceof SaslException) {169SaslException saslEx = (SaslException) ne.getRootCause();170if (shouldConnect && saslEx.getCause() instanceof GSSException) {171// SSL connection successful, expected exception from SaslClient172if (shouldPass)173return true;174}175}176if (!shouldConnect) {177// SSL handshake fails178Exception ex = ne;179while(ex != null && !(ex instanceof CommunicationException)) {180ex = (Exception)ex.getCause();181}182if (ex != null) {183if (ex.getCause() instanceof SSLException) {184if (!shouldPass)185return true;186}187}188}189if (!shouldPass && ne.getRootCause() == null) {190// Expected exception caused by Channel Binding parameter inconsistency191return true;192}193throw ne;194}195}196197198