2
|
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. 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 |
|
|
31 |
package sun.security.krb5.internal;
|
|
32 |
|
|
33 |
import sun.security.krb5.*;
|
|
34 |
import sun.security.util.*;
|
|
35 |
import java.util.Vector;
|
|
36 |
import java.io.IOException;
|
|
37 |
import java.math.BigInteger;
|
73
|
38 |
|
2
|
39 |
/**
|
|
40 |
* Implements the ASN.1 KDC-REP type.
|
|
41 |
*
|
|
42 |
* <xmp>
|
|
43 |
* KDC-REP ::= SEQUENCE {
|
|
44 |
* pvno [0] INTEGER (5),
|
|
45 |
* msg-type [1] INTEGER (11 -- AS -- | 13 -- TGS --),
|
|
46 |
* padata [2] SEQUENCE OF PA-DATA OPTIONAL
|
|
47 |
* -- NOTE: not empty --,
|
|
48 |
* crealm [3] Realm,
|
|
49 |
* cname [4] PrincipalName,
|
|
50 |
* ticket [5] Ticket,
|
|
51 |
* enc-part [6] EncryptedData
|
|
52 |
* -- EncASRepPart or EncTGSRepPart,
|
|
53 |
* -- as appropriate
|
|
54 |
* }
|
|
55 |
* </xmp>
|
|
56 |
*
|
|
57 |
* <p>
|
|
58 |
* This definition reflects the Network Working Group RFC 4120
|
|
59 |
* specification available at
|
|
60 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
|
|
61 |
* http://www.ietf.org/rfc/rfc4120.txt</a>.
|
|
62 |
*/
|
73
|
63 |
public class KDCRep {
|
2
|
64 |
|
73
|
65 |
public Realm crealm;
|
|
66 |
public PrincipalName cname;
|
|
67 |
public Ticket ticket;
|
|
68 |
public EncryptedData encPart;
|
|
69 |
public EncKDCRepPart encKDCRepPart; //not part of ASN.1 encoding
|
|
70 |
private int pvno;
|
|
71 |
private int msgType;
|
|
72 |
private PAData[] pAData = null; //optional
|
|
73 |
private boolean DEBUG = Krb5.DEBUG;
|
2
|
74 |
|
73
|
75 |
public KDCRep(
|
|
76 |
PAData[] new_pAData,
|
|
77 |
Realm new_crealm,
|
|
78 |
PrincipalName new_cname,
|
|
79 |
Ticket new_ticket,
|
|
80 |
EncryptedData new_encPart,
|
|
81 |
int req_type) throws IOException {
|
|
82 |
pvno = Krb5.PVNO;
|
|
83 |
msgType = req_type;
|
|
84 |
if (new_pAData != null) {
|
|
85 |
pAData = new PAData[new_pAData.length];
|
|
86 |
for (int i = 0; i < new_pAData.length; i++) {
|
|
87 |
if (new_pAData[i] == null) {
|
|
88 |
throw new IOException("Cannot create a KDCRep");
|
|
89 |
} else {
|
|
90 |
pAData[i] = (PAData) new_pAData[i].clone();
|
2
|
91 |
}
|
73
|
92 |
}
|
2
|
93 |
}
|
73
|
94 |
crealm = new_crealm;
|
|
95 |
cname = new_cname;
|
|
96 |
ticket = new_ticket;
|
|
97 |
encPart = new_encPart;
|
|
98 |
}
|
2
|
99 |
|
73
|
100 |
public KDCRep() {
|
|
101 |
}
|
2
|
102 |
|
73
|
103 |
public KDCRep(byte[] data, int req_type) throws Asn1Exception,
|
|
104 |
KrbApErrException, RealmException, IOException {
|
|
105 |
init(new DerValue(data), req_type);
|
|
106 |
}
|
|
107 |
|
|
108 |
public KDCRep(DerValue encoding, int req_type) throws Asn1Exception,
|
|
109 |
RealmException, KrbApErrException, IOException {
|
|
110 |
init(encoding, req_type);
|
|
111 |
}
|
2
|
112 |
|
|
113 |
/*
|
|
114 |
// Not used? Don't know what keyusage to use here %%%
|
73
|
115 |
public void decrypt(EncryptionKey key) throws Asn1Exception,
|
|
116 |
IOException, KrbException, RealmException {
|
|
117 |
encKDCRepPart = new EncKDCRepPart(encPart.decrypt(key), msgType);
|
|
118 |
}
|
|
119 |
*/
|
|
120 |
/**
|
|
121 |
* Initializes an KDCRep object.
|
|
122 |
*
|
|
123 |
* @param encoding a single DER-encoded value.
|
|
124 |
* @param req_type reply message type.
|
|
125 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
126 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
127 |
* @exception RealmException if an error occurs while constructing
|
|
128 |
* a Realm object from DER-encoded data.
|
|
129 |
* @exception KrbApErrException if the value read from the DER-encoded
|
|
130 |
* data stream does not match the pre-defined value.
|
|
131 |
*
|
|
132 |
*/
|
|
133 |
protected void init(DerValue encoding, int req_type)
|
2
|
134 |
throws Asn1Exception, RealmException, IOException,
|
73
|
135 |
KrbApErrException {
|
|
136 |
DerValue der, subDer;
|
|
137 |
if ((encoding.getTag() & 0x1F) != req_type) {
|
|
138 |
if (DEBUG) {
|
|
139 |
System.out.println(">>> KDCRep: init() " +
|
|
140 |
"encoding tag is " +
|
|
141 |
encoding.getTag() +
|
|
142 |
" req type is " + req_type);
|
2
|
143 |
}
|
73
|
144 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
145 |
}
|
|
146 |
der = encoding.getData().getDerValue();
|
|
147 |
if (der.getTag() != DerValue.tag_Sequence) {
|
|
148 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
149 |
}
|
|
150 |
subDer = der.getData().getDerValue();
|
|
151 |
if ((subDer.getTag() & 0x1F) == 0x00) {
|
|
152 |
pvno = subDer.getData().getBigInteger().intValue();
|
|
153 |
if (pvno != Krb5.PVNO) {
|
|
154 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
|
2
|
155 |
}
|
73
|
156 |
} else {
|
|
157 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
158 |
}
|
|
159 |
subDer = der.getData().getDerValue();
|
|
160 |
if ((subDer.getTag() & 0x1F) == 0x01) {
|
|
161 |
msgType = subDer.getData().getBigInteger().intValue();
|
|
162 |
if (msgType != req_type) {
|
|
163 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
|
2
|
164 |
}
|
73
|
165 |
} else {
|
|
166 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
167 |
}
|
|
168 |
if ((der.getData().peekByte() & 0x1F) == 0x02) {
|
|
169 |
subDer = der.getData().getDerValue();
|
|
170 |
DerValue[] padata = subDer.getData().getSequence(1);
|
|
171 |
pAData = new PAData[padata.length];
|
|
172 |
for (int i = 0; i < padata.length; i++) {
|
|
173 |
pAData[i] = new PAData(padata[i]);
|
2
|
174 |
}
|
73
|
175 |
} else {
|
|
176 |
pAData = null;
|
|
177 |
}
|
|
178 |
crealm = Realm.parse(der.getData(), (byte) 0x03, false);
|
|
179 |
cname = PrincipalName.parse(der.getData(), (byte) 0x04, false);
|
|
180 |
ticket = Ticket.parse(der.getData(), (byte) 0x05, false);
|
|
181 |
encPart = EncryptedData.parse(der.getData(), (byte) 0x06, false);
|
|
182 |
if (der.getData().available() > 0) {
|
|
183 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
2
|
184 |
}
|
73
|
185 |
}
|
2
|
186 |
|
73
|
187 |
/**
|
|
188 |
* Encodes this object to a byte array.
|
|
189 |
* @return byte array of encoded APReq object.
|
|
190 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
191 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
192 |
*
|
|
193 |
*/
|
|
194 |
public byte[] asn1Encode() throws Asn1Exception, IOException {
|
2
|
195 |
|
73
|
196 |
DerOutputStream bytes = new DerOutputStream();
|
|
197 |
DerOutputStream temp = new DerOutputStream();
|
|
198 |
temp.putInteger(BigInteger.valueOf(pvno));
|
|
199 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
200 |
true, (byte) 0x00), temp);
|
|
201 |
temp = new DerOutputStream();
|
|
202 |
temp.putInteger(BigInteger.valueOf(msgType));
|
|
203 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
204 |
true, (byte) 0x01), temp);
|
|
205 |
if (pAData != null && pAData.length > 0) {
|
|
206 |
DerOutputStream padata_stream = new DerOutputStream();
|
|
207 |
for (int i = 0; i < pAData.length; i++) {
|
|
208 |
padata_stream.write(pAData[i].asn1Encode());
|
|
209 |
}
|
2
|
210 |
temp = new DerOutputStream();
|
73
|
211 |
temp.write(DerValue.tag_SequenceOf, padata_stream);
|
|
212 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
213 |
true, (byte) 0x02), temp);
|
2
|
214 |
}
|
73
|
215 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
216 |
true, (byte) 0x03), crealm.asn1Encode());
|
|
217 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
218 |
true, (byte) 0x04), cname.asn1Encode());
|
|
219 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
220 |
true, (byte) 0x05), ticket.asn1Encode());
|
|
221 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
222 |
true, (byte) 0x06), encPart.asn1Encode());
|
|
223 |
temp = new DerOutputStream();
|
|
224 |
temp.write(DerValue.tag_Sequence, bytes);
|
|
225 |
return temp.toByteArray();
|
|
226 |
}
|
2
|
227 |
}
|