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