Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/jgss/spnego/NotPreferredMech.java
38853 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804819426* @run main/othervm NotPreferredMech27* @summary GSSContext.acceptSecContext fails when a supported mech is not initiator preferred28*/2930import org.ietf.jgss.*;31import sun.security.jgss.*;32import sun.security.jgss.spnego.NegTokenInit;33import sun.security.jgss.spnego.NegTokenTarg;34import sun.security.util.BitArray;35import sun.security.util.DerOutputStream;36import sun.security.util.DerValue;37import sun.security.util.ObjectIdentifier;3839import java.io.ByteArrayOutputStream;40import java.lang.reflect.Constructor;41import java.lang.reflect.Method;4243public class NotPreferredMech {4445public static void main(String[] argv) throws Exception {4647// Generates a NegTokenInit mechTypes field, with an48// unsupported mech as the preferred.49DerOutputStream mech = new DerOutputStream();50mech.write(new Oid("1.2.3.4").getDER());51mech.write(GSSUtil.GSS_KRB5_MECH_OID.getDER());52DerOutputStream mechTypeList = new DerOutputStream();53mechTypeList.write(DerValue.tag_Sequence, mech);5455// Generates a NegTokenInit mechToken field for 1.2.3.4 mech56GSSHeader h1 = new GSSHeader(new ObjectIdentifier("1.2.3.4"), 1);57ByteArrayOutputStream bout = new ByteArrayOutputStream();58h1.encode(bout);59bout.write(new byte[1]);6061// Generates the NegTokenInit token62Constructor<NegTokenInit> ctor = NegTokenInit.class.getDeclaredConstructor(63byte[].class, BitArray.class, byte[].class, byte[].class);64ctor.setAccessible(true);65NegTokenInit initToken = ctor.newInstance(66mechTypeList.toByteArray(),67new BitArray(0),68bout.toByteArray(),69null);70Method m = Class.forName("sun.security.jgss.spnego.SpNegoToken")71.getDeclaredMethod("getEncoded");72m.setAccessible(true);73byte[] spnegoToken = (byte[])m.invoke(initToken);7475// and wraps it into a GSSToken76GSSHeader h = new GSSHeader(77new ObjectIdentifier(GSSUtil.GSS_SPNEGO_MECH_OID.toString()),78spnegoToken.length);79bout = new ByteArrayOutputStream();80h.encode(bout);81bout.write(spnegoToken);82byte[] token = bout.toByteArray();8384// and feeds it to a GSS acceptor85GSSManager man = GSSManager.getInstance();86GSSContext ctxt = man.createContext((GSSCredential) null);87token = ctxt.acceptSecContext(token, 0, token.length);88NegTokenTarg targ = new NegTokenTarg(token);8990// Make sure it's a GO-ON message91Method m2 = NegTokenTarg.class.getDeclaredMethod("getNegotiatedResult");92m2.setAccessible(true);93int negResult = (int)m2.invoke(targ);9495if (negResult != 1 /* ACCEPT_INCOMPLETE */) {96throw new Exception("Not a continue");97}98}99}100101102