View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.exception;
21  
22  
23  import java.security.cert.CertPathBuilderException;
24  import java.security.cert.CertPathValidatorException;
25  import java.security.cert.CertPathValidatorException.BasicReason;
26  import java.security.cert.CertificateExpiredException;
27  import java.security.cert.CertificateNotYetValidException;
28  import java.security.cert.X509Certificate;
29  
30  import javax.security.auth.x500.X500Principal;
31  
32  import org.apache.commons.lang3.exception.ExceptionUtils;
33  import org.apache.directory.api.ldap.model.exception.LdapTlsHandshakeFailCause.LdapApiReason;
34  
35  
36  public final class LdapTlsHandshakeExceptionClassifier
37  {
38      private LdapTlsHandshakeExceptionClassifier()
39      {
40      }
41  
42      public static LdapTlsHandshakeFailCause classify( Throwable cause )
43      {
44          return classify( cause, null );
45      }
46  
47  
48      public static LdapTlsHandshakeFailCause classify( Throwable cause, X509Certificate certificate )
49      {
50          LdapTlsHandshakeFailCause failCause = new LdapTlsHandshakeFailCause();
51          failCause.setCause( cause );
52  
53          Throwable rootCause = ExceptionUtils.getRootCause( cause );
54          failCause.setRootCause( rootCause );
55  
56          if ( rootCause instanceof CertificateExpiredException )
57          {
58              failCause.setReason( BasicReason.EXPIRED );
59              failCause.setReasonPhrase( "Certificate expired" );
60          }
61          else if ( rootCause instanceof CertificateNotYetValidException )
62          {
63              failCause.setReason( BasicReason.NOT_YET_VALID );
64              failCause.setReasonPhrase( "Certificate not yet valid" );
65          }
66          else if ( rootCause instanceof CertPathBuilderException )
67          {
68              failCause.setReason( LdapApiReason.NO_VALID_CERTIFICATION_PATH );
69              failCause.setReasonPhrase( "Failed to build certification path" );
70              if ( certificate != null )
71              {
72                  X500Principal issuerX500Principal = certificate.getIssuerX500Principal();
73                  X500Principal subjectX500Principal = certificate.getSubjectX500Principal();
74                  if ( issuerX500Principal.equals( subjectX500Principal ) )
75                  {
76                      failCause.setReason( LdapApiReason.SELF_SIGNED );
77                      failCause.setReasonPhrase( "Self signed certificate" );
78                  }
79              }
80          }
81          else if ( rootCause instanceof CertPathValidatorException )
82          {
83              CertPathValidatorException cpve = ( CertPathValidatorException ) rootCause;
84              failCause.setReason( cpve.getReason() );
85              failCause.setReasonPhrase( "Failed to verify certification path" );
86          }
87          else
88          {
89              failCause.setReason( BasicReason.UNSPECIFIED );
90              String failMessage = "Undefined";
91              
92              if ( cause != null )
93              {
94                  failMessage += ", " + cause.getClass().getSimpleName();
95              }
96              
97              failCause.setReasonPhrase( failMessage );
98          }
99  
100         return failCause;
101     }
102 }