Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/MethodData.java
41161 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25*26* (C) Copyright IBM Corp. 1999 All Rights Reserved.27* Copyright 1997 The Open Group Research Institute. All rights reserved.28*/2930package sun.security.krb5.internal;3132import sun.security.util.*;33import sun.security.krb5.Asn1Exception;34import java.io.IOException;35import java.math.BigInteger;3637/**38* Implements the ASN.1 EncKrbPrivPart type.39*40* <pre>{@code41* METHOD-DATA ::= SEQUENCE {42* method-type[0] INTEGER,43* method-data[1] OCTET STRING OPTIONAL44* }45* }</pre>46*/47public class MethodData {48private int methodType;49private byte[] methodData = null; //optional5051public MethodData(int type, byte[] data) {52methodType = type;53if (data != null) {54methodData = data.clone();55}56}5758/**59* Constructs a MethodData object.60* @param encoding a Der-encoded data.61* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.62* @exception IOException if an I/O error occurs while reading encoded data.63*/64public MethodData(DerValue encoding) throws Asn1Exception, IOException {65DerValue der;66if (encoding.getTag() != DerValue.tag_Sequence) {67throw new Asn1Exception(Krb5.ASN1_BAD_ID);68}69der = encoding.getData().getDerValue();70if ((der.getTag() & 0x1F) == 0x00) {71BigInteger bint = der.getData().getBigInteger();72methodType = bint.intValue();73}74else75throw new Asn1Exception(Krb5.ASN1_BAD_ID);76if (encoding.getData().available() > 0) {77der = encoding.getData().getDerValue();78if ((der.getTag() & 0x1F) == 0x01) {79methodData = der.getData().getOctetString();80}81else throw new Asn1Exception(Krb5.ASN1_BAD_ID);82}83if (encoding.getData().available() > 0)84throw new Asn1Exception(Krb5.ASN1_BAD_ID);85}8687/**88* Encodes an MethodData object.89* @return the byte array of encoded MethodData object.90* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.91* @exception IOException if an I/O error occurs while reading encoded data.92*/9394public byte[] asn1Encode() throws Asn1Exception, IOException {95DerOutputStream bytes = new DerOutputStream();96DerOutputStream temp = new DerOutputStream();97temp.putInteger(BigInteger.valueOf(methodType));98bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);99if (methodData != null) {100temp = new DerOutputStream();101temp.putOctetString(methodData);102bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);103}104105temp = new DerOutputStream();106temp.write(DerValue.tag_Sequence, bytes);107return temp.toByteArray();108}109110}111112113