jdk/test/sun/security/krb5/auto/OkAsDelegate.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 org.ietf.jgss.GSSCredential;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    26
import org.ietf.jgss.GSSException;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    27
import org.ietf.jgss.Oid;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    28
import sun.security.jgss.GSSUtil;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    29
import sun.security.krb5.Config;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    30
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    31
public class OkAsDelegate {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    32
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    33
    public static void main(String[] args)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    34
            throws Exception {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    35
        OkAsDelegate ok = new OkAsDelegate();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    36
        ok.go(
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    37
                Boolean.valueOf(args[0]),   // FORWARDABLE in krb5.conf on?
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    38
                Boolean.valueOf(args[1]),   // requestDelegState
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    39
                Boolean.valueOf(args[2]),   // requestDelegPolicyState
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    40
                Boolean.valueOf(args[3]),   // DelegState in response
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    41
                Boolean.valueOf(args[4]),   // DelegPolicyState in response
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    42
                Boolean.valueOf(args[5])    // getDelegCred OK?
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
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    46
    void go(
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    47
            boolean forwardable,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    48
            boolean requestDelegState,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    49
            boolean requestDelegPolicyState,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    50
            boolean delegState,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    51
            boolean delegPolicyState,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    52
            boolean delegated
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    53
            ) throws Exception {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    54
        OneKDC kdc = new OneKDC(null);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    55
        kdc.setPolicy("ok-as-delegate",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    56
                System.getProperty("test.kdc.policy.ok-as-delegate"));
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    57
        kdc.writeJAASConf();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    58
        if (!forwardable) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    59
            // The default OneKDC always includes "forwardable = true"
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    60
            // in krb5.conf, override it.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    61
            KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    62
                    "default_keytab_name = " + OneKDC.KTAB);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    63
            Config.refresh();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    64
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    65
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    66
        Context c, s;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    67
        c = Context.fromJAAS("client");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    68
        s = Context.fromJAAS("server");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    69
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    70
        Oid mech = GSSUtil.GSS_KRB5_MECH_OID;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    71
        if (System.getProperty("test.spnego") != null) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    72
            mech = GSSUtil.GSS_SPNEGO_MECH_OID;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    73
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    74
        c.startAsClient(OneKDC.SERVER, mech);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    75
        ExtendedGSSContext cx = (ExtendedGSSContext)c.x();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    76
        cx.requestCredDeleg(requestDelegState);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    77
        cx.requestDelegPolicy(requestDelegPolicyState);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    78
        s.startAsServer(mech);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    79
        ExtendedGSSContext sx = (ExtendedGSSContext)s.x();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    80
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    81
        Context.handshake(c, s);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    82
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    83
        if (cx.getCredDelegState() != delegState) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    84
            throw new Exception("Initiator cred state error");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    85
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    86
        if (sx.getCredDelegState() != delegState) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    87
            throw new Exception("Acceptor cred state error");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    88
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    89
        if (cx.getDelegPolicyState() != delegPolicyState) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    90
            throw new Exception("Initiator cred policy state error");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    91
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    92
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    93
        GSSCredential cred = null;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    94
        try {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    95
            cred = s.x().getDelegCred();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    96
        } catch (GSSException e) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    97
            // leave cred as null
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    98
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
    99
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   100
        if (delegated != (cred != null)) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   101
            throw new Exception("get cred error");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   102
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   103
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents:
diff changeset
   104
}