jdk/test/sun/security/krb5/auto/SaslUnbound.java
author weijun
Sat, 09 Feb 2013 16:43:49 +0800
changeset 15649 f6bd3d34f844
child 23010 6dadb192ad81
permissions -rw-r--r--
8001104: Unbound SASL service: the GSSAPI/krb5 mech Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     1
/*
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     4
 *
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     8
 *
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    13
 * accompanied this code).
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    14
 *
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    18
 *
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    21
 * questions.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    22
 */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    23
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    24
/*
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    25
 * @test
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    26
 * @bug 8001104
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    27
 * @summary Unbound SASL service: the GSSAPI/krb5 mech
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    28
 * @compile -XDignore.symbol.file SaslUnbound.java
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    29
 * @run main/othervm SaslUnbound 0
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    30
 * @run main/othervm/fail SaslUnbound 1
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    31
 * @run main/othervm/fail SaslUnbound 2
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    32
 * @run main/othervm/fail SaslUnbound 3
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    33
 * @run main/othervm/fail SaslUnbound 4
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    34
 */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    35
import java.io.IOException;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    36
import java.util.Arrays;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    37
import java.util.HashMap;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    38
import javax.security.auth.callback.Callback;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    39
import javax.security.auth.callback.CallbackHandler;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    40
import javax.security.auth.callback.UnsupportedCallbackException;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    41
import javax.security.sasl.*;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    42
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    43
public class SaslUnbound {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    44
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    46
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    47
        String serverProtocol, serverName;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    48
        switch (args[0].charAt(0)) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    49
            case '1':       // Using another protocol, should fail
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    50
                serverProtocol = "serv";
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    51
                serverName = null;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    52
                break;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    53
            case '2':       // Using another protocol, should fail
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    54
                serverProtocol = "otherwise";
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    55
                serverName = null;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    56
                break;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    57
            case '3':       // Using another protocol, should fail
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    58
                serverProtocol = "otherwise";
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    59
                serverName = "host." + OneKDC.REALM;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    60
                break;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    61
            case '4':       // Bound to another serverName, should fail.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    62
                serverProtocol = "server";
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    63
                serverName = "host2." + OneKDC.REALM;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    64
                break;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    65
            default:        // Good unbound server
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    66
                serverProtocol = "server";
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    67
                serverName = null;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    68
                break;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    69
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    70
        new OneKDC(null).writeJAASConf();
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    71
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    72
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    73
        HashMap clntprops = new HashMap();
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    74
        clntprops.put(Sasl.QOP, "auth-conf");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    75
        SaslClient sc = Sasl.createSaslClient(
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    76
                new String[]{"GSSAPI"}, null, "server",
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    77
                "host." + OneKDC.REALM, clntprops, null);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    78
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    79
        final HashMap srvprops = new HashMap();
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    80
        srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    81
        SaslServer ss = Sasl.createSaslServer("GSSAPI", serverProtocol,
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    82
                serverName, srvprops,
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    83
                new CallbackHandler() {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    84
                    public void handle(Callback[] callbacks)
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    85
                            throws IOException, UnsupportedCallbackException {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    86
                        for (Callback cb : callbacks) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    87
                            if (cb instanceof RealmCallback) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    88
                                ((RealmCallback) cb).setText(OneKDC.REALM);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    89
                            } else if (cb instanceof AuthorizeCallback) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    90
                                ((AuthorizeCallback) cb).setAuthorized(true);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    91
                            }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    92
                        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    93
                    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    94
                });
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    95
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    96
        byte[] token = new byte[0];
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    97
        while (!sc.isComplete() || !ss.isComplete()) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    98
            if (!sc.isComplete()) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
    99
                token = sc.evaluateChallenge(token);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   100
            }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   101
            if (!ss.isComplete()) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   102
                token = ss.evaluateResponse(token);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   103
            }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   104
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   105
        System.out.println(ss.getNegotiatedProperty(Sasl.BOUND_SERVER_NAME));
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   106
        byte[] hello = "hello".getBytes();
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   107
        token = sc.wrap(hello, 0, hello.length);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   108
        token = ss.unwrap(token, 0, token.length);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   109
        if (!Arrays.equals(hello, token)) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   110
            throw new Exception("Message altered");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   111
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   112
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
diff changeset
   113
}