KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import java.net.ServerSocket JavaDoc;
26
27 // SSL classes
28
import java.security.KeyStore JavaDoc;
29 import java.security.SecureRandom JavaDoc;
30 import javax.net.ssl.KeyManagerFactory;
31 import javax.net.ssl.SSLServerSocketFactory;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLServerSocket;
34
35 import org.lucane.common.crypto.Base64;
36 import org.lucane.common.crypto.KeyTool;
37
38 public class ServerSocketFactory
39 {
40     public static ServerSocket JavaDoc getServerSocket(int port)
41     throws IOException JavaDoc
42     {
43         return new ServerSocket JavaDoc(port);
44     }
45     
46     public static ServerSocket JavaDoc getServerSocket(int port, String JavaDoc privateKey, String JavaDoc storePwd, String JavaDoc keyPwd)
47     throws Exception JavaDoc
48     {
49         SSLContext sslContext = createSSLContext(privateKey, storePwd, keyPwd);
50         SSLServerSocketFactory ssf = sslContext.getServerSocketFactory();
51         SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket(port);
52         serverSocket.setNeedClientAuth(false);
53         return serverSocket;
54     }
55
56     private static SSLContext createSSLContext(String JavaDoc privateKey, String JavaDoc storePwd, String JavaDoc keyPwd)
57     throws Exception JavaDoc
58     {
59         byte[] key = Base64.decode(privateKey);
60         InputStream JavaDoc input = new ByteArrayInputStream JavaDoc(key);
61         
62         KeyStore JavaDoc serverKeyStore = KeyStore.getInstance("JKS");
63         serverKeyStore.load(input, KeyTool.sixCharsMin(storePwd).toCharArray());
64         
65         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
66         kmf.init(serverKeyStore, KeyTool.sixCharsMin(keyPwd).toCharArray());
67         
68         SSLContext sslContext = SSLContext.getInstance("TLS");
69         sslContext.init(kmf.getKeyManagers(), null, new SecureRandom JavaDoc());
70         
71         return sslContext;
72     }
73 }
Popular Tags