View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.httpclient.contrib.ssl;
17  
18  import org.apache.log4j.Logger;
19  
20  import java.security.KeyStore;
21  import java.security.KeyStoreException;
22  import java.security.NoSuchAlgorithmException;
23  import java.security.cert.CertificateException;
24  import java.security.cert.X509Certificate;
25  
26  import javax.net.ssl.TrustManagerFactory;
27  import javax.net.ssl.TrustManager;
28  import javax.net.ssl.X509TrustManager;
29  
30  /**
31   * <p>
32   * EasyX509TrustManager unlike default {@link X509TrustManager} accepts 
33   * self-signed certificates. 
34   * </p>
35   * <p>
36   * This trust manager SHOULD NOT be used for productive systems 
37   * due to security reasons, unless it is a concious decision and 
38   * you are perfectly aware of security implications of accepting 
39   * self-signed certificates
40   * </p>
41   * 
42   * @author <a href="mailto:adrian.sutton@ephox.com">Adrian Sutton</a>
43   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
44   * 
45   * <p>
46   * DISCLAIMER: HttpClient developers DO NOT actively support this component.
47   * The component is provided as a reference material, which may be inappropriate
48   * for use without additional customization.
49   * </p>
50   */
51  
52  public class EasyX509TrustManager implements X509TrustManager
53  {
54      private X509TrustManager standardTrustManager = null;
55  
56      /** Log object for this class. */
57      private static final Logger LOG = Logger.getLogger(EasyX509TrustManager.class);
58  
59      /**
60       * Constructor for EasyX509TrustManager.
61       */
62      public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
63          super();
64          TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
65          factory.init(keystore);
66          TrustManager[] trustmanagers = factory.getTrustManagers();
67          if (trustmanagers.length == 0) {
68              throw new NoSuchAlgorithmException("no trust manager found");
69          }
70          this.standardTrustManager = (X509TrustManager)trustmanagers[0];
71      }
72  
73      /**
74       * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
75       */
76      public void checkClientTrusted(X509Certificate[] certificates,String authType) throws CertificateException {
77          standardTrustManager.checkClientTrusted(certificates,authType);
78      }
79  
80      /**
81       * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
82       */
83      public void checkServerTrusted(X509Certificate[] certificates,String authType) throws CertificateException {
84          if ((certificates != null) && LOG.isDebugEnabled()) {
85              LOG.debug("Server certificate chain:");
86              for (int i = 0; i < certificates.length; i++) {
87                  LOG.debug("X509Certificate[" + i + "]=" + certificates[i]);
88              }
89          }
90          if ((certificates != null) && (certificates.length == 1)) {
91              certificates[0].checkValidity();
92          } else {
93              standardTrustManager.checkServerTrusted(certificates,authType);
94          }
95      }
96  
97      /**
98       * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
99       */
100     public X509Certificate[] getAcceptedIssuers() {
101         return this.standardTrustManager.getAcceptedIssuers();
102     }
103 }