author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 14317 | 1e855efb3783 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1998, 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 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.Serializable; |
|
30 |
import java.security.Security; |
|
31 |
import java.security.Key; |
|
32 |
import java.security.PrivateKey; |
|
33 |
import java.security.Provider; |
|
34 |
import java.security.KeyFactory; |
|
35 |
import java.security.MessageDigest; |
|
36 |
import java.security.GeneralSecurityException; |
|
37 |
import java.security.NoSuchAlgorithmException; |
|
38 |
import java.security.NoSuchProviderException; |
|
39 |
import java.security.UnrecoverableKeyException; |
|
40 |
import java.security.AlgorithmParameters; |
|
41 |
import java.security.spec.PKCS8EncodedKeySpec; |
|
42 |
||
43 |
import javax.crypto.Cipher; |
|
44 |
import javax.crypto.CipherSpi; |
|
45 |
import javax.crypto.SecretKey; |
|
46 |
import javax.crypto.IllegalBlockSizeException; |
|
47 |
import javax.crypto.SealedObject; |
|
48 |
import javax.crypto.spec.*; |
|
49 |
import sun.security.x509.AlgorithmId; |
|
50 |
import sun.security.util.ObjectIdentifier; |
|
51 |
||
52 |
/** |
|
53 |
* This class implements a protection mechanism for private keys. In JCE, we |
|
54 |
* use a stronger protection mechanism than in the JDK, because we can use |
|
55 |
* the <code>Cipher</code> class. |
|
56 |
* Private keys are protected using the JCE mechanism, and are recovered using |
|
57 |
* either the JDK or JCE mechanism, depending on how the key has been |
|
58 |
* protected. This allows us to parse Sun's keystore implementation that ships |
|
59 |
* with JDK 1.2. |
|
60 |
* |
|
61 |
* @author Jan Luehe |
|
62 |
* |
|
63 |
* |
|
64 |
* @see JceKeyStore |
|
65 |
*/ |
|
66 |
||
67 |
final class KeyProtector { |
|
68 |
||
69 |
// defined by SunSoft (SKI project) |
|
70 |
private static final String PBE_WITH_MD5_AND_DES3_CBC_OID |
|
71 |
= "1.3.6.1.4.1.42.2.19.1"; |
|
72 |
||
73 |
// JavaSoft proprietary key-protection algorithm (used to protect private |
|
74 |
// keys in the keystore implementation that comes with JDK 1.2) |
|
75 |
private static final String KEY_PROTECTOR_OID = "1.3.6.1.4.1.42.2.17.1.1"; |
|
76 |
||
77 |
private static final int SALT_LEN = 20; // the salt length |
|
78 |
private static final int DIGEST_LEN = 20; |
|
79 |
||
80 |
// the password used for protecting/recovering keys passed through this |
|
81 |
// key protector |
|
82 |
private char[] password; |
|
83 |
||
84 |
private static final Provider PROV = Security.getProvider("SunJCE"); |
|
85 |
||
86 |
KeyProtector(char[] password) { |
|
87 |
if (password == null) { |
|
88 |
throw new IllegalArgumentException("password can't be null"); |
|
89 |
} |
|
90 |
this.password = password; |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Protects the given cleartext private key, using the password provided at |
|
95 |
* construction time. |
|
96 |
*/ |
|
97 |
byte[] protect(PrivateKey key) |
|
98 |
throws Exception |
|
99 |
{ |
|
100 |
// create a random salt (8 bytes) |
|
101 |
byte[] salt = new byte[8]; |
|
102 |
SunJCE.RANDOM.nextBytes(salt); |
|
103 |
||
104 |
// create PBE parameters from salt and iteration count |
|
105 |
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20); |
|
106 |
||
107 |
// create PBE key from password |
|
108 |
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password); |
|
109 |
SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES"); |
|
110 |
pbeKeySpec.clearPassword(); |
|
111 |
||
112 |
// encrypt private key |
|
113 |
PBEWithMD5AndTripleDESCipher cipher; |
|
114 |
cipher = new PBEWithMD5AndTripleDESCipher(); |
|
115 |
cipher.engineInit(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
116 |
byte[] plain = key.getEncoded(); |
2 | 117 |
byte[] encrKey = cipher.engineDoFinal(plain, 0, plain.length); |
118 |
||
119 |
// wrap encrypted private key in EncryptedPrivateKeyInfo |
|
120 |
// (as defined in PKCS#8) |
|
121 |
AlgorithmParameters pbeParams = |
|
122 |
AlgorithmParameters.getInstance("PBE", PROV); |
|
123 |
pbeParams.init(pbeSpec); |
|
124 |
||
125 |
AlgorithmId encrAlg = new AlgorithmId |
|
126 |
(new ObjectIdentifier(PBE_WITH_MD5_AND_DES3_CBC_OID), pbeParams); |
|
127 |
return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded(); |
|
128 |
} |
|
129 |
||
130 |
/* |
|
131 |
* Recovers the cleartext version of the given key (in protected format), |
|
132 |
* using the password provided at construction time. |
|
133 |
*/ |
|
134 |
Key recover(EncryptedPrivateKeyInfo encrInfo) |
|
135 |
throws UnrecoverableKeyException, NoSuchAlgorithmException |
|
136 |
{ |
|
137 |
byte[] plain; |
|
138 |
||
139 |
try { |
|
140 |
String encrAlg = encrInfo.getAlgorithm().getOID().toString(); |
|
141 |
if (!encrAlg.equals(PBE_WITH_MD5_AND_DES3_CBC_OID) |
|
142 |
&& !encrAlg.equals(KEY_PROTECTOR_OID)) { |
|
143 |
throw new UnrecoverableKeyException("Unsupported encryption " |
|
144 |
+ "algorithm"); |
|
145 |
} |
|
146 |
||
147 |
if (encrAlg.equals(KEY_PROTECTOR_OID)) { |
|
148 |
// JDK 1.2 style recovery |
|
149 |
plain = recover(encrInfo.getEncryptedData()); |
|
150 |
} else { |
|
151 |
byte[] encodedParams = |
|
152 |
encrInfo.getAlgorithm().getEncodedParams(); |
|
153 |
||
154 |
// parse the PBE parameters into the corresponding spec |
|
155 |
AlgorithmParameters pbeParams = |
|
156 |
AlgorithmParameters.getInstance("PBE"); |
|
157 |
pbeParams.init(encodedParams); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
158 |
PBEParameterSpec pbeSpec = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
159 |
pbeParams.getParameterSpec(PBEParameterSpec.class); |
2 | 160 |
|
161 |
// create PBE key from password |
|
162 |
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password); |
|
163 |
SecretKey sKey = |
|
164 |
new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES"); |
|
165 |
pbeKeySpec.clearPassword(); |
|
166 |
||
167 |
// decrypt private key |
|
168 |
PBEWithMD5AndTripleDESCipher cipher; |
|
169 |
cipher = new PBEWithMD5AndTripleDESCipher(); |
|
170 |
cipher.engineInit(Cipher.DECRYPT_MODE, sKey, pbeSpec, null); |
|
171 |
plain=cipher.engineDoFinal(encrInfo.getEncryptedData(), 0, |
|
172 |
encrInfo.getEncryptedData().length); |
|
173 |
} |
|
174 |
||
175 |
// determine the private-key algorithm, and parse private key |
|
176 |
// using the appropriate key factory |
|
177 |
String oidName = new AlgorithmId |
|
178 |
(new PrivateKeyInfo(plain).getAlgorithm().getOID()).getName(); |
|
179 |
KeyFactory kFac = KeyFactory.getInstance(oidName); |
|
180 |
return kFac.generatePrivate(new PKCS8EncodedKeySpec(plain)); |
|
181 |
||
182 |
} catch (NoSuchAlgorithmException ex) { |
|
183 |
// Note: this catch needed to be here because of the |
|
184 |
// later catch of GeneralSecurityException |
|
185 |
throw ex; |
|
186 |
} catch (IOException ioe) { |
|
187 |
throw new UnrecoverableKeyException(ioe.getMessage()); |
|
188 |
} catch (GeneralSecurityException gse) { |
|
189 |
throw new UnrecoverableKeyException(gse.getMessage()); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
/* |
|
194 |
* Recovers the cleartext version of the given key (in protected format), |
|
195 |
* using the password provided at construction time. This method implements |
|
196 |
* the recovery algorithm used by Sun's keystore implementation in |
|
197 |
* JDK 1.2. |
|
198 |
*/ |
|
199 |
private byte[] recover(byte[] protectedKey) |
|
200 |
throws UnrecoverableKeyException, NoSuchAlgorithmException |
|
201 |
{ |
|
202 |
int i, j; |
|
203 |
byte[] digest; |
|
204 |
int numRounds; |
|
205 |
int xorOffset; // offset in xorKey where next digest will be stored |
|
206 |
int encrKeyLen; // the length of the encrpyted key |
|
207 |
||
208 |
MessageDigest md = MessageDigest.getInstance("SHA"); |
|
209 |
||
210 |
// Get the salt associated with this key (the first SALT_LEN bytes of |
|
211 |
// <code>protectedKey</code>) |
|
212 |
byte[] salt = new byte[SALT_LEN]; |
|
213 |
System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN); |
|
214 |
||
215 |
// Determine the number of digest rounds |
|
216 |
encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN; |
|
217 |
numRounds = encrKeyLen / DIGEST_LEN; |
|
218 |
if ((encrKeyLen % DIGEST_LEN) != 0) |
|
219 |
numRounds++; |
|
220 |
||
221 |
// Get the encrypted key portion and store it in "encrKey" |
|
222 |
byte[] encrKey = new byte[encrKeyLen]; |
|
223 |
System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen); |
|
224 |
||
225 |
// Set up the byte array which will be XORed with "encrKey" |
|
226 |
byte[] xorKey = new byte[encrKey.length]; |
|
227 |
||
228 |
// Convert password to byte array, so that it can be digested |
|
229 |
byte[] passwdBytes = new byte[password.length * 2]; |
|
230 |
for (i=0, j=0; i<password.length; i++) { |
|
231 |
passwdBytes[j++] = (byte)(password[i] >> 8); |
|
232 |
passwdBytes[j++] = (byte)password[i]; |
|
233 |
} |
|
234 |
||
235 |
// Compute the digests, and store them in "xorKey" |
|
236 |
for (i = 0, xorOffset = 0, digest = salt; |
|
237 |
i < numRounds; |
|
238 |
i++, xorOffset += DIGEST_LEN) { |
|
239 |
md.update(passwdBytes); |
|
240 |
md.update(digest); |
|
241 |
digest = md.digest(); |
|
242 |
md.reset(); |
|
243 |
// Copy the digest into "xorKey" |
|
244 |
if (i < numRounds - 1) { |
|
245 |
System.arraycopy(digest, 0, xorKey, xorOffset, |
|
246 |
digest.length); |
|
247 |
} else { |
|
248 |
System.arraycopy(digest, 0, xorKey, xorOffset, |
|
249 |
xorKey.length - xorOffset); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
// XOR "encrKey" with "xorKey", and store the result in "plainKey" |
|
254 |
byte[] plainKey = new byte[encrKey.length]; |
|
255 |
for (i = 0; i < plainKey.length; i++) { |
|
256 |
plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]); |
|
257 |
} |
|
258 |
||
259 |
// Check the integrity of the recovered key by concatenating it with |
|
260 |
// the password, digesting the concatenation, and comparing the |
|
261 |
// result of the digest operation with the digest provided at the end |
|
262 |
// of <code>protectedKey</code>. If the two digest values are |
|
263 |
// different, throw an exception. |
|
264 |
md.update(passwdBytes); |
|
265 |
java.util.Arrays.fill(passwdBytes, (byte)0x00); |
|
266 |
passwdBytes = null; |
|
267 |
md.update(plainKey); |
|
268 |
digest = md.digest(); |
|
269 |
md.reset(); |
|
270 |
for (i = 0; i < digest.length; i++) { |
|
271 |
if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) { |
|
272 |
throw new UnrecoverableKeyException("Cannot recover key"); |
|
273 |
} |
|
274 |
} |
|
275 |
return plainKey; |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* Seals the given cleartext key, using the password provided at |
|
280 |
* construction time |
|
281 |
*/ |
|
282 |
SealedObject seal(Key key) |
|
283 |
throws Exception |
|
284 |
{ |
|
285 |
// create a random salt (8 bytes) |
|
286 |
byte[] salt = new byte[8]; |
|
287 |
SunJCE.RANDOM.nextBytes(salt); |
|
288 |
||
289 |
// create PBE parameters from salt and iteration count |
|
290 |
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20); |
|
291 |
||
292 |
// create PBE key from password |
|
293 |
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password); |
|
294 |
SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES"); |
|
295 |
pbeKeySpec.clearPassword(); |
|
296 |
||
297 |
// seal key |
|
298 |
Cipher cipher; |
|
299 |
||
300 |
PBEWithMD5AndTripleDESCipher cipherSpi; |
|
301 |
cipherSpi = new PBEWithMD5AndTripleDESCipher(); |
|
302 |
cipher = new CipherForKeyProtector(cipherSpi, PROV, |
|
303 |
"PBEWithMD5AndTripleDES"); |
|
304 |
cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeSpec); |
|
305 |
return new SealedObjectForKeyProtector(key, cipher); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Unseals the sealed key. |
|
310 |
*/ |
|
311 |
Key unseal(SealedObject so) |
|
312 |
throws NoSuchAlgorithmException, UnrecoverableKeyException |
|
313 |
{ |
|
314 |
try { |
|
315 |
// create PBE key from password |
|
316 |
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password); |
|
317 |
SecretKey skey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES"); |
|
318 |
pbeKeySpec.clearPassword(); |
|
319 |
||
320 |
SealedObjectForKeyProtector soForKeyProtector = null; |
|
321 |
if (!(so instanceof SealedObjectForKeyProtector)) { |
|
322 |
soForKeyProtector = new SealedObjectForKeyProtector(so); |
|
323 |
} else { |
|
324 |
soForKeyProtector = (SealedObjectForKeyProtector)so; |
|
325 |
} |
|
326 |
AlgorithmParameters params = soForKeyProtector.getParameters(); |
|
327 |
if (params == null) { |
|
328 |
throw new UnrecoverableKeyException("Cannot get " + |
|
329 |
"algorithm parameters"); |
|
330 |
} |
|
331 |
PBEWithMD5AndTripleDESCipher cipherSpi; |
|
332 |
cipherSpi = new PBEWithMD5AndTripleDESCipher(); |
|
333 |
Cipher cipher = new CipherForKeyProtector(cipherSpi, PROV, |
|
334 |
"PBEWithMD5AndTripleDES"); |
|
335 |
cipher.init(Cipher.DECRYPT_MODE, skey, params); |
|
336 |
return (Key)soForKeyProtector.getObject(cipher); |
|
337 |
} catch (NoSuchAlgorithmException ex) { |
|
338 |
// Note: this catch needed to be here because of the |
|
339 |
// later catch of GeneralSecurityException |
|
340 |
throw ex; |
|
341 |
} catch (IOException ioe) { |
|
342 |
throw new UnrecoverableKeyException(ioe.getMessage()); |
|
343 |
} catch (ClassNotFoundException cnfe) { |
|
344 |
throw new UnrecoverableKeyException(cnfe.getMessage()); |
|
345 |
} catch (GeneralSecurityException gse) { |
|
346 |
throw new UnrecoverableKeyException(gse.getMessage()); |
|
347 |
} |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
||
352 |
final class CipherForKeyProtector extends javax.crypto.Cipher { |
|
353 |
/** |
|
354 |
* Creates a Cipher object. |
|
355 |
* |
|
356 |
* @param cipherSpi the delegate |
|
357 |
* @param provider the provider |
|
358 |
* @param transformation the transformation |
|
359 |
*/ |
|
360 |
protected CipherForKeyProtector(CipherSpi cipherSpi, |
|
361 |
Provider provider, |
|
362 |
String transformation) { |
|
363 |
super(cipherSpi, provider, transformation); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
final class SealedObjectForKeyProtector extends javax.crypto.SealedObject { |
|
368 |
||
369 |
static final long serialVersionUID = -3650226485480866989L; |
|
370 |
||
371 |
SealedObjectForKeyProtector(Serializable object, Cipher c) |
|
372 |
throws IOException, IllegalBlockSizeException { |
|
373 |
super(object, c); |
|
374 |
} |
|
375 |
||
376 |
SealedObjectForKeyProtector(SealedObject so) { |
|
377 |
super(so); |
|
378 |
} |
|
379 |
||
380 |
AlgorithmParameters getParameters() { |
|
381 |
AlgorithmParameters params = null; |
|
382 |
if (super.encodedParams != null) { |
|
383 |
try { |
|
384 |
params = AlgorithmParameters.getInstance("PBE", "SunJCE"); |
|
385 |
params.init(super.encodedParams); |
|
386 |
} catch (NoSuchProviderException nspe) { |
|
387 |
// eat. |
|
388 |
} catch (NoSuchAlgorithmException nsae) { |
|
389 |
//eat. |
|
390 |
} catch (IOException ioe) { |
|
391 |
//eat. |
|
392 |
} |
|
393 |
} |
|
394 |
return params; |
|
395 |
} |
|
396 |
} |