View Javadoc

1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import javax.net.ssl.SNIHostName;
19  import javax.net.ssl.SNIMatcher;
20  import javax.net.ssl.SNIServerName;
21  import javax.net.ssl.SSLParameters;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  final class Java8SslUtils {
29  
30      private Java8SslUtils() { }
31  
32      static List<String> getSniHostNames(SSLParameters sslParameters) {
33          List<SNIServerName> names = sslParameters.getServerNames();
34          if (names == null || names.isEmpty()) {
35              return Collections.emptyList();
36          }
37          List<String> strings = new ArrayList<String>(names.size());
38  
39          for (SNIServerName serverName : names) {
40              if (serverName instanceof SNIHostName) {
41                  strings.add(((SNIHostName) serverName).getAsciiName());
42              } else {
43                  throw new IllegalArgumentException("Only " + SNIHostName.class.getName()
44                          + " instances are supported, but found: " + serverName);
45              }
46          }
47          return strings;
48      }
49  
50      static void setSniHostNames(SSLParameters sslParameters, List<String> names) {
51          List<SNIServerName> sniServerNames = new ArrayList<SNIServerName>(names.size());
52          for (String name: names) {
53              sniServerNames.add(new SNIHostName(name));
54          }
55          sslParameters.setServerNames(sniServerNames);
56      }
57  
58      static boolean getUseCipherSuitesOrder(SSLParameters sslParameters) {
59          return sslParameters.getUseCipherSuitesOrder();
60      }
61  
62      static void setUseCipherSuitesOrder(SSLParameters sslParameters, boolean useOrder) {
63          sslParameters.setUseCipherSuitesOrder(useOrder);
64      }
65  
66      @SuppressWarnings("unchecked")
67      static void setSNIMatchers(SSLParameters sslParameters, Collection<?> matchers) {
68          sslParameters.setSNIMatchers((Collection<SNIMatcher>) matchers);
69      }
70  
71      @SuppressWarnings("unchecked")
72      static boolean checkSniHostnameMatch(Collection<?> matchers, String hostname) {
73          if (matchers != null && !matchers.isEmpty()) {
74              SNIHostName name = new SNIHostName(hostname);
75              Iterator<SNIMatcher> matcherIt = (Iterator<SNIMatcher>) matchers.iterator();
76              while (matcherIt.hasNext()) {
77                  SNIMatcher matcher = matcherIt.next();
78                  // type 0 is for hostname
79                  if (matcher.getType() == 0 && matcher.matches(name)) {
80                      return true;
81                  }
82              }
83              return false;
84          }
85          return true;
86      }
87  }