Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KRBSafeBody.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.util.Vector;35import java.io.IOException;36import java.math.BigInteger;3738/**39* Implements the ASN.1 KRBSafeBody type.40*41* <pre>{@code42* KRB-SAFE-BODY ::= SEQUENCE {43* user-data [0] OCTET STRING,44* timestamp [1] KerberosTime OPTIONAL,45* usec [2] Microseconds OPTIONAL,46* seq-number [3] UInt32 OPTIONAL,47* s-address [4] HostAddress,48* r-address [5] HostAddress OPTIONAL49* }50* }</pre>51*52* <p>53* This definition reflects the Network Working Group RFC 412054* specification available at55* <a href="http://www.ietf.org/rfc/rfc4120.txt">56* http://www.ietf.org/rfc/rfc4120.txt</a>.57*/5859public class KRBSafeBody {60public byte[] userData = null;61public KerberosTime timestamp; //optional62public Integer usec; //optional63public Integer seqNumber; //optional64public HostAddress sAddress;65public HostAddress rAddress; //optional6667public KRBSafeBody(68byte[] new_userData,69KerberosTime new_timestamp,70Integer new_usec,71Integer new_seqNumber,72HostAddress new_sAddress,73HostAddress new_rAddress74) {75if (new_userData != null) {76userData = new_userData.clone();77}78timestamp = new_timestamp;79usec = new_usec;80seqNumber = new_seqNumber;81sAddress = new_sAddress;82rAddress = new_rAddress;83}848586/**87* Constructs a KRBSafeBody object.88* @param encoding a Der-encoded data.89* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.90* @exception IOException if an I/O error occurs while reading encoded data.91*/92public KRBSafeBody(DerValue encoding) throws Asn1Exception, IOException {93DerValue der;94if (encoding.getTag() != DerValue.tag_Sequence) {95throw new Asn1Exception(Krb5.ASN1_BAD_ID);96}97der = encoding.getData().getDerValue();98if ((der.getTag() & 0x1F) == 0x00) {99userData = der.getData().getOctetString();100}101else102throw new Asn1Exception(Krb5.ASN1_BAD_ID);103timestamp = KerberosTime.parse(encoding.getData(), (byte)0x01, true);104if ((encoding.getData().peekByte() & 0x1F) == 0x02) {105der = encoding.getData().getDerValue();106usec = der.getData().getBigInteger().intValue();107}108if ((encoding.getData().peekByte() & 0x1F) == 0x03) {109der = encoding.getData().getDerValue();110seqNumber = der.getData().getBigInteger().intValue();111}112sAddress = HostAddress.parse(encoding.getData(), (byte)0x04, false);113if (encoding.getData().available() > 0)114rAddress = HostAddress.parse(encoding.getData(), (byte)0x05, true);115if (encoding.getData().available() > 0)116throw new Asn1Exception(Krb5.ASN1_BAD_ID);117}118119/**120* Encodes an KRBSafeBody object.121* @return the byte array of encoded KRBSafeBody 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.putOctetString(userData);129bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);130if (timestamp != null)131bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), timestamp.asn1Encode());132if (usec != null) {133temp = new DerOutputStream();134temp.putInteger(BigInteger.valueOf(usec.intValue()));135bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), temp);136}137if (seqNumber != null) {138temp = new DerOutputStream();139// encode as an unsigned integer (UInt32)140temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));141bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp);142}143bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), sAddress.asn1Encode());144if (rAddress != null)145temp = new DerOutputStream();146temp.write(DerValue.tag_Sequence, bytes);147return temp.toByteArray();148}149150/**151* Parse (unmarshal) a KRBSafeBody from a DER input stream. This form152* parsing might be used when expanding a value which is part of153* a constructed sequence and uses explicitly tagged type.154*155* @exception Asn1Exception on error.156* @param data the Der input stream value, which contains one or more marshaled value.157* @param explicitTag tag number.158* @param optional indicates if this data field is optional159* @return an instance of KRBSafeBody.160*161*/162public static KRBSafeBody parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {163if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag))164return null;165DerValue der = data.getDerValue();166if (explicitTag != (der.getTag() & (byte)0x1F))167throw new Asn1Exception(Krb5.ASN1_BAD_ID);168else {169DerValue subDer = der.getData().getDerValue();170return new KRBSafeBody(subDer);171}172}173174175176}177178179