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 java.lang.reflect.InvocationTargetException;
31  import java.lang.reflect.Method;
32  
33  import javax.net.ssl.HostnameVerifier;
34  import javax.net.ssl.SSLContext;
35  import javax.net.ssl.SSLEngine;
36  import javax.net.ssl.SSLParameters;
37  
38  import org.apache.hc.core5.annotation.Contract;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
41  import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
42  import org.apache.hc.core5.reactor.ssl.TlsDetails;
43  import org.apache.hc.core5.ssl.SSLContexts;
44  import org.conscrypt.Conscrypt;
45  
46  /**
47   * TLS upgrade strategy for non-blocking client connections using Conscrypt TLS library.
48   *
49   * @since 5.0
50   */
51  @Contract(threading = ThreadingBehavior.STATELESS)
52  public class ConscryptClientTlsStrategy extends AbstractClientTlsStrategy {
53  
54      public static TlsStrategy getDefault() {
55          return new ConscryptClientTlsStrategy(
56                  SSLContexts.createDefault(),
57                  HttpsSupport.getDefaultHostnameVerifier());
58      }
59  
60      public static TlsStrategy getSystemDefault() {
61          return new ConscryptClientTlsStrategy(
62                  SSLContexts.createSystemDefault(),
63                  HttpsSupport.getSystemProtocols(),
64                  HttpsSupport.getSystemCipherSuits(),
65                  SSLBufferMode.STATIC,
66                  HttpsSupport.getDefaultHostnameVerifier());
67      }
68  
69      public ConscryptClientTlsStrategy(
70              final SSLContext sslContext,
71              final String[] supportedProtocols,
72              final String[] supportedCipherSuites,
73              final SSLBufferMode sslBufferManagement,
74              final HostnameVerifier hostnameVerifier) {
75          super(sslContext, supportedProtocols, supportedCipherSuites, sslBufferManagement, hostnameVerifier);
76      }
77  
78      public ConscryptClientTlsStrategy(
79              final SSLContext sslcontext,
80              final HostnameVerifier hostnameVerifier) {
81          this(sslcontext, null, null, SSLBufferMode.STATIC, hostnameVerifier);
82      }
83  
84      public ConscryptClientTlsStrategy(final SSLContext sslcontext) {
85          this(sslcontext, HttpsSupport.getDefaultHostnameVerifier());
86      }
87  
88      @Override
89      void applyParameters(final SSLEngine sslEngine, final SSLParameters sslParameters, final String[] appProtocols) {
90          if (Conscrypt.isConscrypt(sslEngine)) {
91              sslEngine.setSSLParameters(sslParameters);
92              Conscrypt.setApplicationProtocols(sslEngine, appProtocols);
93          } else {
94              sslParameters.setApplicationProtocols(appProtocols);
95              sslEngine.setSSLParameters(sslParameters);
96          }
97      }
98  
99      @Override
100     TlsDetails createTlsDetails(final SSLEngine sslEngine) {
101         if (Conscrypt.isConscrypt(sslEngine)) {
102             return new TlsDetails(sslEngine.getSession(), Conscrypt.getApplicationProtocol(sslEngine));
103         }
104         return null;
105     }
106 
107     public static boolean isSupported() {
108         try {
109             final Class<?> clazz = Class.forName("org.conscrypt.Conscrypt");
110             final Method method = clazz.getMethod("isAvailable");
111             return ((Boolean) method.invoke(null)).booleanValue();
112         } catch (final ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
113             return false;
114         }
115     }
116 
117 }