Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * ====================================================================
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
  *
  */
 
 package org.apache.solr.shaded.org.apache.http.conn.ssl;
 
 import java.util.Map;
 import java.util.Set;
 
 
Builder for javax.net.ssl.SSLContext instances.

Please note: the default Oracle JSSE implementation of javax.net.ssl.SSLContext.init(javax.net.ssl.KeyManager[],javax.net.ssl.TrustManager[],java.security.SecureRandom) accepts multiple key and trust managers, however only only first matching type is ever used. See for example: SSLContext.html#init

Since:
4.3
 
 public class SSLContextBuilder {
 
     static final String TLS   = "TLS";
     static final String SSL   = "SSL";
 
     private String protocol;
     private final Set<KeyManagerkeymanagers;
     private final Set<TrustManagertrustmanagers;
     private SecureRandom secureRandom;
 
     public SSLContextBuilder() {
         super();
         this. = new LinkedHashSet<KeyManager>();
         this. = new LinkedHashSet<TrustManager>();
     }
 
     public SSLContextBuilder useTLS() {
         this. = ;
         return this;
     }
 
     public SSLContextBuilder useSSL() {
         this. = ;
         return this;
     }
 
     public SSLContextBuilder useProtocol(final String protocol) {
         this. = protocol;
         return this;
     }
 
    public SSLContextBuilder setSecureRandom(final SecureRandom secureRandom) {
        this. = secureRandom;
        return this;
    }
            final KeyStore truststore,
            final TrustStrategy trustStrategythrows NoSuchAlgorithmExceptionKeyStoreException {
        final TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
                TrustManagerFactory.getDefaultAlgorithm());
        tmfactory.init(truststore);
        final TrustManager[] tms = tmfactory.getTrustManagers();
        if (tms != null) {
            if (trustStrategy != null) {
                for (int i = 0; i < tms.lengthi++) {
                    final TrustManager tm = tms[i];
                    if (tm instanceof X509TrustManager) {
                        tms[i] = new TrustManagerDelegate(
                                (X509TrustManagertmtrustStrategy);
                    }
                }
            }
            for (final TrustManager tm : tms) {
                this..add(tm);
            }
        }
        return this;
    }
            final KeyStore truststorethrows NoSuchAlgorithmExceptionKeyStoreException {
        return loadTrustMaterial(truststorenull);
    }
            final KeyStore keystore,
            final char[] keyPassword)
                throws NoSuchAlgorithmExceptionKeyStoreExceptionUnrecoverableKeyException {
        loadKeyMaterial(keystorekeyPasswordnull);
        return this;
    }
            final KeyStore keystore,
            final char[] keyPassword,
            final PrivateKeyStrategy aliasStrategy)
        final KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
                KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystorekeyPassword);
        final KeyManager[] kms =  kmfactory.getKeyManagers();
        if (kms != null) {
            if (aliasStrategy != null) {
                for (int i = 0; i < kms.lengthi++) {
                    final KeyManager km = kms[i];
                    if (km instanceof X509KeyManager) {
                        kms[i] = new KeyManagerDelegate(
                                (X509KeyManagerkmaliasStrategy);
                    }
                }
            }
            for (final KeyManager km : kms) {
                .add(km);
            }
        }
        return this;
    }
        final SSLContext sslcontext = SSLContext.getInstance(
                this. != null ? this. : );
        sslcontext.init(
                !.isEmpty() ? .toArray(new KeyManager[.size()]) : null,
                !.isEmpty() ? .toArray(new TrustManager[.size()]) : null,
                );
        return sslcontext;
    }
    static class TrustManagerDelegate implements X509TrustManager {
        private final X509TrustManager trustManager;
        private final TrustStrategy trustStrategy;
        TrustManagerDelegate(final X509TrustManager trustManagerfinal TrustStrategy trustStrategy) {
            super();
            this. = trustManager;
            this. = trustStrategy;
        }
        public void checkClientTrusted(
                final X509Certificate[] chainfinal String authTypethrows CertificateException {
            this..checkClientTrusted(chainauthType);
        }
        public void checkServerTrusted(
                final X509Certificate[] chainfinal String authTypethrows CertificateException {
            if (!this..isTrusted(chainauthType)) {
                this..checkServerTrusted(chainauthType);
            }
        }
        public X509Certificate[] getAcceptedIssuers() {
            return this..getAcceptedIssuers();
        }
    }
    static class KeyManagerDelegate implements X509KeyManager {
        private final X509KeyManager keyManager;
        private final PrivateKeyStrategy aliasStrategy;
        KeyManagerDelegate(final X509KeyManager keyManagerfinal PrivateKeyStrategy aliasStrategy) {
            super();
            this. = keyManager;
            this. = aliasStrategy;
        }
        public String[] getClientAliases(
                final String keyTypefinal Principal[] issuers) {
            return this..getClientAliases(keyTypeissuers);
        }
        public String chooseClientAlias(
                final String[] keyTypesfinal Principal[] issuersfinal Socket socket) {
            final Map<StringPrivateKeyDetailsvalidAliases = new HashMap<StringPrivateKeyDetails>();
            for (final String keyTypekeyTypes) {
                final String[] aliases = this..getClientAliases(keyTypeissuers);
                if (aliases != null) {
                    for (final String aliasaliases) {
                        validAliases.put(alias,
                                new PrivateKeyDetails(keyTypethis..getCertificateChain(alias)));
                    }
                }
            }
            return this..chooseAlias(validAliasessocket);
        }
        public String[] getServerAliases(
                final String keyTypefinal Principal[] issuers) {
            return this..getServerAliases(keyTypeissuers);
        }
        public String chooseServerAlias(
                final String keyTypefinal Principal[] issuersfinal Socket socket) {
            final Map<StringPrivateKeyDetailsvalidAliases = new HashMap<StringPrivateKeyDetails>();
            final String[] aliases = this..getServerAliases(keyTypeissuers);
            if (aliases != null) {
                for (final String aliasaliases) {
                    validAliases.put(alias,
                            new PrivateKeyDetails(keyTypethis..getCertificateChain(alias)));
                }
            }
            return this..chooseAlias(validAliasessocket);
        }
        public X509Certificate[] getCertificateChain(final String alias) {
            return this..getCertificateChain(alias);
        }
        public PrivateKey getPrivateKey(final String alias) {
            return this..getPrivateKey(alias);
        }
    }
New to GrepCode? Check out our FAQ X