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.nio.ssl.FixedPortStrategy;
36  import org.apache.hc.core5.http.nio.ssl.SecurePortStrategy;
37  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
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 side-side implementation of {@link TlsStrategy} that upgrades to TLS for endpoints
48   * with the specified local ports.
49   *
50   * @since 5.0
51   */
52  public class H2ServerTlsStrategy implements TlsStrategy {
53  
54      private final SSLContext sslContext;
55      private final SecurePortStrategy securePortStrategy;
56      private final SSLBufferMode sslBufferMode;
57      private final SSLSessionInitializer initializer;
58      private final SSLSessionVerifier verifier;
59  
60      public H2ServerTlsStrategy(
61              final SSLContext sslContext,
62              final SecurePortStrategy securePortStrategy,
63              final SSLBufferMode sslBufferMode,
64              final SSLSessionInitializer initializer,
65              final SSLSessionVerifier verifier) {
66          this.sslContext = Args.notNull(sslContext, "SSL context");
67          this.securePortStrategy = securePortStrategy;
68          this.sslBufferMode = sslBufferMode;
69          this.initializer = initializer;
70          this.verifier = verifier;
71      }
72  
73      public H2ServerTlsStrategy(
74              final SSLContext sslContext,
75              final SecurePortStrategy securePortStrategy,
76              final SSLSessionInitializer initializer,
77              final SSLSessionVerifier verifier) {
78          this(sslContext, securePortStrategy, null, initializer, verifier);
79      }
80  
81      public H2ServerTlsStrategy(
82              final SSLContext sslContext,
83              final SecurePortStrategy securePortStrategy,
84              final SSLSessionVerifier verifier) {
85          this(sslContext, securePortStrategy, null, null, verifier);
86      }
87  
88      public H2ServerTlsStrategy(final SSLContext sslContext, final SecurePortStrategy securePortStrategy) {
89          this(sslContext, securePortStrategy, null, null, null);
90      }
91  
92      public H2ServerTlsStrategy(final int... securePorts) {
93          this(SSLContexts.createSystemDefault(), new FixedPortStrategy(securePorts));
94      }
95  
96      @Override
97      public boolean upgrade(
98              final TransportSecurityLayer tlsSession,
99              final HttpHost host,
100             final SocketAddress localAddress,
101             final SocketAddress remoteAddress,
102             final Object attachment,
103             final Timeout handshakeTimeout) {
104         if (securePortStrategy != null && securePortStrategy.isSecure(localAddress)) {
105             tlsSession.startTls(
106                     sslContext,
107                     host,
108                     sslBufferMode,
109                     H2TlsSupport.enforceRequirements(attachment, initializer),
110                     verifier,
111                     handshakeTimeout);
112             return true;
113         }
114         return false;
115     }
116 
117 }