MoreKvno.java 3.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24 25
 */

/*
 * @test
26
 * @bug 6893158 6907425 7197159
27
 * @run main/othervm MoreKvno
28 29 30
 * @summary AP_REQ check should use key version number
 */

31
import org.ietf.jgss.GSSException;
32
import sun.security.jgss.GSSUtil;
33
import sun.security.krb5.KrbException;
34 35
import sun.security.krb5.PrincipalName;
import sun.security.krb5.internal.ktab.KeyTab;
36
import sun.security.krb5.internal.Krb5;
37 38 39

public class MoreKvno {

40
    static PrincipalName p;
41 42 43 44 45 46 47 48
    public static void main(String[] args)
            throws Exception {

        OneKDC kdc = new OneKDC(null);
        kdc.writeJAASConf();

        // Rewrite keytab, 3 set of keys with different kvno
        KeyTab ktab = KeyTab.create(OneKDC.KTAB);
49 50
        p = new PrincipalName(
            OneKDC.SERVER+"@"+OneKDC.REALM, PrincipalName.KRB_NT_SRV_HST);
51 52 53
        ktab.addEntry(p, "pass1".toCharArray(), 1, true);
        ktab.addEntry(p, "pass3".toCharArray(), 3, true);
        ktab.addEntry(p, "pass2".toCharArray(), 2, true);
54 55
        ktab.save();

56 57 58 59 60 61
        char[] pass = "pass2".toCharArray();
        kdc.addPrincipal(OneKDC.SERVER, pass);
        go(OneKDC.SERVER, "com.sun.security.jgss.krb5.accept", pass);

        pass = "pass3".toCharArray();
        kdc.addPrincipal(OneKDC.SERVER, pass);
62
        // "server" initiate also, check pass2 is used at authentication
63 64 65 66 67 68 69 70
        go(OneKDC.SERVER, "server", pass);

        try {
            pass = "pass4".toCharArray();
            kdc.addPrincipal(OneKDC.SERVER, pass);
            go(OneKDC.SERVER, "com.sun.security.jgss.krb5.accept", pass);
            throw new Exception("This test should fail");
        } catch (GSSException gsse) {
71 72 73 74 75 76 77
            // Since 7197159, different kvno is accepted, this return code
            // will never be thrown out again.
            //KrbException ke = (KrbException)gsse.getCause();
            //if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {
            //    throw new Exception("Not expected failure code: " +
            //            ke.returnCode());
            //}
78
        }
79 80
    }

81
    static void go(String server, String entry, char[] pass) throws Exception {
82
        Context c, s;
83 84

        // Part 1: Test keytab
85 86 87 88 89 90 91 92 93 94
        c = Context.fromUserPass("dummy", "bogus".toCharArray(), false);
        s = Context.fromJAAS(entry);

        c.startAsClient(server, GSSUtil.GSS_KRB5_MECH_OID);
        s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);

        Context.handshake(c, s);

        s.dispose();
        c.dispose();
95 96 97 98 99 100 101 102 103 104 105 106

        // Part 2: Test username/password pair
        c = Context.fromUserPass("dummy", "bogus".toCharArray(), false);
        s = Context.fromUserPass(p.getNameString(), pass, true);

        c.startAsClient(server, GSSUtil.GSS_KRB5_MECH_OID);
        s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);

        Context.handshake(c, s);

        s.dispose();
        c.dispose();
107 108
    }
}