author | weijun |
Tue, 26 Jun 2018 18:55:48 +0800 | |
changeset 50788 | 6274aee1f692 |
parent 50768 | 68fa3d4026ea |
child 52428 | 0e8084c8cbb7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45981 | 2 |
* Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.util; |
|
27 |
||
28 |
import java.io.IOException; |
|
50788 | 29 |
import java.net.IDN; |
28865
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
30 |
import java.net.InetAddress; |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
31 |
import java.net.UnknownHostException; |
2 | 32 |
import java.security.Principal; |
33 |
import java.security.cert.*; |
|
44760 | 34 |
import java.util.*; |
2 | 35 |
import javax.security.auth.x500.X500Principal; |
45981 | 36 |
import javax.net.ssl.SNIHostName; |
2 | 37 |
|
44760 | 38 |
import sun.net.util.IPAddressUtil; |
2 | 39 |
import sun.security.x509.X500Name; |
50768 | 40 |
import sun.security.ssl.SSLLogger; |
2 | 41 |
|
42 |
/** |
|
43 |
* Class to check hostnames against the names specified in a certificate as |
|
44 |
* required for TLS and LDAP. |
|
45 |
* |
|
46 |
*/ |
|
47 |
public class HostnameChecker { |
|
48 |
||
49 |
// Constant for a HostnameChecker for TLS |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
50 |
public static final byte TYPE_TLS = 1; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
51 |
private static final HostnameChecker INSTANCE_TLS = |
2 | 52 |
new HostnameChecker(TYPE_TLS); |
53 |
||
54 |
// Constant for a HostnameChecker for LDAP |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
55 |
public static final byte TYPE_LDAP = 2; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
56 |
private static final HostnameChecker INSTANCE_LDAP = |
2 | 57 |
new HostnameChecker(TYPE_LDAP); |
58 |
||
59 |
// constants for subject alt names of type DNS and IP |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
60 |
private static final int ALTNAME_DNS = 2; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
61 |
private static final int ALTNAME_IP = 7; |
2 | 62 |
|
63 |
// the algorithm to follow to perform the check. Currently unused. |
|
64 |
private final byte checkType; |
|
65 |
||
66 |
private HostnameChecker(byte checkType) { |
|
67 |
this.checkType = checkType; |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Get a HostnameChecker instance. checkType should be one of the |
|
72 |
* TYPE_* constants defined in this class. |
|
73 |
*/ |
|
74 |
public static HostnameChecker getInstance(byte checkType) { |
|
75 |
if (checkType == TYPE_TLS) { |
|
76 |
return INSTANCE_TLS; |
|
77 |
} else if (checkType == TYPE_LDAP) { |
|
78 |
return INSTANCE_LDAP; |
|
79 |
} |
|
80 |
throw new IllegalArgumentException("Unknown check type: " + checkType); |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Perform the check. |
|
85 |
* |
|
44760 | 86 |
* @param expectedName the expected host name or ip address |
87 |
* @param cert the certificate to check against |
|
88 |
* @param chainsToPublicCA true if the certificate chains to a public |
|
89 |
* root CA (as pre-installed in the cacerts file) |
|
90 |
* @throws CertificateException if the name does not match any of |
|
91 |
* the names specified in the certificate |
|
2 | 92 |
*/ |
44760 | 93 |
public void match(String expectedName, X509Certificate cert, |
94 |
boolean chainsToPublicCA) throws CertificateException { |
|
2 | 95 |
if (isIpAddress(expectedName)) { |
96 |
matchIP(expectedName, cert); |
|
97 |
} else { |
|
44760 | 98 |
matchDNS(expectedName, cert, chainsToPublicCA); |
2 | 99 |
} |
100 |
} |
|
101 |
||
44760 | 102 |
public void match(String expectedName, X509Certificate cert) |
103 |
throws CertificateException { |
|
104 |
match(expectedName, cert, false); |
|
105 |
} |
|
106 |
||
2 | 107 |
/** |
108 |
* Test whether the given hostname looks like a literal IPv4 or IPv6 |
|
109 |
* address. The hostname does not need to be a fully qualified name. |
|
110 |
* |
|
111 |
* This is not a strict check that performs full input validation. |
|
112 |
* That means if the method returns true, name need not be a correct |
|
113 |
* IP address, rather that it does not represent a valid DNS hostname. |
|
114 |
* Likewise for IP addresses when it returns false. |
|
115 |
*/ |
|
116 |
private static boolean isIpAddress(String name) { |
|
117 |
if (IPAddressUtil.isIPv4LiteralAddress(name) || |
|
118 |
IPAddressUtil.isIPv6LiteralAddress(name)) { |
|
119 |
return true; |
|
120 |
} else { |
|
121 |
return false; |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Check if the certificate allows use of the given IP address. |
|
127 |
* |
|
128 |
* From RFC2818: |
|
129 |
* In some cases, the URI is specified as an IP address rather than a |
|
130 |
* hostname. In this case, the iPAddress subjectAltName must be present |
|
131 |
* in the certificate and must exactly match the IP in the URI. |
|
132 |
*/ |
|
133 |
private static void matchIP(String expectedIP, X509Certificate cert) |
|
134 |
throws CertificateException { |
|
135 |
Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames(); |
|
136 |
if (subjAltNames == null) { |
|
137 |
throw new CertificateException |
|
138 |
("No subject alternative names present"); |
|
139 |
} |
|
140 |
for (List<?> next : subjAltNames) { |
|
141 |
// For IP address, it needs to be exact match |
|
142 |
if (((Integer)next.get(0)).intValue() == ALTNAME_IP) { |
|
143 |
String ipAddress = (String)next.get(1); |
|
144 |
if (expectedIP.equalsIgnoreCase(ipAddress)) { |
|
145 |
return; |
|
28865
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
146 |
} else { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
147 |
// compare InetAddress objects in order to ensure |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
148 |
// equality between a long IPv6 address and its |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
149 |
// abbreviated form. |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
150 |
try { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
151 |
if (InetAddress.getByName(expectedIP).equals( |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
152 |
InetAddress.getByName(ipAddress))) { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
153 |
return; |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
154 |
} |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
155 |
} catch (UnknownHostException e) { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
156 |
} catch (SecurityException e) {} |
2 | 157 |
} |
158 |
} |
|
159 |
} |
|
160 |
throw new CertificateException("No subject alternative " + |
|
161 |
"names matching " + "IP address " + |
|
162 |
expectedIP + " found"); |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Check if the certificate allows use of the given DNS name. |
|
167 |
* |
|
168 |
* From RFC2818: |
|
169 |
* If a subjectAltName extension of type dNSName is present, that MUST |
|
170 |
* be used as the identity. Otherwise, the (most specific) Common Name |
|
171 |
* field in the Subject field of the certificate MUST be used. Although |
|
172 |
* the use of the Common Name is existing practice, it is deprecated and |
|
173 |
* Certification Authorities are encouraged to use the dNSName instead. |
|
174 |
* |
|
175 |
* Matching is performed using the matching rules specified by |
|
44760 | 176 |
* [RFC5280]. If more than one identity of a given type is present in |
2 | 177 |
* the certificate (e.g., more than one dNSName name, a match in any one |
178 |
* of the set is considered acceptable.) |
|
179 |
*/ |
|
44760 | 180 |
private void matchDNS(String expectedName, X509Certificate cert, |
181 |
boolean chainsToPublicCA) |
|
2 | 182 |
throws CertificateException { |
45981 | 183 |
// Check that the expected name is a valid domain name. |
184 |
try { |
|
185 |
// Using the checking implemented in SNIHostName |
|
186 |
SNIHostName sni = new SNIHostName(expectedName); |
|
187 |
} catch (IllegalArgumentException iae) { |
|
188 |
throw new CertificateException( |
|
189 |
"Illegal given domain name: " + expectedName, iae); |
|
190 |
} |
|
191 |
||
2 | 192 |
Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames(); |
193 |
if (subjAltNames != null) { |
|
194 |
boolean foundDNS = false; |
|
44760 | 195 |
for (List<?> next : subjAltNames) { |
2 | 196 |
if (((Integer)next.get(0)).intValue() == ALTNAME_DNS) { |
197 |
foundDNS = true; |
|
198 |
String dnsName = (String)next.get(1); |
|
44760 | 199 |
if (isMatched(expectedName, dnsName, chainsToPublicCA)) { |
2 | 200 |
return; |
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
if (foundDNS) { |
|
205 |
// if certificate contains any subject alt names of type DNS |
|
206 |
// but none match, reject |
|
207 |
throw new CertificateException("No subject alternative DNS " |
|
208 |
+ "name matching " + expectedName + " found."); |
|
209 |
} |
|
210 |
} |
|
211 |
X500Name subjectName = getSubjectX500Name(cert); |
|
212 |
DerValue derValue = subjectName.findMostSpecificAttribute |
|
213 |
(X500Name.commonName_oid); |
|
214 |
if (derValue != null) { |
|
215 |
try { |
|
44760 | 216 |
if (isMatched(expectedName, derValue.getAsString(), |
217 |
chainsToPublicCA)) { |
|
2 | 218 |
return; |
219 |
} |
|
220 |
} catch (IOException e) { |
|
221 |
// ignore |
|
222 |
} |
|
223 |
} |
|
224 |
String msg = "No name matching " + expectedName + " found"; |
|
225 |
throw new CertificateException(msg); |
|
226 |
} |
|
227 |
||
228 |
||
229 |
/** |
|
230 |
* Return the subject of a certificate as X500Name, by reparsing if |
|
231 |
* necessary. X500Name should only be used if access to name components |
|
21278 | 232 |
* is required, in other cases X500Principal is to be preferred. |
2 | 233 |
* |
234 |
* This method is currently used from within JSSE, do not remove. |
|
235 |
*/ |
|
236 |
public static X500Name getSubjectX500Name(X509Certificate cert) |
|
237 |
throws CertificateParsingException { |
|
238 |
try { |
|
239 |
Principal subjectDN = cert.getSubjectDN(); |
|
240 |
if (subjectDN instanceof X500Name) { |
|
241 |
return (X500Name)subjectDN; |
|
242 |
} else { |
|
243 |
X500Principal subjectX500 = cert.getSubjectX500Principal(); |
|
244 |
return new X500Name(subjectX500.getEncoded()); |
|
245 |
} |
|
246 |
} catch (IOException e) { |
|
247 |
throw(CertificateParsingException) |
|
248 |
new CertificateParsingException().initCause(e); |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
||
253 |
/** |
|
254 |
* Returns true if name matches against template.<p> |
|
255 |
* |
|
256 |
* The matching is performed as per RFC 2818 rules for TLS and |
|
257 |
* RFC 2830 rules for LDAP.<p> |
|
258 |
* |
|
259 |
* The <code>name</code> parameter should represent a DNS name. |
|
260 |
* The <code>template</code> parameter |
|
261 |
* may contain the wildcard character * |
|
262 |
*/ |
|
44760 | 263 |
private boolean isMatched(String name, String template, |
264 |
boolean chainsToPublicCA) { |
|
50788 | 265 |
|
266 |
// Normalize to Unicode, because PSL is in Unicode. |
|
267 |
name = IDN.toUnicode(IDN.toASCII(name)); |
|
268 |
template = IDN.toUnicode(IDN.toASCII(template)); |
|
269 |
||
44760 | 270 |
if (hasIllegalWildcard(name, template, chainsToPublicCA)) { |
271 |
return false; |
|
272 |
} |
|
45981 | 273 |
|
274 |
// check the validity of the domain name template. |
|
275 |
try { |
|
276 |
// Replacing wildcard character '*' with 'x' so as to check |
|
277 |
// the domain name template validity. |
|
278 |
// |
|
279 |
// Using the checking implemented in SNIHostName |
|
50788 | 280 |
new SNIHostName(template.replace('*', 'x')); |
45981 | 281 |
} catch (IllegalArgumentException iae) { |
282 |
// It would be nice to add debug log if not matching. |
|
283 |
return false; |
|
284 |
} |
|
285 |
||
2 | 286 |
if (checkType == TYPE_TLS) { |
287 |
return matchAllWildcards(name, template); |
|
288 |
} else if (checkType == TYPE_LDAP) { |
|
289 |
return matchLeftmostWildcard(name, template); |
|
290 |
} else { |
|
291 |
return false; |
|
292 |
} |
|
293 |
} |
|
294 |
||
44760 | 295 |
/** |
296 |
* Returns true if the template contains an illegal wildcard character. |
|
297 |
*/ |
|
298 |
private static boolean hasIllegalWildcard(String domain, String template, |
|
299 |
boolean chainsToPublicCA) { |
|
300 |
// not ok if it is a single wildcard character or "*." |
|
301 |
if (template.equals("*") || template.equals("*.")) { |
|
50768 | 302 |
if (SSLLogger.isOn) { |
303 |
SSLLogger.fine( |
|
304 |
"Certificate domain name has illegal single " + |
|
305 |
"wildcard character: " + template); |
|
44760 | 306 |
} |
307 |
return true; |
|
308 |
} |
|
309 |
||
310 |
int lastWildcardIndex = template.lastIndexOf("*"); |
|
311 |
||
312 |
// ok if it has no wildcard character |
|
313 |
if (lastWildcardIndex == -1) { |
|
314 |
return false; |
|
315 |
} |
|
316 |
||
317 |
String afterWildcard = template.substring(lastWildcardIndex); |
|
318 |
int firstDotIndex = afterWildcard.indexOf("."); |
|
319 |
||
320 |
// not ok if there is no dot after wildcard (ex: "*com") |
|
321 |
if (firstDotIndex == -1) { |
|
50768 | 322 |
if (SSLLogger.isOn) { |
323 |
SSLLogger.fine( |
|
324 |
"Certificate domain name has illegal wildcard, " + |
|
325 |
"no dot after wildcard character: " + template); |
|
44760 | 326 |
} |
327 |
return true; |
|
328 |
} |
|
329 |
||
330 |
// If the wildcarded domain is a top-level domain under which names |
|
331 |
// can be registered, then a wildcard is not allowed. |
|
332 |
||
333 |
if (!chainsToPublicCA) { |
|
334 |
return false; // skip check for non-public certificates |
|
335 |
} |
|
336 |
Optional<RegisteredDomain> rd = RegisteredDomain.from(domain) |
|
337 |
.filter(d -> d.type() == RegisteredDomain.Type.ICANN); |
|
338 |
||
339 |
if (rd.isPresent()) { |
|
340 |
String wDomain = afterWildcard.substring(firstDotIndex + 1); |
|
341 |
if (rd.get().publicSuffix().equalsIgnoreCase(wDomain)) { |
|
50768 | 342 |
if (SSLLogger.isOn) { |
343 |
SSLLogger.fine( |
|
344 |
"Certificate domain name has illegal " + |
|
345 |
"wildcard for public suffix: " + template); |
|
44760 | 346 |
} |
347 |
return true; |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
return false; |
|
352 |
} |
|
2 | 353 |
|
354 |
/** |
|
355 |
* Returns true if name matches against template.<p> |
|
356 |
* |
|
357 |
* According to RFC 2818, section 3.1 - |
|
358 |
* Names may contain the wildcard character * which is |
|
359 |
* considered to match any single domain name component |
|
360 |
* or component fragment. |
|
361 |
* E.g., *.a.com matches foo.a.com but not |
|
362 |
* bar.foo.a.com. f*.com matches foo.com but not bar.com. |
|
363 |
*/ |
|
364 |
private static boolean matchAllWildcards(String name, |
|
365 |
String template) { |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
366 |
name = name.toLowerCase(Locale.ENGLISH); |
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
367 |
template = template.toLowerCase(Locale.ENGLISH); |
2 | 368 |
StringTokenizer nameSt = new StringTokenizer(name, "."); |
369 |
StringTokenizer templateSt = new StringTokenizer(template, "."); |
|
370 |
||
371 |
if (nameSt.countTokens() != templateSt.countTokens()) { |
|
372 |
return false; |
|
373 |
} |
|
374 |
||
375 |
while (nameSt.hasMoreTokens()) { |
|
376 |
if (!matchWildCards(nameSt.nextToken(), |
|
377 |
templateSt.nextToken())) { |
|
378 |
return false; |
|
379 |
} |
|
380 |
} |
|
381 |
return true; |
|
382 |
} |
|
383 |
||
384 |
||
385 |
/** |
|
386 |
* Returns true if name matches against template.<p> |
|
387 |
* |
|
388 |
* As per RFC 2830, section 3.6 - |
|
389 |
* The "*" wildcard character is allowed. If present, it applies only |
|
390 |
* to the left-most name component. |
|
391 |
* E.g. *.bar.com would match a.bar.com, b.bar.com, etc. but not |
|
392 |
* bar.com. |
|
393 |
*/ |
|
394 |
private static boolean matchLeftmostWildcard(String name, |
|
395 |
String template) { |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
396 |
name = name.toLowerCase(Locale.ENGLISH); |
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
397 |
template = template.toLowerCase(Locale.ENGLISH); |
2 | 398 |
|
44760 | 399 |
// Retrieve leftmost component |
400 |
int templateIdx = template.indexOf("."); |
|
401 |
int nameIdx = name.indexOf("."); |
|
2 | 402 |
|
403 |
if (templateIdx == -1) |
|
404 |
templateIdx = template.length(); |
|
405 |
if (nameIdx == -1) |
|
406 |
nameIdx = name.length(); |
|
407 |
||
408 |
if (matchWildCards(name.substring(0, nameIdx), |
|
409 |
template.substring(0, templateIdx))) { |
|
410 |
||
411 |
// match rest of the name |
|
412 |
return template.substring(templateIdx).equals( |
|
413 |
name.substring(nameIdx)); |
|
414 |
} else { |
|
415 |
return false; |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
||
420 |
/** |
|
421 |
* Returns true if the name matches against the template that may |
|
422 |
* contain wildcard char * <p> |
|
423 |
*/ |
|
424 |
private static boolean matchWildCards(String name, String template) { |
|
425 |
||
44760 | 426 |
int wildcardIdx = template.indexOf("*"); |
2 | 427 |
if (wildcardIdx == -1) |
428 |
return name.equals(template); |
|
429 |
||
430 |
boolean isBeginning = true; |
|
431 |
String beforeWildcard = ""; |
|
432 |
String afterWildcard = template; |
|
433 |
||
434 |
while (wildcardIdx != -1) { |
|
435 |
||
436 |
// match in sequence the non-wildcard chars in the template. |
|
437 |
beforeWildcard = afterWildcard.substring(0, wildcardIdx); |
|
438 |
afterWildcard = afterWildcard.substring(wildcardIdx + 1); |
|
439 |
||
440 |
int beforeStartIdx = name.indexOf(beforeWildcard); |
|
441 |
if ((beforeStartIdx == -1) || |
|
442 |
(isBeginning && beforeStartIdx != 0)) { |
|
443 |
return false; |
|
444 |
} |
|
445 |
isBeginning = false; |
|
446 |
||
447 |
// update the match scope |
|
448 |
name = name.substring(beforeStartIdx + beforeWildcard.length()); |
|
44760 | 449 |
wildcardIdx = afterWildcard.indexOf("*"); |
2 | 450 |
} |
451 |
return name.endsWith(afterWildcard); |
|
452 |
} |
|
453 |
} |