jdk/test/sun/security/krb5/auto/SaslGSS.java
author weijun
Wed, 01 May 2013 21:05:10 +0800
changeset 17209 6f556e154816
child 18793 4d9455e24050
permissions -rw-r--r--
8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17209
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     1
/*
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     4
 *
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     8
 *
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    13
 * accompanied this code).
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    14
 *
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    18
 *
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    21
 * questions.
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    22
 */
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    23
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    24
/*
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    25
 * @test
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    26
 * @bug 8012082
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    27
 * @summary SASL: auth-conf negotiated, but unencrypted data is accepted,
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    28
  *         reset to unencrypt
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    29
 * @compile -XDignore.symbol.file SaslGSS.java
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    30
 * @run main/othervm SaslGSS
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    31
 */
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    32
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    33
import javax.security.auth.callback.Callback;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    34
import javax.security.auth.callback.CallbackHandler;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    35
import javax.security.auth.callback.UnsupportedCallbackException;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    36
import javax.security.sasl.AuthorizeCallback;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    37
import javax.security.sasl.RealmCallback;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    38
import javax.security.sasl.Sasl;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    39
import javax.security.sasl.SaslServer;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    40
import java.io.IOException;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    41
import java.util.HashMap;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    42
import java.util.Locale;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    43
import org.ietf.jgss.*;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    44
import sun.security.jgss.GSSUtil;
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    45
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    46
public class SaslGSS {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    47
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    49
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    50
        String name = "host." + OneKDC.REALM.toLowerCase(Locale.US);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    51
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    52
        new OneKDC(null).writeJAASConf();
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    53
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    54
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    55
        // Client in JGSS so that it can control wrap privacy mode
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    56
        GSSManager m = GSSManager.getInstance();
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    57
        GSSContext sc = m.createContext(
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    58
                        m.createName(OneKDC.SERVER, GSSUtil.NT_GSS_KRB5_PRINCIPAL),
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    59
                        GSSUtil.GSS_KRB5_MECH_OID,
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    60
                        null,
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    61
                        GSSContext.DEFAULT_LIFETIME);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    62
        sc.requestMutualAuth(false);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    63
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    64
        // Server in SASL
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    65
        final HashMap props = new HashMap();
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    66
        props.put(Sasl.QOP, "auth-conf");
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    67
        SaslServer ss = Sasl.createSaslServer("GSSAPI", "server",
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    68
                name, props,
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    69
                new CallbackHandler() {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    70
                    public void handle(Callback[] callbacks)
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    71
                            throws IOException, UnsupportedCallbackException {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    72
                        for (Callback cb : callbacks) {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    73
                            if (cb instanceof RealmCallback) {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    74
                                ((RealmCallback) cb).setText(OneKDC.REALM);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    75
                            } else if (cb instanceof AuthorizeCallback) {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    76
                                ((AuthorizeCallback) cb).setAuthorized(true);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    77
                            }
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    78
                        }
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    79
                    }
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    80
                });
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    81
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    82
        // Handshake
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    83
        byte[] token = new byte[0];
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    84
        token = sc.initSecContext(token, 0, token.length);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    85
        token = ss.evaluateResponse(token);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    86
        token = sc.unwrap(token, 0, token.length, new MessageProp(0, false));
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    87
        token[0] = (byte)(((token[0] & 4) != 0) ? 4 : 2);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    88
        token = sc.wrap(token, 0, token.length, new MessageProp(0, false));
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    89
        ss.evaluateResponse(token);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    90
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    91
        // Talk
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    92
        // 1. Client sends a auth-int message
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    93
        byte[] hello = "hello".getBytes();
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    94
        MessageProp qop = new MessageProp(0, false);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    95
        token = sc.wrap(hello, 0, hello.length, qop);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    96
        // 2. Server accepts it anyway
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    97
        ss.unwrap(token, 0, token.length);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    98
        // 3. Server sends a message
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
    99
        token = ss.wrap(hello, 0, hello.length);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   100
        // 4. Client accepts, should be auth-conf
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   101
        sc.unwrap(token, 0, token.length, qop);
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   102
        if (!qop.getPrivacy()) {
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   103
            throw new Exception();
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   104
        }
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   105
    }
6f556e154816 8012082: SASL: auth-conf negotiated, but unencrypted data is accepted, reset to unencrypt
weijun
parents:
diff changeset
   106
}