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