View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.util;
20  
21  import com.sun.net.ssl.HostnameVerifier;
22  import com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl;
23  
24  import javax.net.ssl.SSLContext;
25  import javax.net.ssl.TrustManager;
26  
27  import java.security.SecureRandom;
28  import java.security.NoSuchAlgorithmException;
29  import java.security.KeyManagementException;
30  
31  /***
32   * Utility to override the JSSE settings for SSL certificate checking
33   * This can be used to disable the SSL certificate checking normally done by Java
34   */
35  public class JsseSettings {
36      
37      /***
38       * don't instantiate this class
39       */
40      private JsseSettings(){
41      }
42      
43      public static void disableSslCertificateChecking() {
44  
45          TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustEverythingManager()};
46  
47          HostnameVerifier doNotVerifyHost = new HostnameVerifier() {
48              public boolean verify(String hostname, String session) { return true; }
49          };
50  
51          try {
52              SSLContext sc = SSLContext.getInstance("SSL");
53              sc.init(null, trustAllCerts, new SecureRandom());
54              HttpsURLConnectionOldImpl.setDefaultSSLSocketFactory(sc.getSocketFactory());
55              HttpsURLConnectionOldImpl.setDefaultHostnameVerifier(doNotVerifyHost);
56          } catch (NoSuchAlgorithmException e) {
57              e.printStackTrace();
58          } catch (KeyManagementException e) {
59              e.printStackTrace();
60          }
61      }
62  
63  }