author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 30905 | bba6fefdd660 |
child 44760 | 61b03b960583 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 2002, 2013, 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.util.*; |
32 |
||
33 |
import java.security.Principal; |
|
34 |
import java.security.cert.*; |
|
35 |
||
36 |
import javax.security.auth.x500.X500Principal; |
|
37 |
||
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
38 |
import sun.security.ssl.ClientKeyExchangeService; |
2 | 39 |
import sun.security.x509.X500Name; |
40 |
||
41 |
import sun.net.util.IPAddressUtil; |
|
42 |
||
43 |
/** |
|
44 |
* Class to check hostnames against the names specified in a certificate as |
|
45 |
* required for TLS and LDAP. |
|
46 |
* |
|
47 |
*/ |
|
48 |
public class HostnameChecker { |
|
49 |
||
50 |
// Constant for a HostnameChecker for TLS |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
51 |
public static final byte TYPE_TLS = 1; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
52 |
private static final HostnameChecker INSTANCE_TLS = |
2 | 53 |
new HostnameChecker(TYPE_TLS); |
54 |
||
55 |
// Constant for a HostnameChecker for LDAP |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
56 |
public static final byte TYPE_LDAP = 2; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
57 |
private static final HostnameChecker INSTANCE_LDAP = |
2 | 58 |
new HostnameChecker(TYPE_LDAP); |
59 |
||
60 |
// 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
|
61 |
private static final int ALTNAME_DNS = 2; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30905
diff
changeset
|
62 |
private static final int ALTNAME_IP = 7; |
2 | 63 |
|
64 |
// the algorithm to follow to perform the check. Currently unused. |
|
65 |
private final byte checkType; |
|
66 |
||
67 |
private HostnameChecker(byte checkType) { |
|
68 |
this.checkType = checkType; |
|
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* Get a HostnameChecker instance. checkType should be one of the |
|
73 |
* TYPE_* constants defined in this class. |
|
74 |
*/ |
|
75 |
public static HostnameChecker getInstance(byte checkType) { |
|
76 |
if (checkType == TYPE_TLS) { |
|
77 |
return INSTANCE_TLS; |
|
78 |
} else if (checkType == TYPE_LDAP) { |
|
79 |
return INSTANCE_LDAP; |
|
80 |
} |
|
81 |
throw new IllegalArgumentException("Unknown check type: " + checkType); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Perform the check. |
|
86 |
* |
|
87 |
* @exception CertificateException if the name does not match any of |
|
88 |
* the names specified in the certificate |
|
89 |
*/ |
|
90 |
public void match(String expectedName, X509Certificate cert) |
|
91 |
throws CertificateException { |
|
92 |
if (isIpAddress(expectedName)) { |
|
93 |
matchIP(expectedName, cert); |
|
94 |
} else { |
|
95 |
matchDNS(expectedName, cert); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Perform the check for Kerberos. |
|
101 |
*/ |
|
4236 | 102 |
public static boolean match(String expectedName, Principal principal) { |
2 | 103 |
String hostName = getServerName(principal); |
104 |
return (expectedName.equalsIgnoreCase(hostName)); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Return the Server name from Kerberos principal. |
|
109 |
*/ |
|
4236 | 110 |
public static String getServerName(Principal principal) { |
30905
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
111 |
ClientKeyExchangeService p = |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
112 |
ClientKeyExchangeService.find("KRB5"); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
113 |
if (p == null) { |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
114 |
throw new AssertionError("Kerberos should have been available"); |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
115 |
} |
bba6fefdd660
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
28865
diff
changeset
|
116 |
return p.getServiceHostName(principal); |
2 | 117 |
} |
118 |
||
119 |
/** |
|
120 |
* Test whether the given hostname looks like a literal IPv4 or IPv6 |
|
121 |
* address. The hostname does not need to be a fully qualified name. |
|
122 |
* |
|
123 |
* This is not a strict check that performs full input validation. |
|
124 |
* That means if the method returns true, name need not be a correct |
|
125 |
* IP address, rather that it does not represent a valid DNS hostname. |
|
126 |
* Likewise for IP addresses when it returns false. |
|
127 |
*/ |
|
128 |
private static boolean isIpAddress(String name) { |
|
129 |
if (IPAddressUtil.isIPv4LiteralAddress(name) || |
|
130 |
IPAddressUtil.isIPv6LiteralAddress(name)) { |
|
131 |
return true; |
|
132 |
} else { |
|
133 |
return false; |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Check if the certificate allows use of the given IP address. |
|
139 |
* |
|
140 |
* From RFC2818: |
|
141 |
* In some cases, the URI is specified as an IP address rather than a |
|
142 |
* hostname. In this case, the iPAddress subjectAltName must be present |
|
143 |
* in the certificate and must exactly match the IP in the URI. |
|
144 |
*/ |
|
145 |
private static void matchIP(String expectedIP, X509Certificate cert) |
|
146 |
throws CertificateException { |
|
147 |
Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames(); |
|
148 |
if (subjAltNames == null) { |
|
149 |
throw new CertificateException |
|
150 |
("No subject alternative names present"); |
|
151 |
} |
|
152 |
for (List<?> next : subjAltNames) { |
|
153 |
// For IP address, it needs to be exact match |
|
154 |
if (((Integer)next.get(0)).intValue() == ALTNAME_IP) { |
|
155 |
String ipAddress = (String)next.get(1); |
|
156 |
if (expectedIP.equalsIgnoreCase(ipAddress)) { |
|
157 |
return; |
|
28865
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
158 |
} else { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
159 |
// compare InetAddress objects in order to ensure |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
160 |
// equality between a long IPv6 address and its |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
161 |
// abbreviated form. |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
162 |
try { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
163 |
if (InetAddress.getByName(expectedIP).equals( |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
164 |
InetAddress.getByName(ipAddress))) { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
165 |
return; |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
166 |
} |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
167 |
} catch (UnknownHostException e) { |
4729ff15079b
8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents:
25859
diff
changeset
|
168 |
} catch (SecurityException e) {} |
2 | 169 |
} |
170 |
} |
|
171 |
} |
|
172 |
throw new CertificateException("No subject alternative " + |
|
173 |
"names matching " + "IP address " + |
|
174 |
expectedIP + " found"); |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Check if the certificate allows use of the given DNS name. |
|
179 |
* |
|
180 |
* From RFC2818: |
|
181 |
* If a subjectAltName extension of type dNSName is present, that MUST |
|
182 |
* be used as the identity. Otherwise, the (most specific) Common Name |
|
183 |
* field in the Subject field of the certificate MUST be used. Although |
|
184 |
* the use of the Common Name is existing practice, it is deprecated and |
|
185 |
* Certification Authorities are encouraged to use the dNSName instead. |
|
186 |
* |
|
187 |
* Matching is performed using the matching rules specified by |
|
188 |
* [RFC2459]. If more than one identity of a given type is present in |
|
189 |
* the certificate (e.g., more than one dNSName name, a match in any one |
|
190 |
* of the set is considered acceptable.) |
|
191 |
*/ |
|
192 |
private void matchDNS(String expectedName, X509Certificate cert) |
|
193 |
throws CertificateException { |
|
194 |
Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames(); |
|
195 |
if (subjAltNames != null) { |
|
196 |
boolean foundDNS = false; |
|
197 |
for ( List<?> next : subjAltNames) { |
|
198 |
if (((Integer)next.get(0)).intValue() == ALTNAME_DNS) { |
|
199 |
foundDNS = true; |
|
200 |
String dnsName = (String)next.get(1); |
|
201 |
if (isMatched(expectedName, dnsName)) { |
|
202 |
return; |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
if (foundDNS) { |
|
207 |
// if certificate contains any subject alt names of type DNS |
|
208 |
// but none match, reject |
|
209 |
throw new CertificateException("No subject alternative DNS " |
|
210 |
+ "name matching " + expectedName + " found."); |
|
211 |
} |
|
212 |
} |
|
213 |
X500Name subjectName = getSubjectX500Name(cert); |
|
214 |
DerValue derValue = subjectName.findMostSpecificAttribute |
|
215 |
(X500Name.commonName_oid); |
|
216 |
if (derValue != null) { |
|
217 |
try { |
|
218 |
if (isMatched(expectedName, derValue.getAsString())) { |
|
219 |
return; |
|
220 |
} |
|
221 |
} catch (IOException e) { |
|
222 |
// ignore |
|
223 |
} |
|
224 |
} |
|
225 |
String msg = "No name matching " + expectedName + " found"; |
|
226 |
throw new CertificateException(msg); |
|
227 |
} |
|
228 |
||
229 |
||
230 |
/** |
|
231 |
* Return the subject of a certificate as X500Name, by reparsing if |
|
232 |
* necessary. X500Name should only be used if access to name components |
|
21278 | 233 |
* is required, in other cases X500Principal is to be preferred. |
2 | 234 |
* |
235 |
* This method is currently used from within JSSE, do not remove. |
|
236 |
*/ |
|
237 |
public static X500Name getSubjectX500Name(X509Certificate cert) |
|
238 |
throws CertificateParsingException { |
|
239 |
try { |
|
240 |
Principal subjectDN = cert.getSubjectDN(); |
|
241 |
if (subjectDN instanceof X500Name) { |
|
242 |
return (X500Name)subjectDN; |
|
243 |
} else { |
|
244 |
X500Principal subjectX500 = cert.getSubjectX500Principal(); |
|
245 |
return new X500Name(subjectX500.getEncoded()); |
|
246 |
} |
|
247 |
} catch (IOException e) { |
|
248 |
throw(CertificateParsingException) |
|
249 |
new CertificateParsingException().initCause(e); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
||
254 |
/** |
|
255 |
* Returns true if name matches against template.<p> |
|
256 |
* |
|
257 |
* The matching is performed as per RFC 2818 rules for TLS and |
|
258 |
* RFC 2830 rules for LDAP.<p> |
|
259 |
* |
|
260 |
* The <code>name</code> parameter should represent a DNS name. |
|
261 |
* The <code>template</code> parameter |
|
262 |
* may contain the wildcard character * |
|
263 |
*/ |
|
264 |
private boolean isMatched(String name, String template) { |
|
265 |
if (checkType == TYPE_TLS) { |
|
266 |
return matchAllWildcards(name, template); |
|
267 |
} else if (checkType == TYPE_LDAP) { |
|
268 |
return matchLeftmostWildcard(name, template); |
|
269 |
} else { |
|
270 |
return false; |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
||
275 |
/** |
|
276 |
* Returns true if name matches against template.<p> |
|
277 |
* |
|
278 |
* According to RFC 2818, section 3.1 - |
|
279 |
* Names may contain the wildcard character * which is |
|
280 |
* considered to match any single domain name component |
|
281 |
* or component fragment. |
|
282 |
* E.g., *.a.com matches foo.a.com but not |
|
283 |
* bar.foo.a.com. f*.com matches foo.com but not bar.com. |
|
284 |
*/ |
|
285 |
private static boolean matchAllWildcards(String name, |
|
286 |
String template) { |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
287 |
name = name.toLowerCase(Locale.ENGLISH); |
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
288 |
template = template.toLowerCase(Locale.ENGLISH); |
2 | 289 |
StringTokenizer nameSt = new StringTokenizer(name, "."); |
290 |
StringTokenizer templateSt = new StringTokenizer(template, "."); |
|
291 |
||
292 |
if (nameSt.countTokens() != templateSt.countTokens()) { |
|
293 |
return false; |
|
294 |
} |
|
295 |
||
296 |
while (nameSt.hasMoreTokens()) { |
|
297 |
if (!matchWildCards(nameSt.nextToken(), |
|
298 |
templateSt.nextToken())) { |
|
299 |
return false; |
|
300 |
} |
|
301 |
} |
|
302 |
return true; |
|
303 |
} |
|
304 |
||
305 |
||
306 |
/** |
|
307 |
* Returns true if name matches against template.<p> |
|
308 |
* |
|
309 |
* As per RFC 2830, section 3.6 - |
|
310 |
* The "*" wildcard character is allowed. If present, it applies only |
|
311 |
* to the left-most name component. |
|
312 |
* E.g. *.bar.com would match a.bar.com, b.bar.com, etc. but not |
|
313 |
* bar.com. |
|
314 |
*/ |
|
315 |
private static boolean matchLeftmostWildcard(String name, |
|
316 |
String template) { |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
317 |
name = name.toLowerCase(Locale.ENGLISH); |
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
5506
diff
changeset
|
318 |
template = template.toLowerCase(Locale.ENGLISH); |
2 | 319 |
|
320 |
// Retreive leftmost component |
|
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
321 |
int templateIdx = template.indexOf('.'); |
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
322 |
int nameIdx = name.indexOf('.'); |
2 | 323 |
|
324 |
if (templateIdx == -1) |
|
325 |
templateIdx = template.length(); |
|
326 |
if (nameIdx == -1) |
|
327 |
nameIdx = name.length(); |
|
328 |
||
329 |
if (matchWildCards(name.substring(0, nameIdx), |
|
330 |
template.substring(0, templateIdx))) { |
|
331 |
||
332 |
// match rest of the name |
|
333 |
return template.substring(templateIdx).equals( |
|
334 |
name.substring(nameIdx)); |
|
335 |
} else { |
|
336 |
return false; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
||
341 |
/** |
|
342 |
* Returns true if the name matches against the template that may |
|
343 |
* contain wildcard char * <p> |
|
344 |
*/ |
|
345 |
private static boolean matchWildCards(String name, String template) { |
|
346 |
||
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
347 |
int wildcardIdx = template.indexOf('*'); |
2 | 348 |
if (wildcardIdx == -1) |
349 |
return name.equals(template); |
|
350 |
||
351 |
boolean isBeginning = true; |
|
352 |
String beforeWildcard = ""; |
|
353 |
String afterWildcard = template; |
|
354 |
||
355 |
while (wildcardIdx != -1) { |
|
356 |
||
357 |
// match in sequence the non-wildcard chars in the template. |
|
358 |
beforeWildcard = afterWildcard.substring(0, wildcardIdx); |
|
359 |
afterWildcard = afterWildcard.substring(wildcardIdx + 1); |
|
360 |
||
361 |
int beforeStartIdx = name.indexOf(beforeWildcard); |
|
362 |
if ((beforeStartIdx == -1) || |
|
363 |
(isBeginning && beforeStartIdx != 0)) { |
|
364 |
return false; |
|
365 |
} |
|
366 |
isBeginning = false; |
|
367 |
||
368 |
// update the match scope |
|
369 |
name = name.substring(beforeStartIdx + beforeWildcard.length()); |
|
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
370 |
wildcardIdx = afterWildcard.indexOf('*'); |
2 | 371 |
} |
372 |
return name.endsWith(afterWildcard); |
|
373 |
} |
|
374 |
} |