author | weijun |
Mon, 17 Dec 2012 12:18:46 +0800 | |
changeset 14895 | 5ad2d7032375 |
parent 10432 | ef33e56c55a9 |
child 18168 | f47169155ea0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9499 | 2 |
* Copyright (c) 2000, 2011, 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, |
|
9499 | 141 |
String keytab) { |
2 | 142 |
|
143 |
if (princ == null) |
|
144 |
throw new IllegalArgumentException( |
|
145 |
"Cannot have null pricipal name to look in keytab."); |
|
146 |
||
147 |
// KeyTab getInstance(keytab) will call KeyTab.getInstance() |
|
148 |
// if keytab is null |
|
149 |
KeyTab ktab = KeyTab.getInstance(keytab); |
|
150 |
return ktab.readServiceKeys(princ); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
154 |
* Obtains a key for a given etype of a principal with possible new salt |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
155 |
* and s2kparams |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
156 |
* @param cname NOT null |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
157 |
* @param password NOT null |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
158 |
* @param etype |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
159 |
* @param snp can be NULL |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
160 |
* @returns never null |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
161 |
*/ |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
162 |
public static EncryptionKey acquireSecretKey(PrincipalName cname, |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
163 |
char[] password, int etype, PAData.SaltAndParams snp) |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
164 |
throws KrbException { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
165 |
String salt; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
166 |
byte[] s2kparams; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
167 |
if (snp != null) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
168 |
salt = snp.salt != null ? snp.salt : cname.getSalt(); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
169 |
s2kparams = snp.params; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
170 |
} else { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
171 |
salt = cname.getSalt(); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
172 |
s2kparams = null; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
173 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
174 |
return acquireSecretKey(password, salt, etype, s2kparams); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
175 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
176 |
|
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
177 |
/** |
7183 | 178 |
* Obtains a key for a given etype with salt and optional s2kparams |
179 |
* @param password NOT null |
|
180 |
* @param salt NOT null |
|
181 |
* @param etype |
|
182 |
* @param s2kparams can be NULL |
|
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
183 |
* @returns never null |
7183 | 184 |
*/ |
185 |
public static EncryptionKey acquireSecretKey(char[] password, |
|
186 |
String salt, int etype, byte[] s2kparams) |
|
187 |
throws KrbException { |
|
188 |
||
189 |
return new EncryptionKey( |
|
190 |
stringToKey(password, salt, s2kparams, etype), |
|
191 |
etype, null); |
|
192 |
} |
|
193 |
||
194 |
/** |
|
2 | 195 |
* Generate a list of keys using the given principal and password. |
196 |
* Construct a key for each configured etype. |
|
197 |
* Caller is responsible for clearing password. |
|
198 |
*/ |
|
199 |
/* |
|
200 |
* Usually, when keyType is decoded from ASN.1 it will contain a |
|
201 |
* value indicating what the algorithm to be used is. However, when |
|
202 |
* converting from a password to a key for the AS-EXCHANGE, this |
|
203 |
* keyType will not be available. Use builtin list of default etypes |
|
204 |
* as the default in that case. If default_tkt_enctypes was set in |
|
205 |
* the libdefaults of krb5.conf, then use that sequence. |
|
206 |
*/ |
|
207 |
public static EncryptionKey[] acquireSecretKeys(char[] password, |
|
7183 | 208 |
String salt) throws KrbException { |
2 | 209 |
|
210 |
int[] etypes = EType.getDefaults("default_tkt_enctypes"); |
|
211 |
if (etypes == null) { |
|
212 |
etypes = EType.getBuiltInDefaults(); |
|
213 |
} |
|
214 |
||
215 |
EncryptionKey[] encKeys = new EncryptionKey[etypes.length]; |
|
216 |
for (int i = 0; i < etypes.length; i++) { |
|
217 |
if (EType.isSupported(etypes[i])) { |
|
218 |
encKeys[i] = new EncryptionKey( |
|
7183 | 219 |
stringToKey(password, salt, null, etypes[i]), |
2 | 220 |
etypes[i], null); |
221 |
} else { |
|
222 |
if (DEBUG) { |
|
223 |
System.out.println("Encryption Type " + |
|
224 |
EType.toString(etypes[i]) + |
|
225 |
" is not supported/enabled"); |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
return encKeys; |
|
230 |
} |
|
231 |
||
232 |
// Used in Krb5AcceptCredential, self |
|
233 |
public EncryptionKey(byte[] keyValue, |
|
234 |
int keyType, |
|
235 |
Integer kvno) { |
|
236 |
||
237 |
if (keyValue != null) { |
|
238 |
this.keyValue = new byte[keyValue.length]; |
|
239 |
System.arraycopy(keyValue, 0, this.keyValue, 0, keyValue.length); |
|
240 |
} else { |
|
241 |
throw new IllegalArgumentException("EncryptionKey: " + |
|
242 |
"Key bytes cannot be null!"); |
|
243 |
} |
|
244 |
this.keyType = keyType; |
|
245 |
this.kvno = kvno; |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Constructs an EncryptionKey by using the specified key type and key |
|
250 |
* value. It is used to recover the key when retrieving data from |
|
251 |
* credential cache file. |
|
252 |
* |
|
253 |
*/ |
|
254 |
// Used in JSSE (KerberosWrapper), Credentials, |
|
255 |
// javax.security.auth.kerberos.KeyImpl |
|
256 |
public EncryptionKey(int keyType, |
|
257 |
byte[] keyValue) { |
|
258 |
this(keyValue, keyType, null); |
|
259 |
} |
|
260 |
||
261 |
private static byte[] stringToKey(char[] password, String salt, |
|
262 |
byte[] s2kparams, int keyType) throws KrbCryptoException { |
|
263 |
||
264 |
char[] slt = salt.toCharArray(); |
|
265 |
char[] pwsalt = new char[password.length + slt.length]; |
|
266 |
System.arraycopy(password, 0, pwsalt, 0, password.length); |
|
267 |
System.arraycopy(slt, 0, pwsalt, password.length, slt.length); |
|
268 |
Arrays.fill(slt, '0'); |
|
269 |
||
270 |
try { |
|
271 |
switch (keyType) { |
|
272 |
case EncryptedData.ETYPE_DES_CBC_CRC: |
|
273 |
case EncryptedData.ETYPE_DES_CBC_MD5: |
|
274 |
return Des.string_to_key_bytes(pwsalt); |
|
275 |
||
276 |
case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: |
|
277 |
return Des3.stringToKey(pwsalt); |
|
278 |
||
279 |
case EncryptedData.ETYPE_ARCFOUR_HMAC: |
|
280 |
return ArcFourHmac.stringToKey(password); |
|
281 |
||
282 |
case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: |
|
283 |
return Aes128.stringToKey(password, salt, s2kparams); |
|
284 |
||
285 |
case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: |
|
286 |
return Aes256.stringToKey(password, salt, s2kparams); |
|
287 |
||
288 |
default: |
|
289 |
throw new IllegalArgumentException("encryption type " + |
|
290 |
EType.toString(keyType) + " not supported"); |
|
291 |
} |
|
292 |
||
293 |
} catch (GeneralSecurityException e) { |
|
294 |
KrbCryptoException ke = new KrbCryptoException(e.getMessage()); |
|
295 |
ke.initCause(e); |
|
296 |
throw ke; |
|
297 |
} finally { |
|
298 |
Arrays.fill(pwsalt, '0'); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
// Used in javax.security.auth.kerberos.KeyImpl |
|
303 |
public EncryptionKey(char[] password, |
|
304 |
String salt, |
|
305 |
String algorithm) throws KrbCryptoException { |
|
306 |
||
307 |
if (algorithm == null || algorithm.equalsIgnoreCase("DES")) { |
|
308 |
keyType = EncryptedData.ETYPE_DES_CBC_MD5; |
|
309 |
} else if (algorithm.equalsIgnoreCase("DESede")) { |
|
310 |
keyType = EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD; |
|
311 |
} else if (algorithm.equalsIgnoreCase("AES128")) { |
|
312 |
keyType = EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96; |
|
313 |
} else if (algorithm.equalsIgnoreCase("ArcFourHmac")) { |
|
314 |
keyType = EncryptedData.ETYPE_ARCFOUR_HMAC; |
|
315 |
} else if (algorithm.equalsIgnoreCase("AES256")) { |
|
316 |
keyType = EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96; |
|
317 |
// validate if AES256 is enabled |
|
318 |
if (!EType.isSupported(keyType)) { |
|
319 |
throw new IllegalArgumentException("Algorithm " + algorithm + |
|
320 |
" not enabled"); |
|
321 |
} |
|
322 |
} else { |
|
323 |
throw new IllegalArgumentException("Algorithm " + algorithm + |
|
324 |
" not supported"); |
|
325 |
} |
|
326 |
||
327 |
keyValue = stringToKey(password, salt, null, keyType); |
|
328 |
kvno = null; |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* Generates a sub-sessionkey from a given session key. |
|
333 |
*/ |
|
334 |
// Used in KrbApRep, KrbApReq |
|
335 |
EncryptionKey(EncryptionKey key) throws KrbCryptoException { |
|
336 |
// generate random sub-session key |
|
337 |
keyValue = Confounder.bytes(key.keyValue.length); |
|
338 |
for (int i = 0; i < keyValue.length; i++) { |
|
339 |
keyValue[i] ^= key.keyValue[i]; |
|
340 |
} |
|
341 |
keyType = key.keyType; |
|
342 |
||
343 |
// check for key parity and weak keys |
|
344 |
try { |
|
345 |
// check for DES key |
|
346 |
if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) || |
|
347 |
(keyType == EncryptedData.ETYPE_DES_CBC_CRC)) { |
|
348 |
// fix DES key parity |
|
349 |
if (!DESKeySpec.isParityAdjusted(keyValue, 0)) { |
|
350 |
keyValue = Des.set_parity(keyValue); |
|
351 |
} |
|
352 |
// check for weak key |
|
353 |
if (DESKeySpec.isWeak(keyValue, 0)) { |
|
354 |
keyValue[7] = (byte)(keyValue[7] ^ 0xF0); |
|
355 |
} |
|
356 |
} |
|
357 |
// check for 3DES key |
|
358 |
if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) { |
|
359 |
// fix 3DES key parity |
|
360 |
if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) { |
|
361 |
keyValue = Des3.parityFix(keyValue); |
|
362 |
} |
|
363 |
// check for weak keys |
|
364 |
byte[] oneKey = new byte[8]; |
|
365 |
for (int i=0; i<keyValue.length; i+=8) { |
|
366 |
System.arraycopy(keyValue, i, oneKey, 0, 8); |
|
367 |
if (DESKeySpec.isWeak(oneKey, 0)) { |
|
368 |
keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0); |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
} catch (GeneralSecurityException e) { |
|
373 |
KrbCryptoException ke = new KrbCryptoException(e.getMessage()); |
|
374 |
ke.initCause(e); |
|
375 |
throw ke; |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* Constructs an instance of EncryptionKey type. |
|
381 |
* @param encoding a single DER-encoded value. |
|
382 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
383 |
* encoded data. |
|
384 |
* @exception IOException if an I/O error occurs while reading encoded |
|
385 |
* data. |
|
386 |
* |
|
387 |
* |
|
388 |
*/ |
|
389 |
// Used in javax.security.auth.kerberos.KeyImpl |
|
390 |
public EncryptionKey(DerValue encoding) throws Asn1Exception, IOException { |
|
391 |
DerValue der; |
|
392 |
if (encoding.getTag() != DerValue.tag_Sequence) { |
|
393 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
394 |
} |
|
395 |
der = encoding.getData().getDerValue(); |
|
396 |
if ((der.getTag() & (byte)0x1F) == (byte)0x00) { |
|
397 |
keyType = der.getData().getBigInteger().intValue(); |
|
398 |
} |
|
399 |
else |
|
400 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
401 |
der = encoding.getData().getDerValue(); |
|
402 |
if ((der.getTag() & (byte)0x1F) == (byte)0x01) { |
|
403 |
keyValue = der.getData().getOctetString(); |
|
404 |
} |
|
405 |
else |
|
406 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
407 |
if (der.getData().available() > 0) { |
|
408 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Returns the ASN.1 encoding of this EncryptionKey. |
|
414 |
* |
|
415 |
* <xmp> |
|
416 |
* EncryptionKey ::= SEQUENCE { |
|
417 |
* keytype[0] INTEGER, |
|
418 |
* keyvalue[1] OCTET STRING } |
|
419 |
* </xmp> |
|
420 |
* |
|
421 |
* <p> |
|
422 |
* This definition reflects the Network Working Group RFC 4120 |
|
423 |
* specification available at |
|
424 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
|
425 |
* http://www.ietf.org/rfc/rfc4120.txt</a>. |
|
426 |
* |
|
427 |
* @return byte array of encoded EncryptionKey object. |
|
428 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
429 |
* encoded data. |
|
430 |
* @exception IOException if an I/O error occurs while reading encoded |
|
431 |
* data. |
|
432 |
* |
|
433 |
*/ |
|
434 |
public synchronized byte[] asn1Encode() throws Asn1Exception, IOException { |
|
435 |
DerOutputStream bytes = new DerOutputStream(); |
|
436 |
DerOutputStream temp = new DerOutputStream(); |
|
437 |
temp.putInteger(keyType); |
|
438 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
|
439 |
(byte)0x00), temp); |
|
440 |
temp = new DerOutputStream(); |
|
441 |
temp.putOctetString(keyValue); |
|
442 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
|
443 |
(byte)0x01), temp); |
|
444 |
temp = new DerOutputStream(); |
|
445 |
temp.write(DerValue.tag_Sequence, bytes); |
|
446 |
return temp.toByteArray(); |
|
447 |
} |
|
448 |
||
449 |
public synchronized void destroy() { |
|
450 |
if (keyValue != null) |
|
451 |
for (int i = 0; i < keyValue.length; i++) |
|
452 |
keyValue[i] = 0; |
|
453 |
} |
|
454 |
||
455 |
||
456 |
/** |
|
457 |
* Parse (unmarshal) an Encryption key from a DER input stream. This form |
|
458 |
* parsing might be used when expanding a value which is part of |
|
459 |
* a constructed sequence and uses explicitly tagged type. |
|
460 |
* |
|
461 |
* @param data the Der input stream value, which contains one or more |
|
462 |
* marshaled value. |
|
463 |
* @param explicitTag tag number. |
|
464 |
* @param optional indicate if this data field is optional |
|
465 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 |
|
466 |
* encoded data. |
|
467 |
* @exception IOException if an I/O error occurs while reading encoded |
|
468 |
* data. |
|
469 |
* @return an instance of EncryptionKey. |
|
470 |
* |
|
471 |
*/ |
|
472 |
public static EncryptionKey parse(DerInputStream data, byte |
|
473 |
explicitTag, boolean optional) throws |
|
474 |
Asn1Exception, IOException { |
|
475 |
if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != |
|
476 |
explicitTag)) { |
|
477 |
return null; |
|
478 |
} |
|
479 |
DerValue der = data.getDerValue(); |
|
480 |
if (explicitTag != (der.getTag() & (byte)0x1F)) { |
|
481 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
482 |
} else { |
|
483 |
DerValue subDer = der.getData().getDerValue(); |
|
484 |
return new EncryptionKey(subDer); |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* Writes key value in FCC format to a <code>CCacheOutputStream</code>. |
|
490 |
* |
|
491 |
* @param cos a <code>CCacheOutputStream</code> to be written to. |
|
492 |
* @exception IOException if an I/O exception occurs. |
|
493 |
* @see sun.security.krb5.internal.ccache.CCacheOutputStream |
|
494 |
* |
|
495 |
*/ |
|
496 |
public synchronized void writeKey(CCacheOutputStream cos) |
|
497 |
throws IOException { |
|
498 |
||
499 |
cos.write16(keyType); |
|
500 |
// we use KRB5_FCC_FVNO_3 |
|
501 |
cos.write16(keyType); // key type is recorded twice. |
|
502 |
cos.write32(keyValue.length); |
|
503 |
for (int i = 0; i < keyValue.length; i++) { |
|
504 |
cos.write8(keyValue[i]); |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
public String toString() { |
|
509 |
return new String("EncryptionKey: keyType=" + keyType |
|
510 |
+ " kvno=" + kvno |
|
511 |
+ " keyValue (hex dump)=" |
|
512 |
+ (keyValue == null || keyValue.length == 0 ? |
|
5457 | 513 |
" Empty Key" : '\n' |
514 |
+ Krb5.hexDumper.encodeBuffer(keyValue) |
|
515 |
+ '\n')); |
|
2 | 516 |
} |
517 |
||
4168 | 518 |
/** |
519 |
* Find a key with given etype |
|
520 |
*/ |
|
2 | 521 |
public static EncryptionKey findKey(int etype, EncryptionKey[] keys) |
4168 | 522 |
throws KrbException { |
523 |
return findKey(etype, null, keys); |
|
524 |
} |
|
525 |
||
526 |
/** |
|
4532 | 527 |
* Determines if a kvno matches another kvno. Used in the method |
528 |
* findKey(type, kvno, keys). Always returns true if either input |
|
529 |
* is null or zero, in case any side does not have kvno info available. |
|
530 |
* |
|
531 |
* Note: zero is included because N/A is not a legal value for kvno |
|
532 |
* in javax.security.auth.kerberos.KerberosKey. Therefore, the info |
|
533 |
* that the kvno is N/A might be lost when converting between this |
|
534 |
* class and KerberosKey. |
|
535 |
*/ |
|
536 |
private static boolean versionMatches(Integer v1, Integer v2) { |
|
537 |
if (v1 == null || v1 == 0 || v2 == null || v2 == 0) { |
|
538 |
return true; |
|
539 |
} |
|
540 |
return v1.equals(v2); |
|
541 |
} |
|
542 |
||
543 |
/** |
|
4168 | 544 |
* Find a key with given etype and kvno |
545 |
* @param kvno if null, return any (first?) key |
|
546 |
*/ |
|
547 |
public static EncryptionKey findKey(int etype, Integer kvno, EncryptionKey[] keys) |
|
2 | 548 |
throws KrbException { |
549 |
||
550 |
// check if encryption type is supported |
|
551 |
if (!EType.isSupported(etype)) { |
|
552 |
throw new KrbException("Encryption type " + |
|
553 |
EType.toString(etype) + " is not supported/enabled"); |
|
554 |
} |
|
555 |
||
556 |
int ktype; |
|
4532 | 557 |
boolean etypeFound = false; |
14895 | 558 |
|
559 |
// When no matched kvno is found, returns tke key of the same |
|
560 |
// etype with the highest kvno |
|
561 |
int kvno_found = 0; |
|
562 |
EncryptionKey key_found = null; |
|
563 |
||
2 | 564 |
for (int i = 0; i < keys.length; i++) { |
565 |
ktype = keys[i].getEType(); |
|
566 |
if (EType.isSupported(ktype)) { |
|
4168 | 567 |
Integer kv = keys[i].getKeyVersionNumber(); |
4532 | 568 |
if (etype == ktype) { |
569 |
etypeFound = true; |
|
570 |
if (versionMatches(kvno, kv)) { |
|
571 |
return keys[i]; |
|
14895 | 572 |
} else if (kv > kvno_found) { |
573 |
// kv is not null |
|
574 |
key_found = keys[i]; |
|
575 |
kvno_found = kv; |
|
4532 | 576 |
} |
2 | 577 |
} |
578 |
} |
|
579 |
} |
|
4532 | 580 |
|
2 | 581 |
// Key not found. |
582 |
// allow DES key to be used for the DES etypes |
|
583 |
if ((etype == EncryptedData.ETYPE_DES_CBC_CRC || |
|
584 |
etype == EncryptedData.ETYPE_DES_CBC_MD5)) { |
|
585 |
for (int i = 0; i < keys.length; i++) { |
|
586 |
ktype = keys[i].getEType(); |
|
587 |
if (ktype == EncryptedData.ETYPE_DES_CBC_CRC || |
|
4168 | 588 |
ktype == EncryptedData.ETYPE_DES_CBC_MD5) { |
589 |
Integer kv = keys[i].getKeyVersionNumber(); |
|
4532 | 590 |
etypeFound = true; |
591 |
if (versionMatches(kvno, kv)) { |
|
4168 | 592 |
return new EncryptionKey(etype, keys[i].getBytes()); |
14895 | 593 |
} else if (kv > kvno_found) { |
594 |
key_found = new EncryptionKey(etype, keys[i].getBytes()); |
|
595 |
kvno_found = kv; |
|
4168 | 596 |
} |
2 | 597 |
} |
598 |
} |
|
599 |
} |
|
4532 | 600 |
if (etypeFound) { |
14895 | 601 |
return key_found; |
602 |
// For compatibility, will not fail here. |
|
603 |
//throw new KrbException(Krb5.KRB_AP_ERR_BADKEYVER); |
|
4532 | 604 |
} |
2 | 605 |
return null; |
606 |
} |
|
607 |
} |