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