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