Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/jgss/spnego/NegTokenTarg.java
38922 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 NegTokenTarg token35* as specified in RFC 247836*37* NegTokenTarg ::= SEQUENCE {38* negResult [0] ENUMERATED {39* accept_completed (0),40* accept_incomplete (1),41* reject (2) } OPTIONAL,42* supportedMech [1] MechType OPTIONAL,43* responseToken [2] OCTET STRING OPTIONAL,44* mechListMIC [3] OCTET STRING OPTIONAL45* }46*47* MechType::= OBJECT IDENTIFIER48*49*50* @author Seema Malkani51* @since 1.652*/5354public class NegTokenTarg extends SpNegoToken {5556private int negResult = 0;57private Oid supportedMech = null;58private byte[] responseToken = null;59private byte[] mechListMIC = null;6061NegTokenTarg(int result, Oid mech, byte[] token, byte[] mechListMIC)62{63super(NEG_TOKEN_TARG_ID);64this.negResult = result;65this.supportedMech = mech;66this.responseToken = token;67this.mechListMIC = mechListMIC;68}6970// Used by sun.security.jgss.wrapper.NativeGSSContext71// to parse SPNEGO tokens72public NegTokenTarg(byte[] in) throws GSSException {73super(NEG_TOKEN_TARG_ID);74parseToken(in);75}7677final byte[] encode() throws GSSException {78try {79// create negTargToken80DerOutputStream targToken = new DerOutputStream();8182// write the negotiated result with CONTEXT 0083DerOutputStream result = new DerOutputStream();84result.putEnumerated(negResult);85targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,86true, (byte) 0x00), result);8788// supportedMech with CONTEXT 0189if (supportedMech != null) {90DerOutputStream mech = new DerOutputStream();91byte[] mechType = supportedMech.getDER();92mech.write(mechType);93targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,94true, (byte) 0x01), mech);95}9697// response Token with CONTEXT 0298if (responseToken != null) {99DerOutputStream rspToken = new DerOutputStream();100rspToken.putOctetString(responseToken);101targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,102true, (byte) 0x02), rspToken);103}104105// mechListMIC with CONTEXT 03106if (mechListMIC != null) {107if (DEBUG) {108System.out.println("SpNegoToken NegTokenTarg: " +109"sending MechListMIC");110}111DerOutputStream mic = new DerOutputStream();112mic.putOctetString(mechListMIC);113targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,114true, (byte) 0x03), mic);115} else if (GSSUtil.useMSInterop()) {116// required for MS-interoperability117if (responseToken != null) {118if (DEBUG) {119System.out.println("SpNegoToken NegTokenTarg: " +120"sending additional token for MS Interop");121}122DerOutputStream rspToken = new DerOutputStream();123rspToken.putOctetString(responseToken);124targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,125true, (byte) 0x03), rspToken);126}127}128129// insert in a SEQUENCE130DerOutputStream out = new DerOutputStream();131out.write(DerValue.tag_Sequence, targToken);132133return out.toByteArray();134135} catch (IOException e) {136throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,137"Invalid SPNEGO NegTokenTarg token : " + e.getMessage());138}139}140141private void parseToken(byte[] in) throws GSSException {142try {143DerValue der = new DerValue(in);144// verify NegotiationToken type token145if (!der.isContextSpecific((byte) NEG_TOKEN_TARG_ID)) {146throw new IOException("SPNEGO NegoTokenTarg : " +147"did not have the right token type");148}149DerValue tmp1 = der.data.getDerValue();150if (tmp1.tag != DerValue.tag_Sequence) {151throw new IOException("SPNEGO NegoTokenTarg : " +152"did not have the Sequence tag");153}154155// parse various fields if present156int lastField = -1;157while (tmp1.data.available() > 0) {158DerValue tmp2 = tmp1.data.getDerValue();159if (tmp2.isContextSpecific((byte)0x00)) {160lastField = checkNextField(lastField, 0);161negResult = tmp2.data.getEnumerated();162if (DEBUG) {163System.out.println("SpNegoToken NegTokenTarg: negotiated" +164" result = " + getNegoResultString(negResult));165}166} else if (tmp2.isContextSpecific((byte)0x01)) {167lastField = checkNextField(lastField, 1);168ObjectIdentifier mech = tmp2.data.getOID();169supportedMech = new Oid(mech.toString());170if (DEBUG) {171System.out.println("SpNegoToken NegTokenTarg: " +172"supported mechanism = " + supportedMech);173}174} else if (tmp2.isContextSpecific((byte)0x02)) {175lastField = checkNextField(lastField, 2);176responseToken = tmp2.data.getOctetString();177} else if (tmp2.isContextSpecific((byte)0x03)) {178lastField = checkNextField(lastField, 3);179if (!GSSUtil.useMSInterop()) {180mechListMIC = tmp2.data.getOctetString();181if (DEBUG) {182System.out.println("SpNegoToken NegTokenTarg: " +183"MechListMIC Token = " +184getHexBytes(mechListMIC));185}186}187}188}189} catch (IOException e) {190throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,191"Invalid SPNEGO NegTokenTarg token : " + e.getMessage());192}193}194195int getNegotiatedResult() {196return negResult;197}198199// Used by sun.security.jgss.wrapper.NativeGSSContext200// to find the supported mech in SPNEGO tokens201public Oid getSupportedMech() {202return supportedMech;203}204205byte[] getResponseToken() {206return responseToken;207}208209byte[] getMechListMIC() {210return mechListMIC;211}212}213214215