1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: SSLContextSetup.java 1605 2008-02-25 21:07:14Z barteo $
26   */
27  package org.microemu;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.security.KeyStore;
32  import java.security.SecureRandom;
33  
34  import javax.net.ssl.HttpsURLConnection;
35  import javax.net.ssl.SSLContext;
36  import javax.net.ssl.TrustManager;
37  import javax.net.ssl.TrustManagerFactory;
38  
39  /**
40   * @author vlads
41   * 
42   * See src/test/ssl/read-me.txt
43   * 
44   */
45  public class SSLContextSetup {
46  	
47  	private static boolean initialized = false;
48  	
49  	public static synchronized void setUp() {
50      	if (initialized) {
51      		return;
52      	}
53      	InputStream is = null;
54          try {
55              KeyStore trustStore = KeyStore.getInstance("JKS");
56              is = SSLContextSetup.class.getResourceAsStream("/test-servers.keystore"); 
57              if (is == null) {
58              	new Error("keystore not found");
59              }
60              trustStore.load(is, "microemu2006".toCharArray());
61              TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");  
62              trustManagerFactory.init(trustStore);  
63              TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
64              
65              SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
66              SSLContext context = SSLContext.getInstance("TLS");
67              context.init(null, trustManagers, secureRandom);
68              HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
69              initialized = true;
70          } catch (Throwable e) {
71              throw new Error(e);
72          } finally {
73          	if (is != null) {
74          		try {
75  					is.close();
76  				} catch (IOException ignore) {
77  				}
78          	}
79          }
80      }
81  }