Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/EncAPRepPart.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.*;33import sun.security.util.*;34import java.util.Vector;35import java.io.IOException;36import java.math.BigInteger;3738/**39* Implements the ASN.1 EncAPRepPart type.40*41* <pre>{@code42* EncAPRepPart ::= [APPLICATION 27] SEQUENCE {43* ctime [0] KerberosTime,44* cusec [1] Microseconds,45* subkey [2] EncryptionKey OPTIONAL,46* seq-number [3] UInt32 OPTIONAL47* }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*/56public class EncAPRepPart {5758public KerberosTime ctime;59public int cusec;60EncryptionKey subKey; //optional61Integer seqNumber; //optional6263public EncAPRepPart(64KerberosTime new_ctime,65int new_cusec,66EncryptionKey new_subKey,67Integer new_seqNumber) {68ctime = new_ctime;69cusec = new_cusec;70subKey = new_subKey;71seqNumber = new_seqNumber;72}7374public EncAPRepPart(byte[] data)75throws Asn1Exception, IOException {76init(new DerValue(data));77}7879public EncAPRepPart(DerValue encoding)80throws Asn1Exception, IOException {81init(encoding);82}8384/**85* Initializes an EncaPRepPart object.86* @param encoding a single DER-encoded value.87* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.88* @exception IOException if an I/O error occurs while reading encoded data.89*/90private void init(DerValue encoding) throws Asn1Exception, IOException {91DerValue der, subDer;92if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x1B)93|| (encoding.isApplication() != true)94|| (encoding.isConstructed() != true)) {95throw new Asn1Exception(Krb5.ASN1_BAD_ID);96}97der = encoding.getData().getDerValue();98if (der.getTag() != DerValue.tag_Sequence) {99throw new Asn1Exception(Krb5.ASN1_BAD_ID);100}101ctime = KerberosTime.parse(der.getData(), (byte) 0x00, true);102subDer = der.getData().getDerValue();103if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x01) {104cusec = subDer.getData().getBigInteger().intValue();105} else {106throw new Asn1Exception(Krb5.ASN1_BAD_ID);107}108if (der.getData().available() > 0) {109subKey = EncryptionKey.parse(der.getData(), (byte) 0x02, true);110} else {111subKey = null;112seqNumber = null;113}114if (der.getData().available() > 0) {115subDer = der.getData().getDerValue();116if ((subDer.getTag() & 0x1F) != 0x03) {117throw new Asn1Exception(Krb5.ASN1_BAD_ID);118}119seqNumber = subDer.getData().getBigInteger().intValue();120} else {121seqNumber = null;122}123if (der.getData().available() > 0) {124throw new Asn1Exception(Krb5.ASN1_BAD_ID);125}126}127128/**129* Encodes an EncAPRepPart object.130* @return byte array of encoded EncAPRepPart object.131* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.132* @exception IOException if an I/O error occurs while reading encoded data.133*/134public byte[] asn1Encode() throws Asn1Exception, IOException {135Vector<DerValue> v = new Vector<>();136DerOutputStream temp = new DerOutputStream();137v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,138true, (byte) 0x00), ctime.asn1Encode()));139temp.putInteger(BigInteger.valueOf(cusec));140v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,141true, (byte) 0x01), temp.toByteArray()));142if (subKey != null) {143v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,144true, (byte) 0x02), subKey.asn1Encode()));145}146if (seqNumber != null) {147temp = new DerOutputStream();148// encode as an unsigned integer (UInt32)149temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));150v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,151true, (byte) 0x03), temp.toByteArray()));152}153DerValue[] der = new DerValue[v.size()];154v.copyInto(der);155temp = new DerOutputStream();156temp.putSequence(der);157DerOutputStream out = new DerOutputStream();158out.write(DerValue.createTag(DerValue.TAG_APPLICATION,159true, (byte) 0x1B), temp);160return out.toByteArray();161}162163public final EncryptionKey getSubKey() {164return subKey;165}166167public final Integer getSeqNumber() {168return seqNumber;169}170}171172173