View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.innergrid.api;
18  
19  import java.security.KeyManagementException;
20  import java.security.NoSuchAlgorithmException;
21  import java.security.cert.CertificateException;
22  import java.security.cert.X509Certificate;
23  
24  import javax.net.ssl.SSLContext;
25  import javax.net.ssl.SSLSocketFactory;
26  import javax.net.ssl.TrustManager;
27  import javax.net.ssl.X509TrustManager;
28  
29  import com.gridsystems.innergrid.kernel.KernelException;
30  
31  /**
32   * SSLConnectionInfo implementation to accept any certificate sent by the Server.
33   *
34   * @author Xmas
35   * @author Rodrigo Ruiz
36   * @version 2.0
37   */
38  public class AcceptAllCertificates implements SSLConnectionInfo {
39  
40    /**
41     * Constructor.
42     */
43    public AcceptAllCertificates() {
44    }
45  
46    /**
47     * {@inheritDoc}
48     */
49    public SSLSocketFactory getSSLSocketFactory() throws KernelException {
50      TrustManager trustman = new AcceptAllTrustManager();
51  
52      try {
53        SSLContext ctx = SSLContext.getInstance("SSL");
54  
55        ctx.init(null, new TrustManager[] { trustman }, null);
56  
57        return (SSLSocketFactory) ctx.getSocketFactory();
58      } catch (KeyManagementException e) {
59        // CLT031=Certificate verify failed
60        throw new CKernelException(e, "CLT031");
61      } catch (NoSuchAlgorithmException e) {
62        // CLT031=Certificate verify failed
63        throw new CKernelException(e, "CLT031");
64      }
65    }
66  
67    /**
68     * TrushManager that accepts all Server Certificates.
69     */
70    private static class AcceptAllTrustManager implements X509TrustManager {
71      /**
72       *  Constructor.
73       */
74      public AcceptAllTrustManager() { }
75  
76      /**
77       * {@inheritDoc}
78       */
79      public X509Certificate[] getAcceptedIssuers() {
80        return new X509Certificate[0];
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public void checkClientTrusted(X509Certificate[] chain, String authType)
87        throws CertificateException {
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public void checkServerTrusted(X509Certificate[] chain, String authType)
94        throws CertificateException {
95      }
96    }
97  
98  }