Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/APRep.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.krb5.EncryptedData;33import sun.security.krb5.Asn1Exception;34import sun.security.util.*;35import java.io.IOException;36import java.math.BigInteger;3738/**39* Implements the ASN.1 AP-REP type.40*41* <pre>{@code42* AP-REP ::= [APPLICATION 15] SEQUENCE {43* pvno [0] INTEGER (5),44* msg-type [1] INTEGER (15),45* enc-part [2] EncryptedData -- EncAPRepPart46* }47* }</pre>48*49* <p>50* This definition reflects the Network Working Group RFC 412051* specification available at52* <a href="http://www.ietf.org/rfc/rfc4120.txt">53* http://www.ietf.org/rfc/rfc4120.txt</a>.54*/55public class APRep {5657public int pvno;58public int msgType;59public EncryptedData encPart;6061public APRep(EncryptedData new_encPart) {62pvno = Krb5.PVNO;63msgType = Krb5.KRB_AP_REP;64encPart = new_encPart;65}6667public APRep(byte[] data) throws Asn1Exception,68KrbApErrException, IOException {69init(new DerValue(data));70}7172public APRep(DerValue encoding) throws Asn1Exception,73KrbApErrException, IOException {74init(encoding);75}7677/**78* Initializes an APRep object.79* @param encoding a single DER-encoded value.80* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.81* @exception IOException if an I/O error occurs while reading encoded data.82* @exception KrbApErrException if the value read from the DER-encoded data83* stream does not match the pre-defined value.84*/85private void init(DerValue encoding) throws Asn1Exception,86KrbApErrException, IOException {8788if (((encoding.getTag() & (byte) (0x1F)) != Krb5.KRB_AP_REP)89|| (encoding.isApplication() != true)90|| (encoding.isConstructed() != true)) {91throw new Asn1Exception(Krb5.ASN1_BAD_ID);92}93DerValue der = encoding.getData().getDerValue();94if (der.getTag() != DerValue.tag_Sequence) {95throw new Asn1Exception(Krb5.ASN1_BAD_ID);96}97DerValue subDer = der.getData().getDerValue();98if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {99throw new Asn1Exception(Krb5.ASN1_BAD_ID);100}101pvno = subDer.getData().getBigInteger().intValue();102if (pvno != Krb5.PVNO) {103throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);104}105subDer = der.getData().getDerValue();106if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x01) {107throw new Asn1Exception(Krb5.ASN1_BAD_ID);108}109msgType = subDer.getData().getBigInteger().intValue();110if (msgType != Krb5.KRB_AP_REP) {111throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);112}113encPart = EncryptedData.parse(der.getData(), (byte) 0x02, false);114if (der.getData().available() > 0) {115throw new Asn1Exception(Krb5.ASN1_BAD_ID);116}117}118119/**120* Encodes an APRep object.121* @return byte array of encoded APRep object.122* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.123* @exception IOException if an I/O error occurs while reading encoded data.124*/125public byte[] asn1Encode() throws Asn1Exception, IOException {126DerOutputStream bytes = new DerOutputStream();127DerOutputStream temp = new DerOutputStream();128temp.putInteger(BigInteger.valueOf(pvno));129bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);130temp = new DerOutputStream();131temp.putInteger(BigInteger.valueOf(msgType));132bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);133bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), encPart.asn1Encode());134temp = new DerOutputStream();135temp.write(DerValue.tag_Sequence, bytes);136DerOutputStream aprep = new DerOutputStream();137aprep.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0F), temp);138return aprep.toByteArray();139}140}141142143