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
31package sun.security.krb5.internal;
32
33import sun.security.util.*;
34import sun.security.krb5.Asn1Exception;
35import java.io.IOException;
36import java.math.BigInteger;
37
38/**
39 * Implements the ASN.1 PAEncTSEnc type.
40 *
41 * <pre>{@code
42 * PA-ENC-TS-ENC                ::= SEQUENCE {
43 *      patimestamp     [0] KerberosTime -- client's time --,
44 *      pausec          [1] Microseconds OPTIONAL
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
55public class PAEncTSEnc {
56    public KerberosTime pATimeStamp;
57    public Integer pAUSec; //optional
58
59    public PAEncTSEnc(
60                      KerberosTime new_pATimeStamp,
61                      Integer new_pAUSec
62                          ) {
63        pATimeStamp = new_pATimeStamp;
64        pAUSec = new_pAUSec;
65    }
66
67    public PAEncTSEnc() {
68        KerberosTime now = KerberosTime.now();
69        pATimeStamp = now;
70        pAUSec = now.getMicroSeconds();
71    }
72
73    /**
74     * Constructs a PAEncTSEnc object.
75     * @param encoding a Der-encoded data.
76     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
77     * @exception IOException if an I/O error occurs while reading encoded data.
78     */
79    public PAEncTSEnc(DerValue encoding) throws Asn1Exception, IOException {
80        DerValue der;
81        if (encoding.getTag() != DerValue.tag_Sequence) {
82            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
83        }
84        pATimeStamp = KerberosTime.parse(encoding.getData(), (byte)0x00, false);
85        if (encoding.getData().available() > 0) {
86            der = encoding.getData().getDerValue();
87            if ((der.getTag() & 0x1F) == 0x01) {
88                pAUSec = der.getData().getBigInteger().intValue();
89            }
90            else throw new Asn1Exception(Krb5.ASN1_BAD_ID);
91        }
92        if (encoding.getData().available() > 0)
93            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
94    }
95
96
97    /**
98     * Encodes a PAEncTSEnc object.
99     * @return the byte array of encoded PAEncTSEnc object.
100     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
101     * @exception IOException if an I/O error occurs while reading encoded data.
102     */
103    public byte[] asn1Encode() throws Asn1Exception, IOException {
104        DerOutputStream bytes = new DerOutputStream();
105        DerOutputStream temp = new DerOutputStream();
106        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), pATimeStamp.asn1Encode());
107        if (pAUSec != null) {
108            temp = new DerOutputStream();
109            temp.putInteger(BigInteger.valueOf(pAUSec.intValue()));
110            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
111        }
112        temp = new DerOutputStream();
113        temp.write(DerValue.tag_Sequence, bytes);
114        return temp.toByteArray();
115    }
116}
117