You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2018/12/18 23:02:27 UTC
[directory-ldap-api] branch master updated: DIRAPI-330: Improve TLS
handshake error handling
This is an automated email from the ASF dual-hosted git repository.
seelmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git
The following commit(s) were added to refs/heads/master by this push:
new 378ccc3 DIRAPI-330: Improve TLS handshake error handling
378ccc3 is described below
commit 378ccc37f2189513761a818123b41a02792b8c4c
Author: Stefan Seelmann <ma...@stefan-seelmann.de>
AuthorDate: Wed Dec 19 00:01:51 2018 +0100
DIRAPI-330: Improve TLS handshake error handling
---
.../ldap/client/api/LdapNetworkConnection.java | 21 +++-
.../model/exception/LdapTlsHandshakeException.java | 111 +++++++++++++++++++++
.../exception/LdapTlsHandshakeExceptionTest.java | 101 +++++++++++++++++++
3 files changed, 229 insertions(+), 4 deletions(-)
diff --git a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
index 7ea74ae..3f33606 100644
--- a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
+++ b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
@@ -83,6 +83,7 @@ import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
import org.apache.directory.api.ldap.model.exception.LdapNoPermissionException;
import org.apache.directory.api.ldap.model.exception.LdapOperationException;
import org.apache.directory.api.ldap.model.exception.LdapOtherException;
+import org.apache.directory.api.ldap.model.exception.LdapTlsHandshakeException;
import org.apache.directory.api.ldap.model.message.AbandonRequest;
import org.apache.directory.api.ldap.model.message.AbandonRequestImpl;
import org.apache.directory.api.ldap.model.message.AddRequest;
@@ -685,11 +686,17 @@ public class LdapNetworkConnection extends AbstractLdapConnection implements Lda
if ( !isSecured )
{
- throw new LdapOperationException( ResultCodeEnum.OTHER, I18n.err( I18n.ERR_04120_TLS_HANDSHAKE_ERROR ) );
+ Throwable cause = ( Throwable ) connectionFuture.getSession().getAttribute( EXCEPTION_KEY );
+ throw new LdapTlsHandshakeException( I18n.err( I18n.ERR_04120_TLS_HANDSHAKE_ERROR ), cause );
}
}
catch ( Exception e )
{
+ if ( e instanceof LdapException )
+ {
+ throw ( LdapException ) e;
+ }
+
String msg = I18n.err( I18n.ERR_04122_SSL_CONTEXT_INIT_FAILURE );
LOG.error( msg, e );
throw new LdapException( msg, e );
@@ -2407,7 +2414,7 @@ public class LdapNetworkConnection extends AbstractLdapConnection implements Lda
{
LOG.warn( cause.getMessage(), cause );
}
-
+
session.setAttribute( EXCEPTION_KEY, cause );
if ( cause instanceof ProtocolEncoderException )
@@ -4548,7 +4555,7 @@ public class LdapNetworkConnection extends AbstractLdapConnection implements Lda
public void sessionClosed( IoSession session ) throws Exception
{
// no need to handle if this session was closed by the user
- if ( !connected.get() )
+ if ( ldapSession == null || !connected.get() )
{
return;
}
@@ -4707,12 +4714,18 @@ public class LdapNetworkConnection extends AbstractLdapConnection implements Lda
if ( !isSecured )
{
- throw new LdapOperationException( ResultCodeEnum.OTHER, I18n.err( I18n.ERR_04120_TLS_HANDSHAKE_ERROR ) );
+ Throwable cause = ( Throwable ) ldapSession.getAttribute( EXCEPTION_KEY );
+ throw new LdapTlsHandshakeException( I18n.err( I18n.ERR_04120_TLS_HANDSHAKE_ERROR ), cause );
}
}
}
catch ( Exception e )
{
+ if ( e instanceof LdapException )
+ {
+ throw ( LdapException ) e;
+ }
+
String msg = I18n.err( I18n.ERR_04122_SSL_CONTEXT_INIT_FAILURE );
LOG.error( msg, e );
throw new LdapException( msg, e );
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeException.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeException.java
new file mode 100644
index 0000000..5a17c59
--- /dev/null
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeException.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.api.ldap.model.exception;
+
+
+import java.security.cert.CertPathBuilderException;
+import java.security.cert.CertPathValidatorException;
+import java.security.cert.CertificateExpiredException;
+import java.security.cert.CertificateNotYetValidException;
+
+import org.apache.commons.lang3.exception.ExceptionUtils;
+
+
+/**
+ * A LdapTlsException is thrown if the SSL/TLS handshake failed.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class LdapTlsHandshakeException extends LdapException
+{
+ private static final long serialVersionUID = 1L;
+
+ private Throwable rootCause;
+ private String reasonPhrase;
+
+
+ /**
+ * The constructor with a reason string argument.
+ *
+ * @param message the message
+ * @param cause the root cause
+ */
+ public LdapTlsHandshakeException( String message, Throwable cause )
+ {
+ super( message, cause );
+ classify();
+ }
+
+
+ private void classify()
+ {
+ rootCause = ExceptionUtils.getRootCause( getCause() );
+
+ if ( rootCause instanceof CertificateExpiredException )
+ {
+ this.reasonPhrase = "Certificate expired";
+ }
+ else if ( rootCause instanceof CertificateNotYetValidException )
+ {
+ this.reasonPhrase = "Certificate not yet valid";
+ }
+ else if ( rootCause instanceof CertPathBuilderException )
+ {
+ this.reasonPhrase = "Failed to build certification path";
+ }
+ else if ( rootCause instanceof CertPathValidatorException )
+ {
+ CertPathValidatorException cpve = ( CertPathValidatorException ) rootCause;
+ cpve.getReason();
+ this.reasonPhrase = "Failed to verify certification path";
+ }
+ else
+ {
+ this.reasonPhrase = "Unspecified";
+ }
+ }
+
+
+ @Override
+ public String getMessage()
+ {
+ String message = super.getMessage();
+
+ message += ", reason: " + reasonPhrase;
+ if ( rootCause != null && rootCause != this )
+ {
+ message += ": " + rootCause.getMessage();
+ }
+
+ return message;
+ }
+
+
+ public String getReasonPhrase()
+ {
+ return reasonPhrase;
+ }
+
+
+ public Throwable getRootCause()
+ {
+ return rootCause;
+ }
+}
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionTest.java
new file mode 100644
index 0000000..3a42947
--- /dev/null
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.api.ldap.model.exception;
+
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.io.IOException;
+import java.security.cert.CertPathBuilderException;
+import java.security.cert.CertPathValidatorException;
+import java.security.cert.CertPathValidatorException.BasicReason;
+import java.security.cert.CertificateExpiredException;
+import java.security.cert.CertificateNotYetValidException;
+
+import org.junit.Test;
+
+
+public class LdapTlsHandshakeExceptionTest
+{
+ @Test
+ public void testClassifyNull()
+ {
+ LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg", null );
+ assertThat( e.getMessage(), equalTo( "msg, reason: Unspecified" ) );
+ assertThat( e.getReasonPhrase(), equalTo( "Unspecified" ) );
+ assertThat( e.getRootCause(), equalTo( null ) );
+ }
+
+
+ @Test
+ public void testClassifyOther()
+ {
+ LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg", new IOException( "foo" ) );
+ assertThat( e.getMessage(), equalTo( "msg, reason: Unspecified: foo" ) );
+ assertThat( e.getReasonPhrase(), equalTo( "Unspecified" ) );
+ assertThat( e.getRootCause(), instanceOf( IOException.class ) );
+ }
+
+
+ @Test
+ public void testClassifyCertificateExpiredException()
+ {
+ LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg", new CertificateExpiredException( "foo" ) );
+ assertThat( e.getMessage(), equalTo( "msg, reason: Certificate expired: foo" ) );
+ assertThat( e.getReasonPhrase(), equalTo( "Certificate expired" ) );
+ assertThat( e.getRootCause(), instanceOf( CertificateExpiredException.class ) );
+ }
+
+
+ @Test
+ public void testClassifyCertificateNotYetValidException()
+ {
+ LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg",
+ new CertificateNotYetValidException( "foo" ) );
+ assertThat( e.getMessage(), equalTo( "msg, reason: Certificate not yet valid: foo" ) );
+ assertThat( e.getReasonPhrase(), equalTo( "Certificate not yet valid" ) );
+ assertThat( e.getRootCause(), instanceOf( CertificateNotYetValidException.class ) );
+ }
+
+
+ @Test
+ public void testClassifyCertPathBuilderException()
+ {
+ LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg",
+ new Exception( new CertPathBuilderException( "foo" ) ) );
+ assertThat( e.getMessage(), equalTo( "msg, reason: Failed to build certification path: foo" ) );
+ assertThat( e.getReasonPhrase(), equalTo( "Failed to build certification path" ) );
+ assertThat( e.getRootCause(), instanceOf( CertPathBuilderException.class ) );
+ }
+
+
+ @Test
+ public void testClassifyCertPathValidatorException()
+ {
+ LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg",
+ new Exception( new Exception( new Exception( new Exception(
+ new CertPathValidatorException( "foo", null, null, -1, BasicReason.ALGORITHM_CONSTRAINED ) ) ) ) ) );
+ assertThat( e.getMessage(), equalTo( "msg, reason: Failed to verify certification path: foo" ) );
+ assertThat( e.getReasonPhrase(), equalTo( "Failed to verify certification path" ) );
+ assertThat( e.getRootCause(), instanceOf( CertPathValidatorException.class ) );
+ }
+}