|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 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 |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8331975 |
| 27 | + * @summary ensure correct name comparison when a system property is set |
| 28 | + * @library /test/lib |
| 29 | + * @compile -XDignore.symbol.file CaseSensitive.java |
| 30 | + * @run main jdk.test.lib.FileInstaller TestHosts TestHosts |
| 31 | + * @run main/othervm -Djdk.net.hosts.file=TestHosts CaseSensitive no |
| 32 | + * @run main/othervm -Djdk.net.hosts.file=TestHosts |
| 33 | + * -Djdk.security.krb5.name.case.sensitive=true CaseSensitive yes |
| 34 | + */ |
| 35 | + |
| 36 | +import jdk.test.lib.Asserts; |
| 37 | +import org.ietf.jgss.GSSException; |
| 38 | +import sun.security.jgss.GSSUtil; |
| 39 | + |
| 40 | +public class CaseSensitive { |
| 41 | + |
| 42 | + public static void main(String[] args) throws Exception { |
| 43 | + switch (args[0]) { |
| 44 | + case "yes" -> testSensitive(); |
| 45 | + case "no" -> testInsensitive(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static void testSensitive() throws Exception { |
| 50 | + var kdc = new OneKDC(null).writeJAASConf(); |
| 51 | + kdc.addPrincipal("hello", "password".toCharArray()); |
| 52 | + kdc.writeKtab(OneKDC.KTAB); |
| 53 | + |
| 54 | + Context c = Context.fromJAAS("client"); |
| 55 | + Context s = Context.fromJAAS("com.sun.security.jgss.krb5.accept"); |
| 56 | + |
| 57 | + // There is only "hello". Cannot talk to "HELLO" |
| 58 | + c.startAsClient("HELLO", GSSUtil.GSS_KRB5_MECH_OID); |
| 59 | + s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
| 60 | + try { |
| 61 | + Context.handshake(c, s); |
| 62 | + throw new RuntimeException("Should not succeed"); |
| 63 | + } catch(GSSException ge) { |
| 64 | + System.out.println(ge.getMessage()); |
| 65 | + System.out.println("No HELLO in db. Expected"); |
| 66 | + } |
| 67 | + |
| 68 | + // Add "HELLO". Can talk to "HELLO" now. |
| 69 | + kdc.addPrincipal("HELLO", "different".toCharArray()); |
| 70 | + kdc.writeKtab(OneKDC.KTAB); |
| 71 | + |
| 72 | + c.startAsClient("HELLO", GSSUtil.GSS_KRB5_MECH_OID); |
| 73 | + s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
| 74 | + Context.handshake(c, s); |
| 75 | + // Name could be partial without realm, so only compare the beginning |
| 76 | + Asserts.assertTrue(c.x().getTargName().toString().startsWith("HELLO"), |
| 77 | + c.x().getTargName().toString()); |
| 78 | + Asserts.assertTrue(s.x().getTargName().toString().startsWith("HELLO"), |
| 79 | + s.x().getTargName().toString()); |
| 80 | + |
| 81 | + // Can also talk to "hello", which has a different password. |
| 82 | + c.startAsClient("hello", GSSUtil.GSS_KRB5_MECH_OID); |
| 83 | + s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
| 84 | + Context.handshake(c, s); |
| 85 | + Asserts.assertTrue(c.x().getTargName().toString().startsWith("hello"), |
| 86 | + c.x().getTargName().toString()); |
| 87 | + Asserts.assertTrue(s.x().getTargName().toString().startsWith("hello"), |
| 88 | + s.x().getTargName().toString()); |
| 89 | + } |
| 90 | + |
| 91 | + static void testInsensitive() throws Exception { |
| 92 | + var kdc = new OneKDC(null).writeJAASConf(); |
| 93 | + kdc.addPrincipal("hello", "password".toCharArray()); |
| 94 | + kdc.writeKtab(OneKDC.KTAB); |
| 95 | + |
| 96 | + Context c = Context.fromJAAS("client"); |
| 97 | + Context s = Context.fromJAAS("com.sun.security.jgss.krb5.accept"); |
| 98 | + |
| 99 | + // There is only "hello" but we can talk to "HELLO". |
| 100 | + c.startAsClient("HELLO", GSSUtil.GSS_KRB5_MECH_OID); |
| 101 | + s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
| 102 | + Context.handshake(c, s); |
| 103 | + Asserts.assertTrue(c.x().getTargName().toString().startsWith("HELLO"), |
| 104 | + c.x().getTargName().toString()); |
| 105 | + Asserts.assertTrue(s.x().getTargName().toString().startsWith("HELLO"), |
| 106 | + s.x().getTargName().toString()); |
| 107 | + } |
| 108 | +} |
0 commit comments