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