jdk/test/sun/security/krb5/auto/Context.java
author weijun
Tue, 08 Jan 2013 14:54:56 +0800
changeset 15006 10d6aacdd67f
parent 14413 e954df027393
child 16065 2d0b844bfbb6
permissions -rw-r--r--
8005447: default principal should act as anyone Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12199
diff changeset
     2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4336
diff changeset
    21
 * questions.
1454
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
import com.sun.security.auth.module.Krb5LoginModule;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
    25
import java.security.Key;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    26
import java.security.PrivilegedActionException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    27
import java.security.PrivilegedExceptionAction;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    28
import java.util.Arrays;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    29
import java.util.HashMap;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    30
import java.util.Map;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    31
import javax.security.auth.Subject;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    32
import javax.security.auth.kerberos.KerberosKey;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    33
import javax.security.auth.kerberos.KerberosTicket;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    34
import javax.security.auth.login.LoginContext;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    35
import org.ietf.jgss.GSSContext;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    36
import org.ietf.jgss.GSSCredential;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    37
import org.ietf.jgss.GSSException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    38
import org.ietf.jgss.GSSManager;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    39
import org.ietf.jgss.GSSName;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    40
import org.ietf.jgss.MessageProp;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    41
import org.ietf.jgss.Oid;
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
    42
import com.sun.security.jgss.ExtendedGSSContext;
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
    43
import com.sun.security.jgss.InquireType;
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
    44
import com.sun.security.jgss.AuthorizationDataEntry;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    45
import com.sun.security.jgss.ExtendedGSSCredential;
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
    46
import java.io.ByteArrayInputStream;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
    47
import java.io.ByteArrayOutputStream;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    48
import java.security.Principal;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    49
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    50
/**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    51
 * Context of a JGSS subject, encapsulating Subject and GSSContext.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    52
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    53
 * Three "constructors", which acquire the (private) credentials and fill
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    54
 * it into the Subject:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    55
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    56
 * 1. static fromJAAS(): Creates a Context using a JAAS login config entry
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    57
 * 2. static fromUserPass(): Creates a Context using a username and a password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    58
 * 3. delegated(): A new context which uses the delegated credentials from a
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    59
 *    previously established acceptor Context
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    60
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    61
 * Two context initiators, which create the GSSContext object inside:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    62
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    63
 * 1. startAsClient()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    64
 * 2. startAsServer()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    65
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    66
 * Privileged action:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    67
 *    doAs(): Performs an action in the name of the Subject
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    68
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    69
 * Handshake process:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    70
 *    static handShake(initiator, acceptor)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    71
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    72
 * A four-phase typical data communication which includes all four GSS
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    73
 * actions (wrap, unwrap, getMic and veryfyMiC):
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    74
 *    static transmit(message, from, to)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    75
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    76
public class Context {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    77
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    78
    private Subject s;
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
    79
    private ExtendedGSSContext x;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    80
    private String name;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    81
    private GSSCredential cred;     // see static method delegated().
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    82
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
    83
    static boolean usingStream = false;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
    84
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    85
    private Context() {}
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    86
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    87
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    88
     * Using the delegated credentials from a previous acceptor
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    89
     * @param c
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    90
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    91
    public Context delegated() throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    92
        Context out = new Context();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    93
        out.s = s;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    94
        try {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    95
            out.cred = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    96
                @Override
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    97
                public GSSCredential run() throws Exception {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    98
                    GSSCredential cred = x.getDelegCred();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
    99
                    if (cred == null && x.getCredDelegState() ||
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   100
                            cred != null && !x.getCredDelegState()) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   101
                        throw new Exception("getCredDelegState not match");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   102
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   103
                    return cred;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   104
                }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   105
            });
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   106
        } catch (PrivilegedActionException pae) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   107
            throw pae.getException();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   108
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   109
        out.name = name + " as " + out.cred.getName().toString();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   110
        return out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   111
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   112
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   113
    /**
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   114
     * No JAAS login at all, can be used to test JGSS without JAAS
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   115
     */
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   116
    public static Context fromThinAir() throws Exception {
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   117
        Context out = new Context();
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   118
        out.s = new Subject();
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   119
        return out;
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   120
    }
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   121
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   122
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   123
     * Logins with a JAAS login config entry name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   124
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   125
    public static Context fromJAAS(final String name) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   126
        Context out = new Context();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   127
        out.name = name;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   128
        LoginContext lc = new LoginContext(name);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   129
        lc.login();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   130
        out.s = lc.getSubject();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   131
        return out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   132
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   133
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   134
    /**
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   135
     * Logins with username/password as a new Subject
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   136
     */
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   137
    public static Context fromUserPass(
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   138
            String user, char[] pass, boolean storeKey) throws Exception {
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   139
        return fromUserPass(new Subject(), user, pass, storeKey);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   140
    }
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11526
diff changeset
   141
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   142
    /**
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   143
     * Logins with username/password as an existing Subject. The
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   144
     * same subject can be used multiple times to simulate multiple logins.
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   145
     * @param s existing subject
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   146
     */
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   147
    public static Context fromUserPass(Subject s,
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   148
            String user, char[] pass, boolean storeKey) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   149
        Context out = new Context();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   150
        out.name = user;
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   151
        out.s = s;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   152
        Krb5LoginModule krb5 = new Krb5LoginModule();
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7816
diff changeset
   153
        Map<String, String> map = new HashMap<>();
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7816
diff changeset
   154
        Map<String, Object> shared = new HashMap<>();
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   155
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   156
        if (pass != null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   157
            map.put("useFirstPass", "true");
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   158
            shared.put("javax.security.auth.login.name", user);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   159
            shared.put("javax.security.auth.login.password", pass);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   160
        } else {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   161
            map.put("doNotPrompt", "true");
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   162
            map.put("useTicketCache", "true");
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   163
            if (user != null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   164
                map.put("principal", user);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   165
            }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1574
diff changeset
   166
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   167
        if (storeKey) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   168
            map.put("storeKey", "true");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   169
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   170
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   171
        krb5.initialize(out.s, null, shared, map);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   172
        krb5.login();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   173
        krb5.commit();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   174
        return out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   175
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   176
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   177
    /**
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   178
     * Logins with username/keytab as an existing Subject. The
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   179
     * same subject can be used multiple times to simulate multiple logins.
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   180
     * @param s existing subject
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   181
     */
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   182
    public static Context fromUserKtab(
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   183
            String user, String ktab, boolean storeKey) throws Exception {
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   184
        return fromUserKtab(new Subject(), user, ktab, storeKey);
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   185
    }
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   186
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   187
    /**
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   188
     * Logins with username/keytab as a new subject,
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   189
     */
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   190
    public static Context fromUserKtab(Subject s,
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   191
            String user, String ktab, boolean storeKey) throws Exception {
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   192
        Context out = new Context();
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   193
        out.name = user;
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   194
        out.s = s;
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   195
        Krb5LoginModule krb5 = new Krb5LoginModule();
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7816
diff changeset
   196
        Map<String, String> map = new HashMap<>();
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   197
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   198
        map.put("doNotPrompt", "true");
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   199
        map.put("useTicketCache", "false");
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   200
        map.put("useKeyTab", "true");
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   201
        map.put("keyTab", ktab);
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   202
        map.put("principal", user);
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   203
        if (storeKey) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   204
            map.put("storeKey", "true");
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   205
        }
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   206
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   207
        krb5.initialize(out.s, null, null, map);
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   208
        krb5.login();
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   209
        krb5.commit();
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   210
        return out;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   211
    }
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   212
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5506
diff changeset
   213
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   214
     * Starts as a client
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   215
     * @param target communication peer
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   216
     * @param mech GSS mech
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   217
     * @throws java.lang.Exception
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   218
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   219
    public void startAsClient(final String target, final Oid mech) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   220
        doAs(new Action() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   221
            @Override
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   222
            public byte[] run(Context me, byte[] dummy) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   223
                GSSManager m = GSSManager.getInstance();
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   224
                me.x = (ExtendedGSSContext)m.createContext(
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   225
                          target.indexOf('@') < 0 ?
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   226
                            m.createName(target, null) :
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   227
                            m.createName(target, GSSName.NT_HOSTBASED_SERVICE),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   228
                        mech,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   229
                        cred,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   230
                        GSSContext.DEFAULT_LIFETIME);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   231
                return null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   232
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   233
        }, null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   234
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   235
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   236
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   237
     * Starts as a server
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   238
     * @param mech GSS mech
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   239
     * @throws java.lang.Exception
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   240
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   241
    public void startAsServer(final Oid mech) throws Exception {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   242
        startAsServer(null, mech, false);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   243
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   244
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   245
    public void startAsServer(final String name, final Oid mech) throws Exception {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   246
        startAsServer(name, mech, false);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   247
    }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   248
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   249
     * Starts as a server with the specified service name
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   250
     * @param name the service name
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   251
     * @param mech GSS mech
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   252
     * @throws java.lang.Exception
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   253
     */
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   254
    public void startAsServer(final String name, final Oid mech, final boolean asInitiator) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   255
        doAs(new Action() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   256
            @Override
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   257
            public byte[] run(Context me, byte[] dummy) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   258
                GSSManager m = GSSManager.getInstance();
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   259
                me.cred = m.createCredential(
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   260
                        name == null ? null :
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   261
                          (name.indexOf('@') < 0 ?
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   262
                            m.createName(name, null) :
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   263
                            m.createName(name, GSSName.NT_HOSTBASED_SERVICE)),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   264
                        GSSCredential.INDEFINITE_LIFETIME,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   265
                        mech,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   266
                        asInitiator?
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   267
                                GSSCredential.INITIATE_AND_ACCEPT:
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   268
                                GSSCredential.ACCEPT_ONLY);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   269
                me.x = (ExtendedGSSContext)m.createContext(me.cred);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   270
                return null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   271
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   272
        }, null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   273
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   274
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   275
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   276
     * Accesses the internal GSSContext object. Currently it's used for --
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   277
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   278
     * 1. calling requestXXX() before handshake
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   279
     * 2. accessing source name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   280
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   281
     * Note: If the application needs to do any privileged call on this
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   282
     * object, please use doAs(). Otherwise, it can be done directly. The
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   283
     * methods listed above are all non-privileged calls.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   284
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   285
     * @return the GSSContext object
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   286
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   287
    public ExtendedGSSContext x() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   288
        return x;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   289
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   290
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   291
    /**
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   292
     * Accesses the internal subject.
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   293
     * @return the subject
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   294
     */
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   295
    public Subject s() {
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   296
        return s;
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   297
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   298
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   299
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   300
     * Disposes the GSSContext within
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   301
     * @throws org.ietf.jgss.GSSException
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   302
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   303
    public void dispose() throws GSSException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   304
        x.dispose();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   305
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   306
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   307
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   308
     * Does something using the Subject inside
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   309
     * @param action the action
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   310
     * @param in the input byte
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   311
     * @return the output byte
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   312
     * @throws java.lang.Exception
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   313
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   314
    public byte[] doAs(final Action action, final byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   315
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   316
            return Subject.doAs(s, new PrivilegedExceptionAction<byte[]>() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   317
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   318
                @Override
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   319
                public byte[] run() throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   320
                    return action.run(Context.this, in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   321
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   322
            });
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   323
        } catch (PrivilegedActionException pae) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   324
            throw pae.getException();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   325
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   326
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   327
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   328
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   329
     * Prints status of GSSContext and Subject
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   330
     * @throws java.lang.Exception
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   331
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   332
    public void status() throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   333
        System.out.println("STATUS OF " + name.toUpperCase());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   334
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   335
            StringBuffer sb = new StringBuffer();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   336
            if (x.getAnonymityState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   337
                sb.append("anon, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   338
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   339
            if (x.getConfState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   340
                sb.append("conf, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   341
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   342
            if (x.getCredDelegState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   343
                sb.append("deleg, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   344
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   345
            if (x.getIntegState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   346
                sb.append("integ, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   347
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   348
            if (x.getMutualAuthState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   349
                sb.append("mutual, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   350
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   351
            if (x.getReplayDetState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   352
                sb.append("rep det, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   353
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   354
            if (x.getSequenceDetState()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   355
                sb.append("seq det, ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   356
            }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   357
            if (x instanceof ExtendedGSSContext) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   358
                if (((ExtendedGSSContext)x).getDelegPolicyState()) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   359
                    sb.append("deleg policy, ");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   360
                }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 3483
diff changeset
   361
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   362
            System.out.println("Context status of " + name + ": " + sb.toString());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   363
            System.out.println(x.getSrcName() + " -> " + x.getTargName());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   364
        } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   365
            ;// Don't care
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   366
        }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   367
        if (s != null) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   368
            System.out.println("====== START SUBJECT CONTENT =====");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   369
            for (Principal p: s.getPrincipals()) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   370
                System.out.println("    Principal: " + p);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   371
            }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   372
            for (Object o : s.getPublicCredentials()) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   373
                System.out.println("    " + o.getClass());
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9035
diff changeset
   374
                System.out.println("        " + o);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   375
            }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   376
            System.out.println("====== Private Credentials Set ======");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   377
            for (Object o : s.getPrivateCredentials()) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   378
                System.out.println("    " + o.getClass());
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   379
                if (o instanceof KerberosTicket) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   380
                    KerberosTicket kt = (KerberosTicket) o;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   381
                    System.out.println("        " + kt.getServer() + " for " + kt.getClient());
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   382
                } else if (o instanceof KerberosKey) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   383
                    KerberosKey kk = (KerberosKey) o;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   384
                    System.out.print("        " + kk.getKeyType() + " " + kk.getVersionNumber() + " " + kk.getAlgorithm() + " ");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   385
                    for (byte b : kk.getEncoded()) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   386
                        System.out.printf("%02X", b & 0xff);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   387
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   388
                    System.out.println();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   389
                } else if (o instanceof Map) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   390
                    Map map = (Map) o;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   391
                    for (Object k : map.keySet()) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   392
                        System.out.println("        " + k + ": " + map.get(k));
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   393
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   394
                } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   395
                    System.out.println("        " + o);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   396
                }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   397
            }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   398
            System.out.println("====== END SUBJECT CONTENT =====");
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   399
        }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   400
        if (x != null && x instanceof ExtendedGSSContext) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   401
            if (x.isEstablished()) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   402
                ExtendedGSSContext ex = (ExtendedGSSContext)x;
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   403
                Key k = (Key)ex.inquireSecContext(
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   404
                        InquireType.KRB5_GET_SESSION_KEY);
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   405
                if (k == null) {
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   406
                    throw new Exception("Session key cannot be null");
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   407
                }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   408
                System.out.println("Session key is: " + k);
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   409
                boolean[] flags = (boolean[])ex.inquireSecContext(
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   410
                        InquireType.KRB5_GET_TKT_FLAGS);
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   411
                if (flags == null) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   412
                    throw new Exception("Ticket flags cannot be null");
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   413
                }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   414
                System.out.println("Ticket flags is: " + Arrays.toString(flags));
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   415
                String authTime = (String)ex.inquireSecContext(
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   416
                        InquireType.KRB5_GET_AUTHTIME);
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   417
                if (authTime == null) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   418
                    throw new Exception("Auth time cannot be null");
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   419
                }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   420
                System.out.println("AuthTime is: " + authTime);
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   421
                if (!x.isInitiator()) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   422
                    AuthorizationDataEntry[] ad = (AuthorizationDataEntry[])ex.inquireSecContext(
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   423
                            InquireType.KRB5_GET_AUTHZ_DATA);
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   424
                    System.out.println("AuthzData is: " + Arrays.toString(ad));
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 3482
diff changeset
   425
                }
3482
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   426
            }
4aaa66ce712d 6710360: export Kerberos session key to applications
weijun
parents: 1575
diff changeset
   427
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   428
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   429
10696
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   430
    public byte[] wrap(byte[] t, final boolean privacy)
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   431
            throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   432
        return doAs(new Action() {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   433
            @Override
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   434
            public byte[] run(Context me, byte[] input) throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   435
                System.out.printf("wrap %s privacy from %s: ", privacy?"with":"without", me.name);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   436
                MessageProp p1 = new MessageProp(0, privacy);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   437
                byte[] out;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   438
                if (usingStream) {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   439
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   440
                    me.x.wrap(new ByteArrayInputStream(input), os, p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   441
                    out = os.toByteArray();
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   442
                } else {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   443
                    out = me.x.wrap(input, 0, input.length, p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   444
                }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   445
                System.out.println(printProp(p1));
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   446
                return out;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   447
            }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   448
        }, t);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   449
    }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   450
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   451
    public byte[] unwrap(byte[] t, final boolean privacy)
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   452
            throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   453
        return doAs(new Action() {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   454
            @Override
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   455
            public byte[] run(Context me, byte[] input) throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   456
                System.out.printf("unwrap %s privacy from %s: ", privacy?"with":"without", me.name);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   457
                MessageProp p1 = new MessageProp(0, privacy);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   458
                byte[] bytes;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   459
                if (usingStream) {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   460
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   461
                    me.x.unwrap(new ByteArrayInputStream(input), os, p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   462
                    bytes = os.toByteArray();
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   463
                } else {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   464
                    bytes = me.x.unwrap(input, 0, input.length, p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   465
                }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   466
                System.out.println(printProp(p1));
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   467
                return bytes;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   468
            }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   469
        }, t);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   470
    }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   471
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   472
    public byte[] getMic(byte[] t) throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   473
        return doAs(new Action() {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   474
            @Override
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   475
            public byte[] run(Context me, byte[] input) throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   476
                MessageProp p1 = new MessageProp(0, true);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   477
                byte[] bytes;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   478
                p1 = new MessageProp(0, true);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   479
                System.out.printf("getMic from %s: ", me.name);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   480
                if (usingStream) {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   481
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   482
                    me.x.getMIC(new ByteArrayInputStream(input), os, p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   483
                    bytes = os.toByteArray();
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   484
                } else {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   485
                    bytes = me.x.getMIC(input, 0, input.length, p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   486
                }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   487
                System.out.println(printProp(p1));
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   488
                return bytes;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   489
            }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   490
        }, t);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   491
    }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   492
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   493
    public void verifyMic(byte[] t, final byte[] msg) throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   494
        doAs(new Action() {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   495
            @Override
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   496
            public byte[] run(Context me, byte[] input) throws Exception {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   497
                MessageProp p1 = new MessageProp(0, true);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   498
                System.out.printf("verifyMic from %s: ", me.name);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   499
                if (usingStream) {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   500
                    me.x.verifyMIC(new ByteArrayInputStream(input),
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   501
                            new ByteArrayInputStream(msg), p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   502
                } else {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   503
                    me.x.verifyMIC(input, 0, input.length,
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   504
                            msg, 0, msg.length,
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   505
                            p1);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   506
                }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   507
                System.out.println(printProp(p1));
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   508
                return null;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   509
            }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   510
        }, t);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   511
    }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   512
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   513
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   514
     * Transmits a message from one Context to another. The sender wraps the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   515
     * message and sends it to the receiver. The receiver unwraps it, creates
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   516
     * a MIC of the clear text and sends it back to the sender. The sender
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   517
     * verifies the MIC against the message sent earlier.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   518
     * @param message the message
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   519
     * @param s1 the sender
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   520
     * @param s2 the receiver
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   521
     * @throws java.lang.Exception If anything goes wrong
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   522
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   523
    static public void transmit(final String message, final Context s1,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   524
            final Context s2) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   525
        final byte[] messageBytes = message.getBytes();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   526
        System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   527
                s1.name, s2.name);
10696
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   528
        byte[] wrapped = s1.wrap(messageBytes, true);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   529
        byte[] unwrapped = s2.unwrap(wrapped, true);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   530
        if (!Arrays.equals(messageBytes, unwrapped)) {
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   531
            throw new Exception("wrap/unwrap mismatch");
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   532
        }
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   533
        byte[] mic = s2.getMic(unwrapped);
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 9499
diff changeset
   534
        s1.verifyMic(mic, messageBytes);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   535
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   536
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   537
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   538
     * Returns a string description of a MessageProp object
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   539
     * @param prop the object
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   540
     * @return the description
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   541
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   542
    static public String printProp(MessageProp prop) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   543
        StringBuffer sb = new StringBuffer();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   544
        sb.append("MessagePop: ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   545
        sb.append("QOP="+ prop.getQOP() + ", ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   546
        sb.append(prop.getPrivacy()?"privacy, ":"");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   547
        sb.append(prop.isDuplicateToken()?"dup, ":"");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   548
        sb.append(prop.isGapToken()?"gap, ":"");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   549
        sb.append(prop.isOldToken()?"old, ":"");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   550
        sb.append(prop.isUnseqToken()?"unseq, ":"");
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
   551
        if (prop.getMinorStatus() != 0) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
   552
            sb.append(prop.getMinorString()+ "(" + prop.getMinorStatus()+")");
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5774
diff changeset
   553
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   554
        return sb.toString();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   555
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   556
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   557
    public Context impersonate(final String someone) throws Exception {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   558
        try {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   559
            GSSCredential creds = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   560
                @Override
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   561
                public GSSCredential run() throws Exception {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   562
                    GSSManager m = GSSManager.getInstance();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   563
                    GSSName other = m.createName(someone, GSSName.NT_USER_NAME);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   564
                    if (Context.this.cred == null) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   565
                        Context.this.cred = m.createCredential(GSSCredential.INITIATE_ONLY);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   566
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   567
                    return ((ExtendedGSSCredential)Context.this.cred).impersonate(other);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   568
                }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   569
            });
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   570
            Context out = new Context();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   571
            out.s = s;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   572
            out.cred = creds;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   573
            out.name = name + " as " + out.cred.getName().toString();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   574
            return out;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   575
        } catch (PrivilegedActionException pae) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   576
            throw pae.getException();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   577
        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   578
    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   579
11526
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   580
    public byte[] take(final byte[] in) throws Exception {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   581
        return doAs(new Action() {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   582
            @Override
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   583
            public byte[] run(Context me, byte[] input) throws Exception {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   584
                if (me.x.isEstablished()) {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   585
                    System.out.println(name + " side established");
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   586
                    if (input != null) {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   587
                        throw new Exception("Context established but " +
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   588
                                "still receive token at " + name);
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   589
                    }
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   590
                    return null;
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   591
                } else {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   592
                    if (me.x.isInitiator()) {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   593
                        System.out.println(name + " call initSecContext");
11526
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   594
                        return me.x.initSecContext(input, 0, input.length);
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   595
                    } else {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 14342
diff changeset
   596
                        System.out.println(name + " call acceptSecContext");
11526
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   597
                        return me.x.acceptSecContext(input, 0, input.length);
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   598
                    }
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   599
                }
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   600
            }
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   601
        }, in);
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   602
    }
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   603
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   604
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   605
     * Handshake (security context establishment process) between two Contexts
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   606
     * @param c the initiator
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   607
     * @param s the acceptor
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   608
     * @throws java.lang.Exception
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   609
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   610
    static public void handshake(final Context c, final Context s) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   611
        byte[] t = new byte[0];
11526
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   612
        while (!c.x.isEstablished() || !s.x.isEstablished()) {
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   613
            t = c.take(t);
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 10696
diff changeset
   614
            t = s.take(t);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   615
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   616
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   617
}