KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > common > net > ClientSocketFactory


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Gilles Viguie <gilles.viguie@free.fr>
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 org.lucane.common.net;
20
21 import java.net.Socket JavaDoc;
22
23 import java.security.SecureRandom JavaDoc;
24 import java.security.KeyStore JavaDoc;
25 import javax.net.ssl.*;
26
27 import org.lucane.common.ConnectInfo;
28 import org.lucane.common.crypto.Base64;
29 import org.lucane.common.crypto.KeyTool;
30
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33
34 public class ClientSocketFactory
35 {
36     public static Socket JavaDoc getSocket(ConnectInfo info)
37     throws Exception JavaDoc
38     {
39         if(info.getPublicKey().equals("nokey"))
40             return getSocket(info.getHostName(), info.getPort());
41         
42         String JavaDoc passwd = info.getName();
43         if(info.isService())
44             passwd = "server";
45         
46         return getSSLSocket(info.getHostName(), info.getPort(), info.getPublicKey(), passwd);
47     }
48     
49     private static Socket JavaDoc getSocket(String JavaDoc hostName, int port)
50     throws Exception JavaDoc
51     {
52         return new Socket JavaDoc(hostName, port);
53     }
54     
55     private static Socket JavaDoc getSSLSocket(String JavaDoc hostName, int port, String JavaDoc publicKey, String JavaDoc password)
56     throws Exception JavaDoc
57     {
58         SSLContext sslContext = createSSLContext(publicKey, password);
59         SSLSocketFactory factory = sslContext.getSocketFactory();
60         SSLSocket socket = (SSLSocket) factory.createSocket(hostName, port);
61         String JavaDoc[] supported = socket.getSupportedCipherSuites();
62         socket.setEnabledCipherSuites(supported);
63         socket.startHandshake();
64         return socket;
65     }
66     
67     private static SSLContext createSSLContext(String JavaDoc publicKey, String JavaDoc password)
68     throws Exception JavaDoc
69     {
70         byte[] key = Base64.decode(publicKey);
71         InputStream JavaDoc input = new ByteArrayInputStream JavaDoc(key);
72         
73         KeyStore JavaDoc serverKeyStore = KeyStore.getInstance("JKS");
74         serverKeyStore.load(input, KeyTool.sixCharsMin(password).toCharArray());
75         
76         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
77         tmf.init(serverKeyStore);
78         
79         SSLContext sslContext = SSLContext.getInstance("TLS");
80         sslContext.init(null,tmf.getTrustManagers(), new SecureRandom JavaDoc());
81         
82         return sslContext;
83     }
84 }
Popular Tags