1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  *
27  *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
28  *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
29  */
30 
31 package sun.security.krb5.internal;
32 
33 import sun.security.util.*;
34 import sun.security.krb5.Asn1Exception;
35 import java.util.Vector;
36 import java.io.IOException;
37 
38 /**
39  * Implements the ASN.1 LastReq type.
40  *
41  * <pre>{@code
42  * LastReq         ::=     SEQUENCE OF SEQUENCE {
43  *         lr-type         [0] Int32,
44  *         lr-value        [1] KerberosTime
45  * }
46  * }</pre>
47  *
48  * <p>
49  * This definition reflects the Network Working Group RFC 4120
50  * specification available at
51  * <a href="http://www.ietf.org/rfc/rfc4120.txt">
52  * http://www.ietf.org/rfc/rfc4120.txt</a>.
53  */
54 
55 public class LastReq {
56     private LastReqEntry[] entry = null;
57 
LastReq(LastReqEntry[] entries)58     public LastReq(LastReqEntry[] entries) throws IOException {
59         if (entries != null) {
60             entry = new LastReqEntry[entries.length];
61             for (int i = 0; i < entries.length; i++) {
62                 if (entries[i] == null) {
63                     throw new IOException("Cannot create a LastReqEntry");
64                 } else {
65                     entry[i] = (LastReqEntry)entries[i].clone();
66                 }
67             }
68         }
69 
70     }
71 
72     /**
73      * Constructs a LastReq object.
74      * @param encoding a Der-encoded data.
75      * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
76      * @exception IOException if an I/O error occurs while reading encoded data.
77      */
78 
LastReq(DerValue encoding)79     public LastReq(DerValue encoding) throws Asn1Exception, IOException {
80         Vector<LastReqEntry> v= new Vector<>();
81         if (encoding.getTag() != DerValue.tag_Sequence) {
82             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
83         }
84         while (encoding.getData().available() > 0) {
85             v.addElement(new LastReqEntry(encoding.getData().getDerValue()));
86         }
87         if (v.size() > 0) {
88             entry = new LastReqEntry[v.size()];
89             v.copyInto(entry);
90         }
91     }
92 
93     /**
94      * Encodes an LastReq object.
95      * @return the byte array of encoded LastReq object.
96      * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
97      * @exception IOException if an I/O error occurs while reading encoded data.
98      */
asn1Encode()99     public byte[] asn1Encode() throws Asn1Exception, IOException {
100         DerOutputStream bytes = new DerOutputStream();
101         if (entry != null && entry.length > 0) {
102             DerOutputStream temp = new DerOutputStream();
103             for (int i = 0; i < entry.length; i++)
104                 temp.write(entry[i].asn1Encode());
105             bytes.write(DerValue.tag_Sequence, temp);
106             return bytes.toByteArray();
107         }
108         return null;
109     }
110 
111     /**
112      * Parse (unmarshal) a last request from a DER input stream.  This form
113      * parsing might be used when expanding a value which is part of
114      * a constructed sequence and uses explicitly tagged type.
115      *
116      * @exception Asn1Exception on error.
117      * @param data the Der input stream value, which contains one or more marshaled value.
118      * @param explicitTag tag number.
119      * @param optional indicates if this data field is optional
120      * @return an instance of LastReq.
121      *
122      */
123 
parse(DerInputStream data, byte explicitTag, boolean optional)124     public static LastReq parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {
125         if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag))
126             return null;
127         DerValue der = data.getDerValue();
128         if (explicitTag != (der.getTag() & (byte)0x1F))  {
129             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
130         }
131         else {
132             DerValue subDer = der.getData().getDerValue();
133             return new LastReq(subDer);
134         }
135     }
136 
137 }
138