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  import javax.net.ssl.SSLEngine;
33  import javax.net.ssl.SSLParameters;
34  
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.function.Factory;
38  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
39  import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
40  import org.apache.hc.core5.reactor.ssl.TlsDetails;
41  import org.apache.hc.core5.ssl.SSLContexts;
42  
43  /**
44   * TLS upgrade strategy for non-blocking client connections.
45   *
46   * @since 5.0
47   */
48  @Contract(threading = ThreadingBehavior.STATELESS)
49  public class DefaultClientTlsStrategy extends AbstractClientTlsStrategy {
50  
51      public static TlsStrategy getDefault() {
52          return new DefaultClientTlsStrategy(
53                  SSLContexts.createDefault(),
54                  HttpsSupport.getDefaultHostnameVerifier());
55      }
56  
57      public static TlsStrategy getSystemDefault() {
58          return new DefaultClientTlsStrategy(
59                  SSLContexts.createSystemDefault(),
60                  HttpsSupport.getSystemProtocols(),
61                  HttpsSupport.getSystemCipherSuits(),
62                  SSLBufferMode.STATIC,
63                  HttpsSupport.getDefaultHostnameVerifier());
64      }
65  
66      /**
67       * @deprecated To be removed.
68       */
69      @Deprecated
70      private Factory<SSLEngine, TlsDetails> tlsDetailsFactory;
71  
72      /**
73       * @deprecated Use {@link DefaultClientTlsStrategy#DefaultClientTlsStrategy(SSLContext, String[], String[], SSLBufferMode, HostnameVerifier)}
74       */
75      @Deprecated
76      public DefaultClientTlsStrategy(
77              final SSLContext sslContext,
78              final String[] supportedProtocols,
79              final String[] supportedCipherSuites,
80              final SSLBufferMode sslBufferManagement,
81              final HostnameVerifier hostnameVerifier,
82              final Factory<SSLEngine, TlsDetails> tlsDetailsFactory) {
83          super(sslContext, supportedProtocols, supportedCipherSuites, sslBufferManagement, hostnameVerifier);
84          this.tlsDetailsFactory = tlsDetailsFactory;
85      }
86  
87      public DefaultClientTlsStrategy(
88              final SSLContext sslContext,
89              final String[] supportedProtocols,
90              final String[] supportedCipherSuites,
91              final SSLBufferMode sslBufferManagement,
92              final HostnameVerifier hostnameVerifier) {
93          super(sslContext, supportedProtocols, supportedCipherSuites, sslBufferManagement, hostnameVerifier);
94      }
95  
96      public DefaultClientTlsStrategy(
97              final SSLContext sslcontext,
98              final HostnameVerifier hostnameVerifier) {
99          this(sslcontext, null, null, SSLBufferMode.STATIC, hostnameVerifier);
100     }
101 
102     public DefaultClientTlsStrategy(final SSLContext sslcontext) {
103         this(sslcontext, HttpsSupport.getDefaultHostnameVerifier());
104     }
105 
106     @Override
107     void applyParameters(final SSLEngine sslEngine, final SSLParameters sslParameters, final String[] appProtocols) {
108         sslParameters.setApplicationProtocols(appProtocols);
109         sslEngine.setSSLParameters(sslParameters);
110     }
111 
112     @Override
113     @SuppressWarnings("deprecated")
114     TlsDetails createTlsDetails(final SSLEngine sslEngine) {
115         return tlsDetailsFactory != null ? tlsDetailsFactory.create(sslEngine) : null;
116     }
117 
118 }