Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/OkAsDelegateXRealm.java
38854 views
/*1* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6853328 717270126* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock OkAsDelegateXRealm false27* KDC no OK-AS-DELEGATE, fail28* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock -Dtest.kdc.policy.ok-as-delegate OkAsDelegateXRealm true29* KDC set OK-AS-DELEGATE for all, succeed30* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock -Dtest.kdc.policy.ok-as-delegate=host/host.r3.local OkAsDelegateXRealm false31* KDC set OK-AS-DELEGATE for host/host.r3.local only, fail32* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock -Dtest.kdc.policy.ok-as-delegate=host/host.r3.local,krbtgt/R2,krbtgt/R3 OkAsDelegateXRealm true33* KDC set OK-AS-DELEGATE for all three, succeed34* @summary Support OK-AS-DELEGATE flag35*/36import java.io.FileOutputStream;37import java.io.IOException;38import java.security.Security;39import javax.security.auth.callback.Callback;40import javax.security.auth.callback.CallbackHandler;41import javax.security.auth.callback.NameCallback;42import javax.security.auth.callback.PasswordCallback;43import javax.security.auth.callback.UnsupportedCallbackException;44import org.ietf.jgss.GSSException;45import sun.security.jgss.GSSUtil;46import sun.security.krb5.Config;4748public class OkAsDelegateXRealm implements CallbackHandler {4950/**51* @param args boolean if the program should succeed52*/53public static void main(String[] args)54throws Exception {5556// Create and start the KDCs. Here we have 3 realms: R1, R2 and R3.57// R1 is trusted by R2, and R2 trusted by R3.58KDC kdc1 = KDC.create("R1");59kdc1.setOption(KDC.Option.OK_AS_DELEGATE,60System.getProperty("test.kdc.policy.ok-as-delegate"));61kdc1.addPrincipal("dummy", "bogus".toCharArray());62kdc1.addPrincipalRandKey("krbtgt/R1");63kdc1.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray());6465KDC kdc2 = KDC.create("R2");66kdc2.setOption(KDC.Option.OK_AS_DELEGATE,67System.getProperty("test.kdc.policy.ok-as-delegate"));68kdc2.addPrincipalRandKey("krbtgt/R2");69kdc2.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray());70kdc2.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray());7172KDC kdc3 = KDC.create("R3");73kdc3.setOption(KDC.Option.OK_AS_DELEGATE,74System.getProperty("test.kdc.policy.ok-as-delegate"));75kdc3.addPrincipalRandKey("krbtgt/R3");76kdc3.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray());77kdc3.addPrincipalRandKey("host/host.r3.local");7879KDC.saveConfig("krb5-localkdc.conf", kdc1, kdc2, kdc3,80"forwardable=true",81"[capaths]",82"R1 = {",83" R2 = .",84" R3 = R2",85"}",86"[domain_realm]",87".r3.local=R3"88);8990System.setProperty("java.security.krb5.conf", "krb5-localkdc.conf");91kdc3.writeKtab("localkdc.ktab");9293FileOutputStream fos = new FileOutputStream("jaas-localkdc.conf");9495// Defines the client and server on R1 and R3 respectively.96fos.write(("com.sun.security.jgss.krb5.initiate {\n" +97" com.sun.security.auth.module.Krb5LoginModule\n" +98" required\n" +99" principal=dummy\n" +100" doNotPrompt=false\n" +101" useTicketCache=false\n" +102" ;\n};\n" +103"com.sun.security.jgss.krb5.accept {\n" +104" com.sun.security.auth.module.Krb5LoginModule required\n" +105" principal=\"host/host.r3.local@R3\"\n" +106" useKeyTab=true\n" +107" keyTab=localkdc.ktab\n" +108" isInitiator=false\n" +109" storeKey=true;\n};\n" +110"\n").getBytes());111fos.close();112113Security.setProperty("auth.login.defaultCallbackHandler",114"OkAsDelegateXRealm");115116System.setProperty("java.security.auth.login.config", "jaas-localkdc.conf");117118Config.refresh();119120Context c = Context.fromJAAS("com.sun.security.jgss.krb5.initiate");121Context s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");122123// Test twice. The frist time the whole cross realm process is tried,124// the second time the cached service ticket is used. This is to make sure125// the behaviors are the same, especailly for the case when one of the126// cross-realm TGTs does not have OK-AS-DELEGATE on.127128for (int i=0; i<2; i++) {129c.startAsClient("[email protected]", GSSUtil.GSS_KRB5_MECH_OID);130s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);131c.x().requestDelegPolicy(true);132133Context.handshake(c, s);134boolean succeed = true;135try {136s.x().getDelegCred();137} catch (GSSException gsse) {138succeed = false;139}140if (succeed != Boolean.parseBoolean(args[0])) {141throw new Exception("Test fail at round #" + i);142}143}144}145146@Override147public void handle(Callback[] callbacks)148throws IOException, UnsupportedCallbackException {149for (Callback callback : callbacks) {150if (callback instanceof NameCallback) {151((NameCallback) callback).setName("dummy");152}153if (callback instanceof PasswordCallback) {154((PasswordCallback) callback).setPassword("bogus".toCharArray());155}156}157}158}159160161162