Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KRBPriv.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 KRB-PRIV type.40*41* <pre>{@code42* KRB-PRIV ::= [APPLICATION 21] SEQUENCE {43* pvno [0] INTEGER (5),44* msg-type [1] INTEGER (21),45* -- NOTE: there is no [2] tag46* enc-part [3] EncryptedData -- EncKrbPrivPart47* }48* }</pre>49*50* <p>51* This definition reflects the Network Working Group RFC 412052* specification available at53* <a href="http://www.ietf.org/rfc/rfc4120.txt">54* http://www.ietf.org/rfc/rfc4120.txt</a>.55*/5657public class KRBPriv {58public int pvno;59public int msgType;60public EncryptedData encPart;6162public KRBPriv(EncryptedData new_encPart) {63pvno = Krb5.PVNO;64msgType = Krb5.KRB_PRIV;65encPart = new_encPart;66}6768public KRBPriv(byte[] data) throws Asn1Exception,69KrbApErrException, IOException {70init(new DerValue(data));71}7273public KRBPriv(DerValue encoding) throws Asn1Exception,74KrbApErrException, IOException {75init(encoding);76}777879/**80* Initializes an KRBPriv object.81* @param encoding a single DER-encoded value.82* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.83* @exception IOException if an I/O error occurs while reading encoded data.84* @exception KrbApErrException if the value read from the DER-encoded data85* stream does not match the pre-defined value.86*/87private void init(DerValue encoding) throws Asn1Exception,88KrbApErrException, IOException {89DerValue der, subDer;90if (((encoding.getTag() & (byte)0x1F) != (byte)0x15)91|| (encoding.isApplication() != true)92|| (encoding.isConstructed() != true))93throw new Asn1Exception(Krb5.ASN1_BAD_ID);94der = encoding.getData().getDerValue();95if (der.getTag() != DerValue.tag_Sequence)96throw new Asn1Exception(Krb5.ASN1_BAD_ID);97subDer = der.getData().getDerValue();98if ((subDer.getTag() & 0x1F) == 0x00) {99pvno = subDer.getData().getBigInteger().intValue();100if (pvno != Krb5.PVNO) {101throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);102}103}104else105throw new Asn1Exception(Krb5.ASN1_BAD_ID);106subDer = der.getData().getDerValue();107if ((subDer.getTag() & 0x1F) == 0x01) {108msgType = subDer.getData().getBigInteger().intValue();109if (msgType != Krb5.KRB_PRIV)110throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);111}112else113throw new Asn1Exception(Krb5.ASN1_BAD_ID);114encPart = EncryptedData.parse(der.getData(), (byte)0x03, false);115if (der.getData().available() >0)116throw new Asn1Exception(Krb5.ASN1_BAD_ID);117}118119/**120* Encodes an KRBPriv object.121* @return byte array of encoded EncAPRepPart 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 temp, bytes;127temp = new DerOutputStream();128temp.putInteger(BigInteger.valueOf(pvno));129bytes = new DerOutputStream();130bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);131temp = new DerOutputStream();132temp.putInteger(BigInteger.valueOf(msgType));133bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);134bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), encPart.asn1Encode());135temp = new DerOutputStream();136temp.write(DerValue.tag_Sequence, bytes);137bytes = new DerOutputStream();138bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x15), temp);139return bytes.toByteArray();140}141142}143144145