author | weijun |
Fri, 12 Jan 2018 08:06:24 +0800 | |
changeset 49783 | 977c6dd636bd |
parent 47417 | 5984d1c9d03d |
child 51293 | 53c3b460503c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49783 | 2 |
* Copyright (c) 1998, 2018, 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.security.Key; |
|
30 |
import java.security.PrivateKey; |
|
31 |
import java.security.Provider; |
|
32 |
import java.security.KeyFactory; |
|
33 |
import java.security.MessageDigest; |
|
34 |
import java.security.GeneralSecurityException; |
|
35 |
import java.security.NoSuchAlgorithmException; |
|
36 |
import java.security.UnrecoverableKeyException; |
|
37 |
import java.security.AlgorithmParameters; |
|
47417 | 38 |
import java.security.spec.InvalidParameterSpecException; |
2 | 39 |
import java.security.spec.PKCS8EncodedKeySpec; |
40 |
||
41 |
import javax.crypto.Cipher; |
|
42 |
import javax.crypto.CipherSpi; |
|
43 |
import javax.crypto.SecretKey; |
|
44 |
import javax.crypto.SealedObject; |
|
45 |
import javax.crypto.spec.*; |
|
46 |
import sun.security.x509.AlgorithmId; |
|
47 |
import sun.security.util.ObjectIdentifier; |
|
48 |
||
49 |
/** |
|
50 |
* This class implements a protection mechanism for private keys. In JCE, we |
|
51 |
* use a stronger protection mechanism than in the JDK, because we can use |
|
52 |
* the <code>Cipher</code> class. |
|
53 |
* Private keys are protected using the JCE mechanism, and are recovered using |
|
54 |
* either the JDK or JCE mechanism, depending on how the key has been |
|
55 |
* protected. This allows us to parse Sun's keystore implementation that ships |
|
56 |
* with JDK 1.2. |
|
57 |
* |
|
58 |
* @author Jan Luehe |
|
59 |
* |
|
60 |
* |
|
61 |
* @see JceKeyStore |
|
62 |
*/ |
|
63 |
||
64 |
final class KeyProtector { |
|
65 |
||
66 |
// defined by SunSoft (SKI project) |
|
67 |
private static final String PBE_WITH_MD5_AND_DES3_CBC_OID |
|
68 |
= "1.3.6.1.4.1.42.2.19.1"; |
|
69 |
||
70 |
// JavaSoft proprietary key-protection algorithm (used to protect private |
|
71 |
// keys in the keystore implementation that comes with JDK 1.2) |
|
72 |
private static final String KEY_PROTECTOR_OID = "1.3.6.1.4.1.42.2.17.1.1"; |
|
73 |
||
47417 | 74 |
private static final int MAX_ITERATION_COUNT = 5000000; |
75 |
private static final int ITERATION_COUNT = 200000; |
|
2 | 76 |
private static final int SALT_LEN = 20; // the salt length |
77 |
private static final int DIGEST_LEN = 20; |
|
78 |
||
79 |
// the password used for protecting/recovering keys passed through this |
|
80 |
// key protector |
|
81 |
private char[] password; |
|
82 |
||
83 |
KeyProtector(char[] password) { |
|
84 |
if (password == null) { |
|
85 |
throw new IllegalArgumentException("password can't be null"); |
|
86 |
} |
|
87 |
this.password = password; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Protects the given cleartext private key, using the password provided at |
|
92 |
* construction time. |
|
93 |
*/ |
|
94 |
byte[] protect(PrivateKey key) |
|
95 |
throws Exception |
|
96 |
{ |
|
97 |
// create a random salt (8 bytes) |
|
98 |
byte[] salt = new byte[8]; |
|
15010 | 99 |
SunJCE.getRandom().nextBytes(salt); |
2 | 100 |
|
101 |
// create PBE parameters from salt and iteration count |
|
47417 | 102 |
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT); |
2 | 103 |
|
104 |
// create PBE key from password |
|
105 |
PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password); |
|
106 |
SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES"); |
|
107 |
pbeKeySpec.clearPassword(); |
|
108 |
||
109 |
// encrypt private key |
|
110 |
PBEWithMD5AndTripleDESCipher cipher; |
|
111 |
cipher = new PBEWithMD5AndTripleDESCipher(); |
|
112 |
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
|
113 |
byte[] plain = key.getEncoded(); |
2 | 114 |
byte[] encrKey = cipher.engineDoFinal(plain, 0, plain.length); |
115 |
||
116 |
// wrap encrypted private key in EncryptedPrivateKeyInfo |
|
117 |
// (as defined in PKCS#8) |
|
118 |
AlgorithmParameters pbeParams = |
|
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
119 |
AlgorithmParameters.getInstance("PBE", SunJCE.getInstance()); |
2 | 120 |
pbeParams.init(pbeSpec); |
121 |
||
122 |
AlgorithmId encrAlg = new AlgorithmId |
|
123 |
(new ObjectIdentifier(PBE_WITH_MD5_AND_DES3_CBC_OID), pbeParams); |
|
124 |
return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded(); |
|
125 |
} |
|
126 |
||
127 |
/* |
|
128 |
* Recovers the cleartext version of the given key (in protected format), |
|
129 |
* using the password provided at construction time. |
|
130 |
*/ |
|
131 |
Key recover(EncryptedPrivateKeyInfo encrInfo) |
|
132 |
throws UnrecoverableKeyException, NoSuchAlgorithmException |
|
133 |
{ |
|
134 |
byte[] plain; |
|
135 |
||
136 |
try { |
|
137 |
String encrAlg = encrInfo.getAlgorithm().getOID().toString(); |
|
138 |
if (!encrAlg.equals(PBE_WITH_MD5_AND_DES3_CBC_OID) |
|
139 |
&& !encrAlg.equals(KEY_PROTECTOR_OID)) { |
|
140 |
throw new UnrecoverableKeyException("Unsupported encryption " |
|
141 |
+ "algorithm"); |
|
142 |
} |
|
143 |
||
144 |
if (encrAlg.equals(KEY_PROTECTOR_OID)) { |
|
145 |
// JDK 1.2 style recovery |
|
146 |
plain = recover(encrInfo.getEncryptedData()); |
|
147 |
} else { |
|
148 |
byte[] encodedParams = |
|
149 |
encrInfo.getAlgorithm().getEncodedParams(); |
|
150 |
||
151 |
// parse the PBE parameters into the corresponding spec |
|
152 |
AlgorithmParameters pbeParams = |
|
153 |
AlgorithmParameters.getInstance("PBE"); |
|
154 |
pbeParams.init(encodedParams); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
155 |
PBEParameterSpec pbeSpec = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
156 |
pbeParams.getParameterSpec(PBEParameterSpec.class); |
47417 | 157 |
if (pbeSpec.getIterationCount() > MAX_ITERATION_COUNT) { |
158 |
throw new IOException("PBE iteration count too large"); |
|
159 |
} |
|
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]; |
|
15010 | 287 |
SunJCE.getRandom().nextBytes(salt); |
2 | 288 |
|
289 |
// create PBE parameters from salt and iteration count |
|
47417 | 290 |
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT); |
2 | 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(); |
|
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
302 |
cipher = new CipherForKeyProtector(cipherSpi, SunJCE.getInstance(), |
2 | 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 |
} |
|
47417 | 331 |
PBEParameterSpec pbeSpec; |
332 |
try { |
|
333 |
pbeSpec = params.getParameterSpec(PBEParameterSpec.class); |
|
334 |
} catch (InvalidParameterSpecException ipse) { |
|
335 |
throw new IOException("Invalid PBE algorithm parameters"); |
|
336 |
} |
|
337 |
if (pbeSpec.getIterationCount() > MAX_ITERATION_COUNT) { |
|
338 |
throw new IOException("PBE iteration count too large"); |
|
339 |
} |
|
2 | 340 |
PBEWithMD5AndTripleDESCipher cipherSpi; |
341 |
cipherSpi = new PBEWithMD5AndTripleDESCipher(); |
|
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
342 |
Cipher cipher = new CipherForKeyProtector(cipherSpi, |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
343 |
SunJCE.getInstance(), |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
344 |
"PBEWithMD5AndTripleDES"); |
2 | 345 |
cipher.init(Cipher.DECRYPT_MODE, skey, params); |
49783 | 346 |
return soForKeyProtector.getKey(cipher); |
2 | 347 |
} catch (NoSuchAlgorithmException ex) { |
348 |
// Note: this catch needed to be here because of the |
|
349 |
// later catch of GeneralSecurityException |
|
350 |
throw ex; |
|
351 |
} catch (IOException ioe) { |
|
352 |
throw new UnrecoverableKeyException(ioe.getMessage()); |
|
353 |
} catch (ClassNotFoundException cnfe) { |
|
354 |
throw new UnrecoverableKeyException(cnfe.getMessage()); |
|
355 |
} catch (GeneralSecurityException gse) { |
|
356 |
throw new UnrecoverableKeyException(gse.getMessage()); |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
||
362 |
final class CipherForKeyProtector extends javax.crypto.Cipher { |
|
363 |
/** |
|
364 |
* Creates a Cipher object. |
|
365 |
* |
|
366 |
* @param cipherSpi the delegate |
|
367 |
* @param provider the provider |
|
368 |
* @param transformation the transformation |
|
369 |
*/ |
|
370 |
protected CipherForKeyProtector(CipherSpi cipherSpi, |
|
371 |
Provider provider, |
|
372 |
String transformation) { |
|
373 |
super(cipherSpi, provider, transformation); |
|
374 |
} |
|
375 |
} |