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 java.net.SocketAddress;
31  
32  import javax.net.ssl.SSLContext;
33  
34  import org.apache.hc.core5.http.HttpHost;
35  import org.apache.hc.core5.http.URIScheme;
36  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
37  import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
38  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
39  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
40  import org.apache.hc.core5.reactor.ssl.TransportSecurityLayer;
41  import org.apache.hc.core5.util.Args;
42  import org.apache.hc.core5.util.Timeout;
43  
44  /**
45   * Basic client-side implementation of {@link TlsStrategy} that upgrades to TLS for all endpoints
46   * with {@code HTTPS} scheme.
47   *
48   * @since 5.0
49   */
50  public class ConscryptClientTlsStrategy implements TlsStrategy {
51  
52      private final SSLContext sslContext;
53      private final SSLBufferMode sslBufferMode;
54      private final SSLSessionInitializer initializer;
55      private final SSLSessionVerifier verifier;
56  
57      public ConscryptClientTlsStrategy(
58              final SSLContext sslContext,
59              final SSLBufferMode sslBufferMode,
60              final SSLSessionInitializer initializer,
61              final SSLSessionVerifier verifier) {
62          this.sslContext = Args.notNull(sslContext, "SSL context");
63          this.sslBufferMode = sslBufferMode;
64          this.initializer = initializer;
65          this.verifier = verifier;
66      }
67  
68      public ConscryptClientTlsStrategy(
69              final SSLContext sslContext,
70              final SSLSessionInitializer initializer,
71              final SSLSessionVerifier verifier) {
72          this(sslContext, null, initializer, verifier);
73      }
74  
75      public ConscryptClientTlsStrategy(
76              final SSLContext sslContext,
77              final SSLSessionVerifier verifier) {
78          this(sslContext, null, null, verifier);
79      }
80  
81      public ConscryptClientTlsStrategy(final SSLContext sslContext) {
82          this(sslContext, null, null, null);
83      }
84  
85      @Override
86      public boolean upgrade(
87              final TransportSecurityLayer tlsSession,
88              final HttpHost host,
89              final SocketAddress localAddress,
90              final SocketAddress remoteAddress,
91              final Object attachment,
92              final Timeout handshakeTimeout) {
93          final String scheme = host != null ? host.getSchemeName() : null;
94          if (URIScheme.HTTPS.same(scheme)) {
95              tlsSession.startTls(
96                      sslContext,
97                      host,
98                      sslBufferMode,
99                      ConscryptSupport.initialize(attachment, initializer),
100                     ConscryptSupport.verify(verifier),
101                     handshakeTimeout);
102             return true;
103         }
104         return false;
105     }
106 
107 }