Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/spnego/NegTokenInit.java
41161 views
/*1* Copyright (c) 2005, 2011, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.jgss.spnego;2627import java.io.*;28import java.util.*;29import org.ietf.jgss.*;30import sun.security.jgss.*;31import sun.security.util.*;3233/**34* Implements the SPNEGO NegTokenInit token35* as specified in RFC 247836*37* NegTokenInit ::= SEQUENCE {38* mechTypes [0] MechTypeList OPTIONAL,39* reqFlags [1] ContextFlags OPTIONAL,40* mechToken [2] OCTET STRING OPTIONAL,41* mechListMIC [3] OCTET STRING OPTIONAL42* }43*44* MechTypeList ::= SEQUENCE OF MechType45*46* MechType::= OBJECT IDENTIFIER47*48* ContextFlags ::= BIT STRING {49* delegFlag (0),50* mutualFlag (1),51* replayFlag (2),52* sequenceFlag (3),53* anonFlag (4),54* confFlag (5),55* integFlag (6)56* }57*58* @author Seema Malkani59* @since 1.660*/6162public class NegTokenInit extends SpNegoToken {6364// DER-encoded mechTypes65private byte[] mechTypes = null;66private Oid[] mechTypeList = null;6768private BitArray reqFlags = null;69private byte[] mechToken = null;70private byte[] mechListMIC = null;7172NegTokenInit(byte[] mechTypes, BitArray flags,73byte[] token, byte[] mechListMIC)74{75super(NEG_TOKEN_INIT_ID);76this.mechTypes = mechTypes;77this.reqFlags = flags;78this.mechToken = token;79this.mechListMIC = mechListMIC;80}8182// Used by sun.security.jgss.wrapper.NativeGSSContext83// to parse SPNEGO tokens84public NegTokenInit(byte[] in) throws GSSException {85super(NEG_TOKEN_INIT_ID);86parseToken(in);87}8889final byte[] encode() throws GSSException {90try {91// create negInitToken92DerOutputStream initToken = new DerOutputStream();9394// DER-encoded mechTypes with CONTEXT 0095if (mechTypes != null) {96initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,97true, (byte) 0x00), mechTypes);98}99100// write context flags with CONTEXT 01101if (reqFlags != null) {102DerOutputStream flags = new DerOutputStream();103flags.putUnalignedBitString(reqFlags);104initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,105true, (byte) 0x01), flags);106}107108// mechToken with CONTEXT 02109if (mechToken != null) {110DerOutputStream dataValue = new DerOutputStream();111dataValue.putOctetString(mechToken);112initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,113true, (byte) 0x02), dataValue);114}115116// mechListMIC with CONTEXT 03117if (mechListMIC != null) {118if (DEBUG) {119System.out.println("SpNegoToken NegTokenInit: " +120"sending MechListMIC");121}122DerOutputStream mic = new DerOutputStream();123mic.putOctetString(mechListMIC);124initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,125true, (byte) 0x03), mic);126}127128// insert in a SEQUENCE129DerOutputStream out = new DerOutputStream();130out.write(DerValue.tag_Sequence, initToken);131132return out.toByteArray();133134} catch (IOException e) {135throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,136"Invalid SPNEGO NegTokenInit token : " + e.getMessage());137}138}139140private void parseToken(byte[] in) throws GSSException {141try {142DerValue der = new DerValue(in);143// verify NegotiationToken type token144if (!der.isContextSpecific((byte) NEG_TOKEN_INIT_ID)) {145throw new IOException("SPNEGO NegoTokenInit : " +146"did not have right token type");147}148DerValue tmp1 = der.data.getDerValue();149if (tmp1.tag != DerValue.tag_Sequence) {150throw new IOException("SPNEGO NegoTokenInit : " +151"did not have the Sequence tag");152}153154// parse various fields if present155int lastField = -1;156while (tmp1.data.available() > 0) {157DerValue tmp2 = tmp1.data.getDerValue();158if (tmp2.isContextSpecific((byte)0x00)) {159// get the DER-encoded sequence of mechTypes160lastField = checkNextField(lastField, 0);161DerInputStream mValue = tmp2.data;162mechTypes = mValue.toByteArray();163164// read all the mechTypes165DerValue[] mList = mValue.getSequence(0);166mechTypeList = new Oid[mList.length];167ObjectIdentifier mech = null;168for (int i = 0; i < mList.length; i++) {169mech = mList[i].getOID();170if (DEBUG) {171System.out.println("SpNegoToken NegTokenInit: " +172"reading Mechanism Oid = " + mech);173}174mechTypeList[i] = new Oid(mech.toString());175}176} else if (tmp2.isContextSpecific((byte)0x01)) {177lastField = checkNextField(lastField, 1);178// received reqFlags, skip it179} else if (tmp2.isContextSpecific((byte)0x02)) {180lastField = checkNextField(lastField, 2);181if (DEBUG) {182System.out.println("SpNegoToken NegTokenInit: " +183"reading Mech Token");184}185mechToken = tmp2.data.getOctetString();186} else if (tmp2.isContextSpecific((byte)0x03)) {187lastField = checkNextField(lastField, 3);188if (!GSSUtil.useMSInterop()) {189mechListMIC = tmp2.data.getOctetString();190if (DEBUG) {191System.out.println("SpNegoToken NegTokenInit: " +192"MechListMIC Token = " +193getHexBytes(mechListMIC));194}195}196}197}198} catch (IOException e) {199throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,200"Invalid SPNEGO NegTokenInit token : " + e.getMessage());201}202}203204byte[] getMechTypes() {205return mechTypes;206}207208// Used by sun.security.jgss.wrapper.NativeGSSContext209// to find the mechs in SPNEGO tokens210public Oid[] getMechTypeList() {211return mechTypeList;212}213214BitArray getReqFlags() {215return reqFlags;216}217218// Used by sun.security.jgss.wrapper.NativeGSSContext219// to access the mech token portion of SPNEGO tokens220public byte[] getMechToken() {221return mechToken;222}223224byte[] getMechListMIC() {225return mechListMIC;226}227228}229230231