author | weijun |
Wed, 09 Apr 2014 17:19:19 +0800 | |
changeset 23732 | 44fe768edfd2 |
parent 18830 | 90956ead732f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18830 | 2 |
* Copyright (c) 2000, 2013, 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 |
package javax.security.auth.kerberos; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.Arrays; |
|
30 |
import javax.crypto.SecretKey; |
|
31 |
import javax.security.auth.Destroyable; |
|
32 |
import javax.security.auth.DestroyFailedException; |
|
33 |
import sun.misc.HexDumpEncoder; |
|
34 |
import sun.security.krb5.Asn1Exception; |
|
35 |
import sun.security.krb5.PrincipalName; |
|
36 |
import sun.security.krb5.EncryptionKey; |
|
37 |
import sun.security.krb5.EncryptedData; |
|
38 |
import sun.security.krb5.KrbException; |
|
39 |
import sun.security.util.DerValue; |
|
40 |
||
41 |
/** |
|
42 |
* This class encapsulates a Kerberos encryption key. It is not associated |
|
43 |
* with a principal and may represent an ephemeral session key. |
|
44 |
* |
|
45 |
* @author Mayank Upadhyay |
|
46 |
* @since 1.4 |
|
47 |
* |
|
48 |
* @serial include |
|
49 |
*/ |
|
50 |
class KeyImpl implements SecretKey, Destroyable, Serializable { |
|
51 |
||
52 |
private static final long serialVersionUID = -7889313790214321193L; |
|
53 |
||
54 |
private transient byte[] keyBytes; |
|
55 |
private transient int keyType; |
|
56 |
private transient volatile boolean destroyed = false; |
|
57 |
||
58 |
||
59 |
/** |
|
60 |
* Constructs a KeyImpl from the given bytes. |
|
61 |
* |
|
62 |
* @param keyBytes the raw bytes for the secret key |
|
63 |
* @param keyType the key type for the secret key as defined by the |
|
64 |
* Kerberos protocol specification. |
|
65 |
*/ |
|
66 |
public KeyImpl(byte[] keyBytes, |
|
67 |
int keyType) { |
|
51 | 68 |
this.keyBytes = keyBytes.clone(); |
2 | 69 |
this.keyType = keyType; |
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Constructs a KeyImpl from a password. |
|
74 |
* |
|
75 |
* @param principal the principal from which to derive the salt |
|
76 |
* @param password the password that should be used to compute the |
|
77 |
* key. |
|
78 |
* @param algorithm the name for the algorithm that this key wil be |
|
79 |
* used for. This parameter may be null in which case "DES" will be |
|
80 |
* assumed. |
|
81 |
*/ |
|
82 |
public KeyImpl(KerberosPrincipal principal, |
|
83 |
char[] password, |
|
84 |
String algorithm) { |
|
85 |
||
86 |
try { |
|
87 |
PrincipalName princ = new PrincipalName(principal.getName()); |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
88 |
EncryptionKey key; |
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
89 |
if ("none".equalsIgnoreCase(algorithm)) { |
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
90 |
key = EncryptionKey.NULL_KEY; |
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
91 |
} else { |
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
92 |
key = new EncryptionKey(password, princ.getSalt(), algorithm); |
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
93 |
} |
2 | 94 |
this.keyBytes = key.getBytes(); |
95 |
this.keyType = key.getEType(); |
|
96 |
} catch (KrbException e) { |
|
97 |
throw new IllegalArgumentException(e.getMessage()); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Returns the keyType for this key as defined in the Kerberos Spec. |
|
103 |
*/ |
|
104 |
public final int getKeyType() { |
|
105 |
if (destroyed) |
|
106 |
throw new IllegalStateException("This key is no longer valid"); |
|
107 |
return keyType; |
|
108 |
} |
|
109 |
||
110 |
/* |
|
111 |
* Methods from java.security.Key |
|
112 |
*/ |
|
113 |
||
114 |
public final String getAlgorithm() { |
|
115 |
return getAlgorithmName(keyType); |
|
116 |
} |
|
117 |
||
118 |
private String getAlgorithmName(int eType) { |
|
119 |
if (destroyed) |
|
120 |
throw new IllegalStateException("This key is no longer valid"); |
|
121 |
||
122 |
switch (eType) { |
|
123 |
case EncryptedData.ETYPE_DES_CBC_CRC: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
124 |
return "des-cbc-crc"; |
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
125 |
|
2 | 126 |
case EncryptedData.ETYPE_DES_CBC_MD5: |
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
127 |
return "des-cbc-md5"; |
2 | 128 |
|
129 |
case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
130 |
return "des3-cbc-sha1-kd"; |
2 | 131 |
|
132 |
case EncryptedData.ETYPE_ARCFOUR_HMAC: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
133 |
return "rc4-hmac"; |
2 | 134 |
|
135 |
case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
136 |
return "aes128-cts-hmac-sha1-96"; |
2 | 137 |
|
138 |
case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
139 |
return "aes256-cts-hmac-sha1-96"; |
2 | 140 |
|
141 |
case EncryptedData.ETYPE_NULL: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
142 |
return "none"; |
2 | 143 |
|
144 |
default: |
|
23732
44fe768edfd2
8035986: KerberosKey algorithm names are not specified
weijun
parents:
18830
diff
changeset
|
145 |
return eType > 0 ? "unknown" : "private"; |
2 | 146 |
} |
147 |
} |
|
148 |
||
149 |
public final String getFormat() { |
|
150 |
if (destroyed) |
|
151 |
throw new IllegalStateException("This key is no longer valid"); |
|
152 |
return "RAW"; |
|
153 |
} |
|
154 |
||
155 |
public final byte[] getEncoded() { |
|
156 |
if (destroyed) |
|
157 |
throw new IllegalStateException("This key is no longer valid"); |
|
51 | 158 |
return keyBytes.clone(); |
2 | 159 |
} |
160 |
||
161 |
public void destroy() throws DestroyFailedException { |
|
162 |
if (!destroyed) { |
|
163 |
destroyed = true; |
|
164 |
Arrays.fill(keyBytes, (byte) 0); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
public boolean isDestroyed() { |
|
169 |
return destroyed; |
|
170 |
} |
|
171 |
||
172 |
/** |
|
18830 | 173 |
* @serialData this {@code KeyImpl} is serialized by |
2 | 174 |
* writing out the ASN1 Encoded bytes of the encryption key. |
175 |
* The ASN1 encoding is defined in RFC4120 and as follows: |
|
176 |
* EncryptionKey ::= SEQUENCE { |
|
177 |
* keytype [0] Int32 -- actually encryption type --, |
|
178 |
* keyvalue [1] OCTET STRING |
|
179 |
* } |
|
180 |
*/ |
|
181 |
private void writeObject(ObjectOutputStream ois) |
|
182 |
throws IOException { |
|
183 |
if (destroyed) { |
|
184 |
throw new IOException("This key is no longer valid"); |
|
185 |
} |
|
186 |
||
187 |
try { |
|
188 |
ois.writeObject((new EncryptionKey(keyType, keyBytes)).asn1Encode()); |
|
189 |
} catch (Asn1Exception ae) { |
|
190 |
throw new IOException(ae.getMessage()); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
private void readObject(ObjectInputStream ois) |
|
195 |
throws IOException, ClassNotFoundException { |
|
196 |
try { |
|
197 |
EncryptionKey encKey = new EncryptionKey(new |
|
198 |
DerValue((byte[])ois.readObject())); |
|
199 |
keyType = encKey.getEType(); |
|
200 |
keyBytes = encKey.getBytes(); |
|
201 |
} catch (Asn1Exception ae) { |
|
202 |
throw new IOException(ae.getMessage()); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
public String toString() { |
|
207 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
208 |
return "EncryptionKey: keyType=" + keyType |
|
209 |
+ " keyBytes (hex dump)=" |
|
210 |
+ (keyBytes == null || keyBytes.length == 0 ? |
|
211 |
" Empty Key" : |
|
5457 | 212 |
'\n' + hd.encodeBuffer(keyBytes) |
2 | 213 |
+ '\n'); |
214 |
||
215 |
||
216 |
} |
|
217 |
||
218 |
public int hashCode() { |
|
219 |
int result = 17; |
|
220 |
if(isDestroyed()) { |
|
221 |
return result; |
|
222 |
} |
|
223 |
result = 37 * result + Arrays.hashCode(keyBytes); |
|
224 |
return 37 * result + keyType; |
|
225 |
} |
|
226 |
||
227 |
public boolean equals(Object other) { |
|
228 |
||
229 |
if (other == this) |
|
230 |
return true; |
|
231 |
||
232 |
if (! (other instanceof KeyImpl)) { |
|
233 |
return false; |
|
234 |
} |
|
235 |
||
236 |
KeyImpl otherKey = ((KeyImpl) other); |
|
237 |
if (isDestroyed() || otherKey.isDestroyed()) { |
|
238 |
return false; |
|
239 |
} |
|
240 |
||
241 |
if(keyType != otherKey.getKeyType() || |
|
242 |
!Arrays.equals(keyBytes, otherKey.getEncoded())) { |
|
243 |
return false; |
|
244 |
} |
|
245 |
||
246 |
return true; |
|
247 |
} |
|
248 |
} |