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    *   https://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 io.netty.util.internal.SuppressJava6Requirement;
19  import io.netty.util.CharsetUtil;
20  
21  import javax.net.ssl.SNIHostName;
22  import javax.net.ssl.SNIMatcher;
23  import javax.net.ssl.SNIServerName;
24  import javax.net.ssl.SSLParameters;
25  import javax.net.ssl.StandardConstants;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  @SuppressJava6Requirement(reason = "Usage guarded by java version check")
33  final class Java8SslUtils {
34  
35      private Java8SslUtils() { }
36  
37      static List<String> getSniHostNames(SSLParameters sslParameters) {
38          List<SNIServerName> names = sslParameters.getServerNames();
39          if (names == null || names.isEmpty()) {
40              return Collections.emptyList();
41          }
42          List<String> strings = new ArrayList<String>(names.size());
43  
44          for (SNIServerName serverName : names) {
45              if (serverName instanceof SNIHostName) {
46                  strings.add(((SNIHostName) serverName).getAsciiName());
47              } else {
48                  throw new IllegalArgumentException("Only " + SNIHostName.class.getName()
49                          + " instances are supported, but found: " + serverName);
50              }
51          }
52          return strings;
53      }
54  
55      static void setSniHostNames(SSLParameters sslParameters, List<String> names) {
56          sslParameters.setServerNames(getSniHostNames(names));
57      }
58  
59      static boolean isValidHostNameForSNI(String hostname) {
60          try {
61              new SNIHostName(hostname);
62              return true;
63          } catch (IllegalArgumentException illegal) {
64              return false;
65          }
66      }
67  
68      static List getSniHostNames(List<String> names) {
69          if (names == null || names.isEmpty()) {
70              return Collections.emptyList();
71          }
72          List<SNIServerName> sniServerNames = new ArrayList<SNIServerName>(names.size());
73          for (String name: names) {
74              sniServerNames.add(new SNIHostName(name.getBytes(CharsetUtil.UTF_8)));
75          }
76          return sniServerNames;
77      }
78  
79      static List getSniHostName(byte[] hostname) {
80          if (hostname == null || hostname.length == 0) {
81              return Collections.emptyList();
82          }
83          return Collections.singletonList(toServerName(hostname));
84      }
85  
86      /**
87       * Converts the given hostname to a {@link SNIServerName}. We convert to bytes as we do not want to do any strict
88       * validation of the {@link SNIHostName} while creating it. If the byte constructor still rejects the name we
89       * expose it as a plain {@link SNIServerName} of type host_name instead of failing.
90       */
91      private static SNIServerName toServerName(byte[] encoded) {
92          try {
93              return new SNIHostName(encoded);
94          } catch (IllegalArgumentException ignored) {
95              return new SNIServerName(StandardConstants.SNI_HOST_NAME, encoded) { };
96          }
97      }
98  
99      static boolean getUseCipherSuitesOrder(SSLParameters sslParameters) {
100         return sslParameters.getUseCipherSuitesOrder();
101     }
102 
103     static void setUseCipherSuitesOrder(SSLParameters sslParameters, boolean useOrder) {
104         sslParameters.setUseCipherSuitesOrder(useOrder);
105     }
106 
107     @SuppressWarnings("unchecked")
108     static void setSNIMatchers(SSLParameters sslParameters, Collection<?> matchers) {
109         sslParameters.setSNIMatchers((Collection<SNIMatcher>) matchers);
110     }
111 
112     @SuppressWarnings("unchecked")
113     static boolean checkSniHostnameMatch(Collection<?> matchers, byte[] hostname) {
114         if (matchers != null && !matchers.isEmpty()) {
115             SNIServerName name = toServerName(hostname);
116             Iterator<SNIMatcher> matcherIt = (Iterator<SNIMatcher>) matchers.iterator();
117             while (matcherIt.hasNext()) {
118                 SNIMatcher matcher = matcherIt.next();
119                 // type 0 is for hostname
120                 if (matcher.getType() == 0 && matcher.matches(name)) {
121                     return true;
122                 }
123             }
124             return false;
125         }
126         return true;
127     }
128 }