|
1 /* |
|
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.security.ssl; |
|
27 |
|
28 import javax.net.ssl.*; |
|
29 import java.util.*; |
|
30 import sun.net.util.IPAddressUtil; |
|
31 |
|
32 /** |
|
33 * A utility class to share the static methods. |
|
34 */ |
|
35 final class Utilities { |
|
36 /** |
|
37 * hex digits |
|
38 */ |
|
39 static final char[] hexDigits = "0123456789ABCDEF".toCharArray(); |
|
40 |
|
41 /** |
|
42 * Puts {@code hostname} into the {@code serverNames} list. |
|
43 * <P> |
|
44 * If the {@code serverNames} does not look like a legal FQDN, it will |
|
45 * not be put into the returned list. |
|
46 * <P> |
|
47 * Note that the returned list does not allow duplicated name type. |
|
48 * |
|
49 * @return a list of {@link SNIServerName} |
|
50 */ |
|
51 static List<SNIServerName> addToSNIServerNameList( |
|
52 List<SNIServerName> serverNames, String hostname) { |
|
53 |
|
54 SNIHostName sniHostName = rawToSNIHostName(hostname); |
|
55 if (sniHostName == null) { |
|
56 return serverNames; |
|
57 } |
|
58 |
|
59 int size = serverNames.size(); |
|
60 List<SNIServerName> sniList = (size != 0) ? |
|
61 new ArrayList<SNIServerName>(serverNames) : |
|
62 new ArrayList<SNIServerName>(1); |
|
63 |
|
64 boolean reset = false; |
|
65 for (int i = 0; i < size; i++) { |
|
66 SNIServerName serverName = sniList.get(i); |
|
67 if (serverName.getType() == StandardConstants.SNI_HOST_NAME) { |
|
68 sniList.set(i, sniHostName); |
|
69 if (Debug.isOn("ssl")) { |
|
70 System.out.println(Thread.currentThread().getName() + |
|
71 ", the previous server name in SNI (" + serverName + |
|
72 ") was replaced with (" + sniHostName + ")"); |
|
73 } |
|
74 reset = true; |
|
75 break; |
|
76 } |
|
77 } |
|
78 |
|
79 if (!reset) { |
|
80 sniList.add(sniHostName); |
|
81 } |
|
82 |
|
83 return Collections.<SNIServerName>unmodifiableList(sniList); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Converts string hostname to {@code SNIHostName}. |
|
88 * <P> |
|
89 * Note that to check whether a hostname is a valid domain name, we cannot |
|
90 * use the hostname resolved from name services. For virtual hosting, |
|
91 * multiple hostnames may be bound to the same IP address, so the hostname |
|
92 * resolved from name services is not always reliable. |
|
93 * |
|
94 * @param hostname |
|
95 * the raw hostname |
|
96 * @return an instance of {@link SNIHostName}, or null if the hostname does |
|
97 * not look like a FQDN |
|
98 */ |
|
99 private static SNIHostName rawToSNIHostName(String hostname) { |
|
100 SNIHostName sniHostName = null; |
|
101 if (hostname != null && hostname.indexOf('.') > 0 && |
|
102 !hostname.endsWith(".") && |
|
103 !IPAddressUtil.isIPv4LiteralAddress(hostname) && |
|
104 !IPAddressUtil.isIPv6LiteralAddress(hostname)) { |
|
105 |
|
106 try { |
|
107 sniHostName = new SNIHostName(hostname); |
|
108 } catch (IllegalArgumentException iae) { |
|
109 // don't bother to handle illegal host_name |
|
110 if (Debug.isOn("ssl")) { |
|
111 System.out.println(Thread.currentThread().getName() + |
|
112 ", \"" + hostname + "\" " + |
|
113 "is not a legal HostName for server name indication"); |
|
114 } |
|
115 } |
|
116 } |
|
117 |
|
118 return sniHostName; |
|
119 } |
|
120 } |