SSLContextUtils.java

  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. package org.apache.commons.net.util;

  18. import java.io.IOException;
  19. import java.security.GeneralSecurityException;

  20. import javax.net.ssl.KeyManager;
  21. import javax.net.ssl.SSLContext;
  22. import javax.net.ssl.TrustManager;

  23. /**
  24.  * General utilities for SSLContext.
  25.  *
  26.  * @since 3.0
  27.  */
  28. public class SSLContextUtils {

  29.     /**
  30.      * Create and initialize an SSLContext.
  31.      *
  32.      * @param protocol     the protocol used to instantiate the context
  33.      * @param keyManager   the key manager, may be {@code null}
  34.      * @param trustManager the trust manager, may be {@code null}
  35.      * @return the initialized context.
  36.      * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
  37.      */
  38.     public static SSLContext createSSLContext(final String protocol, final KeyManager keyManager, final TrustManager trustManager) throws IOException {
  39.         return createSSLContext(protocol, keyManager == null ? null : new KeyManager[] { keyManager },
  40.                 trustManager == null ? null : new TrustManager[] { trustManager });
  41.     }

  42.     /**
  43.      * Create and initialize an SSLContext.
  44.      *
  45.      * @param protocol      the protocol used to instantiate the context
  46.      * @param keyManagers   the array of key managers, may be {@code null} but array entries must not be {@code null}
  47.      * @param trustManagers the array of trust managers, may be {@code null} but array entries must not be {@code null}
  48.      * @return the initialized context.
  49.      * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
  50.      */
  51.     public static SSLContext createSSLContext(final String protocol, final KeyManager[] keyManagers, final TrustManager[] trustManagers) throws IOException {
  52.         final SSLContext ctx;
  53.         try {
  54.             ctx = SSLContext.getInstance(protocol);
  55.             ctx.init(keyManagers, trustManagers, /* SecureRandom */ null);
  56.         } catch (final GeneralSecurityException e) {
  57.             final IOException ioe = new IOException("Could not initialize SSL context");
  58.             ioe.initCause(e);
  59.             throw ioe;
  60.         }
  61.         return ctx;
  62.     }

  63.     private SSLContextUtils() {
  64.         // Not instantiable
  65.     }
  66. }