jdk/test/sun/security/jgss/spnego/NotPreferredMech.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 30820 0d4717a011d3
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25664
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     1
/*
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     4
 *
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     8
 *
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    13
 * accompanied this code).
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    14
 *
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    18
 *
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    21
 * questions.
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    22
 */
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    23
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    24
/*
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    25
 * @test
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    26
 * @bug 8048194
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 25664
diff changeset
    27
 * @modules java.base/sun.security.util
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 25664
diff changeset
    28
 *          java.security.jgss/sun.security.jgss
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    29
 *          java.security.jgss/sun.security.jgss.spnego:+open
25664
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    30
 * @run main/othervm NotPreferredMech
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    31
 * @summary GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    32
 */
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    33
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    34
import org.ietf.jgss.*;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    35
import sun.security.jgss.*;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    36
import sun.security.jgss.spnego.NegTokenInit;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    37
import sun.security.jgss.spnego.NegTokenTarg;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    38
import sun.security.util.BitArray;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    39
import sun.security.util.DerOutputStream;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    40
import sun.security.util.DerValue;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    41
import sun.security.util.ObjectIdentifier;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    42
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    43
import java.io.ByteArrayOutputStream;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    44
import java.lang.reflect.Constructor;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    45
import java.lang.reflect.Method;
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    46
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    47
public class NotPreferredMech {
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    48
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    49
    public static void main(String[] argv) throws Exception {
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    50
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    51
        // Generates a NegTokenInit mechTypes field, with an
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    52
        // unsupported mech as the preferred.
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    53
        DerOutputStream mech = new DerOutputStream();
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    54
        mech.write(new Oid("1.2.3.4").getDER());
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    55
        mech.write(GSSUtil.GSS_KRB5_MECH_OID.getDER());
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    56
        DerOutputStream mechTypeList = new DerOutputStream();
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    57
        mechTypeList.write(DerValue.tag_Sequence, mech);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    58
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    59
        // Generates a NegTokenInit mechToken field for 1.2.3.4 mech
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    60
        GSSHeader h1 = new GSSHeader(new ObjectIdentifier("1.2.3.4"), 1);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    61
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    62
        h1.encode(bout);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    63
        bout.write(new byte[1]);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    64
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    65
        // Generates the NegTokenInit token
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    66
        Constructor<NegTokenInit> ctor = NegTokenInit.class.getDeclaredConstructor(
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    67
                byte[].class, BitArray.class, byte[].class, byte[].class);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    68
        ctor.setAccessible(true);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    69
        NegTokenInit initToken = ctor.newInstance(
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    70
                mechTypeList.toByteArray(),
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    71
                new BitArray(0),
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    72
                bout.toByteArray(),
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    73
                null);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    74
        Method m = Class.forName("sun.security.jgss.spnego.SpNegoToken")
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    75
                .getDeclaredMethod("getEncoded");
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    76
        m.setAccessible(true);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    77
        byte[] spnegoToken = (byte[])m.invoke(initToken);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    78
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    79
        // and wraps it into a GSSToken
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    80
        GSSHeader h = new GSSHeader(
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    81
                new ObjectIdentifier(GSSUtil.GSS_SPNEGO_MECH_OID.toString()),
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    82
                spnegoToken.length);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    83
        bout = new ByteArrayOutputStream();
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    84
        h.encode(bout);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    85
        bout.write(spnegoToken);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    86
        byte[] token = bout.toByteArray();
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    87
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    88
        // and feeds it to a GSS acceptor
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    89
        GSSManager man = GSSManager.getInstance();
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    90
        GSSContext ctxt = man.createContext((GSSCredential) null);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    91
        token = ctxt.acceptSecContext(token, 0, token.length);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    92
        NegTokenTarg targ = new NegTokenTarg(token);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    93
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    94
        // Make sure it's a GO-ON message
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    95
        Method m2 = NegTokenTarg.class.getDeclaredMethod("getNegotiatedResult");
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    96
        m2.setAccessible(true);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    97
        int negResult = (int)m2.invoke(targ);
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    98
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
    99
        if (negResult != 1 /* ACCEPT_INCOMPLETE */) {
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
   100
            throw new Exception("Not a continue");
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
   101
        }
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
   102
    }
66c6924fb620 8048194: GSSContext.acceptSecContext fails when a supported mech is not initiator preferred
weijun
parents:
diff changeset
   103
}