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 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
|
|
27 |
* Copyright 1997 The Open Group Research Institute. All rights reserved.
|
|
28 |
*/
|
|
29 |
|
|
30 |
package sun.security.krb5.internal;
|
|
31 |
|
|
32 |
import sun.security.krb5.*;
|
|
33 |
import sun.security.util.*;
|
|
34 |
import java.util.Vector;
|
|
35 |
import java.io.IOException;
|
|
36 |
import java.math.BigInteger;
|
73
|
37 |
|
2
|
38 |
/**
|
|
39 |
* Implements the ASN.1 Authenticator type.
|
|
40 |
*
|
|
41 |
* <xmp>
|
|
42 |
* Authenticator ::= [APPLICATION 2] SEQUENCE {
|
|
43 |
* authenticator-vno [0] INTEGER (5),
|
|
44 |
* crealm [1] Realm,
|
|
45 |
* cname [2] PrincipalName,
|
|
46 |
* cksum [3] Checksum OPTIONAL,
|
|
47 |
* cusec [4] Microseconds,
|
|
48 |
* ctime [5] KerberosTime,
|
|
49 |
* subkey [6] EncryptionKey OPTIONAL,
|
|
50 |
* seq-number [7] UInt32 OPTIONAL,
|
|
51 |
* authorization-data [8] AuthorizationData OPTIONAL
|
|
52 |
* }
|
|
53 |
* </xmp>
|
|
54 |
*
|
|
55 |
* <p>
|
|
56 |
* This definition reflects the Network Working Group RFC 4120
|
|
57 |
* specification available at
|
|
58 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
|
|
59 |
* http://www.ietf.org/rfc/rfc4120.txt</a>.
|
|
60 |
*/
|
|
61 |
public class Authenticator {
|
73
|
62 |
|
2
|
63 |
public int authenticator_vno;
|
|
64 |
public Realm crealm;
|
|
65 |
public PrincipalName cname;
|
|
66 |
Checksum cksum; //optional
|
|
67 |
public int cusec;
|
|
68 |
public KerberosTime ctime;
|
|
69 |
EncryptionKey subKey; //optional
|
|
70 |
Integer seqNumber; //optional
|
|
71 |
public AuthorizationData authorizationData; //optional
|
|
72 |
|
73
|
73 |
public Authenticator(
|
|
74 |
Realm new_crealm,
|
|
75 |
PrincipalName new_cname,
|
|
76 |
Checksum new_cksum,
|
|
77 |
int new_cusec,
|
|
78 |
KerberosTime new_ctime,
|
|
79 |
EncryptionKey new_subKey,
|
|
80 |
Integer new_seqNumber,
|
|
81 |
AuthorizationData new_authorizationData) {
|
|
82 |
authenticator_vno = Krb5.AUTHNETICATOR_VNO;
|
|
83 |
crealm = new_crealm;
|
|
84 |
cname = new_cname;
|
|
85 |
cksum = new_cksum;
|
|
86 |
cusec = new_cusec;
|
|
87 |
ctime = new_ctime;
|
|
88 |
subKey = new_subKey;
|
|
89 |
seqNumber = new_seqNumber;
|
|
90 |
authorizationData = new_authorizationData;
|
|
91 |
}
|
2
|
92 |
|
73
|
93 |
public Authenticator(byte[] data)
|
|
94 |
throws Asn1Exception, IOException, KrbApErrException, RealmException {
|
|
95 |
init(new DerValue(data));
|
|
96 |
}
|
2
|
97 |
|
73
|
98 |
public Authenticator(DerValue encoding)
|
|
99 |
throws Asn1Exception, IOException, KrbApErrException, RealmException {
|
|
100 |
init(encoding);
|
|
101 |
}
|
2
|
102 |
|
73
|
103 |
/**
|
|
104 |
* Initializes an Authenticator object.
|
|
105 |
* @param encoding a single DER-encoded value.
|
|
106 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
107 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
108 |
* @exception KrbApErrException if the value read from the DER-encoded data
|
|
109 |
* stream does not match the pre-defined value.
|
|
110 |
* @exception RealmException if an error occurs while parsing a Realm object.
|
|
111 |
*/
|
|
112 |
private void init(DerValue encoding)
|
|
113 |
throws Asn1Exception, IOException, KrbApErrException, RealmException {
|
|
114 |
DerValue der, subDer;
|
|
115 |
//may not be the correct error code for a tag
|
|
116 |
//mismatch on an encrypted structure
|
|
117 |
if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x02)
|
|
118 |
|| (encoding.isApplication() != true)
|
|
119 |
|| (encoding.isConstructed() != true)) {
|
|
120 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
121 |
}
|
|
122 |
der = encoding.getData().getDerValue();
|
|
123 |
if (der.getTag() != DerValue.tag_Sequence) {
|
|
124 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
125 |
}
|
|
126 |
subDer = der.getData().getDerValue();
|
|
127 |
if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {
|
2
|
128 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
73
|
129 |
}
|
2
|
130 |
authenticator_vno = subDer.getData().getBigInteger().intValue();
|
73
|
131 |
if (authenticator_vno != 5) {
|
2
|
132 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
|
73
|
133 |
}
|
|
134 |
crealm = Realm.parse(der.getData(), (byte) 0x01, false);
|
|
135 |
cname = PrincipalName.parse(der.getData(), (byte) 0x02, false);
|
|
136 |
cksum = Checksum.parse(der.getData(), (byte) 0x03, true);
|
|
137 |
subDer = der.getData().getDerValue();
|
|
138 |
if ((subDer.getTag() & (byte) 0x1F) == 0x04) {
|
|
139 |
cusec = subDer.getData().getBigInteger().intValue();
|
|
140 |
} else {
|
|
141 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
142 |
}
|
|
143 |
ctime = KerberosTime.parse(der.getData(), (byte) 0x05, false);
|
|
144 |
if (der.getData().available() > 0) {
|
|
145 |
subKey = EncryptionKey.parse(der.getData(), (byte) 0x06, true);
|
|
146 |
} else {
|
|
147 |
subKey = null;
|
|
148 |
seqNumber = null;
|
|
149 |
authorizationData = null;
|
|
150 |
}
|
|
151 |
if (der.getData().available() > 0) {
|
|
152 |
if ((der.getData().peekByte() & 0x1F) == 0x07) {
|
2
|
153 |
subDer = der.getData().getDerValue();
|
73
|
154 |
if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x07) {
|
|
155 |
seqNumber = new Integer(subDer.getData().getBigInteger().intValue());
|
2
|
156 |
}
|
73
|
157 |
}
|
|
158 |
} else {
|
|
159 |
seqNumber = null;
|
|
160 |
authorizationData = null;
|
2
|
161 |
}
|
73
|
162 |
if (der.getData().available() > 0) {
|
|
163 |
authorizationData = AuthorizationData.parse(der.getData(), (byte) 0x08, true);
|
|
164 |
} else {
|
|
165 |
authorizationData = null;
|
|
166 |
}
|
|
167 |
if (der.getData().available() > 0) {
|
|
168 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
169 |
}
|
|
170 |
}
|
2
|
171 |
|
73
|
172 |
/**
|
|
173 |
* Encodes an Authenticator object.
|
|
174 |
* @return byte array of encoded Authenticator object.
|
|
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 |
public byte[] asn1Encode() throws Asn1Exception, IOException {
|
|
179 |
Vector<DerValue> v = new Vector<DerValue>();
|
|
180 |
DerOutputStream temp = new DerOutputStream();
|
|
181 |
temp.putInteger(BigInteger.valueOf(authenticator_vno));
|
|
182 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp.toByteArray()));
|
|
183 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), crealm.asn1Encode()));
|
|
184 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), cname.asn1Encode()));
|
|
185 |
if (cksum != null) {
|
|
186 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), cksum.asn1Encode()));
|
2
|
187 |
}
|
73
|
188 |
temp = new DerOutputStream();
|
|
189 |
temp.putInteger(BigInteger.valueOf(cusec));
|
|
190 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), temp.toByteArray()));
|
|
191 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x05), ctime.asn1Encode()));
|
|
192 |
if (subKey != null) {
|
|
193 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x06), subKey.asn1Encode()));
|
|
194 |
}
|
|
195 |
if (seqNumber != null) {
|
|
196 |
temp = new DerOutputStream();
|
|
197 |
// encode as an unsigned integer (UInt32)
|
|
198 |
temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));
|
|
199 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x07), temp.toByteArray()));
|
|
200 |
}
|
|
201 |
if (authorizationData != null) {
|
|
202 |
v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x08), authorizationData.asn1Encode()));
|
|
203 |
}
|
|
204 |
DerValue der[] = new DerValue[v.size()];
|
|
205 |
v.copyInto(der);
|
|
206 |
temp = new DerOutputStream();
|
|
207 |
temp.putSequence(der);
|
|
208 |
DerOutputStream out = new DerOutputStream();
|
|
209 |
out.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x02), temp);
|
|
210 |
return out.toByteArray();
|
|
211 |
}
|
2
|
212 |
|
|
213 |
public final Checksum getChecksum() {
|
|
214 |
return cksum;
|
|
215 |
}
|
|
216 |
|
|
217 |
public final Integer getSeqNumber() {
|
|
218 |
return seqNumber;
|
|
219 |
}
|
|
220 |
|
|
221 |
public final EncryptionKey getSubKey() {
|
|
222 |
return subKey;
|
|
223 |
}
|
|
224 |
}
|