    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *   http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 *
    017 */
    018
    019package org.apache.commons.net.util;
    020
    021import java.io.IOException;
    022import java.security.GeneralSecurityException;
    023
    024import javax.net.ssl.KeyManager;
    025import javax.net.ssl.SSLContext;
    026import javax.net.ssl.TrustManager;
    027
    028/**
    029 * General utilities for SSLContext.
    030 *
    031 * @since 3.0
    032 */
    033public class SSLContextUtils {
    034
    035    /**
    036     * Create and initialize an SSLContext.
    037     *
    038     * @param protocol     the protocol used to instatiate the context
    039     * @param keyManager   the key manager, may be {@code null}
    040     * @param trustManager the trust manager, may be {@code null}
    041     * @return the initialized context.
    042     * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
    043     */
    044    public static SSLContext createSSLContext(final String protocol, final KeyManager keyManager, final TrustManager trustManager) throws IOException {
    045        return createSSLContext(protocol, keyManager == null ? null : new KeyManager[] { keyManager },
    046                trustManager == null ? null : new TrustManager[] { trustManager });
    047    }
    048
    049    /**
    050     * Create and initialize an SSLContext.
    051     *
    052     * @param protocol      the protocol used to instatiate the context
    053     * @param keyManagers   the array of key managers, may be {@code null} but array entries must not be {@code null}
    054     * @param trustManagers the array of trust managers, may be {@code null} but array entries must not be {@code null}
    055     * @return the initialized context.
    056     * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
    057     */
    058    public static SSLContext createSSLContext(final String protocol, final KeyManager[] keyManagers, final TrustManager[] trustManagers) throws IOException {
    059        final SSLContext ctx;
    060        try {
    061            ctx = SSLContext.getInstance(protocol);
    062            ctx.init(keyManagers, trustManagers, /* SecureRandom */ null);
    063        } catch (final GeneralSecurityException e) {
    064            final IOException ioe = new IOException("Could not initialize SSL context");
    065            ioe.initCause(e);
    066            throw ioe;
    067        }
    068        return ctx;
    069    }
    070
    071    private SSLContextUtils() {
    072        // Not instantiable
    073    }
    074}