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