jdk/test/sun/security/krb5/auto/CrossRealm.java
author weijun
Thu, 18 Mar 2010 18:26:37 +0800
changeset 5154 07af3c279166
parent 3046 dd50d75d88e6
child 5506 202f599c92aa
permissions -rw-r--r--
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     1
/*
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
     2
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     4
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     8
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    13
 * accompanied this code).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    14
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    18
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    21
 * have any questions.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    22
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    23
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    24
/*
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    25
 * @test
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    26
 * @bug 6706974
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    27
 * @summary Add krb5 test infrastructure
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    28
 */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    29
import java.io.File;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    30
import java.io.FileOutputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    31
import java.io.IOException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    32
import java.security.Security;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    33
import javax.security.auth.callback.Callback;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    34
import javax.security.auth.callback.CallbackHandler;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    35
import javax.security.auth.callback.NameCallback;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    36
import javax.security.auth.callback.PasswordCallback;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    37
import javax.security.auth.callback.UnsupportedCallbackException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    38
import org.ietf.jgss.GSSContext;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    39
import org.ietf.jgss.GSSManager;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    40
import org.ietf.jgss.GSSName;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    41
import sun.security.jgss.GSSUtil;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    42
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    43
public class CrossRealm implements CallbackHandler {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    45
        startKDCs();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    46
        xRealmAuth();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    47
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    48
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    49
    static void startKDCs() throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    50
        // Create and start the KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    51
        KDC kdc1 = KDC.create("RABBIT.HOLE");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    52
        kdc1.addPrincipal("dummy", "bogus".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    53
        kdc1.addPrincipalRandKey("krbtgt/RABBIT.HOLE");
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    54
        kdc1.addPrincipal("krbtgt/SNAKE.HOLE@RABBIT.HOLE",
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    55
                "rabbit->snake".toCharArray());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    56
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    57
        KDC kdc2 = KDC.create("SNAKE.HOLE");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    58
        kdc2.addPrincipalRandKey("krbtgt/SNAKE.HOLE");
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    59
        kdc2.addPrincipal("krbtgt/SNAKE.HOLE@RABBIT.HOLE",
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    60
                "rabbit->snake".toCharArray());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    61
        kdc2.addPrincipalRandKey("host/www.snake.hole");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    62
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    63
        KDC.saveConfig("krb5-localkdc.conf", kdc1, kdc2,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    64
                "forwardable=true",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    65
                "[domain_realm]",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    66
                ".snake.hole=SNAKE.HOLE");
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    67
        new File("krb5-localkdc.conf").deleteOnExit();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    68
        System.setProperty("java.security.krb5.conf", "krb5-localkdc.conf");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    69
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    70
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    71
    static void xRealmAuth() throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    72
        Security.setProperty("auth.login.defaultCallbackHandler", "CrossRealm");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    73
        System.setProperty("java.security.auth.login.config", "jaas-localkdc.conf");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    74
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 1454
diff changeset
    75
        new File("jaas-localkdc.conf").deleteOnExit();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    76
        FileOutputStream fos = new FileOutputStream("jaas-localkdc.conf");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    77
        fos.write(("com.sun.security.jgss.krb5.initiate {\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    78
                "    com.sun.security.auth.module.Krb5LoginModule\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    79
                "    required\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    80
                "    principal=dummy\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    81
                "    doNotPrompt=false\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    82
                "    useTicketCache=false\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    83
                "    ;\n" +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    84
                "};").getBytes());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    85
        fos.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    86
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    87
        GSSManager m = GSSManager.getInstance();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    88
        m.createContext(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    89
                m.createName("host@www.snake.hole", GSSName.NT_HOSTBASED_SERVICE),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    90
                GSSUtil.GSS_KRB5_MECH_OID,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    91
                null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    92
                GSSContext.DEFAULT_LIFETIME).initSecContext(new byte[0], 0, 0);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    93
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    94
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    95
    @Override
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    96
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    97
        for (Callback callback : callbacks) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    98
            if (callback instanceof NameCallback) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    99
                ((NameCallback) callback).setName("dummy");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   100
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   101
            if (callback instanceof PasswordCallback) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   102
                ((PasswordCallback) callback).setPassword("bogus".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   103
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   104
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   105
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   106
}