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   * @deprecated Use {@link DefaultClientTlsStrategy}
61   */
62  @Deprecated
63  public class SSLConnectionSocketFactoryBuilder {
64  
65      public static SSLConnectionSocketFactoryBuilder create() {
66          return new SSLConnectionSocketFactoryBuilder();
67      }
68  
69      private SSLContext sslContext;
70      private String[] tlsVersions;
71      private String[] ciphers;
72      private HostnameVerifier hostnameVerifier;
73      private boolean systemProperties;
74  
75      /**
76       * Assigns {@link SSLContext} instance.
77       */
78      public SSLConnectionSocketFactoryBuilder setSslContext(final SSLContext sslContext) {
79          this.sslContext = sslContext;
80          return this;
81      }
82  
83      /**
84       * Assigns enabled {@code TLS} versions.
85       */
86      public final SSLConnectionSocketFactoryBuilder setTlsVersions(final String... tlslVersions) {
87          this.tlsVersions = tlslVersions;
88          return this;
89      }
90  
91      /**
92       * Assigns enabled {@code TLS} versions.
93       */
94      public final SSLConnectionSocketFactoryBuilder setTlsVersions(final TLS... tlslVersions) {
95          this.tlsVersions = new String[tlslVersions.length];
96          for (int i = 0; i < tlslVersions.length; i++) {
97              this.tlsVersions[i] = tlslVersions[i].id;
98          }
99          return this;
100     }
101 
102     /**
103      * Assigns enabled ciphers.
104      */
105     public final SSLConnectionSocketFactoryBuilder setCiphers(final String... ciphers) {
106         this.ciphers = ciphers;
107         return this;
108     }
109 
110 
111     /**
112      * Assigns {@link HostnameVerifier} instance.
113      */
114     public SSLConnectionSocketFactoryBuilder setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
115         this.hostnameVerifier = hostnameVerifier;
116         return this;
117     }
118 
119     /**
120      * Use system properties when creating and configuring default
121      * implementations.
122      */
123     public final SSLConnectionSocketFactoryBuilder useSystemProperties() {
124         this.systemProperties = true;
125         return this;
126     }
127 
128     public SSLConnectionSocketFactory build() {
129         final javax.net.ssl.SSLSocketFactory socketFactory;
130         if (sslContext != null) {
131             socketFactory = sslContext.getSocketFactory();
132         } else {
133             if (systemProperties) {
134                 socketFactory = (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
135             } else {
136                 socketFactory = SSLContexts.createDefault().getSocketFactory();
137             }
138         }
139         final String[] tlsVersionsCopy;
140         if (tlsVersions != null) {
141             tlsVersionsCopy = tlsVersions;
142         } else {
143             tlsVersionsCopy = systemProperties ? HttpsSupport.getSystemProtocols() : null;
144         }
145         final String[] ciphersCopy;
146         if (ciphers != null) {
147             ciphersCopy = ciphers;
148         } else {
149             ciphersCopy = systemProperties ? HttpsSupport.getSystemCipherSuits() : null;
150         }
151         return new SSLConnectionSocketFactory(
152                 socketFactory,
153                 tlsVersionsCopy,
154                 ciphersCopy,
155                 hostnameVerifier != null ? hostnameVerifier : HttpsSupport.getDefaultHostnameVerifier());
156     }
157 
158 }