Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/DynamicKeytab.java
38853 views
/*1* Copyright (c) 2011, 2013, 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 689407226* @compile -XDignore.symbol.file DynamicKeytab.java27* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock DynamicKeytab28* @summary always refresh keytab29*/3031import java.io.File;32import java.io.FileOutputStream;33import java.nio.file.Files;34import java.nio.file.Paths;35import org.ietf.jgss.GSSException;36import sun.security.jgss.GSSUtil;37import sun.security.krb5.KrbException;38import sun.security.krb5.internal.Krb5;3940public class DynamicKeytab {4142Context c, s;43public static void main(String[] args)44throws Exception {45new DynamicKeytab().go();46}4748void go() throws Exception {49OneKDC k = new OneKDC(null);50k.writeJAASConf();5152Files.delete(Paths.get(OneKDC.KTAB));5354// Starts with no keytab55c = Context.fromJAAS("client");56s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");5758// Test 1: read new key 1 from keytab59k.addPrincipal(OneKDC.SERVER, "pass1".toCharArray());60k.writeKtab(OneKDC.KTAB);61connect();6263// Test 2: service key cached, find 1 in keytab (now contains 1 and 2)64k.addPrincipal(OneKDC.SERVER, "pass2".toCharArray());65k.appendKtab(OneKDC.KTAB);66connect();6768// Test 3: re-login. Now find 2 in keytab69c = Context.fromJAAS("client");70connect();7172// Test 4: re-login, KDC use 3 this time.73c = Context.fromJAAS("client");74// Put 3 and 4 into keytab but keep the real key back to 3.75k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());76k.appendKtab(OneKDC.KTAB);77k.addPrincipal(OneKDC.SERVER, "pass4".toCharArray());78k.appendKtab(OneKDC.KTAB);79k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());80connect();8182// Test 5: invalid keytab file, should ignore83try (FileOutputStream fos = new FileOutputStream(OneKDC.KTAB)) {84fos.write("BADBADBAD".getBytes());85}86connect();8788// Test 6: delete keytab file, identical to revoke all89Files.delete(Paths.get(OneKDC.KTAB));90try {91connect();92throw new Exception("Should not success");93} catch (GSSException gsse) {94System.out.println(gsse);95KrbException ke = (KrbException)gsse.getCause();96// KrbApReq.authenticate(*) if (dkey == null)...97// This should have been Krb5.KRB_AP_ERR_NOKEY98if (ke.returnCode() != Krb5.API_INVALID_ARG) {99throw new Exception("Not expected failure code: " +100ke.returnCode());101}102}103104// Test 7: 3 revoked, should fail (now contains only 5)105k.addPrincipal(OneKDC.SERVER, "pass5".toCharArray());106k.writeKtab(OneKDC.KTAB); // overwrite keytab, which means107// old key is revoked108try {109connect();110throw new Exception("Should not success");111} catch (GSSException gsse) {112System.out.println(gsse);113// Since 7197159, different kvno is accepted, this return code114// will never be thrown out again.115//KrbException ke = (KrbException)gsse.getCause();116//if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {117// throw new Exception("Not expected failure code: " +118// ke.returnCode());119//}120}121122// Test 8: an empty KDC means revoke all123KDC.create("EMPTY.REALM").writeKtab(OneKDC.KTAB);124try {125connect();126throw new Exception("Should not success");127} catch (GSSException gsse) {128System.out.println(gsse);129KrbException ke = (KrbException)gsse.getCause();130// KrbApReq.authenticate(*) if (dkey == null)...131// This should have been Krb5.KRB_AP_ERR_NOKEY132if (ke.returnCode() != Krb5.API_INVALID_ARG) {133throw new Exception("Not expected failure code: " +134ke.returnCode());135}136}137}138139void connect() throws Exception {140Thread.sleep(2000); // make sure ktab timestamp is different141c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);142s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);143Context.handshake(c, s);144}145}146147148