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/26 23:53:36 UTC

[directory-ldap-api] branch master updated: Extract certificate exception classification to make it reusable

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 648db0c  Extract certificate exception classification to make it reusable
648db0c is described below

commit 648db0cae63358b131ab4c52e329e6aefcfdedb4
Author: Stefan Seelmann <ma...@stefan-seelmann.de>
AuthorDate: Thu Dec 27 00:53:05 2018 +0100

    Extract certificate exception classification to make it reusable
---
 .../model/exception/LdapTlsHandshakeException.java |  54 ++--------
 .../LdapTlsHandshakeExceptionClassifier.java       |  96 +++++++++++++++++
 .../model/exception/LdapTlsHandshakeFailCause.java | 120 +++++++++++++++++++++
 .../LdapTlsHandshakeExceptionClassifierTest.java   | 106 ++++++++++++++++++
 .../exception/LdapTlsHandshakeExceptionTest.java   |  14 +--
 5 files changed, 329 insertions(+), 61 deletions(-)

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
index 5a17c59..07b254a 100644
--- 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
@@ -20,14 +20,6 @@
 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.
  * 
@@ -37,8 +29,7 @@ public class LdapTlsHandshakeException extends LdapException
 {
     private static final long serialVersionUID = 1L;
 
-    private Throwable rootCause;
-    private String reasonPhrase;
+    private LdapTlsHandshakeFailCause failCause;
 
 
     /**
@@ -50,36 +41,13 @@ public class LdapTlsHandshakeException extends LdapException
     public LdapTlsHandshakeException( String message, Throwable cause )
     {
         super( message, cause );
-        classify();
+        this.failCause = LdapTlsHandshakeExceptionClassifier.classify( cause, null );
     }
 
 
-    private void classify()
+    public LdapTlsHandshakeFailCause getFailCause()
     {
-        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";
-        }
+        return failCause;
     }
 
 
@@ -88,7 +56,8 @@ public class LdapTlsHandshakeException extends LdapException
     {
         String message = super.getMessage();
 
-        message += ", reason: " + reasonPhrase;
+        message += ", reason: " + failCause.getReasonPhrase();
+        Throwable rootCause = failCause.getRootCause();
         if ( rootCause != null && rootCause != this )
         {
             message += ": " + rootCause.getMessage();
@@ -97,15 +66,4 @@ public class LdapTlsHandshakeException extends LdapException
         return message;
     }
 
-
-    public String getReasonPhrase()
-    {
-        return reasonPhrase;
-    }
-
-
-    public Throwable getRootCause()
-    {
-        return rootCause;
-    }
 }
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionClassifier.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionClassifier.java
new file mode 100644
index 0000000..86537a6
--- /dev/null
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionClassifier.java
@@ -0,0 +1,96 @@
+/*
+ *  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.CertPathValidatorException.BasicReason;
+import java.security.cert.CertificateExpiredException;
+import java.security.cert.CertificateNotYetValidException;
+import java.security.cert.X509Certificate;
+
+import javax.security.auth.x500.X500Principal;
+
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.directory.api.ldap.model.exception.LdapTlsHandshakeFailCause.LdapApiReason;
+
+
+public final class LdapTlsHandshakeExceptionClassifier
+{
+    private LdapTlsHandshakeExceptionClassifier()
+    {
+    }
+
+    public static LdapTlsHandshakeFailCause classify( Throwable cause )
+    {
+        return classify( cause, null );
+    }
+
+
+    public static LdapTlsHandshakeFailCause classify( Throwable cause, X509Certificate certificate )
+    {
+        LdapTlsHandshakeFailCause failCause = new LdapTlsHandshakeFailCause();
+        failCause.setCause( cause );
+
+        Throwable rootCause = ExceptionUtils.getRootCause( cause );
+        failCause.setRootCause( rootCause );
+
+        if ( rootCause instanceof CertificateExpiredException )
+        {
+            failCause.setReason( BasicReason.EXPIRED );
+            failCause.setReasonPhrase( "Certificate expired" );
+        }
+        else if ( rootCause instanceof CertificateNotYetValidException )
+        {
+            failCause.setReason( BasicReason.NOT_YET_VALID );
+            failCause.setReasonPhrase( "Certificate not yet valid" );
+        }
+        else if ( rootCause instanceof CertPathBuilderException )
+        {
+            failCause.setReason( LdapApiReason.NO_VALID_CERTIFICATION_PATH );
+            failCause.setReasonPhrase( "Failed to build certification path" );
+            if ( certificate != null )
+            {
+                X500Principal issuerX500Principal = certificate.getIssuerX500Principal();
+                X500Principal subjectX500Principal = certificate.getSubjectX500Principal();
+                if ( issuerX500Principal.equals( subjectX500Principal ) )
+                {
+                    failCause.setReason( LdapApiReason.SELF_SIGNED );
+                    failCause.setReasonPhrase( "Self signed certificate" );
+                }
+            }
+        }
+        else if ( rootCause instanceof CertPathValidatorException )
+        {
+            CertPathValidatorException cpve = ( CertPathValidatorException ) rootCause;
+            failCause.setReason( cpve.getReason() );
+            failCause.setReasonPhrase( "Failed to verify certification path" );
+        }
+        else
+        {
+            failCause.setReason( BasicReason.UNSPECIFIED );
+            failCause.setReasonPhrase( "Unspecified" );
+        }
+
+        return failCause;
+    }
+
+}
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeFailCause.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeFailCause.java
new file mode 100644
index 0000000..102ba68
--- /dev/null
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeFailCause.java
@@ -0,0 +1,120 @@
+/*
+ *  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.CertPathValidatorException.BasicReason;
+import java.security.cert.CertPathValidatorException.Reason;
+
+
+public class LdapTlsHandshakeFailCause
+{
+    private Throwable cause;
+    private Throwable rootCause;
+    private Reason reason;
+    private String reasonPhrase;
+
+
+    public LdapTlsHandshakeFailCause()
+    {
+    }
+
+
+    public LdapTlsHandshakeFailCause( Throwable cause, Throwable rootCause, Reason reason, String reasonPhrase )
+    {
+        this.cause = cause;
+        this.rootCause = rootCause;
+        this.reason = reason;
+        this.reasonPhrase = reasonPhrase;
+    }
+
+
+    public Throwable getCause()
+    {
+        return cause;
+    }
+
+
+    public void setCause( Throwable cause )
+    {
+        this.cause = cause;
+    }
+
+
+    public Throwable getRootCause()
+    {
+        return rootCause;
+    }
+
+
+    public void setRootCause( Throwable rootCause )
+    {
+        this.rootCause = rootCause;
+    }
+
+
+    public Reason getReason()
+    {
+        return reason;
+    }
+
+
+    public void setReason( Reason reason )
+    {
+        this.reason = reason;
+    }
+
+
+    public String getReasonPhrase()
+    {
+        return reasonPhrase;
+    }
+
+
+    public void setReasonPhrase( String reasonPhrase )
+    {
+        this.reasonPhrase = reasonPhrase;
+    }
+
+    /**
+     * Additional reasons.
+     * 
+     * @see BasicReason
+     *
+     */
+    public enum LdapApiReason implements Reason
+    {
+        NO_VALID_CERTIFICATION_PATH,
+        SELF_SIGNED,
+        HOST_NAME_VERIFICATION_FAILED,
+    }
+
+
+    public String getMessage()
+    {
+        String message = reasonPhrase;
+        if ( rootCause != null && rootCause != cause )
+        {
+            message += ": " + rootCause.getMessage();
+        }
+        return message;
+    }
+
+}
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionClassifierTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionClassifierTest.java
new file mode 100644
index 0000000..4bdb1ee
--- /dev/null
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/exception/LdapTlsHandshakeExceptionClassifierTest.java
@@ -0,0 +1,106 @@
+/*
+ *  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 Licensclassifier.  You may obtain a copy of the License at
+ *  
+ *    http://www.apachclassifier.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 Licensclassifier. 
+ *  
+ */
+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.CertPathValidatorException.Reason;
+import java.security.cert.CertificateExpiredException;
+import java.security.cert.CertificateNotYetValidException;
+
+import org.apache.directory.api.ldap.model.exception.LdapTlsHandshakeFailCause.LdapApiReason;
+import org.junit.Test;
+
+
+public class LdapTlsHandshakeExceptionClassifierTest
+{
+    @Test
+    public void testClassifyNull()
+    {
+        LdapTlsHandshakeFailCause classification = LdapTlsHandshakeExceptionClassifier.classify( null );
+        assertThat( classification.getReason(), equalTo( ( Reason ) BasicReason.UNSPECIFIED ) );
+        assertThat( classification.getReasonPhrase(), equalTo( "Unspecified" ) );
+        assertThat( classification.getRootCause(), equalTo( null ) );
+    }
+
+
+    @Test
+    public void testClassifyOther()
+    {
+        LdapTlsHandshakeFailCause classification = LdapTlsHandshakeExceptionClassifier
+            .classify( new IOException( "foo" ) );
+        assertThat( classification.getReason(), equalTo( ( Reason ) BasicReason.UNSPECIFIED ) );
+        assertThat( classification.getReasonPhrase(), equalTo( "Unspecified" ) );
+        assertThat( classification.getRootCause(), instanceOf( IOException.class ) );
+    }
+
+
+    @Test
+    public void testClassifyCertificateExpiredException()
+    {
+        LdapTlsHandshakeFailCause classification = LdapTlsHandshakeExceptionClassifier
+            .classify( new CertificateExpiredException( "foo" ) );
+        assertThat( classification.getReason(), equalTo( ( Reason ) BasicReason.EXPIRED ) );
+        assertThat( classification.getReasonPhrase(), equalTo( "Certificate expired" ) );
+        assertThat( classification.getRootCause(), instanceOf( CertificateExpiredException.class ) );
+    }
+
+
+    @Test
+    public void testClassifyCertificateNotYetValidException()
+    {
+        LdapTlsHandshakeFailCause classification = LdapTlsHandshakeExceptionClassifier
+            .classify( new CertificateNotYetValidException( "foo" ) );
+        assertThat( classification.getReason(), equalTo( ( Reason ) BasicReason.NOT_YET_VALID ) );
+        assertThat( classification.getReasonPhrase(), equalTo( "Certificate not yet valid" ) );
+        assertThat( classification.getRootCause(), instanceOf( CertificateNotYetValidException.class ) );
+    }
+
+
+    @Test
+    public void testClassifyCertPathBuilderException()
+    {
+        LdapTlsHandshakeFailCause classification = LdapTlsHandshakeExceptionClassifier
+            .classify( new Exception( new CertPathBuilderException( "foo" ) ) );
+        assertThat( classification.getReason(), equalTo( ( Reason ) LdapApiReason.NO_VALID_CERTIFICATION_PATH ) );
+        assertThat( classification.getReasonPhrase(), equalTo( "Failed to build certification path" ) );
+        assertThat( classification.getRootCause(), instanceOf( CertPathBuilderException.class ) );
+    }
+
+
+    @Test
+    public void testClassifyCertPathValidatorException()
+    {
+        LdapTlsHandshakeFailCause classification = LdapTlsHandshakeExceptionClassifier.classify(
+            new Exception( new Exception( new Exception( new Exception(
+                new CertPathValidatorException( "foo", null, null, -1, BasicReason.ALGORITHM_CONSTRAINED ) ) ) ) ) );
+        assertThat( classification.getReason(), equalTo( ( Reason ) BasicReason.ALGORITHM_CONSTRAINED ) );
+        assertThat( classification.getReasonPhrase(), equalTo( "Failed to verify certification path" ) );
+        assertThat( classification.getRootCause(), instanceOf( CertPathValidatorException.class ) );
+    }
+
+}
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
index 3a42947..2309b11 100644
--- 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
@@ -21,7 +21,6 @@ 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;
@@ -41,8 +40,6 @@ public class LdapTlsHandshakeExceptionTest
     {
         LdapTlsHandshakeException e = new LdapTlsHandshakeException( "msg", null );
         assertThat( e.getMessage(), equalTo( "msg, reason: Unspecified" ) );
-        assertThat( e.getReasonPhrase(), equalTo( "Unspecified" ) );
-        assertThat( e.getRootCause(), equalTo( null ) );
     }
 
 
@@ -51,8 +48,6 @@ public class LdapTlsHandshakeExceptionTest
     {
         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 ) );
     }
 
 
@@ -61,8 +56,6 @@ public class LdapTlsHandshakeExceptionTest
     {
         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 ) );
     }
 
 
@@ -72,8 +65,6 @@ public class LdapTlsHandshakeExceptionTest
         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 ) );
     }
 
 
@@ -83,8 +74,6 @@ public class LdapTlsHandshakeExceptionTest
         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 ) );
     }
 
 
@@ -95,7 +84,6 @@ public class LdapTlsHandshakeExceptionTest
             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 ) );
     }
+
 }