jdk/test/sun/security/krb5/auto/OkAsDelegateXRealm.java
author weijun
Fri, 27 Nov 2009 08:51:28 +0800
changeset 4336 4c792c19266e
child 5506 202f599c92aa
permissions -rw-r--r--
6853328: Support OK-AS-DELEGATE flag Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     1
/*
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     4
 *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     8
 *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    13
 * accompanied this code).
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    14
 *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    18
 *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    21
 * have any questions.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    22
 */
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    23
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    24
import com.sun.security.jgss.ExtendedGSSContext;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    25
import java.io.File;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    26
import java.io.FileOutputStream;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    27
import java.io.IOException;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    28
import java.security.Security;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    29
import javax.security.auth.callback.Callback;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    30
import javax.security.auth.callback.CallbackHandler;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    31
import javax.security.auth.callback.NameCallback;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    32
import javax.security.auth.callback.PasswordCallback;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    33
import javax.security.auth.callback.UnsupportedCallbackException;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    34
import org.ietf.jgss.GSSContext;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    35
import org.ietf.jgss.GSSCredential;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    36
import org.ietf.jgss.GSSException;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    37
import org.ietf.jgss.GSSManager;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    38
import org.ietf.jgss.GSSName;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    39
import sun.security.jgss.GSSUtil;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    40
import sun.security.krb5.Config;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    41
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    42
public class OkAsDelegateXRealm implements CallbackHandler {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    43
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    44
    /**
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    45
     * @param args boolean if the program should succeed
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    46
     */
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    47
    public static void main(String[] args)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    48
            throws Exception {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    49
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    50
        // Create and start the KDCs. Here we have 3 realms: R1, R2 and R3.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    51
        // R1 is trusted by R2, and R2 trusted by R3.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    52
        KDC kdc1 = KDC.create("R1");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    53
        kdc1.setPolicy("ok-as-delegate",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    54
                System.getProperty("test.kdc.policy.ok-as-delegate"));
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    55
        kdc1.addPrincipal("dummy", "bogus".toCharArray());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    56
        kdc1.addPrincipalRandKey("krbtgt/R1");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    57
        kdc1.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    58
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    59
        KDC kdc2 = KDC.create("R2");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    60
        kdc2.setPolicy("ok-as-delegate",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    61
                System.getProperty("test.kdc.policy.ok-as-delegate"));
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    62
        kdc2.addPrincipalRandKey("krbtgt/R2");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    63
        kdc2.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    64
        kdc2.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    65
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    66
        KDC kdc3 = KDC.create("R3");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    67
        kdc3.setPolicy("ok-as-delegate",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    68
                System.getProperty("test.kdc.policy.ok-as-delegate"));
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    69
        kdc3.addPrincipalRandKey("krbtgt/R3");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    70
        kdc3.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    71
        kdc3.addPrincipalRandKey("host/host.r3.local");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    72
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    73
        KDC.saveConfig("krb5-localkdc.conf", kdc1, kdc2, kdc3,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    74
                "forwardable=true",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    75
                "[capaths]",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    76
                "R1 = {",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    77
                "    R2 = .",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    78
                "    R3 = R2",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    79
                "}",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    80
                "[domain_realm]",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    81
                ".r3.local=R3"
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    82
                );
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    83
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    84
        System.setProperty("java.security.krb5.conf", "krb5-localkdc.conf");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    85
        kdc3.writeKtab("localkdc.ktab");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    86
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    87
        FileOutputStream fos = new FileOutputStream("jaas-localkdc.conf");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    88
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    89
        // Defines the client and server on R1 and R3 respectively.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    90
        fos.write(("com.sun.security.jgss.krb5.initiate {\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    91
                "    com.sun.security.auth.module.Krb5LoginModule\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    92
                "    required\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    93
                "    principal=dummy\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    94
                "    doNotPrompt=false\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    95
                "    useTicketCache=false\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    96
                "    ;\n};\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    97
                "com.sun.security.jgss.krb5.accept {\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    98
                "    com.sun.security.auth.module.Krb5LoginModule required\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    99
                "    principal=\"host/host.r3.local@R3\"\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   100
                "    useKeyTab=true\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   101
                "    keyTab=localkdc.ktab\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   102
                "    isInitiator=false\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   103
                "    storeKey=true;\n};\n" +
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   104
                "\n").getBytes());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   105
        fos.close();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   106
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   107
        Security.setProperty("auth.login.defaultCallbackHandler",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   108
                "OkAsDelegateXRealm");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   109
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   110
        System.setProperty("java.security.auth.login.config", "jaas-localkdc.conf");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   111
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   112
        new File("krb5-localkdc.conf").deleteOnExit();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   113
        new File("localkdc.ktab").deleteOnExit();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   114
        new File("jaas-localkdc.conf").deleteOnExit();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   115
        Config.refresh();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   116
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   117
        Context c = Context.fromJAAS("com.sun.security.jgss.krb5.initiate");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   118
        Context s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   119
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   120
        // Test twice. The frist time the whole cross realm process is tried,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   121
        // the second time the cached service ticket is used. This is to make sure
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   122
        // the behaviors are the same, especailly for the case when one of the
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   123
        // cross-realm TGTs does not have OK-AS-DELEGATE on.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   124
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   125
        for (int i=0; i<2; i++) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   126
            c.startAsClient("host@host.r3.local", GSSUtil.GSS_KRB5_MECH_OID);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   127
            s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   128
            c.x().requestDelegPolicy(true);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   129
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   130
            Context.handshake(c, s);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   131
            boolean succeed = true;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   132
            try {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   133
                s.x().getDelegCred();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   134
            } catch (GSSException gsse) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   135
                succeed = false;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   136
            }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   137
            if (succeed != Boolean.parseBoolean(args[0])) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   138
                throw new Exception("Test fail at round #" + i);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   139
            }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   140
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   141
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   142
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   143
    @Override
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   144
    public void handle(Callback[] callbacks)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   145
            throws IOException, UnsupportedCallbackException {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   146
        for (Callback callback : callbacks) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   147
            if (callback instanceof NameCallback) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   148
                ((NameCallback) callback).setName("dummy");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   149
            }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   150
            if (callback instanceof PasswordCallback) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   151
                ((PasswordCallback) callback).setPassword("bogus".toCharArray());
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   152
            }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   153
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   154
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   155
}
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   156