Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/krb5/MicToken.java
41161 views
/*1* Copyright (c) 2000, 2006, 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.krb5;2627import org.ietf.jgss.*;28import java.io.InputStream;29import java.io.OutputStream;30import java.io.IOException;31import java.io.ByteArrayInputStream;32import java.io.ByteArrayOutputStream;3334class MicToken extends MessageToken {3536public MicToken(Krb5Context context,37byte[] tokenBytes, int tokenOffset, int tokenLen,38MessageProp prop) throws GSSException {39super(Krb5Token.MIC_ID, context,40tokenBytes, tokenOffset, tokenLen, prop);41}4243public MicToken(Krb5Context context,44InputStream is, MessageProp prop)45throws GSSException {46super(Krb5Token.MIC_ID, context, is, prop);47}4849public void verify(byte[] data, int offset, int len) throws GSSException {50if (!verifySignAndSeqNumber(null, data, offset, len, null))51throw new GSSException(GSSException.BAD_MIC, -1,52"Corrupt checksum or sequence number in MIC token");53}5455public void verify(InputStream data) throws GSSException {56byte[] dataBytes = null;57try {58dataBytes = new byte[data.available()];59data.read(dataBytes);60} catch (IOException e) {61// Error reading application data62throw new GSSException(GSSException.BAD_MIC, -1,63"Corrupt checksum or sequence number in MIC token");64}65verify(dataBytes, 0, dataBytes.length);66}6768public MicToken(Krb5Context context, MessageProp prop,69byte[] data, int pos, int len)70throws GSSException {71super(Krb5Token.MIC_ID, context);7273// debug("Application data to MicToken verify is [" +74// getHexBytes(data, pos, len) + "]\n");75if (prop == null) prop = new MessageProp(0, false);76genSignAndSeqNumber(prop, null, data, pos, len, null);77}7879public MicToken(Krb5Context context, MessageProp prop,80InputStream data)81throws GSSException, IOException {82super(Krb5Token.MIC_ID, context);83byte[] dataBytes = new byte[data.available()];84data.read(dataBytes);8586//debug("Application data to MicToken cons is [" +87// getHexBytes(dataBytes) + "]\n");88if (prop == null) prop = new MessageProp(0, false);89genSignAndSeqNumber(prop, null, dataBytes, 0, dataBytes.length, null);90}9192protected int getSealAlg(boolean confRequested, int qop) {93return (SEAL_ALG_NONE);94}9596public int encode(byte[] outToken, int offset)97throws IOException, GSSException {98// Token is small99ByteArrayOutputStream bos = new ByteArrayOutputStream();100super.encode(bos);101byte[] token = bos.toByteArray();102System.arraycopy(token, 0, outToken, offset, token.length);103return token.length;104}105106public byte[] encode() throws IOException, GSSException{107// XXX Fine tune this initial size108ByteArrayOutputStream bos = new ByteArrayOutputStream(50);109encode(bos);110return bos.toByteArray();111}112113}114115116