blob: 2f0498956958977aab6d1dbf1788b84a83469657 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any 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 java.util.Vector;
35import sun.security.util.*;
36import java.io.IOException;
37import java.math.BigInteger;
38
39/**
40 * Implements the ASN.1 KRB_KDC_REQ type.
41 *
42 * <xmp>
43 * KDC-REQ ::= SEQUENCE {
44 * -- NOTE: first tag is [1], not [0]
45 * pvno [1] INTEGER (5) ,
46 * msg-type [2] INTEGER (10 -- AS -- | 12 -- TGS --),
47 * padata [3] SEQUENCE OF PA-DATA OPTIONAL
48 * -- NOTE: not empty --,
49 * req-body [4] KDC-REQ-BODY
50 * }
51 * </xmp>
52 *
53 * <p>
54 * This definition reflects the Network Working Group RFC 4120
55 * specification available at
56 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
57 * http://www.ietf.org/rfc/rfc4120.txt</a>.
58 */
59
60public class KDCReq {
61 public KDCReqBody reqBody;
62
63 private int pvno;
64 private int msgType;
65 private PAData[] pAData = null; //optional
66
67 public KDCReq(PAData[] new_pAData, KDCReqBody new_reqBody,
68 int req_type) throws IOException {
69 pvno = Krb5.PVNO;
70 msgType = req_type;
71 if (new_pAData != null) {
72 pAData = new PAData[new_pAData.length];
73 for (int i = 0; i < new_pAData.length; i++) {
74 if (new_pAData[i] == null) {
75 throw new IOException("Cannot create a KDCRep");
76 } else {
77 pAData[i] = (PAData)new_pAData[i].clone();
78 }
79 }
80 }
81 reqBody = new_reqBody;
82 }
83
84 public KDCReq() {
85 }
86
87 public KDCReq(byte[] data, int req_type) throws Asn1Exception,
88 IOException, KrbException {
89 init(new DerValue(data), req_type);
90 }
91
92 /**
93 * Creates an KDCReq object from a DerValue object and asn1 type.
94 *
95 * @param der a DER value of an KDCReq object.
96 * @param req_type a encoded asn1 type value.
97 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
98 * @exception IOException if an I/O error occurs while reading encoded data.
99 * @exceptoin KrbErrException
100 */
101 public KDCReq(DerValue der, int req_type) throws Asn1Exception,
102 IOException, KrbException {
103 init(der, req_type);
104 }
105
106 /**
107 * Initializes a KDCReq object from a DerValue. The DER encoding
108 * must be in the format specified by the KRB_KDC_REQ ASN.1 notation.
109 *
110 * @param encoding a DER-encoded KDCReq object.
111 * @param req_type an int indicating whether it's KRB_AS_REQ or KRB_TGS_REQ type
112 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
113 * @exception IOException if an I/O error occurs while reading encoded data.
114 * @exception KrbException if an error occurs while constructing a Realm object,
115 * or a Krb object from DER-encoded data.
116 */
117 protected void init(DerValue encoding, int req_type) throws Asn1Exception,
118 IOException, KrbException {
119 DerValue der, subDer;
120 BigInteger bint;
121 if ((encoding.getTag() & 0x1F) != req_type) {
122 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
123 }
124 der = encoding.getData().getDerValue();
125 if (der.getTag() != DerValue.tag_Sequence) {
126 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
127 }
128 subDer = der.getData().getDerValue();
129 if ((subDer.getTag() & 0x01F) == 0x01) {
130 bint = subDer.getData().getBigInteger();
131 this.pvno = bint.intValue();
132 if (this.pvno != Krb5.PVNO)
133 throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
134 }
135 else
136 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
137 subDer = der.getData().getDerValue();
138 if ((subDer.getTag() & 0x01F) == 0x02) {
139 bint = subDer.getData().getBigInteger();
140 this.msgType = bint.intValue();
141 if (this.msgType != req_type)
142 throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
143 }
144 else
145 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
146 subDer = der.getData().getDerValue();
147 if ((subDer.getTag() & 0x01F) == 0x03) {
148 DerValue subsubDer = subDer.getData().getDerValue();
149 if (subsubDer.getTag() != DerValue.tag_SequenceOf) {
150 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
151 }
152 Vector<PAData> v = new Vector<PAData> ();
153 while (subsubDer.getData().available() > 0) {
154 v.addElement(new PAData(subsubDer.getData().getDerValue()));
155 }
156 if (v.size() > 0) {
157 pAData = new PAData[v.size()];
158 v.copyInto(pAData);
159 }
160 }
161 else pAData = null;
162 subDer = der.getData().getDerValue();
163 if ((subDer.getTag() & 0x01F) == 0x04) {
164 DerValue subsubDer = subDer.getData().getDerValue();
165 reqBody = new KDCReqBody(subsubDer, msgType);
166 }
167 else
168 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
169 }
170
171 /**
172 * Encodes this object to a byte array.
173 *
174 * @return an byte array of encoded data.
175 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
176 * @exception IOException if an I/O error occurs while reading encoded data.
177 *
178 */
179 public byte[] asn1Encode() throws Asn1Exception, IOException {
180 DerOutputStream temp, bytes, out;
181 temp = new DerOutputStream();
182 temp.putInteger(BigInteger.valueOf(pvno));
183 out = new DerOutputStream();
184 out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
185 temp = new DerOutputStream();
186 temp.putInteger(BigInteger.valueOf(msgType));
187 out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), temp);
188 if (pAData != null && pAData.length > 0) {
189 temp = new DerOutputStream();
190 for (int i = 0; i < pAData.length; i++) {
191 temp.write(pAData[i].asn1Encode());
192 }
193 bytes = new DerOutputStream();
194 bytes.write(DerValue.tag_SequenceOf, temp);
195 out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), bytes);
196 }
197 out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), reqBody.asn1Encode(msgType));
198 bytes = new DerOutputStream();
199 bytes.write(DerValue.tag_Sequence, out);
200 out = new DerOutputStream();
201 out.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)msgType), bytes);
202 return out.toByteArray();
203 }
204
205 public byte[] asn1EncodeReqBody() throws Asn1Exception, IOException
206 {
207 return reqBody.asn1Encode(msgType);
208 }
209
210}