View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.localserver;
29  
30  import java.net.URL;
31  import java.security.KeyStore;
32  import java.security.NoSuchAlgorithmException;
33  
34  import javax.net.ssl.KeyManager;
35  import javax.net.ssl.KeyManagerFactory;
36  import javax.net.ssl.SSLContext;
37  import javax.net.ssl.TrustManager;
38  import javax.net.ssl.TrustManagerFactory;
39  
40  public class SSLTestContexts {
41  
42      private static KeyManagerFactory createKeyManagerFactory() throws NoSuchAlgorithmException {
43          final String algo = KeyManagerFactory.getDefaultAlgorithm();
44          try {
45              return KeyManagerFactory.getInstance(algo);
46          } catch (final NoSuchAlgorithmException ex) {
47              return KeyManagerFactory.getInstance("SunX509");
48          }
49      }
50  
51      public static SSLContext createServerSSLContext() throws Exception {
52          final ClassLoader cl = SSLTestContexts.class.getClassLoader();
53          final URL url = cl.getResource("test.keystore");
54          final KeyStore keystore  = KeyStore.getInstance("jks");
55          keystore.load(url.openStream(), "nopassword".toCharArray());
56          final KeyManagerFactory kmfactory = createKeyManagerFactory();
57          kmfactory.init(keystore, "nopassword".toCharArray());
58          final KeyManager[] keymanagers = kmfactory.getKeyManagers();
59          final SSLContext sslcontext = SSLContext.getInstance("TLS");
60          sslcontext.init(keymanagers, null, null);
61          return sslcontext;
62      }
63  
64      private static TrustManagerFactory createTrustManagerFactory() throws NoSuchAlgorithmException {
65          final String algo = TrustManagerFactory.getDefaultAlgorithm();
66          try {
67              return TrustManagerFactory.getInstance(algo);
68          } catch (final NoSuchAlgorithmException ex) {
69              return TrustManagerFactory.getInstance("SunX509");
70          }
71      }
72  
73      public static SSLContext createClientSSLContext() throws Exception {
74          final ClassLoader cl = SSLTestContexts.class.getClassLoader();
75          final URL url = cl.getResource("test.keystore");
76          final KeyStore keystore  = KeyStore.getInstance("jks");
77          keystore.load(url.openStream(), "nopassword".toCharArray());
78          final TrustManagerFactory tmfactory = createTrustManagerFactory();
79          tmfactory.init(keystore);
80          final TrustManager[] trustmanagers = tmfactory.getTrustManagers();
81          final SSLContext sslcontext = SSLContext.getInstance("TLS");
82          sslcontext.init(null, trustmanagers, null);
83          return sslcontext;
84      }
85  
86  }