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.http.nio.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.reactor.ssl.SSLBufferMode;
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.TransportSecurityLayer;
40  import org.apache.hc.core5.ssl.SSLContexts;
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 BasicClientTlsStrategy 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 BasicClientTlsStrategy(
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 BasicClientTlsStrategy(
69              final SSLContext sslContext,
70              final SSLSessionInitializer initializer,
71              final SSLSessionVerifier verifier) {
72          this(sslContext, null, initializer, verifier);
73      }
74  
75      public BasicClientTlsStrategy(
76              final SSLContext sslContext,
77              final SSLSessionVerifier verifier) {
78          this(sslContext, null, null, verifier);
79      }
80  
81      public BasicClientTlsStrategy(final SSLContext sslContext) {
82          this(sslContext, null, null, null);
83      }
84  
85      public BasicClientTlsStrategy() {
86          this(SSLContexts.createSystemDefault());
87      }
88  
89      @Override
90      public boolean upgrade(
91              final TransportSecurityLayer tlsSession,
92              final HttpHost host,
93              final SocketAddress localAddress,
94              final SocketAddress remoteAddress,
95              final Object attachment,
96              final Timeout handshakeTimeout) {
97          final String scheme = host != null ? host.getSchemeName() : null;
98          if (URIScheme.HTTPS.same(scheme)) {
99              tlsSession.startTls(sslContext, host, sslBufferMode,
100                     TlsSupport.enforceStrongSecurity(initializer), verifier, handshakeTimeout);
101             return true;
102         }
103         return false;
104     }
105 
106 }