Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KRBSafe.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.Checksum;33import sun.security.krb5.Asn1Exception;34import sun.security.krb5.RealmException;35import sun.security.util.*;36import java.io.IOException;37import java.math.BigInteger;3839/**40* Implements the ASN.1 KRBSafe type.41*42* <pre>{@code43* KRB-SAFE ::= [APPLICATION 20] SEQUENCE {44* pvno [0] INTEGER (5),45* msg-type [1] INTEGER (20),46* safe-body [2] KRB-SAFE-BODY,47* cksum [3] Checksum48* }49* }</pre>50*51* <p>52* This definition reflects the Network Working Group RFC 412053* specifications available at54* <a href="http://www.ietf.org/rfc/rfc4120.txt">55* http://www.ietf.org/rfc/rfc4120.txt</a>.56*/5758public class KRBSafe {59public int pvno;60public int msgType;61public KRBSafeBody safeBody;62public Checksum cksum;6364public KRBSafe(KRBSafeBody new_safeBody, Checksum new_cksum) {65pvno = Krb5.PVNO;66msgType = Krb5.KRB_SAFE;67safeBody = new_safeBody;68cksum = new_cksum;69}7071public KRBSafe(byte[] data) throws Asn1Exception,72RealmException, KrbApErrException, IOException {73init(new DerValue(data));74}7576public KRBSafe(DerValue encoding) throws Asn1Exception,77RealmException, KrbApErrException, IOException {78init(encoding);79}8081/**82* Initializes an KRBSafe object.83* @param encoding a single DER-encoded value.84* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.85* @exception IOException if an I/O error occurs while reading encoded data.86* @exception RealmException if an error occurs while parsing a Realm object.87* @exception KrbApErrException if the value read from the DER-encoded data88* stream does not match the pre-defined value.89*/90private void init(DerValue encoding) throws Asn1Exception,91RealmException, KrbApErrException, IOException {92DerValue der, subDer;93if (((encoding.getTag() & (byte)0x1F) != (byte)0x14)94|| (encoding.isApplication() != true)95|| (encoding.isConstructed() != true))96throw new Asn1Exception(Krb5.ASN1_BAD_ID);97der = encoding.getData().getDerValue();98if (der.getTag() != DerValue.tag_Sequence)99throw new Asn1Exception(Krb5.ASN1_BAD_ID);100subDer = der.getData().getDerValue();101if ((subDer.getTag() & 0x1F) == 0x00) {102pvno = subDer.getData().getBigInteger().intValue();103if (pvno != Krb5.PVNO)104throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);105}106else107throw new Asn1Exception(Krb5.ASN1_BAD_ID);108subDer = der.getData().getDerValue();109if ((subDer.getTag() & 0x1F) == 0x01) {110msgType = subDer.getData().getBigInteger().intValue();111if (msgType != Krb5.KRB_SAFE)112throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);113}114115else116throw new Asn1Exception(Krb5.ASN1_BAD_ID);117safeBody = KRBSafeBody.parse(der.getData(), (byte)0x02, false);118cksum = Checksum.parse(der.getData(), (byte)0x03, false);119if (der.getData().available() > 0)120throw new Asn1Exception(Krb5.ASN1_BAD_ID);121}122123/**124* Encodes an KRBSafe object.125* @return byte array of encoded KRBSafe object.126* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.127* @exception IOException if an I/O error occurs while reading encoded data.128*/129public byte[] asn1Encode() throws Asn1Exception, IOException {130DerOutputStream temp = new DerOutputStream();131DerOutputStream bytes = new DerOutputStream();132temp.putInteger(BigInteger.valueOf(pvno));133bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);134temp = new DerOutputStream();135temp.putInteger(BigInteger.valueOf(msgType));136bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);137bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), safeBody.asn1Encode());138bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), cksum.asn1Encode());139temp = new DerOutputStream();140temp.write(DerValue.tag_Sequence, bytes);141bytes = new DerOutputStream();142bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x14), temp);143return bytes.toByteArray();144}145}146147148