View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.ssl;
29  
30  import javax.net.ssl.HostnameVerifier;
31  import javax.net.ssl.SSLContext;
32  
33  import org.apache.hc.core5.http.ssl.TLS;
34  import org.apache.hc.core5.ssl.SSLContexts;
35  
36  /**
37   * Builder for {@link SSLConnectionSocketFactory} instances.
38   * <p>
39   * When a particular component is not explicitly set this class will
40   * use its default implementation. System properties will be taken
41   * into account when configuring the default implementations when
42   * {@link #useSystemProperties()} method is called prior to calling
43   * {@link #build()}.
44   * </p>
45   * <ul>
46   *  <li>ssl.TrustManagerFactory.algorithm</li>
47   *  <li>javax.net.ssl.trustStoreType</li>
48   *  <li>javax.net.ssl.trustStore</li>
49   *  <li>javax.net.ssl.trustStoreProvider</li>
50   *  <li>javax.net.ssl.trustStorePassword</li>
51   *  <li>ssl.KeyManagerFactory.algorithm</li>
52   *  <li>javax.net.ssl.keyStoreType</li>
53   *  <li>javax.net.ssl.keyStore</li>
54   *  <li>javax.net.ssl.keyStoreProvider</li>
55   *  <li>javax.net.ssl.keyStorePassword</li>
56   *  <li>https.protocols</li>
57   *  <li>https.cipherSuites</li>
58   * </ul>
59   *
60   * @since 5.0
61   */
62  public class SSLConnectionSocketFactoryBuilder {
63  
64      public static SSLConnectionSocketFactoryBuilder create() {
65          return new SSLConnectionSocketFactoryBuilder();
66      }
67  
68      private SSLContext sslContext;
69      private String[] tlsVersions;
70      private String[] ciphers;
71      private HostnameVerifier hostnameVerifier;
72      private boolean systemProperties;
73  
74      /**
75       * Assigns {@link SSLContext} instance.
76       */
77      public SSLConnectionSocketFactoryBuilder setSslContext(final SSLContext sslContext) {
78          this.sslContext = sslContext;
79          return this;
80      }
81  
82      /**
83       * Assigns enabled {@code TLS} versions.
84       */
85      public final SSLConnectionSocketFactoryBuilder setTlsVersions(final String... tlslVersions) {
86          this.tlsVersions = tlslVersions;
87          return this;
88      }
89  
90      /**
91       * Assigns enabled {@code TLS} versions.
92       */
93      public final SSLConnectionSocketFactoryBuilder setTlsVersions(final TLS... tlslVersions) {
94          this.tlsVersions = new String[tlslVersions.length];
95          for (int i = 0; i < tlslVersions.length; i++) {
96              this.tlsVersions[i] = tlslVersions[i].id;
97          }
98          return this;
99      }
100 
101     /**
102      * Assigns enabled ciphers.
103      */
104     public final SSLConnectionSocketFactoryBuilder setCiphers(final String... ciphers) {
105         this.ciphers = ciphers;
106         return this;
107     }
108 
109 
110     /**
111      * Assigns {@link HostnameVerifier} instance.
112      */
113     public SSLConnectionSocketFactoryBuilder setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
114         this.hostnameVerifier = hostnameVerifier;
115         return this;
116     }
117 
118     /**
119      * Use system properties when creating and configuring default
120      * implementations.
121      */
122     public final SSLConnectionSocketFactoryBuilder useSystemProperties() {
123         this.systemProperties = true;
124         return this;
125     }
126 
127     public SSLConnectionSocketFactory build() {
128         final javax.net.ssl.SSLSocketFactory socketFactory;
129         if (sslContext != null) {
130             socketFactory = sslContext.getSocketFactory();
131         } else {
132             if (systemProperties) {
133                 socketFactory = (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
134             } else {
135                 socketFactory = SSLContexts.createDefault().getSocketFactory();
136             }
137         }
138         final String[] tlsVersionsCopy;
139         if (tlsVersions != null) {
140             tlsVersionsCopy = tlsVersions;
141         } else {
142             tlsVersionsCopy = systemProperties ? HttpsSupport.getSystemProtocols() : null;
143         }
144         final String[] ciphersCopy;
145         if (ciphers != null) {
146             ciphersCopy = ciphers;
147         } else {
148             ciphersCopy = systemProperties ? HttpsSupport.getSystemCipherSuits() : null;
149         }
150         return new SSLConnectionSocketFactory(
151                 socketFactory,
152                 tlsVersionsCopy,
153                 ciphersCopy,
154                 hostnameVerifier != null ? hostnameVerifier : HttpsSupport.getDefaultHostnameVerifier());
155     }
156 
157 }