KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > echoserver > ssl > BogusSSLContextFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.example.echoserver.ssl;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.security.GeneralSecurityException JavaDoc;
25 import java.security.KeyStore JavaDoc;
26 import java.security.Security JavaDoc;
27
28 import javax.net.ssl.KeyManagerFactory;
29 import javax.net.ssl.SSLContext;
30
31 /**
32  * Factory to create a bougus SSLContext.
33  *
34  * @author The Apache Directory Project (mina-dev@directory.apache.org)
35  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
36  */

37 public class BogusSSLContextFactory {
38
39     /**
40      * Protocol to use.
41      */

42     private static final String JavaDoc PROTOCOL = "TLS";
43
44     private static final String JavaDoc KEY_MANAGER_FACTORY_ALGORITHM;
45
46     static {
47         String JavaDoc algorithm = Security
48                 .getProperty("ssl.KeyManagerFactory.algorithm");
49         if (algorithm == null) {
50             algorithm = "SunX509";
51         }
52
53         KEY_MANAGER_FACTORY_ALGORITHM = algorithm;
54     }
55
56     /**
57      * Bougus Server certificate keystore file name.
58      */

59     private static final String JavaDoc BOGUS_KEYSTORE = "bogus.cert";
60
61     // NOTE: The keystore was generated using keytool:
62
// keytool -genkey -alias bogus -keysize 512 -validity 3650
63
// -keyalg RSA -dname "CN=bogus.com, OU=XXX CA,
64
// O=Bogus Inc, L=Stockholm, S=Stockholm, C=SE"
65
// -keypass boguspw -storepass boguspw -keystore bogus.cert
66

67     /**
68      * Bougus keystore password.
69      */

70     private static final char[] BOGUS_PW = { 'b', 'o', 'g', 'u', 's', 'p', 'w' };
71
72     private static SSLContext serverInstance = null;
73
74     private static SSLContext clientInstance = null;
75
76     /**
77      * Get SSLContext singleton.
78      *
79      * @return SSLContext
80      * @throws java.security.GeneralSecurityException
81      *
82      */

83     public static SSLContext getInstance(boolean server)
84             throws GeneralSecurityException JavaDoc {
85         SSLContext retInstance = null;
86         if (server) {
87             if (serverInstance == null) {
88                 synchronized (BogusSSLContextFactory.class) {
89                     if (serverInstance == null) {
90                         try {
91                             serverInstance = createBougusServerSSLContext();
92                         } catch (Exception JavaDoc ioe) {
93                             throw new GeneralSecurityException JavaDoc(
94                                     "Can't create Server SSLContext:" + ioe);
95                         }
96                     }
97                 }
98             }
99             retInstance = serverInstance;
100         } else {
101             if (clientInstance == null) {
102                 synchronized (BogusSSLContextFactory.class) {
103                     if (clientInstance == null) {
104                         clientInstance = createBougusClientSSLContext();
105                     }
106                 }
107             }
108             retInstance = clientInstance;
109         }
110         return retInstance;
111     }
112
113     private static SSLContext createBougusServerSSLContext()
114             throws GeneralSecurityException JavaDoc, IOException JavaDoc {
115         // Create keystore
116
KeyStore JavaDoc ks = KeyStore.getInstance("JKS");
117         InputStream JavaDoc in = null;
118         try {
119             in = BogusSSLContextFactory.class
120                     .getResourceAsStream(BOGUS_KEYSTORE);
121             ks.load(in, BOGUS_PW);
122         } finally {
123             if (in != null) {
124                 try {
125                     in.close();
126                 } catch (IOException JavaDoc ignored) {
127                 }
128             }
129         }
130
131         // Set up key manager factory to use our key store
132
KeyManagerFactory kmf = KeyManagerFactory
133                 .getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
134         kmf.init(ks, BOGUS_PW);
135
136         // Initialize the SSLContext to work with our key managers.
137
SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
138         sslContext.init(kmf.getKeyManagers(),
139                 BogusTrustManagerFactory.X509_MANAGERS, null);
140
141         return sslContext;
142     }
143
144     private static SSLContext createBougusClientSSLContext()
145             throws GeneralSecurityException JavaDoc {
146         SSLContext context = SSLContext.getInstance(PROTOCOL);
147         context.init(null, BogusTrustManagerFactory.X509_MANAGERS, null);
148         return context;
149     }
150
151 }
152
Popular Tags