jdk/test/sun/security/krb5/auto/DynamicKeytab.java
author weijun
Sun, 01 May 2011 14:22:32 +0800
changeset 9542 6f715a596a2e
parent 9499 f3115698a012
child 14895 5ad2d7032375
permissions -rw-r--r--
7040916: DynamicKeyTab test fails on Windows Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     1
/*
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     4
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     8
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    13
 * accompanied this code).
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    14
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    18
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    21
 * questions.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    22
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    23
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    24
/*
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    25
 * @test
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    26
 * @bug 6894072
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    27
 * @run main/othervm DynamicKeytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    28
 * @summary always refresh keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    29
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    30
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    31
import java.io.File;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    32
import java.io.FileOutputStream;
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    33
import java.nio.file.Files;
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    34
import java.nio.file.Paths;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    35
import org.ietf.jgss.GSSException;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    36
import sun.security.jgss.GSSUtil;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    37
import sun.security.krb5.KrbException;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    38
import sun.security.krb5.internal.Krb5;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    39
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    40
public class DynamicKeytab {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    41
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    42
    Context c, s;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    43
    public static void main(String[] args)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    44
            throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    45
        new DynamicKeytab().go();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    46
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    47
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    48
    void go() throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    49
        OneKDC k = new OneKDC(null);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    50
        k.writeJAASConf();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    51
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    52
        Files.delete(Paths.get(OneKDC.KTAB));
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    53
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    54
        // Starts with no keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    55
        c = Context.fromJAAS("client");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    56
        s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    57
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    58
        // Test 1: read new key 1 from keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    59
        k.addPrincipal(OneKDC.SERVER, "pass1".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    60
        k.writeKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    61
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    62
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    63
        // Test 2: service key cached, find 1 in keytab (now contains 1 and 2)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    64
        k.addPrincipal(OneKDC.SERVER, "pass2".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    65
        k.appendKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    66
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    67
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    68
        // Test 3: re-login. Now find 2 in keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    69
        c = Context.fromJAAS("client");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    70
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    71
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    72
        // Test 4: re-login, KDC use 3 this time.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    73
        c = Context.fromJAAS("client");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    74
        // Put 3 and 4 into keytab but keep the real key back to 3.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    75
        k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    76
        k.appendKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    77
        k.addPrincipal(OneKDC.SERVER, "pass4".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    78
        k.appendKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    79
        k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    80
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    81
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    82
        // Test 5: invalid keytab file, should ignore
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    83
        try (FileOutputStream fos = new FileOutputStream(OneKDC.KTAB)) {
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    84
            fos.write("BADBADBAD".getBytes());
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    85
        }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    86
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    87
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    88
        // Test 6: delete keytab file, identical to revoke all
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    89
        Files.delete(Paths.get(OneKDC.KTAB));
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    90
        try {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    91
            connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    92
            throw new Exception("Should not success");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    93
        } catch (GSSException gsse) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    94
            System.out.println(gsse);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    95
            KrbException ke = (KrbException)gsse.getCause();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    96
            // KrbApReq.authenticate(*) if (dkey == null)...
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    97
            // This should have been Krb5.KRB_AP_ERR_NOKEY
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    98
            if (ke.returnCode() != Krb5.API_INVALID_ARG) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    99
                throw new Exception("Not expected failure code: " +
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   100
                        ke.returnCode());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   101
            }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   102
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   103
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   104
        // Test 7: 3 revoked, should fail (now contains only 5)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   105
        k.addPrincipal(OneKDC.SERVER, "pass5".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   106
        k.writeKtab(OneKDC.KTAB);   // overwrite keytab, which means
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   107
                                    // old key is revoked
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   108
        try {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   109
            connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   110
            throw new Exception("Should not success");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   111
        } catch (GSSException gsse) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   112
            System.out.println(gsse);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   113
            KrbException ke = (KrbException)gsse.getCause();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   114
            if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   115
                throw new Exception("Not expected failure code: " +
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   116
                        ke.returnCode());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   117
            }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   118
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   119
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   120
        // Test 8: an empty KDC means revoke all
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   121
        KDC.create("EMPTY.REALM").writeKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   122
        try {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   123
            connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   124
            throw new Exception("Should not success");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   125
        } catch (GSSException gsse) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   126
            System.out.println(gsse);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   127
            KrbException ke = (KrbException)gsse.getCause();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   128
            // KrbApReq.authenticate(*) if (dkey == null)...
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   129
            // This should have been Krb5.KRB_AP_ERR_NOKEY
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   130
            if (ke.returnCode() != Krb5.API_INVALID_ARG) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   131
                throw new Exception("Not expected failure code: " +
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   132
                        ke.returnCode());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   133
            }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   134
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   135
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   136
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   137
    void connect() throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   138
        Thread.sleep(2000);     // make sure ktab timestamp is different
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   139
        c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   140
        s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   141
        Context.handshake(c, s);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   142
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   143
}