Path: blob/master/test/jdk/sun/security/krb5/auto/DynamicKeytab.java
41152 views
/*1* Copyright (c) 2011, 2018, 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 6894072 819448626* @summary always refresh keytab27* @library /test/lib28* @compile -XDignore.symbol.file DynamicKeytab.java29* @run main jdk.test.lib.FileInstaller TestHosts TestHosts30* @run main/othervm -Djdk.net.hosts.file=TestHosts DynamicKeytab31*/3233import java.io.File;34import java.io.FileOutputStream;35import java.nio.file.Files;36import java.nio.file.Paths;37import org.ietf.jgss.GSSException;38import sun.security.jgss.GSSUtil;39import sun.security.krb5.KrbException;40import sun.security.krb5.internal.Krb5;4142public class DynamicKeytab {4344Context c, s;45public static void main(String[] args)46throws Exception {47new DynamicKeytab().go();48}4950void go() throws Exception {51OneKDC k = new OneKDC(null);52k.writeJAASConf();5354Files.delete(Paths.get(OneKDC.KTAB));5556// Starts with no keytab57c = Context.fromJAAS("client");58s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");5960// Test 1: read new key 1 from keytab61k.addPrincipal(OneKDC.SERVER, "pass1".toCharArray());62k.writeKtab(OneKDC.KTAB);63connect();6465// Test 2: service key cached, find 1 in keytab (now contains 1 and 2)66k.addPrincipal(OneKDC.SERVER, "pass2".toCharArray());67k.appendKtab(OneKDC.KTAB);68connect();6970// Test 3: re-login. Now find 2 in keytab71c = Context.fromJAAS("client");72connect();7374// Test 4: re-login, KDC use 3 this time.75c = Context.fromJAAS("client");76// Put 3 and 4 into keytab but keep the real key back to 3.77k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());78k.appendKtab(OneKDC.KTAB);79k.addPrincipal(OneKDC.SERVER, "pass4".toCharArray());80k.appendKtab(OneKDC.KTAB);81k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());82connect();8384// Test 5: invalid keytab file, should ignore85try (FileOutputStream fos = new FileOutputStream(OneKDC.KTAB)) {86fos.write("BADBADBAD".getBytes());87}88connect();8990// Test 6: delete keytab file, identical to revoke all91Files.delete(Paths.get(OneKDC.KTAB));92try {93connect();94throw new Exception("Should not success");95} catch (GSSException gsse) {96System.out.println(gsse);97KrbException ke = (KrbException)gsse.getCause();98// KrbApReq.authenticate(*) if (dkey == null)...99// This should have been Krb5.KRB_AP_ERR_NOKEY100if (ke.returnCode() != Krb5.API_INVALID_ARG) {101throw new Exception("Not expected failure code: " +102ke.returnCode());103}104}105106// Test 7: 3 revoked, should fail (now contains only 5)107k.addPrincipal(OneKDC.SERVER, "pass5".toCharArray());108k.writeKtab(OneKDC.KTAB); // overwrite keytab, which means109// old key is revoked110try {111connect();112throw new Exception("Should not success");113} catch (GSSException gsse) {114System.out.println(gsse);115// Since 7197159, different kvno is accepted, this return code116// will never be thrown out again.117//KrbException ke = (KrbException)gsse.getCause();118//if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {119// throw new Exception("Not expected failure code: " +120// ke.returnCode());121//}122}123124// Test 8: an empty KDC means revoke all125KDC.create("EMPTY.REALM").writeKtab(OneKDC.KTAB);126try {127connect();128throw new Exception("Should not success");129} catch (GSSException gsse) {130System.out.println(gsse);131KrbException ke = (KrbException)gsse.getCause();132// KrbApReq.authenticate(*) if (dkey == null)...133// This should have been Krb5.KRB_AP_ERR_NOKEY134if (ke.returnCode() != Krb5.API_INVALID_ARG) {135throw new Exception("Not expected failure code: " +136ke.returnCode());137}138}139}140141void connect() throws Exception {142Thread.sleep(2000); // make sure ktab timestamp is different143c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);144s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);145Context.handshake(c, s);146}147}148149150