author | vinnie |
Thu, 11 Apr 2013 17:57:08 +0100 | |
changeset 16909 | 78a1749a43e2 |
parent 10336 | 0bb1999251f8 |
child 16910 | d0ed88d92afe |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
10336
diff
changeset
|
2 |
* Copyright (c) 2005, 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 com.sun.crypto.provider; |
|
27 |
||
4348 | 28 |
import java.io.ObjectStreamException; |
2 | 29 |
import java.nio.ByteBuffer; |
30 |
import java.nio.CharBuffer; |
|
31 |
import java.nio.charset.Charset; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.security.KeyRep; |
|
34 |
import java.security.GeneralSecurityException; |
|
35 |
import java.security.NoSuchAlgorithmException; |
|
36 |
import java.security.spec.InvalidKeySpecException; |
|
37 |
import javax.crypto.Mac; |
|
38 |
import javax.crypto.SecretKey; |
|
39 |
import javax.crypto.spec.PBEKeySpec; |
|
40 |
||
41 |
/** |
|
42 |
* This class represents a PBE key derived using PBKDF2 defined |
|
43 |
* in PKCS#5 v2.0. meaning that |
|
44 |
* 1) the password must consist of characters which will be converted |
|
45 |
* to bytes using UTF-8 character encoding. |
|
46 |
* 2) salt, iteration count, and to be derived key length are supplied |
|
47 |
* |
|
48 |
* @author Valerie Peng |
|
49 |
* |
|
50 |
*/ |
|
51 |
final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey { |
|
52 |
||
53 |
static final long serialVersionUID = -2234868909660948157L; |
|
54 |
||
55 |
private char[] passwd; |
|
56 |
private byte[] salt; |
|
57 |
private int iterCount; |
|
58 |
private byte[] key; |
|
59 |
||
60 |
private Mac prf; |
|
61 |
||
62 |
private static byte[] getPasswordBytes(char[] passwd) { |
|
63 |
Charset utf8 = Charset.forName("UTF-8"); |
|
64 |
CharBuffer cb = CharBuffer.wrap(passwd); |
|
65 |
ByteBuffer bb = utf8.encode(cb); |
|
66 |
||
67 |
int len = bb.limit(); |
|
68 |
byte[] passwdBytes = new byte[len]; |
|
69 |
bb.get(passwdBytes, 0, len); |
|
70 |
||
71 |
return passwdBytes; |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Creates a PBE key from a given PBE key specification. |
|
76 |
* |
|
77 |
* @param key the given PBE key specification |
|
78 |
*/ |
|
79 |
PBKDF2KeyImpl(PBEKeySpec keySpec, String prfAlgo) |
|
80 |
throws InvalidKeySpecException { |
|
81 |
char[] passwd = keySpec.getPassword(); |
|
82 |
if (passwd == null) { |
|
83 |
// Should allow an empty password. |
|
84 |
this.passwd = new char[0]; |
|
85 |
} else { |
|
86 |
this.passwd = passwd.clone(); |
|
87 |
} |
|
88 |
// Convert the password from char[] to byte[] |
|
89 |
byte[] passwdBytes = getPasswordBytes(this.passwd); |
|
90 |
||
91 |
this.salt = keySpec.getSalt(); |
|
92 |
if (salt == null) { |
|
93 |
throw new InvalidKeySpecException("Salt not found"); |
|
94 |
} |
|
95 |
this.iterCount = keySpec.getIterationCount(); |
|
96 |
if (iterCount == 0) { |
|
97 |
throw new InvalidKeySpecException("Iteration count not found"); |
|
98 |
} else if (iterCount < 0) { |
|
99 |
throw new InvalidKeySpecException("Iteration count is negative"); |
|
100 |
} |
|
101 |
int keyLength = keySpec.getKeyLength(); |
|
102 |
if (keyLength == 0) { |
|
103 |
throw new InvalidKeySpecException("Key length not found"); |
|
104 |
} else if (keyLength == 0) { |
|
105 |
throw new InvalidKeySpecException("Key length is negative"); |
|
106 |
} |
|
107 |
try { |
|
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
10336
diff
changeset
|
108 |
this.prf = Mac.getInstance(prfAlgo, SunJCE.getInstance()); |
2 | 109 |
} catch (NoSuchAlgorithmException nsae) { |
110 |
// not gonna happen; re-throw just in case |
|
111 |
InvalidKeySpecException ike = new InvalidKeySpecException(); |
|
112 |
ike.initCause(nsae); |
|
113 |
throw ike; |
|
114 |
} |
|
115 |
this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength); |
|
116 |
} |
|
117 |
||
7043 | 118 |
private static byte[] deriveKey(final Mac prf, final byte[] password, |
119 |
byte[] salt, int iterCount, int keyLengthInBit) { |
|
2 | 120 |
int keyLength = keyLengthInBit/8; |
121 |
byte[] key = new byte[keyLength]; |
|
122 |
try { |
|
123 |
int hlen = prf.getMacLength(); |
|
124 |
int intL = (keyLength + hlen - 1)/hlen; // ceiling |
|
125 |
int intR = keyLength - (intL - 1)*hlen; // residue |
|
126 |
byte[] ui = new byte[hlen]; |
|
127 |
byte[] ti = new byte[hlen]; |
|
4348 | 128 |
// SecretKeySpec cannot be used, since password can be empty here. |
129 |
SecretKey macKey = new SecretKey() { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
130 |
private static final long serialVersionUID = 7874493593505141603L; |
4348 | 131 |
@Override |
132 |
public String getAlgorithm() { |
|
133 |
return prf.getAlgorithm(); |
|
134 |
} |
|
135 |
@Override |
|
136 |
public String getFormat() { |
|
137 |
return "RAW"; |
|
138 |
} |
|
139 |
@Override |
|
140 |
public byte[] getEncoded() { |
|
141 |
return password; |
|
142 |
} |
|
143 |
@Override |
|
144 |
public int hashCode() { |
|
145 |
return Arrays.hashCode(password) * 41 + |
|
146 |
prf.getAlgorithm().toLowerCase().hashCode(); |
|
147 |
} |
|
148 |
@Override |
|
149 |
public boolean equals(Object obj) { |
|
150 |
if (this == obj) return true; |
|
151 |
if (this.getClass() != obj.getClass()) return false; |
|
152 |
SecretKey sk = (SecretKey)obj; |
|
7043 | 153 |
return prf.getAlgorithm().equalsIgnoreCase( |
154 |
sk.getAlgorithm()) && |
|
155 |
Arrays.equals(password, sk.getEncoded()); |
|
4348 | 156 |
} |
157 |
}; |
|
2 | 158 |
prf.init(macKey); |
159 |
||
160 |
byte[] ibytes = new byte[4]; |
|
161 |
for (int i = 1; i <= intL; i++) { |
|
162 |
prf.update(salt); |
|
163 |
ibytes[3] = (byte) i; |
|
164 |
ibytes[2] = (byte) ((i >> 8) & 0xff); |
|
165 |
ibytes[1] = (byte) ((i >> 16) & 0xff); |
|
166 |
ibytes[0] = (byte) ((i >> 24) & 0xff); |
|
167 |
prf.update(ibytes); |
|
168 |
prf.doFinal(ui, 0); |
|
169 |
System.arraycopy(ui, 0, ti, 0, ui.length); |
|
170 |
||
171 |
for (int j = 2; j <= iterCount; j++) { |
|
172 |
prf.update(ui); |
|
173 |
prf.doFinal(ui, 0); |
|
174 |
// XOR the intermediate Ui's together. |
|
175 |
for (int k = 0; k < ui.length; k++) { |
|
176 |
ti[k] ^= ui[k]; |
|
177 |
} |
|
178 |
} |
|
179 |
if (i == intL) { |
|
180 |
System.arraycopy(ti, 0, key, (i-1)*hlen, intR); |
|
181 |
} else { |
|
182 |
System.arraycopy(ti, 0, key, (i-1)*hlen, hlen); |
|
183 |
} |
|
184 |
} |
|
185 |
} catch (GeneralSecurityException gse) { |
|
186 |
throw new RuntimeException("Error deriving PBKDF2 keys"); |
|
187 |
} |
|
188 |
return key; |
|
189 |
} |
|
190 |
||
191 |
public byte[] getEncoded() { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
192 |
return key.clone(); |
2 | 193 |
} |
194 |
||
195 |
public String getAlgorithm() { |
|
196 |
return "PBKDF2With" + prf.getAlgorithm(); |
|
197 |
} |
|
198 |
||
199 |
public int getIterationCount() { |
|
200 |
return iterCount; |
|
201 |
} |
|
202 |
||
203 |
public char[] getPassword() { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
204 |
return passwd.clone(); |
2 | 205 |
} |
206 |
||
207 |
public byte[] getSalt() { |
|
208 |
return salt.clone(); |
|
209 |
} |
|
210 |
||
211 |
public String getFormat() { |
|
212 |
return "RAW"; |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Calculates a hash code value for the object. |
|
217 |
* Objects that are equal will also have the same hashcode. |
|
218 |
*/ |
|
219 |
public int hashCode() { |
|
220 |
int retval = 0; |
|
221 |
for (int i = 1; i < this.key.length; i++) { |
|
222 |
retval += this.key[i] * i; |
|
223 |
} |
|
224 |
return(retval ^= getAlgorithm().toLowerCase().hashCode()); |
|
225 |
} |
|
226 |
||
227 |
public boolean equals(Object obj) { |
|
228 |
if (obj == this) |
|
229 |
return true; |
|
230 |
||
231 |
if (!(obj instanceof SecretKey)) |
|
232 |
return false; |
|
233 |
||
234 |
SecretKey that = (SecretKey) obj; |
|
235 |
||
236 |
if (!(that.getAlgorithm().equalsIgnoreCase(getAlgorithm()))) |
|
237 |
return false; |
|
238 |
if (!(that.getFormat().equalsIgnoreCase("RAW"))) |
|
239 |
return false; |
|
240 |
byte[] thatEncoded = that.getEncoded(); |
|
241 |
boolean ret = Arrays.equals(key, that.getEncoded()); |
|
242 |
java.util.Arrays.fill(thatEncoded, (byte)0x00); |
|
243 |
return ret; |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Replace the PBE key to be serialized. |
|
248 |
* |
|
249 |
* @return the standard KeyRep object to be serialized |
|
250 |
* |
|
251 |
* @throws ObjectStreamException if a new object representing |
|
252 |
* this PBE key could not be created |
|
253 |
*/ |
|
4348 | 254 |
private Object writeReplace() throws ObjectStreamException { |
2 | 255 |
return new KeyRep(KeyRep.Type.SECRET, getAlgorithm(), |
256 |
getFormat(), getEncoded()); |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Ensures that the password bytes of this key are |
|
261 |
* erased when there are no more references to it. |
|
262 |
*/ |
|
263 |
protected void finalize() throws Throwable { |
|
264 |
try { |
|
265 |
if (this.passwd != null) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
266 |
java.util.Arrays.fill(this.passwd, '0'); |
2 | 267 |
this.passwd = null; |
268 |
} |
|
269 |
if (this.key != null) { |
|
270 |
java.util.Arrays.fill(this.key, (byte)0x00); |
|
271 |
this.key = null; |
|
272 |
} |
|
273 |
} finally { |
|
274 |
super.finalize(); |
|
275 |
} |
|
276 |
} |
|
277 |
} |