Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/LastReq.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;3637/**38* Implements the ASN.1 LastReq type.39*40* <pre>{@code41* LastReq ::= SEQUENCE OF SEQUENCE {42* lr-type [0] Int32,43* lr-value [1] KerberosTime44* }45* }</pre>46*47* <p>48* This definition reflects the Network Working Group RFC 412049* specification available at50* <a href="http://www.ietf.org/rfc/rfc4120.txt">51* http://www.ietf.org/rfc/rfc4120.txt</a>.52*/5354public class LastReq {55private LastReqEntry[] entry = null;5657public LastReq(LastReqEntry[] entries) throws IOException {58if (entries != null) {59entry = new LastReqEntry[entries.length];60for (int i = 0; i < entries.length; i++) {61if (entries[i] == null) {62throw new IOException("Cannot create a LastReqEntry");63} else {64entry[i] = (LastReqEntry)entries[i].clone();65}66}67}6869}7071/**72* Constructs a LastReq object.73* @param encoding a Der-encoded data.74* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.75* @exception IOException if an I/O error occurs while reading encoded data.76*/7778public LastReq(DerValue encoding) throws Asn1Exception, IOException {79Vector<LastReqEntry> v= new Vector<>();80if (encoding.getTag() != DerValue.tag_Sequence) {81throw new Asn1Exception(Krb5.ASN1_BAD_ID);82}83while (encoding.getData().available() > 0) {84v.addElement(new LastReqEntry(encoding.getData().getDerValue()));85}86if (v.size() > 0) {87entry = new LastReqEntry[v.size()];88v.copyInto(entry);89}90}9192/**93* Encodes an LastReq object.94* @return the byte array of encoded LastReq object.95* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.96* @exception IOException if an I/O error occurs while reading encoded data.97*/98public byte[] asn1Encode() throws Asn1Exception, IOException {99DerOutputStream bytes = new DerOutputStream();100if (entry != null && entry.length > 0) {101DerOutputStream temp = new DerOutputStream();102for (int i = 0; i < entry.length; i++)103temp.write(entry[i].asn1Encode());104bytes.write(DerValue.tag_Sequence, temp);105return bytes.toByteArray();106}107return null;108}109110/**111* Parse (unmarshal) a last request from a DER input stream. This form112* parsing might be used when expanding a value which is part of113* a constructed sequence and uses explicitly tagged type.114*115* @exception Asn1Exception on error.116* @param data the Der input stream value, which contains one or more marshaled value.117* @param explicitTag tag number.118* @param optional indicates if this data field is optional119* @return an instance of LastReq.120*121*/122123public static LastReq parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {124if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag))125return null;126DerValue der = data.getDerValue();127if (explicitTag != (der.getTag() & (byte)0x1F)) {128throw new Asn1Exception(Krb5.ASN1_BAD_ID);129}130else {131DerValue subDer = der.getData().getDerValue();132return new LastReq(subDer);133}134}135136}137138139