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.krb5.*;
34import sun.security.util.*;
35import java.io.IOException;
36import java.math.BigInteger;
37
38/**
39 * Implements the ASN.1 AP-REQ type.
40 *
41 * <pre>{@code
42 * AP-REQ               ::= [APPLICATION 14] SEQUENCE {
43 *      pvno            [0] INTEGER (5),
44 *      msg-type        [1] INTEGER (14),
45 *      ap-options      [2] APOptions,
46 *      ticket          [3] Ticket,
47 *      authenticator   [4] EncryptedData -- Authenticator
48 * }
49 * }</pre>
50 *
51 * <p>
52 * This definition reflects the Network Working Group RFC 4120
53 * specification available at
54 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
55 * http://www.ietf.org/rfc/rfc4120.txt</a>.
56 */
57public class APReq {
58
59    public int pvno;
60    public int msgType;
61    public APOptions apOptions;
62    public Ticket ticket;
63    public EncryptedData authenticator;
64
65    public APReq(
66            APOptions new_apOptions,
67            Ticket new_ticket,
68            EncryptedData new_authenticator) {
69        pvno = Krb5.PVNO;
70        msgType = Krb5.KRB_AP_REQ;
71        apOptions = new_apOptions;
72        ticket = new_ticket;
73        authenticator = new_authenticator;
74    }
75
76    public APReq(byte[] data) throws Asn1Exception, IOException, KrbApErrException, RealmException {
77        init(new DerValue(data));
78    }
79
80    public APReq(DerValue encoding) throws Asn1Exception, IOException, KrbApErrException, RealmException {
81        init(encoding);
82    }
83
84    /**
85     * Initializes an APReq 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     * @exception KrbApErrException if the value read from the DER-encoded data stream does not match the pre-defined value.
90     * @exception RealmException if an error occurs while parsing a Realm object.
91     */
92    private void init(DerValue encoding) throws Asn1Exception,
93            IOException, KrbApErrException, RealmException {
94        DerValue der, subDer;
95        if (((encoding.getTag() & (byte) 0x1F) != Krb5.KRB_AP_REQ)
96                || (encoding.isApplication() != true)
97                || (encoding.isConstructed() != true)) {
98            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
99        }
100        der = encoding.getData().getDerValue();
101        if (der.getTag() != DerValue.tag_Sequence) {
102            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
103        }
104        subDer = der.getData().getDerValue();
105        if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {
106            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
107        }
108        pvno = subDer.getData().getBigInteger().intValue();
109        if (pvno != Krb5.PVNO) {
110            throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
111        }
112        subDer = der.getData().getDerValue();
113        if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x01) {
114            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
115        }
116        msgType = subDer.getData().getBigInteger().intValue();
117        if (msgType != Krb5.KRB_AP_REQ) {
118            throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
119        }
120        apOptions = APOptions.parse(der.getData(), (byte) 0x02, false);
121        ticket = Ticket.parse(der.getData(), (byte) 0x03, false);
122        authenticator = EncryptedData.parse(der.getData(), (byte) 0x04, false);
123        if (der.getData().available() > 0) {
124            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
125        }
126    }
127
128    /**
129     * Encodes an APReq object.
130     * @return byte array of encoded APReq 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     */
134    public byte[] asn1Encode() throws Asn1Exception, IOException {
135        DerOutputStream bytes = new DerOutputStream();
136        DerOutputStream temp = new DerOutputStream();
137        temp.putInteger(BigInteger.valueOf(pvno));
138        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
139        temp = new DerOutputStream();
140        temp.putInteger(BigInteger.valueOf(msgType));
141        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
142        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), apOptions.asn1Encode());
143        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), ticket.asn1Encode());
144        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), authenticator.asn1Encode());
145        temp = new DerOutputStream();
146        temp.write(DerValue.tag_Sequence, bytes);
147        DerOutputStream apreq = new DerOutputStream();
148        apreq.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0E), temp);
149        return apreq.toByteArray();
150    }
151}
152