author | weijun |
Thu, 24 Jun 2010 14:26:35 +0800 | |
changeset 5975 | 076cd013e5e4 |
parent 5802 | ea99d72d3c19 |
child 7183 | d8ccc1c73358 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2010, 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 |
* |
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
29 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
30 |
*/ |
|
31 |
||
32 |
package sun.security.krb5; |
|
33 |
||
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.security.GeneralSecurityException; |
|
39 |
import java.util.Arrays; |
|
40 |
import sun.security.krb5.internal.ktab.KeyTab; |
|
41 |
import sun.security.krb5.internal.ccache.CCacheOutputStream; |
|
42 |
import javax.crypto.spec.DESKeySpec; |
|
43 |
import javax.crypto.spec.DESedeKeySpec; |
|
44 |
||
45 |
/** |
|
46 |
* This class encapsulates the concept of an EncryptionKey. An encryption |
|
47 |
* key is defined in RFC 4120 as: |
|
48 |
* |
|
49 |
* EncryptionKey ::= SEQUENCE { |
|
50 |
* keytype [0] Int32 -- actually encryption type --, |
|
51 |
* keyvalue [1] OCTET STRING |
|
52 |
* } |
|
53 |
* |
|
54 |
* keytype |
|
55 |
* This field specifies the encryption type of the encryption key |
|
56 |
* that follows in the keyvalue field. Although its name is |
|
57 |
* "keytype", it actually specifies an encryption type. Previously, |
|
58 |
* multiple cryptosystems that performed encryption differently but |
|
59 |
* were capable of using keys with the same characteristics were |
|
60 |
* permitted to share an assigned number to designate the type of |
|
61 |
* key; this usage is now deprecated. |
|
62 |
* |
|
63 |
* keyvalue |
|
64 |
* This field contains the key itself, encoded as an octet string. |
|
65 |
*/ |
|
66 |
||
67 |
public class EncryptionKey |
|
68 |
implements Cloneable { |
|
69 |
||
70 |
public static final EncryptionKey NULL_KEY = |
|
71 |
new EncryptionKey(new byte[] {}, EncryptedData.ETYPE_NULL, null); |
|
72 |
||
73 |
private int keyType; |
|
74 |
private byte[] keyValue; |
|
75 |
private Integer kvno; // not part of ASN1 encoding; |
|
76 |
||
77 |
private static final boolean DEBUG = Krb5.DEBUG; |
|
78 |
||
79 |
public synchronized int getEType() { |
|
80 |
return keyType; |
|
81 |
} |
|
82 |
||
83 |
public final Integer getKeyVersionNumber() { |
|
84 |
return kvno; |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Returns the raw key bytes, not in any ASN.1 encoding. |
|
89 |
*/ |
|
90 |
public final byte[] getBytes() { |
|
91 |
// This method cannot be called outside sun.security, hence no |
|
92 |
// cloning. getEncoded() calls this method. |
|
93 |
return keyValue; |
|
94 |
} |
|
95 |
||
96 |
public synchronized Object clone() { |
|
97 |
return new EncryptionKey(keyValue, keyType, kvno); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Obtains the latest version of the secret key of |
|
102 |
* the principal from a keytab. |
|
103 |
* |
|
104 |
* @param princ the principal whose secret key is desired |
|
105 |
* @param keytab the path to the keytab file. A value of null |
|
106 |
* will be accepted to indicate that the default path should be |
|
107 |
* searched. |
|
108 |
* @returns the secret key or null if none was found. |
|
109 |
*/ |
|
110 |
/* |
|
111 |
// Replaced by acquireSecretKeys |
|
112 |
public static EncryptionKey acquireSecretKey(PrincipalName princ, |
|
113 |
String keytab) |
|
114 |
throws KrbException, IOException { |
|
115 |
||
116 |
if (princ == null) { |
|
117 |
throw new IllegalArgumentException( |
|
118 |
"Cannot have null pricipal name to look in keytab."); |
|
119 |
} |
|
120 |
||
121 |
KeyTab ktab = KeyTab.getInstance(keytab); |
|
122 |
||
123 |
if (ktab == null) |
|
124 |
return null; |
|
125 |
||
126 |
return ktab.readServiceKey(princ); |
|
127 |
} |
|
128 |
*/ |
|
129 |
||
130 |
/** |
|
131 |
* Obtains all versions of the secret key of the principal from a |
|
132 |
* keytab. |
|
133 |
* |
|
134 |
* @Param princ the principal whose secret key is desired |
|
135 |
* @param keytab the path to the keytab file. A value of null |
|
136 |
* will be accepted to indicate that the default path should be |
|
137 |
* searched. |
|
138 |
* @returns an array of secret keys or null if none were found. |
|
139 |
*/ |
|
140 |
public static EncryptionKey[] acquireSecretKeys(PrincipalName princ, |
|
141 |
String keytab) |
|
142 |
throws KrbException, IOException { |
|
143 |
||
144 |
if (princ == null) |
|
145 |
throw new IllegalArgumentException( |
|
146 |
"Cannot have null pricipal name to look in keytab."); |
|
147 |
||
148 |
// KeyTab getInstance(keytab) will call KeyTab.getInstance() |
|
149 |
// if keytab is null |
|
150 |
KeyTab ktab = KeyTab.getInstance(keytab); |
|
151 |
||
152 |
if (ktab == null) { |
|
153 |
return null; |
|
154 |
} |
|
155 |
||
156 |
return ktab.readServiceKeys(princ); |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Generate a list of keys using the given principal and password. |
|
161 |
* Construct a key for each configured etype. |
|
162 |
* Caller is responsible for clearing password. |
|
163 |
*/ |
|
164 |
/* |
|
165 |
* Usually, when keyType is decoded from ASN.1 it will contain a |
|
166 |
* value indicating what the algorithm to be used is. However, when |
|
167 |
* converting from a password to a key for the AS-EXCHANGE, this |
|
168 |
* keyType will not be available. Use builtin list of default etypes |
|
169 |
* as the default in that case. If default_tkt_enctypes was set in |
|
170 |
* the libdefaults of krb5.conf, then use that sequence. |
|
171 |
*/ |
|
172 |
// Used in Krb5LoginModule |
|
173 |
public static EncryptionKey[] acquireSecretKeys(char[] password, |
|
174 |
String salt) throws KrbException { |
|
175 |
return (acquireSecretKeys(password, salt, false, 0, null)); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Generates a list of keys using the given principal, password, |
|
180 |
* and the pre-authentication values. |
|
181 |
*/ |
|
182 |
public static EncryptionKey[] acquireSecretKeys(char[] password, |
|
183 |
String salt, boolean pa_exists, int pa_etype, byte[] pa_s2kparams) |
|
184 |
throws KrbException { |
|
185 |
||
186 |
int[] etypes = EType.getDefaults("default_tkt_enctypes"); |
|
187 |
if (etypes == null) { |
|
188 |
etypes = EType.getBuiltInDefaults(); |
|
189 |
} |
|
190 |
||
191 |
EncryptionKey[] encKeys = new EncryptionKey[etypes.length]; |
|
192 |
for (int i = 0; i < etypes.length; i++) { |
|
193 |
if (EType.isSupported(etypes[i])) { |
|
5802
ea99d72d3c19
6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents:
5774
diff
changeset
|
194 |
byte[] s2kparams = (pa_exists && etypes[i] == pa_etype) |
ea99d72d3c19
6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents:
5774
diff
changeset
|
195 |
? pa_s2kparams : null; |
2 | 196 |
encKeys[i] = new EncryptionKey( |
5802
ea99d72d3c19
6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents:
5774
diff
changeset
|
197 |
stringToKey(password, salt, s2kparams, etypes[i]), |
2 | 198 |
etypes[i], null); |
199 |
} else { |
|
200 |
if (DEBUG) { |
|
201 |
System.out.println("Encryption Type " + |
|
202 |
EType.toString(etypes[i]) + |
|
203 |
" is not supported/enabled"); |
|
204 |
} |
|
205 |
} |
|
206 |
} |
|
207 |
return encKeys; |
|
208 |
} |
|
209 |
||
210 |
// Used in Krb5AcceptCredential, self |
|
211 |
public EncryptionKey(byte[] keyValue, |
|
212 |
int keyType, |
|
213 |
Integer kvno) { |
|
214 |
||
215 |
if (keyValue != null) { |
|
216 |
this.keyValue = new byte[keyValue.length]; |
|
217 |
System.arraycopy(keyValue, 0, this.keyValue, 0, keyValue.length); |
|
218 |
} else { |
|
219 |
throw new IllegalArgumentException("EncryptionKey: " + |
|
220 |
"Key bytes cannot be null!"); |
|
221 |
} |
|
222 |
this.keyType = keyType; |
|
223 |
this.kvno = kvno; |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Constructs an EncryptionKey by using the specified key type and key |
|
228 |
* value. It is used to recover the key when retrieving data from |
|
229 |
* credential cache file. |
|
230 |
* |
|
231 |
*/ |
|
232 |
// Used in JSSE (KerberosWrapper), Credentials, |
|
233 |
// javax.security.auth.kerberos.KeyImpl |
|
234 |
public EncryptionKey(int keyType, |
|
235 |
byte[] keyValue) { |
|
236 |
this(keyValue, keyType, null); |
|
237 |
} |
|
238 |
||
239 |
private static byte[] stringToKey(char[] password, String salt, |
|
240 |
byte[] s2kparams, int keyType) throws KrbCryptoException { |
|
241 |
||
242 |
char[] slt = salt.toCharArray(); |
|
243 |
char[] pwsalt = new char[password.length + slt.length]; |
|
244 |
System.arraycopy(password, 0, pwsalt, 0, password.length); |
|
245 |
System.arraycopy(slt, 0, pwsalt, password.length, slt.length); |
|
246 |
Arrays.fill(slt, '0'); |
|
247 |
||
248 |
try { |
|
249 |
switch (keyType) { |
|
250 |
case EncryptedData.ETYPE_DES_CBC_CRC: |
|
251 |
case EncryptedData.ETYPE_DES_CBC_MD5: |
|
252 |
return Des.string_to_key_bytes(pwsalt); |
|
253 |
||
254 |
case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: |
|
255 |
return Des3.stringToKey(pwsalt); |
|
256 |
||
257 |
case EncryptedData.ETYPE_ARCFOUR_HMAC: |
|
258 |
return ArcFourHmac.stringToKey(password); |
|
259 |
||
260 |
case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: |
|
261 |
return Aes128.stringToKey(password, salt, s2kparams); |
|
262 |
||
263 |
case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: |
|
264 |
return Aes256.stringToKey(password, salt, s2kparams); |
|
265 |
||
266 |
default: |
|
267 |
throw new IllegalArgumentException("encryption type " + |
|
268 |
EType.toString(keyType) + " not supported"); |
|
269 |
} |
|
270 |
||
271 |
} catch (GeneralSecurityException e) { |
|
272 |
KrbCryptoException ke = new KrbCryptoException(e.getMessage()); |
|
273 |
ke.initCause(e); |
|
274 |
throw ke; |
|
275 |
} finally { |
|
276 |
Arrays.fill(pwsalt, '0'); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
// Used in javax.security.auth.kerberos.KeyImpl |
|
281 |
public EncryptionKey(char[] password, |
|
282 |
String salt, |
|
283 |
String algorithm) throws KrbCryptoException { |
|
284 |
||
285 |
if (algorithm == null || algorithm.equalsIgnoreCase("DES")) { |
|
286 |
keyType = EncryptedData.ETYPE_DES_CBC_MD5; |
|
287 |
} else if (algorithm.equalsIgnoreCase("DESede")) { |
|
288 |
keyType = EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD; |
|
289 |
} else if (algorithm.equalsIgnoreCase("AES128")) { |
|
290 |
keyType = EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96; |
|
291 |
} else if (algorithm.equalsIgnoreCase("ArcFourHmac")) { |
|
292 |
keyType = EncryptedData.ETYPE_ARCFOUR_HMAC; |
|
293 |
} else if (algorithm.equalsIgnoreCase("AES256")) { |
|
294 |
keyType = EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96; |
|
295 |
// validate if AES256 is enabled |
|
296 |
if (!EType.isSupported(keyType)) { |
|
297 |
throw new IllegalArgumentException("Algorithm " + algorithm + |
|
298 |
" not enabled"); |
|
299 |
} |
|
300 |
} else { |
|
301 |
throw new IllegalArgumentException("Algorithm " + algorithm + |
|
302 |
" not supported"); |
|
303 |
} |
|
304 |
||
305 |
keyValue = stringToKey(password, salt, null, keyType); |
|
306 |
kvno = null; |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Generates a sub-sessionkey from a given session key. |
|
311 |
*/ |
|
312 |
// Used in KrbApRep, KrbApReq |
|
313 |
EncryptionKey(EncryptionKey key) throws KrbCryptoException { |
|
314 |
// generate random sub-session key |
|
315 |
keyValue = Confounder.bytes(key.keyValue.length); |
|
316 |
for (int i = 0; i < keyValue.length; i++) { |
|
317 |
keyValue[i] ^= key.keyValue[i]; |
|
318 |
} |
|
319 |
keyType = key.keyType; |
|
320 |
||
321 |
// check for key parity and weak keys |
|
322 |
try { |
|
323 |
// check for DES key |
|
324 |
if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) || |
|
325 |
(keyType == EncryptedData.ETYPE_DES_CBC_CRC)) { |
|
326 |
// fix DES key parity |
|
327 |
if (!DESKeySpec.isParityAdjusted(keyValue, 0)) { |
|
328 |
keyValue = Des.set_parity(keyValue); |
|
329 |
} |
|
330 |
// check for weak key |
|
331 |
if (DESKeySpec.isWeak(keyValue, 0)) { |
|
332 |
keyValue[7] = (byte)(keyValue[7] ^ 0xF0); |
|
333 |
} |
|
334 |
} |
|
335 |
// check for 3DES key |
|
336 |
if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) { |
|
337 |
// fix 3DES key parity |
|
338 |
if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) { |
|
339 |
keyValue = Des3.parityFix(keyValue); |
|
340 |
} |
|
341 |
// check for weak keys |
|
342 |
byte[] oneKey = new byte[8]; |
|
343 |
for (int i=0; i<keyValue.length; i+=8) { |
|
344 |
System.arraycopy(keyValue, i, oneKey, 0, 8); |
|
345 |
if (DESKeySpec.isWeak(oneKey, 0)) { |
|
346 |
keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0); |
|
347 |
} |
|
348 |
} |
|
349 |
} |
|
350 |
} catch (GeneralSecurityException e) { |
|
351 |
KrbCryptoException ke = new KrbCryptoException(e.getMessage()); |
|
352 |
ke.initCause(e); |
|
353 |
throw ke; |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Constructs an instance of EncryptionKey type. |
|
359 |
* @param encoding a single DER-encoded value. |
|
360 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
361 |
* encoded data. |
|
362 |
* @exception IOException if an I/O error occurs while reading encoded |
|
363 |
* data. |
|
364 |
* |
|
365 |
* |
|
366 |
*/ |
|
367 |
// Used in javax.security.auth.kerberos.KeyImpl |
|
368 |
public EncryptionKey(DerValue encoding) throws Asn1Exception, IOException { |
|
369 |
DerValue der; |
|
370 |
if (encoding.getTag() != DerValue.tag_Sequence) { |
|
371 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
372 |
} |
|
373 |
der = encoding.getData().getDerValue(); |
|
374 |
if ((der.getTag() & (byte)0x1F) == (byte)0x00) { |
|
375 |
keyType = der.getData().getBigInteger().intValue(); |
|
376 |
} |
|
377 |
else |
|
378 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
379 |
der = encoding.getData().getDerValue(); |
|
380 |
if ((der.getTag() & (byte)0x1F) == (byte)0x01) { |
|
381 |
keyValue = der.getData().getOctetString(); |
|
382 |
} |
|
383 |
else |
|
384 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
385 |
if (der.getData().available() > 0) { |
|
386 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* Returns the ASN.1 encoding of this EncryptionKey. |
|
392 |
* |
|
393 |
* <xmp> |
|
394 |
* EncryptionKey ::= SEQUENCE { |
|
395 |
* keytype[0] INTEGER, |
|
396 |
* keyvalue[1] OCTET STRING } |
|
397 |
* </xmp> |
|
398 |
* |
|
399 |
* <p> |
|
400 |
* This definition reflects the Network Working Group RFC 4120 |
|
401 |
* specification available at |
|
402 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
|
403 |
* http://www.ietf.org/rfc/rfc4120.txt</a>. |
|
404 |
* |
|
405 |
* @return byte array of encoded EncryptionKey object. |
|
406 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
407 |
* encoded data. |
|
408 |
* @exception IOException if an I/O error occurs while reading encoded |
|
409 |
* data. |
|
410 |
* |
|
411 |
*/ |
|
412 |
public synchronized byte[] asn1Encode() throws Asn1Exception, IOException { |
|
413 |
DerOutputStream bytes = new DerOutputStream(); |
|
414 |
DerOutputStream temp = new DerOutputStream(); |
|
415 |
temp.putInteger(keyType); |
|
416 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
|
417 |
(byte)0x00), temp); |
|
418 |
temp = new DerOutputStream(); |
|
419 |
temp.putOctetString(keyValue); |
|
420 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
|
421 |
(byte)0x01), temp); |
|
422 |
temp = new DerOutputStream(); |
|
423 |
temp.write(DerValue.tag_Sequence, bytes); |
|
424 |
return temp.toByteArray(); |
|
425 |
} |
|
426 |
||
427 |
public synchronized void destroy() { |
|
428 |
if (keyValue != null) |
|
429 |
for (int i = 0; i < keyValue.length; i++) |
|
430 |
keyValue[i] = 0; |
|
431 |
} |
|
432 |
||
433 |
||
434 |
/** |
|
435 |
* Parse (unmarshal) an Encryption key from a DER input stream. This form |
|
436 |
* parsing might be used when expanding a value which is part of |
|
437 |
* a constructed sequence and uses explicitly tagged type. |
|
438 |
* |
|
439 |
* @param data the Der input stream value, which contains one or more |
|
440 |
* marshaled value. |
|
441 |
* @param explicitTag tag number. |
|
442 |
* @param optional indicate if this data field is optional |
|
443 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
444 |
* encoded data. |
|
445 |
* @exception IOException if an I/O error occurs while reading encoded |
|
446 |
* data. |
|
447 |
* @return an instance of EncryptionKey. |
|
448 |
* |
|
449 |
*/ |
|
450 |
public static EncryptionKey parse(DerInputStream data, byte |
|
451 |
explicitTag, boolean optional) throws |
|
452 |
Asn1Exception, IOException { |
|
453 |
if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != |
|
454 |
explicitTag)) { |
|
455 |
return null; |
|
456 |
} |
|
457 |
DerValue der = data.getDerValue(); |
|
458 |
if (explicitTag != (der.getTag() & (byte)0x1F)) { |
|
459 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
460 |
} else { |
|
461 |
DerValue subDer = der.getData().getDerValue(); |
|
462 |
return new EncryptionKey(subDer); |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Writes key value in FCC format to a <code>CCacheOutputStream</code>. |
|
468 |
* |
|
469 |
* @param cos a <code>CCacheOutputStream</code> to be written to. |
|
470 |
* @exception IOException if an I/O exception occurs. |
|
471 |
* @see sun.security.krb5.internal.ccache.CCacheOutputStream |
|
472 |
* |
|
473 |
*/ |
|
474 |
public synchronized void writeKey(CCacheOutputStream cos) |
|
475 |
throws IOException { |
|
476 |
||
477 |
cos.write16(keyType); |
|
478 |
// we use KRB5_FCC_FVNO_3 |
|
479 |
cos.write16(keyType); // key type is recorded twice. |
|
480 |
cos.write32(keyValue.length); |
|
481 |
for (int i = 0; i < keyValue.length; i++) { |
|
482 |
cos.write8(keyValue[i]); |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
public String toString() { |
|
487 |
return new String("EncryptionKey: keyType=" + keyType |
|
488 |
+ " kvno=" + kvno |
|
489 |
+ " keyValue (hex dump)=" |
|
490 |
+ (keyValue == null || keyValue.length == 0 ? |
|
5457 | 491 |
" Empty Key" : '\n' |
492 |
+ Krb5.hexDumper.encodeBuffer(keyValue) |
|
493 |
+ '\n')); |
|
2 | 494 |
} |
495 |
||
4168 | 496 |
/** |
497 |
* Find a key with given etype |
|
498 |
*/ |
|
2 | 499 |
public static EncryptionKey findKey(int etype, EncryptionKey[] keys) |
4168 | 500 |
throws KrbException { |
501 |
return findKey(etype, null, keys); |
|
502 |
} |
|
503 |
||
504 |
/** |
|
4532 | 505 |
* Determines if a kvno matches another kvno. Used in the method |
506 |
* findKey(type, kvno, keys). Always returns true if either input |
|
507 |
* is null or zero, in case any side does not have kvno info available. |
|
508 |
* |
|
509 |
* Note: zero is included because N/A is not a legal value for kvno |
|
510 |
* in javax.security.auth.kerberos.KerberosKey. Therefore, the info |
|
511 |
* that the kvno is N/A might be lost when converting between this |
|
512 |
* class and KerberosKey. |
|
513 |
*/ |
|
514 |
private static boolean versionMatches(Integer v1, Integer v2) { |
|
515 |
if (v1 == null || v1 == 0 || v2 == null || v2 == 0) { |
|
516 |
return true; |
|
517 |
} |
|
518 |
return v1.equals(v2); |
|
519 |
} |
|
520 |
||
521 |
/** |
|
4168 | 522 |
* Find a key with given etype and kvno |
523 |
* @param kvno if null, return any (first?) key |
|
524 |
*/ |
|
525 |
public static EncryptionKey findKey(int etype, Integer kvno, EncryptionKey[] keys) |
|
2 | 526 |
throws KrbException { |
527 |
||
528 |
// check if encryption type is supported |
|
529 |
if (!EType.isSupported(etype)) { |
|
530 |
throw new KrbException("Encryption type " + |
|
531 |
EType.toString(etype) + " is not supported/enabled"); |
|
532 |
} |
|
533 |
||
534 |
int ktype; |
|
4532 | 535 |
boolean etypeFound = false; |
2 | 536 |
for (int i = 0; i < keys.length; i++) { |
537 |
ktype = keys[i].getEType(); |
|
538 |
if (EType.isSupported(ktype)) { |
|
4168 | 539 |
Integer kv = keys[i].getKeyVersionNumber(); |
4532 | 540 |
if (etype == ktype) { |
541 |
etypeFound = true; |
|
542 |
if (versionMatches(kvno, kv)) { |
|
543 |
return keys[i]; |
|
544 |
} |
|
2 | 545 |
} |
546 |
} |
|
547 |
} |
|
4532 | 548 |
|
2 | 549 |
// Key not found. |
550 |
// allow DES key to be used for the DES etypes |
|
551 |
if ((etype == EncryptedData.ETYPE_DES_CBC_CRC || |
|
552 |
etype == EncryptedData.ETYPE_DES_CBC_MD5)) { |
|
553 |
for (int i = 0; i < keys.length; i++) { |
|
554 |
ktype = keys[i].getEType(); |
|
555 |
if (ktype == EncryptedData.ETYPE_DES_CBC_CRC || |
|
4168 | 556 |
ktype == EncryptedData.ETYPE_DES_CBC_MD5) { |
557 |
Integer kv = keys[i].getKeyVersionNumber(); |
|
4532 | 558 |
etypeFound = true; |
559 |
if (versionMatches(kvno, kv)) { |
|
4168 | 560 |
return new EncryptionKey(etype, keys[i].getBytes()); |
561 |
} |
|
2 | 562 |
} |
563 |
} |
|
564 |
} |
|
4532 | 565 |
if (etypeFound) { |
566 |
throw new KrbException(Krb5.KRB_AP_ERR_BADKEYVER); |
|
567 |
} |
|
2 | 568 |
return null; |
569 |
} |
|
570 |
} |