1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
79 if (matcher.getType() == 0 && matcher.matches(name)) {
80 return true;
81 }
82 }
83 return false;
84 }
85 return true;
86 }
87 }