KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > util > JSSE


1 /*
2  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3  * Use is subject to license terms.
4  */

5
6 package com.sun.enterprise.tools.admingui.util;
7
8 import java.security.NoSuchAlgorithmException JavaDoc;
9 import java.security.KeyManagementException JavaDoc;
10
11 import javax.net.ssl.HttpsURLConnection;
12 import java.security.cert.X509Certificate JavaDoc;
13 import javax.net.ssl.SSLContext;
14 import javax.net.ssl.HostnameVerifier;
15 import javax.net.ssl.SSLSession;
16 import javax.net.ssl.X509TrustManager;
17
18 import com.sun.appserv.management.client.TrustAnyTrustManager;
19
20
21 /**
22  * This class trusts any server generated certifcate blindly to
23  * handle ssl connection. We have commented out the code to generate
24  * client certificate, and store server certificate in truststore during
25  * handshake. We use it if necessary later, right now we just trust it.
26  */

27 public class JSSE {
28
29    /* public String getHostFromCertificate() throws Exception {
30         HttpsURLConnection https = getHttpsURLConnection();
31         https.connect();
32         //We don't have to do the following, may be we can getaway with just
33         //accepting any server certificate.
34         Certificate[] cert = https.getServerCertificates();
35         generateTrustStore(cert[0]);
36         String dn = getDistinguishedName(cert[0]);
37         String hostName = getHostNameFromDN(dn);
38         return hostName;
39     }
40     private void generateTrustStore(Certificate cert) throws Exception {
41         File f = new File("certdb.jks");
42         FileOutputStream fout = new FileOutputStream(f);
43         KeyStore key = KeyStore.getInstance("JKS");//default is JKS
44         key.load(null, null); //initialize keystore
45         key.setCertificateEntry("s1as", cert);
46         key.store(fout, new char[]{'c', 'h', 'a', 'n', 'g', 'e', 'i', 't'});
47         System.setProperty("javax.net.ssl.trustStore", "out.jks");
48         System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
49
50     }
51     private String getDistinguishedName(Certificate cert) {
52         String dn = ((X509Certificate)cert).getSubjectX500Principal().getName();
53         return dn;
54     }
55     private String getHostNameFromDN(String dn) {
56         StringTokenizer str = new StringTokenizer(dn, ",");
57         String s = str.nextToken();
58         return s.substring(s.indexOf("=")+1);
59     }*/

60     public static void trustAnyServerCertificate() throws NoSuchAlgorithmException JavaDoc, KeyManagementException JavaDoc {
61         SSLContext sslc = SSLContext.getInstance("SSLv3");
62         final X509TrustManager[] tms = TrustAnyTrustManager.getInstanceArray();
63         sslc.init(null, tms, null);
64         if (sslc != null) {
65             HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory());
66         }
67     if(HttpsURLConnection.getDefaultHostnameVerifier() instanceof AcceptAnyHostName) {
68         return;
69     }
70         HostnameVerifier hv = new AcceptAnyHostName();
71         HttpsURLConnection.setDefaultHostnameVerifier(hv);
72     }
73
74     private static class AcceptAnyHostName implements HostnameVerifier{
75         public boolean verify(String JavaDoc s, SSLSession ssl) {
76             return true;
77         }
78     }
79
80
81 }
82
Popular Tags