1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
88
89
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
120 if (matcher.getType() == 0 && matcher.matches(name)) {
121 return true;
122 }
123 }
124 return false;
125 }
126 return true;
127 }
128 }