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