001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.echoserver.ssl;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.security.GeneralSecurityException;
025: import java.security.KeyStore;
026: import java.security.Security;
027:
028: import javax.net.ssl.KeyManagerFactory;
029: import javax.net.ssl.SSLContext;
030:
031: /**
032: * Factory to create a bougus SSLContext.
033: *
034: * @author The Apache MINA Project (dev@mina.apache.org)
035: * @version $Rev: 576647 $, $Date: 2007-09-17 19:41:29 -0600 (Mon, 17 Sep 2007) $
036: */
037: public class BogusSslContextFactory {
038:
039: /**
040: * Protocol to use.
041: */
042: private static final String PROTOCOL = "TLS";
043:
044: private static final String KEY_MANAGER_FACTORY_ALGORITHM;
045:
046: static {
047: String algorithm = Security
048: .getProperty("ssl.KeyManagerFactory.algorithm");
049: if (algorithm == null) {
050: algorithm = "SunX509";
051: }
052:
053: KEY_MANAGER_FACTORY_ALGORITHM = algorithm;
054: }
055:
056: /**
057: * Bougus Server certificate keystore file name.
058: */
059: private static final String BOGUS_KEYSTORE = "bogus.cert";
060:
061: // NOTE: The keystore was generated using keytool:
062: // keytool -genkey -alias bogus -keysize 512 -validity 3650
063: // -keyalg RSA -dname "CN=bogus.com, OU=XXX CA,
064: // O=Bogus Inc, L=Stockholm, S=Stockholm, C=SE"
065: // -keypass boguspw -storepass boguspw -keystore bogus.cert
066:
067: /**
068: * Bougus keystore password.
069: */
070: private static final char[] BOGUS_PW = { 'b', 'o', 'g', 'u', 's',
071: 'p', 'w' };
072:
073: private static SSLContext serverInstance = null;
074:
075: private static SSLContext clientInstance = null;
076:
077: /**
078: * Get SSLContext singleton.
079: *
080: * @return SSLContext
081: * @throws java.security.GeneralSecurityException
082: *
083: */
084: public static SSLContext getInstance(boolean server)
085: throws GeneralSecurityException {
086: SSLContext retInstance = null;
087: if (server) {
088: if (serverInstance == null) {
089: synchronized (BogusSslContextFactory.class) {
090: if (serverInstance == null) {
091: try {
092: serverInstance = createBougusServerSslContext();
093: } catch (Exception ioe) {
094: throw new GeneralSecurityException(
095: "Can't create Server SSLContext:"
096: + ioe);
097: }
098: }
099: }
100: }
101: retInstance = serverInstance;
102: } else {
103: if (clientInstance == null) {
104: synchronized (BogusSslContextFactory.class) {
105: if (clientInstance == null) {
106: clientInstance = createBougusClientSslContext();
107: }
108: }
109: }
110: retInstance = clientInstance;
111: }
112: return retInstance;
113: }
114:
115: private static SSLContext createBougusServerSslContext()
116: throws GeneralSecurityException, IOException {
117: // Create keystore
118: KeyStore ks = KeyStore.getInstance("JKS");
119: InputStream in = null;
120: try {
121: in = BogusSslContextFactory.class
122: .getResourceAsStream(BOGUS_KEYSTORE);
123: ks.load(in, BOGUS_PW);
124: } finally {
125: if (in != null) {
126: try {
127: in.close();
128: } catch (IOException ignored) {
129: }
130: }
131: }
132:
133: // Set up key manager factory to use our key store
134: KeyManagerFactory kmf = KeyManagerFactory
135: .getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
136: kmf.init(ks, BOGUS_PW);
137:
138: // Initialize the SSLContext to work with our key managers.
139: SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
140: sslContext.init(kmf.getKeyManagers(),
141: BogusTrustManagerFactory.X509_MANAGERS, null);
142:
143: return sslContext;
144: }
145:
146: private static SSLContext createBougusClientSslContext()
147: throws GeneralSecurityException {
148: SSLContext context = SSLContext.getInstance(PROTOCOL);
149: context
150: .init(null, BogusTrustManagerFactory.X509_MANAGERS,
151: null);
152: return context;
153: }
154:
155: }
|