author | weijun |
Wed, 03 Jul 2019 11:43:01 +0800 | |
changeset 55570 | 1e95931e7d8f |
parent 55258 | d65d3c37232c |
child 58679 | 9c3209ff7550 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
48651
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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; |
|
32 |
||
33 |
import java.util.Arrays; |
|
34 |
import sun.security.util.*; |
|
35 |
import sun.security.krb5.internal.*; |
|
36 |
import sun.security.krb5.internal.crypto.*; |
|
37 |
import java.io.IOException; |
|
38 |
import java.math.BigInteger; |
|
39 |
||
40 |
/** |
|
41 |
* This class encapsulates the concept of a Kerberos checksum. |
|
42 |
*/ |
|
43 |
public class Checksum { |
|
44 |
||
45 |
private int cksumType; |
|
46 |
private byte[] checksum; |
|
47 |
||
48 |
// ----------------------------------------------+-------------+----------- |
|
49 |
// Checksum type |sumtype |checksum |
|
50 |
// |value | size |
|
51 |
// ----------------------------------------------+-------------+----------- |
|
52 |
public static final int CKSUMTYPE_NULL = 0; // 0 |
|
53 |
public static final int CKSUMTYPE_CRC32 = 1; // 4 |
|
54 |
public static final int CKSUMTYPE_RSA_MD4 = 2; // 16 |
|
55 |
public static final int CKSUMTYPE_RSA_MD4_DES = 3; // 24 |
|
56 |
public static final int CKSUMTYPE_DES_MAC = 4; // 16 |
|
57 |
public static final int CKSUMTYPE_DES_MAC_K = 5; // 8 |
|
58 |
public static final int CKSUMTYPE_RSA_MD4_DES_K = 6; // 16 |
|
59 |
public static final int CKSUMTYPE_RSA_MD5 = 7; // 16 |
|
60 |
public static final int CKSUMTYPE_RSA_MD5_DES = 8; // 24 |
|
61 |
||
62 |
// draft-ietf-krb-wg-crypto-07.txt |
|
63 |
public static final int CKSUMTYPE_HMAC_SHA1_DES3_KD = 12; // 20 |
|
64 |
||
65 |
// draft-raeburn-krb-rijndael-krb-07.txt |
|
66 |
public static final int CKSUMTYPE_HMAC_SHA1_96_AES128 = 15; // 96 |
|
67 |
public static final int CKSUMTYPE_HMAC_SHA1_96_AES256 = 16; // 96 |
|
68 |
||
48651
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
47216
diff
changeset
|
69 |
// rfc8009 |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
47216
diff
changeset
|
70 |
public static final int CKSUMTYPE_HMAC_SHA256_128_AES128 = 19; // 96 |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
47216
diff
changeset
|
71 |
public static final int CKSUMTYPE_HMAC_SHA384_192_AES256 = 20; // 96 |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
47216
diff
changeset
|
72 |
|
2 | 73 |
// draft-brezak-win2k-krb-rc4-hmac-04.txt |
74 |
public static final int CKSUMTYPE_HMAC_MD5_ARCFOUR = -138; |
|
75 |
||
76 |
static int CKSUMTYPE_DEFAULT; |
|
77 |
static int SAFECKSUMTYPE_DEFAULT; |
|
78 |
||
79 |
private static boolean DEBUG = Krb5.DEBUG; |
|
80 |
static { |
|
14327 | 81 |
initStatic(); |
82 |
} |
|
83 |
||
84 |
public static void initStatic() { |
|
2 | 85 |
String temp = null; |
86 |
Config cfg = null; |
|
87 |
try { |
|
88 |
cfg = Config.getInstance(); |
|
14327 | 89 |
temp = cfg.get("libdefaults", "default_checksum"); |
2 | 90 |
if (temp != null) |
91 |
{ |
|
14327 | 92 |
CKSUMTYPE_DEFAULT = Config.getType(temp); |
2 | 93 |
} else { |
94 |
/* |
|
95 |
* If the default checksum is not |
|
96 |
* specified in the configuration we |
|
97 |
* set it to RSA_MD5. We follow the MIT and |
|
98 |
* SEAM implementation. |
|
99 |
*/ |
|
100 |
CKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5; |
|
101 |
} |
|
102 |
} catch (Exception exc) { |
|
103 |
if (DEBUG) { |
|
104 |
System.out.println("Exception in getting default checksum "+ |
|
105 |
"value from the configuration " + |
|
106 |
"Setting default checksum to be RSA-MD5"); |
|
107 |
exc.printStackTrace(); |
|
108 |
} |
|
109 |
CKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5; |
|
110 |
} |
|
111 |
||
112 |
||
113 |
try { |
|
14327 | 114 |
temp = cfg.get("libdefaults", "safe_checksum_type"); |
2 | 115 |
if (temp != null) |
116 |
{ |
|
14327 | 117 |
SAFECKSUMTYPE_DEFAULT = Config.getType(temp); |
2 | 118 |
} else { |
119 |
SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES; |
|
120 |
} |
|
121 |
} catch (Exception exc) { |
|
122 |
if (DEBUG) { |
|
123 |
System.out.println("Exception in getting safe default " + |
|
124 |
"checksum value " + |
|
125 |
"from the configuration Setting " + |
|
126 |
"safe default checksum to be RSA-MD5"); |
|
127 |
exc.printStackTrace(); |
|
128 |
} |
|
129 |
SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES; |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Constructs a new Checksum using the raw data and type. |
|
32003 | 135 |
* @param data the byte array of checksum. |
136 |
* @param new_cksumType the type of checksum. |
|
2 | 137 |
* |
138 |
*/ |
|
139 |
// used in InitialToken |
|
140 |
public Checksum(byte[] data, int new_cksumType) { |
|
141 |
cksumType = new_cksumType; |
|
142 |
checksum = data; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Constructs a new Checksum by calculating the checksum over the data |
|
147 |
* using specified checksum type. |
|
32003 | 148 |
* @param new_cksumType the type of checksum. |
149 |
* @param data the data that needs to be performed a checksum calculation on. |
|
2 | 150 |
*/ |
151 |
public Checksum(int new_cksumType, byte[] data) |
|
152 |
throws KdcErrException, KrbCryptoException { |
|
153 |
||
154 |
cksumType = new_cksumType; |
|
155 |
CksumType cksumEngine = CksumType.getInstance(cksumType); |
|
156 |
if (!cksumEngine.isSafe()) { |
|
157 |
checksum = cksumEngine.calculateChecksum(data, data.length); |
|
158 |
} else { |
|
159 |
throw new KdcErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Constructs a new Checksum by calculating the keyed checksum |
|
165 |
* over the data using specified checksum type. |
|
32003 | 166 |
* @param new_cksumType the type of checksum. |
167 |
* @param data the data that needs to be performed a checksum calculation on. |
|
2 | 168 |
*/ |
169 |
// KrbSafe, KrbTgsReq |
|
170 |
public Checksum(int new_cksumType, byte[] data, |
|
171 |
EncryptionKey key, int usage) |
|
172 |
throws KdcErrException, KrbApErrException, KrbCryptoException { |
|
173 |
cksumType = new_cksumType; |
|
174 |
CksumType cksumEngine = CksumType.getInstance(cksumType); |
|
175 |
if (!cksumEngine.isSafe()) |
|
176 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM); |
|
177 |
checksum = |
|
178 |
cksumEngine.calculateKeyedChecksum(data, |
|
179 |
data.length, |
|
180 |
key.getBytes(), |
|
181 |
usage); |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Verifies the keyed checksum over the data passed in. |
|
186 |
*/ |
|
187 |
public boolean verifyKeyedChecksum(byte[] data, EncryptionKey key, |
|
188 |
int usage) |
|
189 |
throws KdcErrException, KrbApErrException, KrbCryptoException { |
|
190 |
CksumType cksumEngine = CksumType.getInstance(cksumType); |
|
191 |
if (!cksumEngine.isSafe()) |
|
192 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM); |
|
193 |
return cksumEngine.verifyKeyedChecksum(data, |
|
194 |
data.length, |
|
195 |
key.getBytes(), |
|
196 |
checksum, |
|
197 |
usage); |
|
198 |
} |
|
199 |
||
55570
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
200 |
// =============== ATTENTION! Use with care ================== |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
201 |
// According to https://tools.ietf.org/html/rfc3961#section-6.1, |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
202 |
// An unkeyed checksum should only be used "in limited circumstances |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
203 |
// where the lack of a key does not provide a window for an attack, |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
204 |
// preferably as part of an encrypted message". |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
205 |
public boolean verifyAnyChecksum(byte[] data, EncryptionKey key, |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
206 |
int usage) |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
207 |
throws KdcErrException, KrbCryptoException { |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
208 |
CksumType cksumEngine = CksumType.getInstance(cksumType); |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
209 |
if (!cksumEngine.isSafe()) { |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
210 |
return cksumEngine.verifyChecksum(data, checksum); |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
211 |
} else { |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
212 |
return cksumEngine.verifyKeyedChecksum(data, |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
213 |
data.length, |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
214 |
key.getBytes(), |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
215 |
checksum, |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
216 |
usage); |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
217 |
} |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
218 |
} |
1e95931e7d8f
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents:
55258
diff
changeset
|
219 |
|
2 | 220 |
/* |
221 |
public Checksum(byte[] data) throws KdcErrException, KrbCryptoException { |
|
222 |
this(Checksum.CKSUMTYPE_DEFAULT, data); |
|
223 |
} |
|
224 |
*/ |
|
225 |
||
226 |
boolean isEqual(Checksum cksum) throws KdcErrException { |
|
227 |
if (cksumType != cksum.cksumType) |
|
228 |
return false; |
|
229 |
CksumType cksumEngine = CksumType.getInstance(cksumType); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
230 |
return CksumType.isChecksumEqual(checksum, cksum.checksum); |
2 | 231 |
} |
232 |
||
233 |
/** |
|
234 |
* Constructs an instance of Checksum from an ASN.1 encoded representation. |
|
235 |
* @param encoding a single DER-encoded value. |
|
236 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
237 |
* encoded data. |
|
238 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
239 |
* |
|
240 |
*/ |
|
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
48651
diff
changeset
|
241 |
public Checksum(DerValue encoding) throws Asn1Exception, IOException { |
2 | 242 |
DerValue der; |
243 |
if (encoding.getTag() != DerValue.tag_Sequence) { |
|
244 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
245 |
} |
|
246 |
der = encoding.getData().getDerValue(); |
|
247 |
if ((der.getTag() & (byte)0x1F) == (byte)0x00) { |
|
248 |
cksumType = der.getData().getBigInteger().intValue(); |
|
249 |
} |
|
250 |
else |
|
251 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
252 |
der = encoding.getData().getDerValue(); |
|
253 |
if ((der.getTag() & (byte)0x1F) == (byte)0x01) { |
|
254 |
checksum = der.getData().getOctetString(); |
|
255 |
} |
|
256 |
else |
|
257 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
258 |
if (encoding.getData().available() > 0) { |
|
259 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Encodes a Checksum object. |
|
32003 | 265 |
* <pre>{@code |
2 | 266 |
* Checksum ::= SEQUENCE { |
267 |
* cksumtype [0] Int32, |
|
268 |
* checksum [1] OCTET STRING |
|
269 |
* } |
|
32003 | 270 |
* }</pre> |
2 | 271 |
* |
272 |
* <p> |
|
273 |
* This definition reflects the Network Working Group RFC 4120 |
|
274 |
* specification available at |
|
275 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
|
276 |
* http://www.ietf.org/rfc/rfc4120.txt</a>. |
|
277 |
* @return byte array of enocded Checksum. |
|
278 |
* @exception Asn1Exception if an error occurs while decoding an |
|
279 |
* ASN1 encoded data. |
|
280 |
* @exception IOException if an I/O error occurs while reading |
|
281 |
* encoded data. |
|
282 |
* |
|
283 |
*/ |
|
284 |
public byte[] asn1Encode() throws Asn1Exception, IOException { |
|
285 |
DerOutputStream bytes = new DerOutputStream(); |
|
286 |
DerOutputStream temp = new DerOutputStream(); |
|
287 |
temp.putInteger(BigInteger.valueOf(cksumType)); |
|
288 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
289 |
true, (byte)0x00), temp); |
|
290 |
temp = new DerOutputStream(); |
|
291 |
temp.putOctetString(checksum); |
|
292 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
293 |
true, (byte)0x01), temp); |
|
294 |
temp = new DerOutputStream(); |
|
295 |
temp.write(DerValue.tag_Sequence, bytes); |
|
296 |
return temp.toByteArray(); |
|
297 |
} |
|
298 |
||
299 |
||
300 |
/** |
|
301 |
* Parse (unmarshal) a checksum object from a DER input stream. This form |
|
302 |
* parsing might be used when expanding a value which is part of |
|
303 |
* a constructed sequence and uses explicitly tagged type. |
|
304 |
* |
|
305 |
* @exception Asn1Exception if an error occurs while decoding an |
|
306 |
* ASN1 encoded data. |
|
307 |
* @exception IOException if an I/O error occurs while reading |
|
308 |
* encoded data. |
|
309 |
* @param data the Der input stream value, which contains one or more |
|
310 |
* marshaled value. |
|
311 |
* @param explicitTag tag number. |
|
312 |
* @param optional indicates if this data field is optional |
|
313 |
* @return an instance of Checksum. |
|
314 |
* |
|
315 |
*/ |
|
316 |
public static Checksum parse(DerInputStream data, |
|
317 |
byte explicitTag, boolean optional) |
|
318 |
throws Asn1Exception, IOException { |
|
319 |
||
320 |
if ((optional) && |
|
321 |
(((byte)data.peekByte() & (byte)0x1F) != explicitTag)) { |
|
322 |
return null; |
|
323 |
} |
|
324 |
DerValue der = data.getDerValue(); |
|
325 |
if (explicitTag != (der.getTag() & (byte)0x1F)) { |
|
326 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
327 |
} else { |
|
328 |
DerValue subDer = der.getData().getDerValue(); |
|
329 |
return new Checksum(subDer); |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Returns the raw bytes of the checksum, not in ASN.1 encoded form. |
|
335 |
*/ |
|
336 |
public final byte[] getBytes() { |
|
337 |
return checksum; |
|
338 |
} |
|
339 |
||
340 |
public final int getType() { |
|
341 |
return cksumType; |
|
342 |
} |
|
343 |
||
344 |
@Override public boolean equals(Object obj) { |
|
345 |
if (this == obj) { |
|
346 |
return true; |
|
347 |
} |
|
348 |
if (!(obj instanceof Checksum)) { |
|
349 |
return false; |
|
350 |
} |
|
351 |
||
352 |
try { |
|
353 |
return isEqual((Checksum)obj); |
|
354 |
} catch (KdcErrException kee) { |
|
355 |
return false; |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
@Override public int hashCode() { |
|
360 |
int result = 17; |
|
361 |
result = 37 * result + cksumType; |
|
362 |
if (checksum != null) { |
|
363 |
result = 37 * result + Arrays.hashCode(checksum); |
|
364 |
} |
|
365 |
return result; |
|
366 |
} |
|
367 |
} |