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
018package org.apache.commons.net.util;
019
020import java.io.IOException;
021import java.security.GeneralSecurityException;
022
023import javax.net.ssl.KeyManager;
024import javax.net.ssl.SSLContext;
025import javax.net.ssl.TrustManager;
026
027/**
028 * General utilities for SSLContext.
029 *
030 * @since 3.0
031 */
032public class SSLContextUtils {
033
034    /**
035     * Create and initialize an SSLContext.
036     *
037     * @param protocol     the protocol used to instantiate the context
038     * @param keyManager   the key manager, may be {@code null}
039     * @param trustManager the trust manager, may be {@code null}
040     * @return the initialized context.
041     * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
042     */
043    public static SSLContext createSSLContext(final String protocol, final KeyManager keyManager, final TrustManager trustManager) throws IOException {
044        return createSSLContext(protocol, keyManager == null ? null : new KeyManager[] { keyManager },
045                trustManager == null ? null : new TrustManager[] { trustManager });
046    }
047
048    /**
049     * Create and initialize an SSLContext.
050     *
051     * @param protocol      the protocol used to instantiate the context
052     * @param keyManagers   the array of key managers, may be {@code null} but array entries must not be {@code null}
053     * @param trustManagers the array of trust managers, may be {@code null} but array entries must not be {@code null}
054     * @return the initialized context.
055     * @throws IOException this is used to wrap any {@link GeneralSecurityException} that occurs
056     */
057    public static SSLContext createSSLContext(final String protocol, final KeyManager[] keyManagers, final TrustManager[] trustManagers) throws IOException {
058        final SSLContext ctx;
059        try {
060            ctx = SSLContext.getInstance(protocol);
061            ctx.init(keyManagers, trustManagers, /* SecureRandom */ null);
062        } catch (final GeneralSecurityException e) {
063            final IOException ioe = new IOException("Could not initialize SSL context");
064            ioe.initCause(e);
065            throw ioe;
066        }
067        return ctx;
068    }
069
070    private SSLContextUtils() {
071        // Not instantiable
072    }
073}