KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > SSLUtil


1 /*
2  * $Id: SSLUtil.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.util;
26
27 import java.io.IOException JavaDoc;
28 import java.security.GeneralSecurityException JavaDoc;
29 import java.security.KeyStore JavaDoc;
30
31 import javax.net.ssl.*;
32
33 /**
34  * KeyStoreUtil - Utilities for setting up SSL connections with specific client certificates
35  *
36  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
37  * @version $Rev: 5462 $
38  * @since 3.0
39  */

40 public class SSLUtil {
41
42     public static final String JavaDoc module = SSLUtil.class.getName();
43     private static boolean loadedProps = false;
44
45     static {
46         SSLUtil. loadJsseProperties();
47     }
48
49     public static KeyManager[] getKeyManagers(KeyStore JavaDoc ks, String JavaDoc password, String JavaDoc alias) throws GeneralSecurityException JavaDoc {
50         KeyManagerFactory factory = KeyManagerFactory.getInstance("SunX509");
51         factory.init(ks, password.toCharArray());
52         KeyManager[] keyManagers = factory.getKeyManagers();
53         if (alias != null) {
54             for (int i = 0; i < keyManagers.length; i++) {
55                 if (keyManagers[i] instanceof X509KeyManager) {
56                     keyManagers[i] = new AliasKeyManager((X509KeyManager)keyManagers[i], alias);
57                 }
58             }
59         }
60         return keyManagers;
61     }
62
63     public static TrustManager[] getTrustManagers(KeyStore JavaDoc ks) throws GeneralSecurityException JavaDoc {
64         TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509");
65         factory.init(ks);
66         return factory.getTrustManagers();
67     }
68
69     public static SSLSocketFactory getSSLSocketFactory(KeyStore JavaDoc ks, String JavaDoc password, String JavaDoc alias) throws IOException JavaDoc, GeneralSecurityException JavaDoc {
70         KeyStore JavaDoc trustStore = KeyStoreUtil.getTrustStore();
71         TrustManager[] tm = getTrustManagers(trustStore);
72         KeyManager[] km = getKeyManagers(ks, password, alias);
73
74         SSLContext context = SSLContext.getInstance("SSL");
75         context.init(km, tm, null);
76         return context.getSocketFactory();
77     }
78
79     public static SSLSocketFactory getSSLSocketFactory(String JavaDoc alias) throws IOException JavaDoc, GeneralSecurityException JavaDoc {
80         return getSSLSocketFactory(KeyStoreUtil.getKeyStore(), KeyStoreUtil.getKeyStorePassword(), alias);
81     }
82
83     public static SSLSocketFactory getSSLSocketFactory() throws IOException JavaDoc, GeneralSecurityException JavaDoc {
84         return getSSLSocketFactory(null);
85     }
86
87     public static SSLServerSocketFactory getSSLServerSocketFactory(KeyStore JavaDoc ks, String JavaDoc password, String JavaDoc alias) throws IOException JavaDoc, GeneralSecurityException JavaDoc {
88         KeyStore JavaDoc trustStore = KeyStoreUtil.getTrustStore();
89         TrustManager[] tm = getTrustManagers(trustStore);
90         KeyManager[] km = getKeyManagers(ks, password, alias);
91
92         SSLContext context = SSLContext.getInstance("SSL");
93         context.init(km, tm, null);
94         return context.getServerSocketFactory();
95     }
96
97     public static void loadJsseProperties() {
98         loadJsseProperties(false);
99     }
100
101     public static synchronized void loadJsseProperties(boolean debug) {
102         if (!loadedProps) {
103             String JavaDoc protocol = UtilProperties.getPropertyValue("jsse.properties", "java.protocol.handler.pkgs", "NONE");
104             String JavaDoc proxyHost = UtilProperties.getPropertyValue("jsse.properties", "https.proxyHost", "NONE");
105             String JavaDoc proxyPort = UtilProperties.getPropertyValue("jsse.properties", "https.proxyPort", "NONE");
106             String JavaDoc cypher = UtilProperties.getPropertyValue("jsse.properties", "https.cipherSuites", "NONE");
107             if (protocol != null && !protocol.equals("NONE")) {
108                 System.setProperty("java.protocol.handler.pkgs", protocol);
109             }
110             if (proxyHost != null && !proxyHost.equals("NONE")) {
111                 System.setProperty("https.proxyHost", proxyHost);
112             }
113             if (proxyPort != null && !proxyPort.equals("NONE")) {
114                 System.setProperty("https.proxyPort", proxyPort);
115             }
116             if (cypher != null && !cypher.equals("NONE")) {
117                 System.setProperty("https.cipherSuites", cypher);
118             }
119
120             // set up the keystore properties
121
System.setProperty("javax.net.ssl.keyStore", KeyStoreUtil.getKeyStoreFileName());
122             System.setProperty("javax.net.ssl.keyStorePassword", KeyStoreUtil.getKeyStorePassword());
123             System.setProperty("javax.net.ssl.trustStore", KeyStoreUtil.getTrustStoreFileName());
124             System.setProperty("javax.net.ssl.trustStorePassword", KeyStoreUtil.getTrustStorePassword());
125             if (debug) {
126                 System.setProperty("javax.net.debug","ssl:handshake");
127             }
128             loadedProps = true;
129         }
130     }
131 }
132
Popular Tags