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.SSLBufferManagement;
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  
42  /**
43   * Basic side-side implementation of {@link TlsStrategy} that upgrades to TLS for endpoints
44   * with the specified local ports.
45   *
46   * @since 5.0
47   */
48  public class BasicServerTlsStrategy implements TlsStrategy {
49  
50      private final SSLContext sslContext;
51      private final SecurePortStrategy securePortStrategy;
52      private final SSLBufferManagement sslBufferManagement;
53      private final SSLSessionInitializer initializer;
54      private final SSLSessionVerifier verifier;
55  
56      public BasicServerTlsStrategy(
57              final SSLContext sslContext,
58              final SecurePortStrategy securePortStrategy,
59              final SSLBufferManagement sslBufferManagement,
60              final SSLSessionInitializer initializer,
61              final SSLSessionVerifier verifier) {
62          this.sslContext = Args.notNull(sslContext, "SSL context");
63          this.securePortStrategy = securePortStrategy;
64          this.sslBufferManagement = sslBufferManagement;
65          this.initializer = initializer;
66          this.verifier = verifier;
67      }
68  
69      public BasicServerTlsStrategy(
70              final SSLContext sslContext,
71              final SecurePortStrategy securePortStrategy,
72              final SSLSessionInitializer initializer,
73              final SSLSessionVerifier verifier) {
74          this(sslContext, securePortStrategy, null, initializer, verifier);
75      }
76  
77      public BasicServerTlsStrategy(
78              final SSLContext sslContext,
79              final SecurePortStrategy securePortStrategy,
80              final SSLSessionVerifier verifier) {
81          this(sslContext, securePortStrategy, null, null, verifier);
82      }
83  
84      public BasicServerTlsStrategy(final SSLContext sslContext, final SecurePortStrategy securePortStrategy) {
85          this(sslContext, securePortStrategy, null, null, null);
86      }
87  
88      public BasicServerTlsStrategy(final SecurePortStrategy securePortStrategy) {
89          this(SSLContexts.createSystemDefault(), securePortStrategy);
90      }
91  
92      @Override
93      public boolean upgrade(
94              final TransportSecurityLayer tlsSession,
95              final HttpHost host,
96              final SocketAddress localAddress,
97              final SocketAddress remoteAddress,
98              final Object attachment) {
99          if (securePortStrategy != null && securePortStrategy.isSecure(localAddress)) {
100             tlsSession.startTls(sslContext, sslBufferManagement, initializer, verifier);
101             return true;
102         }
103         return false;
104     }
105 
106 }