Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapCBPropertiesTest.java
66645 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* @modules java.base/sun.security.util28* @run main/othervm LdapCBPropertiesTest true true com.sun.jndi.ldap.tls.cbtype tls-server-end-point29* @run main/othervm LdapCBPropertiesTest false false com.sun.jndi.ldap.tls.cbtype tls-server-end-point30* @run main/othervm LdapCBPropertiesTest true true com.sun.jndi.ldap.tls.cbtype tls-server-end-point com.sun.jndi.ldap.connect.timeout 200031* @run main/othervm LdapCBPropertiesTest false false com.sun.jndi.ldap.tls.cbtype tls-server-end-point com.sun.jndi.ldap.connect.timeout 200032* @run main/othervm LdapCBPropertiesTest false true com.sun.jndi.ldap.tls.cbtype tls-unknown33* @run main/othervm LdapCBPropertiesTest false true jdk.internal.sasl.tlschannelbinding value34* @summary test new JNDI property to control the Channel Binding data35*/3637import javax.naming.AuthenticationException;38import javax.naming.CommunicationException;39import javax.naming.Context;40import javax.naming.NamingException;41import javax.naming.directory.DirContext;42import javax.naming.directory.InitialDirContext;43import java.net.InetAddress;44import java.net.URI;45import java.util.Hashtable;4647import org.ietf.jgss.GSSException;4849import javax.net.ssl.SSLException;50import javax.net.ssl.SSLServerSocket;51import javax.net.ssl.SSLServerSocketFactory;52import javax.security.sasl.SaslException;5354import jdk.test.lib.net.URIBuilder;5556import sun.security.util.ChannelBindingException;5758public class LdapCBPropertiesTest {59/*60* Where do we find the keystores?61*/62static String pathToStores = "../../../../javax/net/ssl/etc";63static String keyStoreFile = "keystore";64static String trustStoreFile = "truststore";65static String passwd = "passphrase";6667static boolean debug = false;6869public static void main(String[] args) throws Exception {70String keyFilename =71System.getProperty("test.src", "./") + "/" + pathToStores +72"/" + keyStoreFile;73String trustFilename =74System.getProperty("test.src", "./") + "/" + pathToStores +75"/" + trustStoreFile;7677System.setProperty("javax.net.ssl.keyStore", keyFilename);78System.setProperty("javax.net.ssl.keyStorePassword", passwd);79System.setProperty("javax.net.ssl.trustStore", trustFilename);80System.setProperty("javax.net.ssl.trustStorePassword", passwd);8182if (debug)83System.setProperty("javax.net.debug", "all");8485/*86* Start the tests.87*/88new LdapCBPropertiesTest(args);89}9091/*92* Primary constructor, used to drive remainder of the test.93*/94LdapCBPropertiesTest(String[] args) throws Exception {95InetAddress loopback = InetAddress.getLoopbackAddress();96SSLServerSocketFactory sslssf =97(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();98SSLServerSocket sslServerSocket =99(SSLServerSocket) sslssf.createServerSocket(0, 0, loopback);100int serverPort = sslServerSocket.getLocalPort();101102try (var ignore = new BaseLdapServer(sslServerSocket).start()) {103doClientSide(serverPort, args);104}105}106107/*108* Define the client side of the test.109*110* The server should start at this time already111*/112void doClientSide(int serverPort, String[] args) throws Exception {113boolean passed = false;114boolean shouldPass = Boolean.parseBoolean(args[0]);115boolean shouldConnect = Boolean.parseBoolean(args[1]);116// set disableEndpointIdentification to disable hostname verification117if (shouldConnect) {118System.setProperty(119"com.sun.jndi.ldap.object.disableEndpointIdentification", "true");120}121122// Set up the environment for creating the initial context123Hashtable env = new Hashtable();124URI uri = URIBuilder.newBuilder()125.scheme("ldaps")126.loopback()127.port(serverPort)128.build();129env.put(Context.PROVIDER_URL, uri.toString());130env.put(Context.INITIAL_CONTEXT_FACTORY,131"com.sun.jndi.ldap.LdapCtxFactory");132env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");133134// read properties135for (int i = 2; i < args.length; i += 2) {136env.put(args[i], args[i + 1]);137if (debug)138System.out.println("Env=" + args[i] + "=" + args[i + 1]);139}140141try {142DirContext ctx = new InitialDirContext(env);143passed = shouldPass;144ctx.close();145} catch (NamingException ne) {146// only NamingException is allowed147if (debug)148System.out.println("Exception=" + ne + " cause=" + ne.getRootCause());149passed = handleNamingException(ne, shouldPass, shouldConnect);150} catch(Exception e) {151System.err.println("Failed: caught an unexpected Exception - " + e);152throw e;153} finally {154// test if internal property accessible to application155if(shouldPass &&156env.get("jdk.internal.sasl.tlschannelbinding") != null) {157throw new Exception(158"Test FAILED: jdk.internal.sasl.tlschannelbinding should not be accessible");159}160}161if (!passed) {162throw new Exception(163"Test FAILED: NamingException exception should be thrown");164}165System.out.println("Test PASSED");166}167168private static boolean handleNamingException(NamingException ne, boolean shouldPass, boolean shouldConnect)169throws NamingException {170if (ne instanceof AuthenticationException &&171ne.getRootCause() instanceof SaslException) {172SaslException saslEx = (SaslException) ne.getRootCause();173if (shouldConnect && saslEx.getCause() instanceof GSSException) {174// SSL connection successful, expected exception from SaslClient175if (shouldPass)176return true;177}178}179if (!shouldConnect) {180// SSL handshake fails181Exception ex = ne;182while(ex != null && !(ex instanceof CommunicationException)) {183ex = (Exception)ex.getCause();184}185if (ex != null) {186if (ex.getCause() instanceof SSLException) {187if (!shouldPass)188return true;189}190}191}192Throwable rc = ne.getRootCause();193if (!shouldPass && (rc == null || rc instanceof ChannelBindingException)) {194// Expected exception caused by Channel Binding parameter inconsistency195return true;196}197throw ne;198}199}200201202