author | stefank |
Mon, 25 Aug 2014 09:10:13 +0200 | |
changeset 26314 | f8bc1966fb30 |
parent 25859 | 3317bb8137f4 |
child 27936 | ca9ee8e3d527 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22309 | 2 |
* Copyright (c) 2003, 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 sun.security.pkcs11; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.security.spec.AlgorithmParameterSpec; |
|
30 |
import java.security.spec.*; |
|
31 |
||
6122
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
32 |
import java.util.Locale; |
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
33 |
|
2 | 34 |
import javax.crypto.*; |
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.*; |
|
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
40 |
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
41 |
import sun.security.util.KeyUtil; |
2 | 42 |
|
43 |
/** |
|
44 |
* RSA Cipher implementation class. We currently only support |
|
45 |
* PKCS#1 v1.5 padding on top of CKM_RSA_PKCS. |
|
46 |
* |
|
47 |
* @author Andreas Sterbenz |
|
48 |
* @since 1.5 |
|
49 |
*/ |
|
50 |
final class P11RSACipher extends CipherSpi { |
|
51 |
||
52 |
// minimum length of PKCS#1 v1.5 padding |
|
53 |
private final static int PKCS1_MIN_PADDING_LENGTH = 11; |
|
54 |
||
55 |
// constant byte[] of length 0 |
|
56 |
private final static byte[] B0 = new byte[0]; |
|
57 |
||
58 |
// mode constant for public key encryption |
|
59 |
private final static int MODE_ENCRYPT = 1; |
|
60 |
// mode constant for private key decryption |
|
61 |
private final static int MODE_DECRYPT = 2; |
|
62 |
// mode constant for private key encryption (signing) |
|
63 |
private final static int MODE_SIGN = 3; |
|
64 |
// mode constant for public key decryption (verifying) |
|
65 |
private final static int MODE_VERIFY = 4; |
|
66 |
||
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
67 |
// padding type constant for NoPadding |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
68 |
private final static int PAD_NONE = 1; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
69 |
// padding type constant for PKCS1Padding |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
70 |
private final static int PAD_PKCS1 = 2; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
71 |
|
2 | 72 |
// token instance |
73 |
private final Token token; |
|
74 |
||
75 |
// algorithm name (always "RSA") |
|
76 |
private final String algorithm; |
|
77 |
||
78 |
// mechanism id |
|
79 |
private final long mechanism; |
|
80 |
||
81 |
// associated session, if any |
|
82 |
private Session session; |
|
83 |
||
84 |
// mode, one of MODE_* above |
|
85 |
private int mode; |
|
86 |
||
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
87 |
// padding, one of PAD_* above |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
88 |
private int padType; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
89 |
|
2 | 90 |
private byte[] buffer; |
91 |
private int bufOfs; |
|
92 |
||
93 |
// key, if init() was called |
|
94 |
private P11Key p11Key; |
|
95 |
||
96 |
// flag indicating whether an operation is initialized |
|
97 |
private boolean initialized; |
|
98 |
||
99 |
// maximum input data size allowed |
|
100 |
// for decryption, this is the length of the key |
|
101 |
// for encryption, length of the key minus minimum padding length |
|
102 |
private int maxInputSize; |
|
103 |
||
104 |
// maximum output size. this is the length of the key |
|
105 |
private int outputSize; |
|
106 |
||
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
107 |
// cipher parameter for TLS RSA premaster secret |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
108 |
private AlgorithmParameterSpec spec = null; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
109 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
110 |
// the source of randomness |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
111 |
private SecureRandom random; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
112 |
|
2 | 113 |
P11RSACipher(Token token, String algorithm, long mechanism) |
114 |
throws PKCS11Exception { |
|
115 |
super(); |
|
116 |
this.token = token; |
|
117 |
this.algorithm = "RSA"; |
|
118 |
this.mechanism = mechanism; |
|
119 |
} |
|
120 |
||
121 |
// modes do not make sense for RSA, but allow ECB |
|
122 |
// see JCE spec |
|
123 |
protected void engineSetMode(String mode) throws NoSuchAlgorithmException { |
|
124 |
if (mode.equalsIgnoreCase("ECB") == false) { |
|
125 |
throw new NoSuchAlgorithmException("Unsupported mode " + mode); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
protected void engineSetPadding(String padding) |
|
130 |
throws NoSuchPaddingException { |
|
6122
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
131 |
String lowerPadding = padding.toLowerCase(Locale.ENGLISH); |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
132 |
if (lowerPadding.equals("pkcs1padding")) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
133 |
padType = PAD_PKCS1; |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
134 |
} else if (lowerPadding.equals("nopadding")) { |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
135 |
padType = PAD_NONE; |
2 | 136 |
} else { |
137 |
throw new NoSuchPaddingException("Unsupported padding " + padding); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
// return 0 as block size, we are not a block cipher |
|
142 |
// see JCE spec |
|
143 |
protected int engineGetBlockSize() { |
|
144 |
return 0; |
|
145 |
} |
|
146 |
||
147 |
// return the output size |
|
148 |
// see JCE spec |
|
149 |
protected int engineGetOutputSize(int inputLen) { |
|
150 |
return outputSize; |
|
151 |
} |
|
152 |
||
153 |
// no IV, return null |
|
154 |
// see JCE spec |
|
155 |
protected byte[] engineGetIV() { |
|
156 |
return null; |
|
157 |
} |
|
158 |
||
159 |
// no parameters, return null |
|
160 |
// see JCE spec |
|
161 |
protected AlgorithmParameters engineGetParameters() { |
|
162 |
return null; |
|
163 |
} |
|
164 |
||
165 |
// see JCE spec |
|
166 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
|
167 |
throws InvalidKeyException { |
|
168 |
implInit(opmode, key); |
|
169 |
} |
|
170 |
||
171 |
// see JCE spec |
|
172 |
protected void engineInit(int opmode, Key key, |
|
173 |
AlgorithmParameterSpec params, SecureRandom random) |
|
174 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
175 |
if (params != null) { |
|
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
176 |
if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
177 |
throw new InvalidAlgorithmParameterException( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
178 |
"Parameters not supported"); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
179 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
180 |
spec = params; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
181 |
this.random = random; // for TLS RSA premaster secret |
2 | 182 |
} |
183 |
implInit(opmode, key); |
|
184 |
} |
|
185 |
||
186 |
// see JCE spec |
|
187 |
protected void engineInit(int opmode, Key key, AlgorithmParameters params, |
|
188 |
SecureRandom random) |
|
189 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
190 |
if (params != null) { |
|
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
191 |
throw new InvalidAlgorithmParameterException( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
192 |
"Parameters not supported"); |
2 | 193 |
} |
194 |
implInit(opmode, key); |
|
195 |
} |
|
196 |
||
197 |
private void implInit(int opmode, Key key) throws InvalidKeyException { |
|
198 |
cancelOperation(); |
|
199 |
p11Key = P11KeyFactory.convertKey(token, key, algorithm); |
|
200 |
boolean encrypt; |
|
201 |
if (opmode == Cipher.ENCRYPT_MODE) { |
|
202 |
encrypt = true; |
|
203 |
} else if (opmode == Cipher.DECRYPT_MODE) { |
|
204 |
encrypt = false; |
|
205 |
} else if (opmode == Cipher.WRAP_MODE) { |
|
206 |
if (p11Key.isPublic() == false) { |
|
207 |
throw new InvalidKeyException |
|
208 |
("Wrap has to be used with public keys"); |
|
209 |
} |
|
290
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
210 |
// No further setup needed for C_Wrap(). We'll initialize later if |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
211 |
// we can't use C_Wrap(). |
2 | 212 |
return; |
213 |
} else if (opmode == Cipher.UNWRAP_MODE) { |
|
214 |
if (p11Key.isPrivate() == false) { |
|
215 |
throw new InvalidKeyException |
|
216 |
("Unwrap has to be used with private keys"); |
|
217 |
} |
|
2180
9994f4f08a59
6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents:
290
diff
changeset
|
218 |
// No further setup needed for C_Unwrap(). We'll initialize later |
9994f4f08a59
6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents:
290
diff
changeset
|
219 |
// if we can't use C_Unwrap(). |
9994f4f08a59
6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents:
290
diff
changeset
|
220 |
return; |
2 | 221 |
} else { |
222 |
throw new InvalidKeyException("Unsupported mode: " + opmode); |
|
223 |
} |
|
224 |
if (p11Key.isPublic()) { |
|
225 |
mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY; |
|
226 |
} else if (p11Key.isPrivate()) { |
|
227 |
mode = encrypt ? MODE_SIGN : MODE_DECRYPT; |
|
228 |
} else { |
|
229 |
throw new InvalidKeyException("Unknown key type: " + p11Key); |
|
230 |
} |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
8578
diff
changeset
|
231 |
int n = (p11Key.length() + 7) >> 3; |
2 | 232 |
outputSize = n; |
233 |
buffer = new byte[n]; |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
234 |
maxInputSize = ((padType == PAD_PKCS1 && encrypt) ? |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
6122
diff
changeset
|
235 |
(n - PKCS1_MIN_PADDING_LENGTH) : n); |
2 | 236 |
try { |
237 |
initialize(); |
|
238 |
} catch (PKCS11Exception e) { |
|
239 |
throw new InvalidKeyException("init() failed", e); |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
private void cancelOperation() { |
|
244 |
token.ensureValid(); |
|
245 |
if (initialized == false) { |
|
246 |
return; |
|
247 |
} |
|
248 |
initialized = false; |
|
249 |
if ((session == null) || (token.explicitCancel == false)) { |
|
250 |
return; |
|
251 |
} |
|
252 |
if (session.hasObjects() == false) { |
|
253 |
session = token.killSession(session); |
|
254 |
return; |
|
255 |
} |
|
256 |
try { |
|
257 |
PKCS11 p11 = token.p11; |
|
258 |
int inLen = maxInputSize; |
|
259 |
int outLen = buffer.length; |
|
260 |
switch (mode) { |
|
261 |
case MODE_ENCRYPT: |
|
262 |
p11.C_Encrypt |
|
263 |
(session.id(), buffer, 0, inLen, buffer, 0, outLen); |
|
264 |
break; |
|
265 |
case MODE_DECRYPT: |
|
266 |
p11.C_Decrypt |
|
267 |
(session.id(), buffer, 0, inLen, buffer, 0, outLen); |
|
268 |
break; |
|
269 |
case MODE_SIGN: |
|
270 |
byte[] tmpBuffer = new byte[maxInputSize]; |
|
271 |
p11.C_Sign |
|
272 |
(session.id(), tmpBuffer); |
|
273 |
break; |
|
274 |
case MODE_VERIFY: |
|
275 |
p11.C_VerifyRecover |
|
276 |
(session.id(), buffer, 0, inLen, buffer, 0, outLen); |
|
277 |
break; |
|
278 |
default: |
|
279 |
throw new ProviderException("internal error"); |
|
280 |
} |
|
281 |
} catch (PKCS11Exception e) { |
|
282 |
// XXX ensure this always works, ignore error |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
private void ensureInitialized() throws PKCS11Exception { |
|
287 |
token.ensureValid(); |
|
288 |
if (initialized == false) { |
|
289 |
initialize(); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
private void initialize() throws PKCS11Exception { |
|
294 |
if (session == null) { |
|
295 |
session = token.getOpSession(); |
|
296 |
} |
|
297 |
PKCS11 p11 = token.p11; |
|
298 |
CK_MECHANISM ckMechanism = new CK_MECHANISM(mechanism); |
|
299 |
switch (mode) { |
|
300 |
case MODE_ENCRYPT: |
|
301 |
p11.C_EncryptInit(session.id(), ckMechanism, p11Key.keyID); |
|
302 |
break; |
|
303 |
case MODE_DECRYPT: |
|
304 |
p11.C_DecryptInit(session.id(), ckMechanism, p11Key.keyID); |
|
305 |
break; |
|
306 |
case MODE_SIGN: |
|
307 |
p11.C_SignInit(session.id(), ckMechanism, p11Key.keyID); |
|
308 |
break; |
|
309 |
case MODE_VERIFY: |
|
310 |
p11.C_VerifyRecoverInit(session.id(), ckMechanism, p11Key.keyID); |
|
311 |
break; |
|
312 |
default: |
|
313 |
throw new AssertionError("internal error"); |
|
314 |
} |
|
315 |
bufOfs = 0; |
|
316 |
initialized = true; |
|
317 |
} |
|
318 |
||
319 |
private void implUpdate(byte[] in, int inOfs, int inLen) { |
|
320 |
try { |
|
321 |
ensureInitialized(); |
|
322 |
} catch (PKCS11Exception e) { |
|
323 |
throw new ProviderException("update() failed", e); |
|
324 |
} |
|
325 |
if ((inLen == 0) || (in == null)) { |
|
326 |
return; |
|
327 |
} |
|
328 |
if (bufOfs + inLen > maxInputSize) { |
|
329 |
bufOfs = maxInputSize + 1; |
|
330 |
return; |
|
331 |
} |
|
332 |
System.arraycopy(in, inOfs, buffer, bufOfs, inLen); |
|
333 |
bufOfs += inLen; |
|
334 |
} |
|
335 |
||
336 |
private int implDoFinal(byte[] out, int outOfs, int outLen) |
|
337 |
throws BadPaddingException, IllegalBlockSizeException { |
|
338 |
if (bufOfs > maxInputSize) { |
|
339 |
throw new IllegalBlockSizeException("Data must not be longer " |
|
340 |
+ "than " + maxInputSize + " bytes"); |
|
341 |
} |
|
342 |
try { |
|
343 |
ensureInitialized(); |
|
344 |
PKCS11 p11 = token.p11; |
|
345 |
int n; |
|
346 |
switch (mode) { |
|
347 |
case MODE_ENCRYPT: |
|
348 |
n = p11.C_Encrypt |
|
349 |
(session.id(), buffer, 0, bufOfs, out, outOfs, outLen); |
|
350 |
break; |
|
351 |
case MODE_DECRYPT: |
|
352 |
n = p11.C_Decrypt |
|
353 |
(session.id(), buffer, 0, bufOfs, out, outOfs, outLen); |
|
354 |
break; |
|
355 |
case MODE_SIGN: |
|
356 |
byte[] tmpBuffer = new byte[bufOfs]; |
|
357 |
System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs); |
|
358 |
tmpBuffer = p11.C_Sign(session.id(), tmpBuffer); |
|
359 |
if (tmpBuffer.length > outLen) { |
|
360 |
throw new BadPaddingException("Output buffer too small"); |
|
361 |
} |
|
362 |
System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length); |
|
363 |
n = tmpBuffer.length; |
|
364 |
break; |
|
365 |
case MODE_VERIFY: |
|
366 |
n = p11.C_VerifyRecover |
|
367 |
(session.id(), buffer, 0, bufOfs, out, outOfs, outLen); |
|
368 |
break; |
|
369 |
default: |
|
370 |
throw new ProviderException("internal error"); |
|
371 |
} |
|
372 |
return n; |
|
373 |
} catch (PKCS11Exception e) { |
|
374 |
throw (BadPaddingException)new BadPaddingException |
|
375 |
("doFinal() failed").initCause(e); |
|
376 |
} finally { |
|
377 |
initialized = false; |
|
378 |
session = token.releaseSession(session); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
// see JCE spec |
|
383 |
protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) { |
|
384 |
implUpdate(in, inOfs, inLen); |
|
385 |
return B0; |
|
386 |
} |
|
387 |
||
388 |
// see JCE spec |
|
389 |
protected int engineUpdate(byte[] in, int inOfs, int inLen, |
|
390 |
byte[] out, int outOfs) throws ShortBufferException { |
|
391 |
implUpdate(in, inOfs, inLen); |
|
392 |
return 0; |
|
393 |
} |
|
394 |
||
395 |
// see JCE spec |
|
396 |
protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen) |
|
397 |
throws IllegalBlockSizeException, BadPaddingException { |
|
398 |
implUpdate(in, inOfs, inLen); |
|
399 |
int n = implDoFinal(buffer, 0, buffer.length); |
|
400 |
byte[] out = new byte[n]; |
|
401 |
System.arraycopy(buffer, 0, out, 0, n); |
|
402 |
return out; |
|
403 |
} |
|
404 |
||
405 |
// see JCE spec |
|
406 |
protected int engineDoFinal(byte[] in, int inOfs, int inLen, |
|
407 |
byte[] out, int outOfs) throws ShortBufferException, |
|
408 |
IllegalBlockSizeException, BadPaddingException { |
|
409 |
implUpdate(in, inOfs, inLen); |
|
410 |
return implDoFinal(out, outOfs, out.length - outOfs); |
|
411 |
} |
|
412 |
||
290
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
413 |
private byte[] doFinal() throws BadPaddingException, |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
414 |
IllegalBlockSizeException { |
2 | 415 |
byte[] t = new byte[2048]; |
416 |
int n = implDoFinal(t, 0, t.length); |
|
417 |
byte[] out = new byte[n]; |
|
418 |
System.arraycopy(t, 0, out, 0, n); |
|
419 |
return out; |
|
420 |
} |
|
421 |
||
422 |
// see JCE spec |
|
423 |
protected byte[] engineWrap(Key key) throws InvalidKeyException, |
|
424 |
IllegalBlockSizeException { |
|
425 |
String keyAlg = key.getAlgorithm(); |
|
290
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
426 |
P11Key sKey = null; |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
427 |
try { |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
428 |
// The conversion may fail, e.g. trying to wrap an AES key on |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
429 |
// a token that does not support AES, or when the key size is |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
430 |
// not within the range supported by the token. |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
431 |
sKey = P11SecretKeyFactory.convertKey(token, key, keyAlg); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
432 |
} catch (InvalidKeyException ike) { |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
433 |
byte[] toBeWrappedKey = key.getEncoded(); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
434 |
if (toBeWrappedKey == null) { |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
435 |
throw new InvalidKeyException |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
436 |
("wrap() failed, no encoding available", ike); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
437 |
} |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
438 |
// Directly encrypt the key encoding when key conversion failed |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
439 |
implInit(Cipher.ENCRYPT_MODE, p11Key); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
440 |
implUpdate(toBeWrappedKey, 0, toBeWrappedKey.length); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
441 |
try { |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
442 |
return doFinal(); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
443 |
} catch (BadPaddingException bpe) { |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
444 |
// should not occur |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
445 |
throw new InvalidKeyException("wrap() failed", bpe); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
446 |
} finally { |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
447 |
// Restore original mode |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
448 |
implInit(Cipher.WRAP_MODE, p11Key); |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
449 |
} |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
450 |
} |
2 | 451 |
Session s = null; |
452 |
try { |
|
453 |
s = token.getOpSession(); |
|
290
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
454 |
return token.p11.C_WrapKey(s.id(), new CK_MECHANISM(mechanism), |
519d4185fbe2
6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
2
diff
changeset
|
455 |
p11Key.keyID, sKey.keyID); |
2 | 456 |
} catch (PKCS11Exception e) { |
457 |
throw new InvalidKeyException("wrap() failed", e); |
|
458 |
} finally { |
|
459 |
token.releaseSession(s); |
|
460 |
} |
|
461 |
} |
|
462 |
||
463 |
// see JCE spec |
|
464 |
protected Key engineUnwrap(byte[] wrappedKey, String algorithm, |
|
465 |
int type) throws InvalidKeyException, NoSuchAlgorithmException { |
|
22309 | 466 |
|
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
467 |
boolean isTlsRsaPremasterSecret = |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
468 |
algorithm.equals("TlsRsaPremasterSecret"); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
469 |
Exception failover = null; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
470 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
471 |
SecureRandom secureRandom = random; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
472 |
if (secureRandom == null && isTlsRsaPremasterSecret) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
473 |
secureRandom = new SecureRandom(); |
2 | 474 |
} |
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
475 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
476 |
// Should C_Unwrap be preferred for non-TLS RSA premaster secret? |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
477 |
if (token.supportsRawSecretKeyImport()) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
478 |
// XXX implement unwrap using C_Unwrap() for all keys |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
479 |
implInit(Cipher.DECRYPT_MODE, p11Key); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
480 |
if (wrappedKey.length > maxInputSize) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
481 |
throw new InvalidKeyException("Key is too long for unwrapping"); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
482 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
483 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
484 |
byte[] encoded = null; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
485 |
implUpdate(wrappedKey, 0, wrappedKey.length); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
486 |
try { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
487 |
encoded = doFinal(); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
488 |
} catch (BadPaddingException e) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
489 |
if (isTlsRsaPremasterSecret) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
490 |
failover = e; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
491 |
} else { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
492 |
throw new InvalidKeyException("Unwrapping failed", e); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
493 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
494 |
} catch (IllegalBlockSizeException e) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
495 |
// should not occur, handled with length check above |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
496 |
throw new InvalidKeyException("Unwrapping failed", e); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
497 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
498 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
499 |
if (isTlsRsaPremasterSecret) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
500 |
if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
501 |
throw new IllegalStateException( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
502 |
"No TlsRsaPremasterSecretParameterSpec specified"); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
503 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
504 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
505 |
// polish the TLS premaster secret |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
506 |
TlsRsaPremasterSecretParameterSpec psps = |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
507 |
(TlsRsaPremasterSecretParameterSpec)spec; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
508 |
encoded = KeyUtil.checkTlsPreMasterSecretKey( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
509 |
psps.getClientVersion(), psps.getServerVersion(), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
510 |
secureRandom, encoded, (failover != null)); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
511 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
512 |
|
2 | 513 |
return ConstructKeys.constructKey(encoded, algorithm, type); |
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
514 |
} else { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
515 |
Session s = null; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
516 |
SecretKey secretKey = null; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
517 |
try { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
518 |
try { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
519 |
s = token.getObjSession(); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
520 |
long keyType = CKK_GENERIC_SECRET; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
521 |
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
522 |
new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
523 |
new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
524 |
}; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
525 |
attributes = token.getAttributes( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
526 |
O_IMPORT, CKO_SECRET_KEY, keyType, attributes); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
527 |
long keyID = token.p11.C_UnwrapKey(s.id(), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
528 |
new CK_MECHANISM(mechanism), p11Key.keyID, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
529 |
wrappedKey, attributes); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
530 |
secretKey = P11Key.secretKey(s, keyID, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
531 |
algorithm, 48 << 3, attributes); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
532 |
} catch (PKCS11Exception e) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
533 |
if (isTlsRsaPremasterSecret) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
534 |
failover = e; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
535 |
} else { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
536 |
throw new InvalidKeyException("unwrap() failed", e); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
537 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
538 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
539 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
540 |
if (isTlsRsaPremasterSecret) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
541 |
byte[] replacer = new byte[48]; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
542 |
if (failover == null) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
543 |
// Does smart compiler dispose this operation? |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
544 |
secureRandom.nextBytes(replacer); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
545 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
546 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
547 |
TlsRsaPremasterSecretParameterSpec psps = |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
548 |
(TlsRsaPremasterSecretParameterSpec)spec; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
549 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
550 |
// Please use the tricky failover and replacer byte array |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
551 |
// as the parameters so that smart compiler won't dispose |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
552 |
// the unused variable . |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
553 |
secretKey = polishPreMasterSecretKey(token, s, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
554 |
failover, replacer, secretKey, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
555 |
psps.getClientVersion(), psps.getServerVersion()); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
556 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
557 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
558 |
return secretKey; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
559 |
} finally { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
560 |
token.releaseSession(s); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
561 |
} |
2 | 562 |
} |
563 |
} |
|
564 |
||
565 |
// see JCE spec |
|
566 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
8578
diff
changeset
|
567 |
int n = P11KeyFactory.convertKey(token, key, algorithm).length(); |
2 | 568 |
return n; |
569 |
} |
|
23733
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
570 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
571 |
private static SecretKey polishPreMasterSecretKey( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
572 |
Token token, Session session, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
573 |
Exception failover, byte[] replacer, SecretKey secretKey, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
574 |
int clientVersion, int serverVersion) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
575 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
576 |
if (failover != null) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
577 |
CK_VERSION version = new CK_VERSION( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
578 |
(clientVersion >>> 8) & 0xFF, clientVersion & 0xFF); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
579 |
try { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
580 |
CK_ATTRIBUTE[] attributes = token.getAttributes( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
581 |
O_GENERATE, CKO_SECRET_KEY, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
582 |
CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
583 |
long keyID = token.p11.C_GenerateKey(session.id(), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
584 |
// new CK_MECHANISM(CKM_TLS_PRE_MASTER_KEY_GEN, version), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
585 |
new CK_MECHANISM(CKM_SSL3_PRE_MASTER_KEY_GEN, version), |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
586 |
attributes); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
587 |
return P11Key.secretKey(session, |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
588 |
keyID, "TlsRsaPremasterSecret", 48 << 3, attributes); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
589 |
} catch (PKCS11Exception e) { |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
590 |
throw new ProviderException( |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
591 |
"Could not generate premaster secret", e); |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
592 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
593 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
594 |
|
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
595 |
return secretKey; |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
596 |
} |
b9b80421cfa7
8028192: Use of PKCS11-NSS provider in FIPS mode broken
xuelei
parents:
22309
diff
changeset
|
597 |
|
2 | 598 |
} |
599 |
||
600 |
final class ConstructKeys { |
|
601 |
/** |
|
602 |
* Construct a public key from its encoding. |
|
603 |
* |
|
604 |
* @param encodedKey the encoding of a public key. |
|
605 |
* |
|
606 |
* @param encodedKeyAlgorithm the algorithm the encodedKey is for. |
|
607 |
* |
|
608 |
* @return a public key constructed from the encodedKey. |
|
609 |
*/ |
|
610 |
private static final PublicKey constructPublicKey(byte[] encodedKey, |
|
611 |
String encodedKeyAlgorithm) |
|
612 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
613 |
try { |
|
614 |
KeyFactory keyFactory = |
|
615 |
KeyFactory.getInstance(encodedKeyAlgorithm); |
|
616 |
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey); |
|
617 |
return keyFactory.generatePublic(keySpec); |
|
618 |
} catch (NoSuchAlgorithmException nsae) { |
|
619 |
throw new NoSuchAlgorithmException("No installed providers " + |
|
620 |
"can create keys for the " + |
|
621 |
encodedKeyAlgorithm + |
|
622 |
"algorithm", nsae); |
|
623 |
} catch (InvalidKeySpecException ike) { |
|
624 |
throw new InvalidKeyException("Cannot construct public key", ike); |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* Construct a private key from its encoding. |
|
630 |
* |
|
631 |
* @param encodedKey the encoding of a private key. |
|
632 |
* |
|
633 |
* @param encodedKeyAlgorithm the algorithm the wrapped key is for. |
|
634 |
* |
|
635 |
* @return a private key constructed from the encodedKey. |
|
636 |
*/ |
|
637 |
private static final PrivateKey constructPrivateKey(byte[] encodedKey, |
|
638 |
String encodedKeyAlgorithm) throws InvalidKeyException, |
|
639 |
NoSuchAlgorithmException { |
|
640 |
try { |
|
641 |
KeyFactory keyFactory = |
|
642 |
KeyFactory.getInstance(encodedKeyAlgorithm); |
|
643 |
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey); |
|
644 |
return keyFactory.generatePrivate(keySpec); |
|
645 |
} catch (NoSuchAlgorithmException nsae) { |
|
646 |
throw new NoSuchAlgorithmException("No installed providers " + |
|
647 |
"can create keys for the " + |
|
648 |
encodedKeyAlgorithm + |
|
649 |
"algorithm", nsae); |
|
650 |
} catch (InvalidKeySpecException ike) { |
|
651 |
throw new InvalidKeyException("Cannot construct private key", ike); |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
/** |
|
656 |
* Construct a secret key from its encoding. |
|
657 |
* |
|
658 |
* @param encodedKey the encoding of a secret key. |
|
659 |
* |
|
660 |
* @param encodedKeyAlgorithm the algorithm the secret key is for. |
|
661 |
* |
|
662 |
* @return a secret key constructed from the encodedKey. |
|
663 |
*/ |
|
664 |
private static final SecretKey constructSecretKey(byte[] encodedKey, |
|
665 |
String encodedKeyAlgorithm) { |
|
666 |
return new SecretKeySpec(encodedKey, encodedKeyAlgorithm); |
|
667 |
} |
|
668 |
||
669 |
static final Key constructKey(byte[] encoding, String keyAlgorithm, |
|
670 |
int keyType) throws InvalidKeyException, NoSuchAlgorithmException { |
|
671 |
switch (keyType) { |
|
672 |
case Cipher.SECRET_KEY: |
|
673 |
return constructSecretKey(encoding, keyAlgorithm); |
|
674 |
case Cipher.PRIVATE_KEY: |
|
675 |
return constructPrivateKey(encoding, keyAlgorithm); |
|
676 |
case Cipher.PUBLIC_KEY: |
|
677 |
return constructPublicKey(encoding, keyAlgorithm); |
|
678 |
default: |
|
679 |
throw new InvalidKeyException("Unknown keytype " + keyType); |
|
680 |
} |
|
681 |
} |
|
682 |
} |