|
65 | 65 | import javax.net.ssl.SSLSocketFactory; |
66 | 66 |
|
67 | 67 | import org.apache.catalina.LifecycleException; |
| 68 | +import org.apache.tomcat.util.ExceptionUtils; |
68 | 69 | import org.apache.tomcat.util.buf.StringUtils; |
69 | 70 | import org.apache.tomcat.util.collections.SynchronizedStack; |
70 | 71 | import org.ietf.jgss.GSSContext; |
@@ -2794,30 +2795,49 @@ private DirContext createTlsDirContext(JNDIConnection connection, Hashtable<Stri |
2794 | 2795 | } |
2795 | 2796 | } |
2796 | 2797 | LdapContext result = null; |
| 2798 | + StartTlsResponse tls = null; |
2797 | 2799 | try { |
2798 | 2800 | result = new InitialLdapContext(env, null); |
2799 | | - StartTlsResponse tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest()); |
| 2801 | + tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest()); |
| 2802 | + if (getHostnameVerifier() != null) { |
| 2803 | + tls.setHostnameVerifier(getHostnameVerifier()); |
| 2804 | + } |
| 2805 | + if (getCipherSuitesArray() != null) { |
| 2806 | + tls.setEnabledCipherSuites(getCipherSuitesArray()); |
| 2807 | + } |
| 2808 | + SSLSession negotiate = tls.negotiate(getSSLSocketFactory()); |
| 2809 | + containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol())); |
| 2810 | + for (Map.Entry<String,Object> savedEntry : savedEnv.entrySet()) { |
| 2811 | + result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue()); |
| 2812 | + } |
| 2813 | + connection.tls = tls; |
| 2814 | + } catch (Throwable t) { |
| 2815 | + ExceptionUtils.handleThrowable(t); |
| 2816 | + NamingException ne; |
| 2817 | + if (t instanceof NamingException) { |
| 2818 | + ne = (NamingException) t; |
| 2819 | + } else { |
| 2820 | + ne = new NamingException(t.getMessage()); |
| 2821 | + ne.initCause(t); |
| 2822 | + } |
2800 | 2823 | try { |
2801 | | - if (getHostnameVerifier() != null) { |
2802 | | - tls.setHostnameVerifier(getHostnameVerifier()); |
2803 | | - } |
2804 | | - if (getCipherSuitesArray() != null) { |
2805 | | - tls.setEnabledCipherSuites(getCipherSuitesArray()); |
| 2824 | + if (tls != null) { |
| 2825 | + tls.close(); |
2806 | 2826 | } |
2807 | | - SSLSession negotiate = tls.negotiate(getSSLSocketFactory()); |
2808 | | - containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol())); |
2809 | | - connection.tls = tls; |
2810 | | - } catch (IOException ioe) { |
2811 | | - NamingException ne = new NamingException(ioe.getMessage()); |
2812 | | - ne.initCause(ioe); |
2813 | | - throw ne; |
| 2827 | + } catch (Throwable t1) { |
| 2828 | + ExceptionUtils.handleThrowable(t1); |
| 2829 | + ne.addSuppressed(t1); |
2814 | 2830 | } |
2815 | | - } finally { |
2816 | | - if (result != null) { |
2817 | | - for (Map.Entry<String,Object> savedEntry : savedEnv.entrySet()) { |
2818 | | - result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue()); |
| 2831 | + try { |
| 2832 | + if (result != null) { |
| 2833 | + result.close(); |
2819 | 2834 | } |
| 2835 | + } catch (Throwable t2) { |
| 2836 | + ExceptionUtils.handleThrowable(t2); |
| 2837 | + ne.addSuppressed(t2); |
2820 | 2838 | } |
| 2839 | + result = null; |
| 2840 | + throw ne; |
2821 | 2841 | } |
2822 | 2842 | return result; |
2823 | 2843 | } |
|
0 commit comments