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
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
3232import java .security .cert .*;
33+ import java .util .Collection ;
34+ import java .util .List ;
3335
3436import jdk .test .lib .security .CertUtils ;
3537import 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