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          sslParameters.setServerNames(getSniHostNames(names));
52      }
53  
54      static List getSniHostNames(List<String> names) {
55          if (names == null || names.isEmpty()) {
56              return Collections.emptyList();
57          }
58          List<SNIServerName> sniServerNames = new ArrayList<SNIServerName>(names.size());
59          for (String name: names) {
60              sniServerNames.add(new SNIHostName(name));
61          }
62          return sniServerNames;
63      }
64  
65      static List getSniHostName(byte[] hostname) {
66          if (hostname == null || hostname.length == 0) {
67              return Collections.emptyList();
68          }
69          return Collections.singletonList(new SNIHostName(hostname));
70      }
71  
72      static boolean getUseCipherSuitesOrder(SSLParameters sslParameters) {
73          return sslParameters.getUseCipherSuitesOrder();
74      }
75  
76      static void setUseCipherSuitesOrder(SSLParameters sslParameters, boolean useOrder) {
77          sslParameters.setUseCipherSuitesOrder(useOrder);
78      }
79  
80      @SuppressWarnings("unchecked")
81      static void setSNIMatchers(SSLParameters sslParameters, Collection<?> matchers) {
82          sslParameters.setSNIMatchers((Collection<SNIMatcher>) matchers);
83      }
84  
85      @SuppressWarnings("unchecked")
86      static boolean checkSniHostnameMatch(Collection<?> matchers, byte[] hostname) {
87          if (matchers != null && !matchers.isEmpty()) {
88              SNIHostName name = new SNIHostName(hostname);
89              Iterator<SNIMatcher> matcherIt = (Iterator<SNIMatcher>) matchers.iterator();
90              while (matcherIt.hasNext()) {
91                  SNIMatcher matcher = matcherIt.next();
92                  // type 0 is for hostname
93                  if (matcher.getType() == 0 && matcher.matches(name)) {
94                      return true;
95                  }
96              }
97              return false;
98          }
99          return true;
100     }
101 }