Skip to content

Commit 72e987e

Browse files
committed
7192189: Support endpoint identification algorithm in RFC 6125
Reviewed-by: xuelei, rhalade
1 parent 288d1af commit 72e987e

File tree

8 files changed

+29
-43
lines changed

8 files changed

+29
-43
lines changed

src/java.base/share/classes/sun/security/util/HostnameChecker.java

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -178,7 +178,7 @@ private static void matchIP(String expectedIP, X509Certificate cert)
178178
* Certification Authorities are encouraged to use the dNSName instead.
179179
*
180180
* Matching is performed using the matching rules specified by
181-
* [RFC5280]. If more than one identity of a given type is present in
181+
* [RFC6125]. If more than one identity of a given type is present in
182182
* the certificate (e.g., more than one dNSName name, a match in any one
183183
* of the set is considered acceptable.)
184184
*/
@@ -262,7 +262,7 @@ public static X500Name getSubjectX500Name(X509Certificate cert)
262262
/**
263263
* Returns true if name matches against template.<p>
264264
*
265-
* The matching is performed as per RFC 2818 rules for TLS and
265+
* The matching is performed as per RFC 2818/6125 rules for TLS and
266266
* RFC 2830 rules for LDAP.<p>
267267
*
268268
* The <code>name</code> parameter should represent a DNS name. The
@@ -299,9 +299,7 @@ private boolean isMatched(String name, String template,
299299
return false;
300300
}
301301

302-
if (checkType == TYPE_TLS) {
303-
return matchAllWildcards(name, template);
304-
} else if (checkType == TYPE_LDAP) {
302+
if (checkType == TYPE_TLS || checkType == TYPE_LDAP) {
305303
return matchLeftmostWildcard(name, template);
306304
} else {
307305
return false;
@@ -371,37 +369,6 @@ private static boolean hasIllegalWildcard(
371369
return false;
372370
}
373371

374-
/**
375-
* Returns true if name matches against template.<p>
376-
*
377-
* According to RFC 2818, section 3.1 -
378-
* Names may contain the wildcard character * which is
379-
* considered to match any single domain name component
380-
* or component fragment.
381-
* E.g., *.a.com matches foo.a.com but not
382-
* bar.foo.a.com. f*.com matches foo.com but not bar.com.
383-
*/
384-
private static boolean matchAllWildcards(String name,
385-
String template) {
386-
name = name.toLowerCase(Locale.ENGLISH);
387-
template = template.toLowerCase(Locale.ENGLISH);
388-
StringTokenizer nameSt = new StringTokenizer(name, ".");
389-
StringTokenizer templateSt = new StringTokenizer(template, ".");
390-
391-
if (nameSt.countTokens() != templateSt.countTokens()) {
392-
return false;
393-
}
394-
395-
while (nameSt.hasMoreTokens()) {
396-
if (!matchWildCards(nameSt.nextToken(),
397-
templateSt.nextToken())) {
398-
return false;
399-
}
400-
}
401-
return true;
402-
}
403-
404-
405372
/**
406373
* Returns true if name matches against template.<p>
407374
*

test/jdk/sun/security/util/HostnameMatcher/TestHostnameChecker.java renamed to test/jdk/sun/security/util/HostnameChecker/TestHostnameChecker.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,16 +23,19 @@
2323

2424
/*
2525
* @test
26-
* @bug 4514108
27-
* @summary Verify host name matching behaves as defined in RFC2818.
26+
* @bug 4514108 7192189
27+
* @summary Verify host name matching behaves as defined in RFC2818 and RFC6125.
2828
* @library /test/lib
29-
* @modules java.base/sun.security.util
29+
* @modules java.base/sun.security.util java.base/sun.security.x509
3030
*/
3131

3232
import java.security.cert.*;
33+
import java.util.Collection;
34+
import java.util.List;
3335

3436
import jdk.test.lib.security.CertUtils;
3537
import sun.security.util.*;
38+
import sun.security.x509.X509CertImpl;
3639

3740
/**
3841
* Certificate 1:
@@ -193,10 +196,17 @@ public static void main(String[] args) throws Exception {
193196
check(checker, "altfoo2.com", cert3, true);
194197
check(checker, "5.6.7.8", cert3, true);
195198
check(checker, "foo.bar.com", cert4, true);
196-
check(checker, "altfoo.bar.com", cert4, true);
199+
check(checker, "altfoo.bar.com", cert4, false);
197200
check(checker, "2001:db8:3c4d:15::1a2f:1a2b", cert5, true);
198201
check(checker, "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b", cert5, true);
199202
check(checker, "2002:db8:3c4d:15::1a2f:1a2b", cert5, false);
203+
check(checker, "foo.bar.example.net", mock("foo.*.example.net"), false);
204+
check(checker, "baz1.example.net", mock("baz*.example.net"), true);
205+
check(checker, "foobaz.example.net", mock("*baz.example.net"), true);
206+
check(checker, "buzz.example.net", mock("b*z.example.net"), true);
207+
check(checker, "公司.example.net", mock("xn--5*.example.net"), false);
208+
check(checker, "公司.江利子.example.net",
209+
mock("*.xn--kcry6tjko.example.net"), true);
200210

201211
checker = HostnameChecker.getInstance(
202212
HostnameChecker.TYPE_LDAP);
@@ -214,6 +224,15 @@ public static void main(String[] args) throws Exception {
214224
check(checker, "altfoo.bar.com", cert4, false);
215225
}
216226

227+
private static X509Certificate mock(String domain) {
228+
return new X509CertImpl() {
229+
@Override
230+
public Collection<List<?>> getSubjectAlternativeNames() {
231+
return List.of(List.of(2, domain));
232+
}
233+
};
234+
}
235+
217236
private static void check(HostnameChecker checker, String name,
218237
X509Certificate cert, boolean expectedResult)
219238
throws Exception {
@@ -224,7 +243,7 @@ private static void check(HostnameChecker checker, String name,
224243
}
225244
} catch (CertificateException e) {
226245
if (expectedResult == true) {
227-
throw e;
246+
throw new Exception("Failed valid test: " + name, e);
228247
}
229248
}
230249
System.out.println("OK: " + name);

0 commit comments

Comments
 (0)