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.core5.http2.ssl;
29  
30  import javax.net.ssl.SSLEngine;
31  import javax.net.ssl.SSLException;
32  import javax.net.ssl.SSLParameters;
33  
34  import org.apache.hc.core5.http.ssl.TLS;
35  import org.apache.hc.core5.http.ssl.TlsCiphers;
36  import org.apache.hc.core5.net.NamedEndpoint;
37  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
38  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
39  import org.apache.hc.core5.reactor.ssl.TlsDetails;
40  import org.conscrypt.Conscrypt;
41  
42  /**
43   * Conscrypt TLS support methods
44   *
45   * @since 5.0
46   */
47  public final class ConscryptSupport {
48  
49      public static SSLSessionInitializer initialize(
50              final Object attachment,
51              final SSLSessionInitializer initializer) {
52          return new SSLSessionInitializer() {
53  
54              @Override
55              public void initialize(final NamedEndpoint endpoint, final SSLEngine sslEngine) {
56                  final SSLParameters sslParameters = sslEngine.getSSLParameters();
57                  sslParameters.setProtocols(TLS.excludeWeak(sslParameters.getProtocols()));
58                  sslParameters.setCipherSuites(TlsCiphers.excludeH2Blacklisted(sslParameters.getCipherSuites()));
59                  H2TlsSupport.setEnableRetransmissions(sslParameters, false);
60                  final String[] appProtocols = H2TlsSupport.selectApplicationProtocols(attachment);
61                  if (Conscrypt.isConscrypt(sslEngine)) {
62                      sslEngine.setSSLParameters(sslParameters);
63                      Conscrypt.setApplicationProtocols(sslEngine, appProtocols);
64                  } else {
65                      H2TlsSupport.setApplicationProtocols(sslParameters, appProtocols);
66                      sslEngine.setSSLParameters(sslParameters);
67                  }
68                  if (initializer != null) {
69                      initializer.initialize(endpoint, sslEngine);
70                  }
71              }
72  
73          };
74      }
75  
76      public static SSLSessionVerifierLSessionVerifier.html#SSLSessionVerifier">SSLSessionVerifier verify(final SSLSessionVerifier verifier) {
77          return new SSLSessionVerifier() {
78  
79              @Override
80              public TlsDetails verify(final NamedEndpoint endpoint, final SSLEngine sslEngine) throws SSLException {
81                  TlsDetails tlsDetails = verifier != null ? verifier.verify(endpoint, sslEngine) : null;
82                  if (tlsDetails == null && Conscrypt.isConscrypt(sslEngine)) {
83                      tlsDetails = new TlsDetails(sslEngine.getSession(), Conscrypt.getApplicationProtocol(sslEngine));
84                  }
85                  return tlsDetails;
86              }
87  
88          };
89      }
90  
91  }