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.reactor.ssl.SSLBufferMode;
36  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
37  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
38  import org.apache.hc.core5.reactor.ssl.TransportSecurityLayer;
39  import org.apache.hc.core5.ssl.SSLContexts;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.Timeout;
42  
43  /**
44   * Basic side-side implementation of {@link TlsStrategy} that upgrades to TLS for endpoints
45   * with the specified local ports.
46   *
47   * @since 5.0
48   */
49  public class BasicServerTlsStrategy implements TlsStrategy {
50  
51      private final SSLContext sslContext;
52      @SuppressWarnings("deprecation")
53      private final SecurePortStrategy securePortStrategy;
54      private final SSLBufferMode sslBufferMode;
55      private final SSLSessionInitializer initializer;
56      private final SSLSessionVerifier verifier;
57  
58      /**
59       * @deprecated Use {@link BasicServerTlsStrategy#BasicServerTlsStrategy(SSLContext, SSLBufferMode, SSLSessionInitializer, SSLSessionVerifier)}
60       */
61      @Deprecated
62      public BasicServerTlsStrategy(
63              final SSLContext sslContext,
64              final SecurePortStrategy securePortStrategy,
65              final SSLBufferMode sslBufferMode,
66              final SSLSessionInitializer initializer,
67              final SSLSessionVerifier verifier) {
68          this.sslContext = Args.notNull(sslContext, "SSL context");
69          this.securePortStrategy = securePortStrategy;
70          this.sslBufferMode = sslBufferMode;
71          this.initializer = initializer;
72          this.verifier = verifier;
73      }
74  
75      /**
76       * @deprecated Use {@link BasicServerTlsStrategy#BasicServerTlsStrategy(SSLContext, SSLSessionInitializer, SSLSessionVerifier)}
77       */
78      @Deprecated
79      public BasicServerTlsStrategy(
80              final SSLContext sslContext,
81              final SecurePortStrategy securePortStrategy,
82              final SSLSessionInitializer initializer,
83              final SSLSessionVerifier verifier) {
84          this(sslContext, securePortStrategy, null, initializer, verifier);
85      }
86  
87      /**
88       * @deprecated Use {@link BasicServerTlsStrategy#BasicServerTlsStrategy(SSLContext, SSLSessionVerifier)}
89       */
90      @Deprecated
91      public BasicServerTlsStrategy(
92              final SSLContext sslContext,
93              final SecurePortStrategy securePortStrategy,
94              final SSLSessionVerifier verifier) {
95          this(sslContext, securePortStrategy, null, null, verifier);
96      }
97  
98      /**
99       * @deprecated Use {@link BasicServerTlsStrategy#BasicServerTlsStrategy(SSLContext)}
100      */
101     @Deprecated
102     public BasicServerTlsStrategy(final SSLContext sslContext, final SecurePortStrategy securePortStrategy) {
103         this(sslContext, securePortStrategy, null, null, null);
104     }
105 
106     /**
107      * @deprecated Use {@link BasicServerTlsStrategy#BasicServerTlsStrategy()}
108      */
109     @Deprecated
110     public BasicServerTlsStrategy(final SecurePortStrategy securePortStrategy) {
111         this(SSLContexts.createSystemDefault(), securePortStrategy);
112     }
113 
114     public BasicServerTlsStrategy(
115             final SSLContext sslContext,
116             final SSLBufferMode sslBufferMode,
117             final SSLSessionInitializer initializer,
118             final SSLSessionVerifier verifier) {
119         this.sslContext = Args.notNull(sslContext, "SSL context");
120         this.sslBufferMode = sslBufferMode;
121         this.initializer = initializer;
122         this.verifier = verifier;
123         this.securePortStrategy = null;
124     }
125 
126     public BasicServerTlsStrategy(
127             final SSLContext sslContext,
128             final SSLSessionInitializer initializer,
129             final SSLSessionVerifier verifier) {
130         this(sslContext, (SSLBufferMode) null, initializer, verifier);
131     }
132 
133     public BasicServerTlsStrategy(
134             final SSLContext sslContext,
135             final SSLSessionVerifier verifier) {
136         this(sslContext, (SSLBufferMode) null, null, verifier);
137     }
138 
139     public BasicServerTlsStrategy(final SSLContext sslContext) {
140         this(sslContext, null, null, null, null);
141     }
142 
143     public BasicServerTlsStrategy() {
144         this(SSLContexts.createSystemDefault());
145     }
146 
147     private boolean isApplicable(final SocketAddress localAddress) {
148         return securePortStrategy == null || securePortStrategy.isSecure(localAddress);
149     }
150 
151     @Override
152     public boolean upgrade(
153             final TransportSecurityLayer tlsSession,
154             final HttpHost host,
155             final SocketAddress localAddress,
156             final SocketAddress remoteAddress,
157             final Object attachment,
158             final Timeout handshakeTimeout) {
159         if (isApplicable(localAddress)) {
160             tlsSession.startTls(sslContext, host, sslBufferMode,
161                     TlsSupport.enforceStrongSecurity(initializer), verifier, handshakeTimeout);
162             return true;
163         }
164         return false;
165     }
166 
167 }