author | valeriep |
Tue, 20 Mar 2012 15:06:13 -0700 | |
changeset 12201 | d77ed23f4992 |
parent 10336 | 0bb1999251f8 |
child 16080 | 0e6266b88242 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12201
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, 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 sun.security.pkcs11; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.spec.*; |
|
32 |
||
33 |
import javax.crypto.*; |
|
34 |
import javax.crypto.interfaces.*; |
|
35 |
import javax.crypto.spec.*; |
|
36 |
||
37 |
import static sun.security.pkcs11.TemplateManager.*; |
|
38 |
import sun.security.pkcs11.wrapper.*; |
|
39 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
40 |
||
41 |
/** |
|
42 |
* KeyAgreement implementation class. This class currently supports |
|
43 |
* DH. |
|
44 |
* |
|
45 |
* @author Andreas Sterbenz |
|
46 |
* @since 1.5 |
|
47 |
*/ |
|
48 |
final class P11KeyAgreement extends KeyAgreementSpi { |
|
49 |
||
50 |
// token instance |
|
51 |
private final Token token; |
|
52 |
||
53 |
// algorithm name |
|
54 |
private final String algorithm; |
|
55 |
||
56 |
// mechanism id |
|
57 |
private final long mechanism; |
|
58 |
||
59 |
// private key, if initialized |
|
60 |
private P11Key privateKey; |
|
61 |
||
62 |
// other sides public value ("y"), if doPhase() already called |
|
63 |
private BigInteger publicValue; |
|
64 |
||
65 |
// length of the secret to be derived |
|
66 |
private int secretLen; |
|
67 |
||
68 |
// KeyAgreement from SunJCE as fallback for > 2 party agreement |
|
69 |
private KeyAgreement multiPartyAgreement; |
|
70 |
||
71 |
P11KeyAgreement(Token token, String algorithm, long mechanism) { |
|
72 |
super(); |
|
73 |
this.token = token; |
|
74 |
this.algorithm = algorithm; |
|
75 |
this.mechanism = mechanism; |
|
76 |
} |
|
77 |
||
78 |
// see JCE spec |
|
79 |
protected void engineInit(Key key, SecureRandom random) |
|
80 |
throws InvalidKeyException { |
|
81 |
if (key instanceof PrivateKey == false) { |
|
82 |
throw new InvalidKeyException |
|
83 |
("Key must be instance of PrivateKey"); |
|
84 |
} |
|
85 |
privateKey = P11KeyFactory.convertKey(token, key, algorithm); |
|
86 |
publicValue = null; |
|
87 |
multiPartyAgreement = null; |
|
88 |
} |
|
89 |
||
90 |
// see JCE spec |
|
91 |
protected void engineInit(Key key, AlgorithmParameterSpec params, |
|
92 |
SecureRandom random) throws InvalidKeyException, |
|
93 |
InvalidAlgorithmParameterException { |
|
94 |
if (params != null) { |
|
95 |
throw new InvalidAlgorithmParameterException |
|
96 |
("Parameters not supported"); |
|
97 |
} |
|
98 |
engineInit(key, random); |
|
99 |
} |
|
100 |
||
101 |
// see JCE spec |
|
102 |
protected Key engineDoPhase(Key key, boolean lastPhase) |
|
103 |
throws InvalidKeyException, IllegalStateException { |
|
104 |
if (privateKey == null) { |
|
105 |
throw new IllegalStateException("Not initialized"); |
|
106 |
} |
|
107 |
if (publicValue != null) { |
|
108 |
throw new IllegalStateException("Phase already executed"); |
|
109 |
} |
|
110 |
// PKCS#11 only allows key agreement between 2 parties |
|
111 |
// JCE allows >= 2 parties. To support that case (for compatibility |
|
112 |
// and to pass JCK), fall back to SunJCE in this case. |
|
113 |
// NOTE that we initialize using the P11Key, which will fail if it |
|
114 |
// is sensitive/unextractable. However, this is not an issue in the |
|
115 |
// compatibility configuration, which is all we are targeting here. |
|
116 |
if ((multiPartyAgreement != null) || (lastPhase == false)) { |
|
117 |
if (multiPartyAgreement == null) { |
|
118 |
try { |
|
119 |
multiPartyAgreement = KeyAgreement.getInstance |
|
120 |
("DH", P11Util.getSunJceProvider()); |
|
121 |
multiPartyAgreement.init(privateKey); |
|
122 |
} catch (NoSuchAlgorithmException e) { |
|
123 |
throw new InvalidKeyException |
|
124 |
("Could not initialize multi party agreement", e); |
|
125 |
} |
|
126 |
} |
|
127 |
return multiPartyAgreement.doPhase(key, lastPhase); |
|
128 |
} |
|
129 |
if ((key instanceof PublicKey == false) |
|
130 |
|| (key.getAlgorithm().equals(algorithm) == false)) { |
|
131 |
throw new InvalidKeyException |
|
132 |
("Key must be a PublicKey with algorithm DH"); |
|
133 |
} |
|
134 |
BigInteger p, g, y; |
|
135 |
if (key instanceof DHPublicKey) { |
|
136 |
DHPublicKey dhKey = (DHPublicKey)key; |
|
137 |
y = dhKey.getY(); |
|
138 |
DHParameterSpec params = dhKey.getParams(); |
|
139 |
p = params.getP(); |
|
140 |
g = params.getG(); |
|
141 |
} else { |
|
142 |
// normally, DH PublicKeys will always implement DHPublicKey |
|
143 |
// just in case not, attempt conversion |
|
144 |
P11DHKeyFactory kf = new P11DHKeyFactory(token, "DH"); |
|
145 |
try { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
146 |
DHPublicKeySpec spec = kf.engineGetKeySpec( |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
147 |
key, DHPublicKeySpec.class); |
2 | 148 |
y = spec.getY(); |
149 |
p = spec.getP(); |
|
150 |
g = spec.getG(); |
|
151 |
} catch (InvalidKeySpecException e) { |
|
152 |
throw new InvalidKeyException("Could not obtain key values", e); |
|
153 |
} |
|
154 |
} |
|
155 |
// if parameters of private key are accessible, verify that |
|
156 |
// they match parameters of public key |
|
157 |
// XXX p and g should always be readable, even if the key is sensitive |
|
158 |
if (privateKey instanceof DHPrivateKey) { |
|
159 |
DHPrivateKey dhKey = (DHPrivateKey)privateKey; |
|
160 |
DHParameterSpec params = dhKey.getParams(); |
|
161 |
if ((p.equals(params.getP()) == false) |
|
162 |
|| (g.equals(params.getG()) == false)) { |
|
163 |
throw new InvalidKeyException |
|
164 |
("PublicKey DH parameters must match PrivateKey DH parameters"); |
|
165 |
} |
|
166 |
} |
|
167 |
publicValue = y; |
|
168 |
// length of the secret is length of key |
|
169 |
secretLen = (p.bitLength() + 7) >> 3; |
|
170 |
return null; |
|
171 |
} |
|
172 |
||
173 |
// see JCE spec |
|
174 |
protected byte[] engineGenerateSecret() throws IllegalStateException { |
|
175 |
if (multiPartyAgreement != null) { |
|
176 |
byte[] val = multiPartyAgreement.generateSecret(); |
|
177 |
multiPartyAgreement = null; |
|
178 |
return val; |
|
179 |
} |
|
180 |
if ((privateKey == null) || (publicValue == null)) { |
|
181 |
throw new IllegalStateException("Not initialized correctly"); |
|
182 |
} |
|
183 |
Session session = null; |
|
184 |
try { |
|
185 |
session = token.getOpSession(); |
|
186 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
|
187 |
new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY), |
|
188 |
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET), |
|
189 |
}; |
|
190 |
attributes = token.getAttributes |
|
191 |
(O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes); |
|
192 |
long keyID = token.p11.C_DeriveKey(session.id(), |
|
193 |
new CK_MECHANISM(mechanism, publicValue), privateKey.keyID, |
|
194 |
attributes); |
|
195 |
attributes = new CK_ATTRIBUTE[] { |
|
196 |
new CK_ATTRIBUTE(CKA_VALUE) |
|
197 |
}; |
|
198 |
token.p11.C_GetAttributeValue(session.id(), keyID, attributes); |
|
199 |
byte[] secret = attributes[0].getByteArray(); |
|
200 |
token.p11.C_DestroyObject(session.id(), keyID); |
|
12201
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
201 |
// Some vendors, e.g. NSS, trim off the leading 0x00 byte(s) from |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
202 |
// the generated secret. Thus, we need to check the secret length |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
203 |
// and trim/pad it so the returned value has the same length as |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
204 |
// the modulus size |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
205 |
if (secret.length == secretLen) { |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
206 |
return secret; |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
207 |
} else { |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
208 |
if (secret.length > secretLen) { |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
209 |
// Shouldn't happen; but check just in case |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
210 |
throw new ProviderException("generated secret is out-of-range"); |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
211 |
} |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
212 |
byte[] newSecret = new byte[secretLen]; |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
213 |
System.arraycopy(secret, 0, newSecret, secretLen - secret.length, |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
214 |
secret.length); |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
215 |
return newSecret; |
d77ed23f4992
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents:
10336
diff
changeset
|
216 |
} |
2 | 217 |
} catch (PKCS11Exception e) { |
218 |
throw new ProviderException("Could not derive key", e); |
|
219 |
} finally { |
|
220 |
publicValue = null; |
|
221 |
token.releaseSession(session); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
// see JCE spec |
|
226 |
protected int engineGenerateSecret(byte[] sharedSecret, int |
|
227 |
offset) throws IllegalStateException, ShortBufferException { |
|
228 |
if (multiPartyAgreement != null) { |
|
229 |
int n = multiPartyAgreement.generateSecret(sharedSecret, offset); |
|
230 |
multiPartyAgreement = null; |
|
231 |
return n; |
|
232 |
} |
|
233 |
if (offset + secretLen > sharedSecret.length) { |
|
234 |
throw new ShortBufferException("Need " + secretLen |
|
235 |
+ " bytes, only " + (sharedSecret.length - offset) + " available"); |
|
236 |
} |
|
237 |
byte[] secret = engineGenerateSecret(); |
|
238 |
System.arraycopy(secret, 0, sharedSecret, offset, secret.length); |
|
239 |
return secret.length; |
|
240 |
} |
|
241 |
||
242 |
// see JCE spec |
|
243 |
protected SecretKey engineGenerateSecret(String algorithm) |
|
244 |
throws IllegalStateException, NoSuchAlgorithmException, |
|
245 |
InvalidKeyException { |
|
246 |
if (multiPartyAgreement != null) { |
|
247 |
SecretKey key = multiPartyAgreement.generateSecret(algorithm); |
|
248 |
multiPartyAgreement = null; |
|
249 |
return key; |
|
250 |
} |
|
251 |
if (algorithm == null) { |
|
252 |
throw new NoSuchAlgorithmException("Algorithm must not be null"); |
|
253 |
} |
|
254 |
if (algorithm.equals("TlsPremasterSecret")) { |
|
255 |
// For now, only perform native derivation for TlsPremasterSecret |
|
256 |
// as that is required for FIPS compliance. |
|
257 |
// For other algorithms, there are unresolved issues regarding |
|
258 |
// how this should work in JCE plus a Solaris truncation bug. |
|
259 |
// (bug not yet filed). |
|
260 |
return nativeGenerateSecret(algorithm); |
|
261 |
} |
|
262 |
byte[] secret = engineGenerateSecret(); |
|
263 |
// Maintain compatibility for SunJCE: |
|
264 |
// verify secret length is sensible for algorithm / truncate |
|
265 |
// return generated key itself if possible |
|
266 |
int keyLen; |
|
267 |
if (algorithm.equalsIgnoreCase("DES")) { |
|
268 |
keyLen = 8; |
|
269 |
} else if (algorithm.equalsIgnoreCase("DESede")) { |
|
270 |
keyLen = 24; |
|
271 |
} else if (algorithm.equalsIgnoreCase("Blowfish")) { |
|
272 |
keyLen = Math.min(56, secret.length); |
|
273 |
} else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) { |
|
274 |
keyLen = secret.length; |
|
275 |
} else { |
|
276 |
throw new NoSuchAlgorithmException |
|
277 |
("Unknown algorithm " + algorithm); |
|
278 |
} |
|
279 |
if (secret.length < keyLen) { |
|
280 |
throw new InvalidKeyException("Secret too short"); |
|
281 |
} |
|
282 |
if (algorithm.equalsIgnoreCase("DES") || |
|
283 |
algorithm.equalsIgnoreCase("DESede")) { |
|
284 |
for (int i = 0; i < keyLen; i+=8) { |
|
285 |
P11SecretKeyFactory.fixDESParity(secret, i); |
|
286 |
} |
|
287 |
} |
|
288 |
return new SecretKeySpec(secret, 0, keyLen, algorithm); |
|
289 |
} |
|
290 |
||
291 |
private SecretKey nativeGenerateSecret(String algorithm) |
|
292 |
throws IllegalStateException, NoSuchAlgorithmException, |
|
293 |
InvalidKeyException { |
|
294 |
if ((privateKey == null) || (publicValue == null)) { |
|
295 |
throw new IllegalStateException("Not initialized correctly"); |
|
296 |
} |
|
297 |
long keyType = CKK_GENERIC_SECRET; |
|
298 |
Session session = null; |
|
299 |
try { |
|
300 |
session = token.getObjSession(); |
|
301 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
|
302 |
new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY), |
|
303 |
new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType), |
|
304 |
}; |
|
305 |
attributes = token.getAttributes |
|
306 |
(O_GENERATE, CKO_SECRET_KEY, keyType, attributes); |
|
307 |
long keyID = token.p11.C_DeriveKey(session.id(), |
|
308 |
new CK_MECHANISM(mechanism, publicValue), privateKey.keyID, |
|
309 |
attributes); |
|
310 |
CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] { |
|
311 |
new CK_ATTRIBUTE(CKA_VALUE_LEN), |
|
312 |
}; |
|
313 |
token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes); |
|
314 |
int keyLen = (int)lenAttributes[0].getLong(); |
|
315 |
SecretKey key = P11Key.secretKey |
|
316 |
(session, keyID, algorithm, keyLen << 3, attributes); |
|
317 |
if ("RAW".equals(key.getFormat())) { |
|
318 |
// Workaround for Solaris bug 6318543. |
|
319 |
// Strip leading zeroes ourselves if possible (key not sensitive). |
|
320 |
// This should be removed once the Solaris fix is available |
|
321 |
// as here we always retrieve the CKA_VALUE even for tokens |
|
322 |
// that do not have that bug. |
|
323 |
byte[] keyBytes = key.getEncoded(); |
|
324 |
byte[] newBytes = P11Util.trimZeroes(keyBytes); |
|
325 |
if (keyBytes != newBytes) { |
|
326 |
key = new SecretKeySpec(newBytes, algorithm); |
|
327 |
} |
|
328 |
} |
|
329 |
return key; |
|
330 |
} catch (PKCS11Exception e) { |
|
331 |
throw new InvalidKeyException("Could not derive key", e); |
|
332 |
} finally { |
|
333 |
publicValue = null; |
|
334 |
token.releaseSession(session); |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
} |