From aac0e74c3c57b8c9892926fa5c010adf5a9faa2f Mon Sep 17 00:00:00 2001 From: Christoph Langer Date: Sat, 4 May 2024 06:31:18 +0000 Subject: [PATCH 1/2] 8325579: Inconsistent behavior in com.sun.jndi.ldap.Connection::createSocket Backport-of: fbb424f95d73e99c7a533ed5dae8fbb6d13dc93b --- .../classes/com/sun/jndi/ldap/Connection.java | 47 ++-- .../share/classes/module-info.java | 24 +- .../ldap/LdapSSLHandshakeFailureTest.java | 254 ++++++++++++------ 3 files changed, 210 insertions(+), 115 deletions(-) diff --git a/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java b/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java index f71b1bb1400..b7cfa1ef132 100644 --- a/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java +++ b/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -120,17 +120,15 @@ public final class Connection implements Runnable { private static final boolean debug = false; - private static final int dump = 0; // > 0 r, > 1 rw - private final Thread worker; // Initialized in constructor - private boolean v3 = true; // Set in setV3() + private boolean v3 = true; // Set in setV3() public final String host; // used by LdapClient for generating exception messages - // used by StartTlsResponse when creating an SSL socket + // used by StartTlsResponse when creating an SSL socket public final int port; // used by LdapClient for generating exception messages - // used by StartTlsResponse when creating an SSL socket + // used by StartTlsResponse when creating an SSL socket private boolean bound = false; // Set in setBound() @@ -319,30 +317,37 @@ private SocketFactory getSocketFactory(String socketFactoryName) throws Exceptio } private Socket createConnectionSocket(String host, int port, SocketFactory factory, - int connectTimeout) throws Exception { + int connectTimeout) throws IOException { Socket socket = null; + // if timeout is supplied, try to use unconnected socket for connecting with timeout if (connectTimeout > 0) { - // create unconnected socket and then connect it if timeout - // is supplied - InetSocketAddress endpoint = - createInetSocketAddress(host, port); - // unconnected socket - socket = factory.createSocket(); - // connect socket with a timeout - socket.connect(endpoint, connectTimeout); if (debug) { - System.err.println("Connection: creating socket with " + - "a connect timeout"); + System.err.println("Connection: creating socket with a connect timeout"); + } + try { + // unconnected socket + socket = factory.createSocket(); + } catch (IOException e) { + // unconnected socket is likely not supported by the SocketFactory + if (debug) { + System.err.println("Connection: unconnected socket not supported by SocketFactory"); + } + } + if (socket != null) { + InetSocketAddress endpoint = createInetSocketAddress(host, port); + // connect socket with a timeout + socket.connect(endpoint, connectTimeout); } } + + // either no timeout was supplied or unconnected socket did not work if (socket == null) { // create connected socket - socket = factory.createSocket(host, port); if (debug) { - System.err.println("Connection: creating connected socket with" + - " no connect timeout"); + System.err.println("Connection: creating connected socket with no connect timeout"); } + socket = factory.createSocket(host, port); } return socket; } @@ -351,7 +356,7 @@ private Socket createConnectionSocket(String host, int port, SocketFactory facto // the SSL handshake following socket connection as part of the timeout. // So explicitly set a socket read timeout, trigger the SSL handshake, // then reset the timeout. - private void initialSSLHandshake(SSLSocket sslSocket , int connectTimeout) throws Exception { + private void initialSSLHandshake(SSLSocket sslSocket, int connectTimeout) throws Exception { if (!IS_HOSTNAME_VERIFICATION_DISABLED) { SSLParameters param = sslSocket.getSSLParameters(); diff --git a/src/java.naming/share/classes/module-info.java b/src/java.naming/share/classes/module-info.java index f7d0ace806d..09e1093c13a 100644 --- a/src/java.naming/share/classes/module-info.java +++ b/src/java.naming/share/classes/module-info.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -36,21 +36,33 @@ * The following implementation specific environment properties are supported by the * default LDAP Naming Service Provider implementation in the JDK: *