View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.net.util;
20  
21  import java.io.IOException;
22  import java.security.GeneralSecurityException;
23  import javax.net.ssl.KeyManager;
24  import javax.net.ssl.SSLContext;
25  import javax.net.ssl.TrustManager;
26  
27  /**
28   * General utilities for SSLContext.
29   * @since 3.0
30   */
31  public class SSLContextUtils {
32  
33      private SSLContextUtils() {
34          // Not instantiable
35      }
36  
37      /**
38       * Create and initialise sn SSLContext.
39       * @param protocol the protocol used to instatiate the context
40       * @param keyManager the key manager, may be {@code null}
41       * @param trustManager the trust manager, may be {@code null}
42       * @return the initialised context.
43       * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
44       */
45      public static SSLContext createSSLContext(String protocol, KeyManager keyManager, TrustManager trustManager) throws IOException {
46          return createSSLContext(protocol,
47                  keyManager == null ? null : new KeyManager[] { keyManager },
48                  trustManager == null ? null : new TrustManager[] { trustManager });
49      }
50  
51      /**
52       * Create and initialise sn SSLContext.
53       * @param protocol the protocol used to instatiate the context
54       * @param keyManagers the array of key managers, may be {@code null} but array entries must not be {@code null}
55       * @param trustManagers the array of trust managers, may be {@code null} but array entries must not be {@code null}
56       * @return the initialised context.
57       * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
58       */
59      public static SSLContext createSSLContext(String protocol, KeyManager[] keyManagers, TrustManager[] trustManagers)
60          throws IOException {
61          SSLContext ctx;
62          try {
63              ctx = SSLContext.getInstance(protocol);
64              ctx.init(keyManagers, trustManagers, /*SecureRandom*/ null);
65          } catch (GeneralSecurityException e) {
66              IOException ioe = new IOException("Could not initialize SSL context");
67              ioe.initCause(e);
68              throw ioe;
69          }
70          return ctx;
71      }
72  }