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.concurrent.FutureCallback;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.http.URIScheme;
37  import org.apache.hc.core5.net.NamedEndpoint;
38  import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
39  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
40  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
41  import org.apache.hc.core5.reactor.ssl.TransportSecurityLayer;
42  import org.apache.hc.core5.ssl.SSLContexts;
43  import org.apache.hc.core5.util.Args;
44  import org.apache.hc.core5.util.Timeout;
45  
46  /**
47   * Basic client-side implementation of {@link TlsStrategy} that upgrades to TLS for all endpoints
48   * with {@code HTTPS} scheme.
49   *
50   * @since 5.0
51   */
52  public class BasicClientTlsStrategy implements TlsStrategy {
53  
54      private final SSLContext sslContext;
55      private final SSLBufferMode sslBufferMode;
56      private final SSLSessionInitializer initializer;
57      private final SSLSessionVerifier verifier;
58  
59      public BasicClientTlsStrategy(
60              final SSLContext sslContext,
61              final SSLBufferMode sslBufferMode,
62              final SSLSessionInitializer initializer,
63              final SSLSessionVerifier verifier) {
64          this.sslContext = Args.notNull(sslContext, "SSL context");
65          this.sslBufferMode = sslBufferMode;
66          this.initializer = initializer;
67          this.verifier = verifier;
68      }
69  
70      public BasicClientTlsStrategy(
71              final SSLContext sslContext,
72              final SSLSessionInitializer initializer,
73              final SSLSessionVerifier verifier) {
74          this(sslContext, null, initializer, verifier);
75      }
76  
77      public BasicClientTlsStrategy(
78              final SSLContext sslContext,
79              final SSLSessionVerifier verifier) {
80          this(sslContext, null, null, verifier);
81      }
82  
83      public BasicClientTlsStrategy(final SSLContext sslContext) {
84          this(sslContext, null, null, null);
85      }
86  
87      public BasicClientTlsStrategy() {
88          this(SSLContexts.createSystemDefault());
89      }
90  
91      /**
92       * Constructor with the default SSL context based on system properties and custom {@link  SSLSessionVerifier} verifier.
93       * @param verifier the custom {@link SSLSessionVerifier}.
94       * @see SSLContext
95       * @since 5.2
96       */
97      public BasicClientTlsStrategy(final SSLSessionVerifier verifier) {
98          this(SSLContexts.createSystemDefault(), verifier);
99      }
100 
101     @Override
102     public void upgrade(
103             final TransportSecurityLayer tlsSession,
104             final NamedEndpoint endpoint,
105             final Object attachment,
106             final Timeout handshakeTimeout,
107             final FutureCallback<TransportSecurityLayer> callback) {
108         tlsSession.startTls(sslContext, endpoint, sslBufferMode,
109                 TlsSupport.enforceStrongSecurity(initializer), verifier, handshakeTimeout, callback);
110     }
111 
112     /**
113      * @deprecated use {@link #upgrade(TransportSecurityLayer, NamedEndpoint, Object, Timeout, FutureCallback)}
114      */
115     @Deprecated
116     @Override
117     public boolean upgrade(
118             final TransportSecurityLayer tlsSession,
119             final HttpHost host,
120             final SocketAddress localAddress,
121             final SocketAddress remoteAddress,
122             final Object attachment,
123             final Timeout handshakeTimeout) {
124         final String scheme = host != null ? host.getSchemeName() : null;
125         if (URIScheme.HTTPS.same(scheme)) {
126             upgrade(tlsSession, host, attachment, handshakeTimeout, null);
127             return true;
128         }
129         return false;
130     }
131 
132 }