jdk/test/sun/security/krb5/auto/DynamicKeytab.java
author stefank
Mon, 04 Apr 2016 09:15:01 +0200
changeset 37241 b9961c99c356
parent 31474 7b79555b8073
permissions -rw-r--r--
8152538: UL doesn't inline the LogTagSet::is_level check Reviewed-by: mlarsson, brutisso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15006
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
9499
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
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14895
diff changeset
    27
 * @compile -XDignore.symbol.file DynamicKeytab.java
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    28
 * @run main/othervm DynamicKeytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    29
 * @summary always refresh keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    30
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    31
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    32
import java.io.File;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    33
import java.io.FileOutputStream;
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    34
import java.nio.file.Files;
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    35
import java.nio.file.Paths;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    36
import org.ietf.jgss.GSSException;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    37
import sun.security.jgss.GSSUtil;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    38
import sun.security.krb5.KrbException;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    39
import sun.security.krb5.internal.Krb5;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    40
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    41
public class DynamicKeytab {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    42
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    43
    Context c, s;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    44
    public static void main(String[] args)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    45
            throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    46
        new DynamicKeytab().go();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    47
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    48
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    49
    void go() throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    50
        OneKDC k = new OneKDC(null);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    51
        k.writeJAASConf();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    52
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    53
        Files.delete(Paths.get(OneKDC.KTAB));
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    54
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    55
        // Starts with no keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    56
        c = Context.fromJAAS("client");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    57
        s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    58
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    59
        // Test 1: read new key 1 from keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    60
        k.addPrincipal(OneKDC.SERVER, "pass1".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    61
        k.writeKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    62
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    63
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    64
        // Test 2: service key cached, find 1 in keytab (now contains 1 and 2)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    65
        k.addPrincipal(OneKDC.SERVER, "pass2".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    66
        k.appendKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    67
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    68
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    69
        // Test 3: re-login. Now find 2 in keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    70
        c = Context.fromJAAS("client");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    71
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    72
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    73
        // Test 4: re-login, KDC use 3 this time.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    74
        c = Context.fromJAAS("client");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    75
        // Put 3 and 4 into keytab but keep the real key back to 3.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    76
        k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    77
        k.appendKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    78
        k.addPrincipal(OneKDC.SERVER, "pass4".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    79
        k.appendKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    80
        k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    81
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    82
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    83
        // Test 5: invalid keytab file, should ignore
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    84
        try (FileOutputStream fos = new FileOutputStream(OneKDC.KTAB)) {
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    85
            fos.write("BADBADBAD".getBytes());
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    86
        }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    87
        connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    88
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    89
        // Test 6: delete keytab file, identical to revoke all
9542
6f715a596a2e 7040916: DynamicKeyTab test fails on Windows
weijun
parents: 9499
diff changeset
    90
        Files.delete(Paths.get(OneKDC.KTAB));
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    91
        try {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    92
            connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    93
            throw new Exception("Should not success");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    94
        } catch (GSSException gsse) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    95
            System.out.println(gsse);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    96
            KrbException ke = (KrbException)gsse.getCause();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    97
            // KrbApReq.authenticate(*) if (dkey == null)...
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    98
            // This should have been Krb5.KRB_AP_ERR_NOKEY
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    99
            if (ke.returnCode() != Krb5.API_INVALID_ARG) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   100
                throw new Exception("Not expected failure code: " +
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   101
                        ke.returnCode());
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
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   105
        // Test 7: 3 revoked, should fail (now contains only 5)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   106
        k.addPrincipal(OneKDC.SERVER, "pass5".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   107
        k.writeKtab(OneKDC.KTAB);   // overwrite keytab, which means
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   108
                                    // old key is revoked
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   109
        try {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   110
            connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   111
            throw new Exception("Should not success");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   112
        } catch (GSSException gsse) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   113
            System.out.println(gsse);
14895
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   114
            // Since 7197159, different kvno is accepted, this return code
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   115
            // will never be thrown out again.
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   116
            //KrbException ke = (KrbException)gsse.getCause();
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   117
            //if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   118
            //    throw new Exception("Not expected failure code: " +
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   119
            //            ke.returnCode());
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 9542
diff changeset
   120
            //}
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   121
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   122
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   123
        // Test 8: an empty KDC means revoke all
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   124
        KDC.create("EMPTY.REALM").writeKtab(OneKDC.KTAB);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   125
        try {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   126
            connect();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   127
            throw new Exception("Should not success");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   128
        } catch (GSSException gsse) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   129
            System.out.println(gsse);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   130
            KrbException ke = (KrbException)gsse.getCause();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   131
            // KrbApReq.authenticate(*) if (dkey == null)...
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   132
            // This should have been Krb5.KRB_AP_ERR_NOKEY
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   133
            if (ke.returnCode() != Krb5.API_INVALID_ARG) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   134
                throw new Exception("Not expected failure code: " +
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   135
                        ke.returnCode());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   136
            }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   137
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   138
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   139
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   140
    void connect() throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   141
        Thread.sleep(2000);     // make sure ktab timestamp is different
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   142
        c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   143
        s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   144
        Context.handshake(c, s);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   145
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   146
}