Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/EncKrbPrivPart.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* (C) Copyright IBM Corp. 1999 All Rights Reserved.26* Copyright 1997 The Open Group Research Institute. All rights reserved.27*/2829package sun.security.krb5.internal;3031import sun.security.util.*;32import sun.security.krb5.Asn1Exception;33import java.util.Vector;34import java.io.IOException;35import java.math.BigInteger;3637/**38* Implements the ASN.1 EncKrbPrivPart type.39*40* <pre>{@code41* EncKrbPrivPart ::= [APPLICATION 28] SEQUENCE {42* user-data [0] OCTET STRING,43* timestamp [1] KerberosTime OPTIONAL,44* usec [2] Microseconds OPTIONAL,45* seq-number [3] UInt32 OPTIONAL,46* s-address [4] HostAddress -- sender's addr --,47* r-address [5] HostAddress OPTIONAL -- recip's addr48* }49* }</pre>50*51* <p>52* This definition reflects the Network Working Group RFC 412053* specification available at54* <a href="http://www.ietf.org/rfc/rfc4120.txt">55* http://www.ietf.org/rfc/rfc4120.txt</a>.56*/57public class EncKrbPrivPart {5859public byte[] userData = null;60public KerberosTime timestamp; //optional61public Integer usec; //optional62public Integer seqNumber; //optional63public HostAddress sAddress; //optional64public HostAddress rAddress; //optional6566public EncKrbPrivPart(67byte[] new_userData,68KerberosTime new_timestamp,69Integer new_usec,70Integer new_seqNumber,71HostAddress new_sAddress,72HostAddress new_rAddress) {73if (new_userData != null) {74userData = new_userData.clone();75}76timestamp = new_timestamp;77usec = new_usec;78seqNumber = new_seqNumber;79sAddress = new_sAddress;80rAddress = new_rAddress;81}8283public EncKrbPrivPart(byte[] data) throws Asn1Exception, IOException {84init(new DerValue(data));85}8687public EncKrbPrivPart(DerValue encoding) throws Asn1Exception, IOException {88init(encoding);89}9091/**92* Initializes an EncKrbPrivPart object.93* @param encoding a single DER-encoded value.94* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.95* @exception IOException if an I/O error occurs while reading encoded data.96*/97private void init(DerValue encoding) throws Asn1Exception, IOException {98DerValue der, subDer;99if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x1C)100|| (encoding.isApplication() != true)101|| (encoding.isConstructed() != true)) {102throw new Asn1Exception(Krb5.ASN1_BAD_ID);103}104der = encoding.getData().getDerValue();105if (der.getTag() != DerValue.tag_Sequence) {106throw new Asn1Exception(Krb5.ASN1_BAD_ID);107}108subDer = der.getData().getDerValue();109if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x00) {110userData = subDer.getData().getOctetString();111} else {112throw new Asn1Exception(Krb5.ASN1_BAD_ID);113}114timestamp = KerberosTime.parse(der.getData(), (byte) 0x01, true);115if ((der.getData().peekByte() & 0x1F) == 0x02) {116subDer = der.getData().getDerValue();117usec = subDer.getData().getBigInteger().intValue();118} else {119usec = null;120}121if ((der.getData().peekByte() & 0x1F) == 0x03) {122subDer = der.getData().getDerValue();123seqNumber = subDer.getData().getBigInteger().intValue();124} else {125seqNumber = null;126}127sAddress = HostAddress.parse(der.getData(), (byte) 0x04, false);128if (der.getData().available() > 0) {129rAddress = HostAddress.parse(der.getData(), (byte) 0x05, true);130}131if (der.getData().available() > 0) {132throw new Asn1Exception(Krb5.ASN1_BAD_ID);133}134}135136/**137* Encodes an EncKrbPrivPart object.138* @return byte array of encoded EncKrbPrivPart object.139* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.140* @exception IOException if an I/O error occurs while reading encoded data.141*/142public byte[] asn1Encode() throws Asn1Exception, IOException {143DerOutputStream temp = new DerOutputStream();144DerOutputStream bytes = new DerOutputStream();145146temp.putOctetString(userData);147bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);148if (timestamp != null) {149bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), timestamp.asn1Encode());150}151if (usec != null) {152temp = new DerOutputStream();153temp.putInteger(BigInteger.valueOf(usec.intValue()));154bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), temp);155}156if (seqNumber != null) {157temp = new DerOutputStream();158// encode as an unsigned integer (UInt32)159temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));160bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), temp);161}162bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), sAddress.asn1Encode());163if (rAddress != null) {164bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x05), rAddress.asn1Encode());165}166temp = new DerOutputStream();167temp.write(DerValue.tag_Sequence, bytes);168bytes = new DerOutputStream();169bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x1C), temp);170return bytes.toByteArray();171}172}173174175