author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 20751 | jdk/src/share/classes/sun/security/pkcs11/P11KeyPairGenerator.java@56898cefcda9 |
child 36249 | 8b92e4bedbd4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
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.math.BigInteger; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.spec.*; |
|
32 |
||
33 |
import javax.crypto.spec.DHParameterSpec; |
|
34 |
||
35 |
import sun.security.provider.ParameterCache; |
|
36 |
||
37 |
import static sun.security.pkcs11.TemplateManager.*; |
|
38 |
import sun.security.pkcs11.wrapper.*; |
|
39 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
40 |
||
2596 | 41 |
import sun.security.rsa.RSAKeyFactory; |
42 |
||
2 | 43 |
/** |
44 |
* KeyPairGenerator implementation class. This class currently supports |
|
45 |
* RSA, DSA, DH, and EC. |
|
46 |
* |
|
47 |
* Note that for DSA and DH we rely on the Sun and SunJCE providers to |
|
48 |
* obtain the parameters from. |
|
49 |
* |
|
50 |
* @author Andreas Sterbenz |
|
51 |
* @since 1.5 |
|
52 |
*/ |
|
53 |
final class P11KeyPairGenerator extends KeyPairGeneratorSpi { |
|
54 |
||
55 |
// token instance |
|
56 |
private final Token token; |
|
57 |
||
58 |
// algorithm name |
|
59 |
private final String algorithm; |
|
60 |
||
61 |
// mechanism id |
|
62 |
private final long mechanism; |
|
63 |
||
64 |
// selected or default key size, always valid |
|
65 |
private int keySize; |
|
66 |
||
67 |
// parameters specified via init, if any |
|
68 |
private AlgorithmParameterSpec params; |
|
69 |
||
70 |
// for RSA, selected or default value of public exponent, always valid |
|
2596 | 71 |
private BigInteger rsaPublicExponent = RSAKeyGenParameterSpec.F4; |
2 | 72 |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
73 |
// the supported keysize range of the native PKCS11 library |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
74 |
// if the value cannot be retrieved or unspecified, -1 is used. |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
75 |
private final int minKeySize; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
76 |
private final int maxKeySize; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
77 |
|
2 | 78 |
// SecureRandom instance, if specified in init |
79 |
private SecureRandom random; |
|
80 |
||
81 |
P11KeyPairGenerator(Token token, String algorithm, long mechanism) |
|
82 |
throws PKCS11Exception { |
|
83 |
super(); |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
84 |
int minKeyLen = -1; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
85 |
int maxKeyLen = -1; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
86 |
try { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
87 |
CK_MECHANISM_INFO mechInfo = token.getMechanismInfo(mechanism); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
88 |
if (mechInfo != null) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
89 |
minKeyLen = (int) mechInfo.ulMinKeySize; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
90 |
maxKeyLen = (int) mechInfo.ulMaxKeySize; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
91 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
92 |
} catch (PKCS11Exception p11e) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
93 |
// Should never happen |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
94 |
throw new ProviderException |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
95 |
("Unexpected error while getting mechanism info", p11e); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
96 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
97 |
// set default key sizes and apply our own algorithm-specific limits |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
98 |
// override lower limit to disallow unsecure keys being generated |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
99 |
// override upper limit to deter DOS attack |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
100 |
if (algorithm.equals("EC")) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
101 |
keySize = 256; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
102 |
if ((minKeyLen == -1) || (minKeyLen < 112)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
103 |
minKeyLen = 112; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
104 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
105 |
if ((maxKeyLen == -1) || (maxKeyLen > 2048)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
106 |
maxKeyLen = 2048; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
107 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
108 |
} else { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
109 |
// RSA, DH, and DSA |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
110 |
keySize = 1024; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
111 |
if ((minKeyLen == -1) || (minKeyLen < 512)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
112 |
minKeyLen = 512; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
113 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
114 |
if (algorithm.equals("RSA")) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
115 |
if ((maxKeyLen == -1) || (maxKeyLen > 64 * 1024)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
116 |
maxKeyLen = 64 * 1024; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
117 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
118 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
119 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
120 |
|
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
121 |
// auto-adjust default keysize in case it's out-of-range |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
122 |
if ((minKeyLen != -1) && (keySize < minKeyLen)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
123 |
keySize = minKeyLen; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
124 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
125 |
if ((maxKeyLen != -1) && (keySize > maxKeyLen)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
126 |
keySize = maxKeyLen; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
127 |
} |
2 | 128 |
this.token = token; |
129 |
this.algorithm = algorithm; |
|
130 |
this.mechanism = mechanism; |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
131 |
this.minKeySize = minKeyLen; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
132 |
this.maxKeySize = maxKeyLen; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
133 |
initialize(keySize, null); |
2 | 134 |
} |
135 |
||
136 |
// see JCA spec |
|
137 |
public void initialize(int keySize, SecureRandom random) { |
|
138 |
token.ensureValid(); |
|
139 |
try { |
|
2596 | 140 |
checkKeySize(keySize, null); |
2 | 141 |
} catch (InvalidAlgorithmParameterException e) { |
142 |
throw new InvalidParameterException(e.getMessage()); |
|
143 |
} |
|
144 |
this.params = null; |
|
145 |
if (algorithm.equals("EC")) { |
|
146 |
params = P11ECKeyFactory.getECParameterSpec(keySize); |
|
147 |
if (params == null) { |
|
2596 | 148 |
throw new InvalidParameterException( |
149 |
"No EC parameters available for key size " |
|
150 |
+ keySize + " bits"); |
|
2 | 151 |
} |
152 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
153 |
this.keySize = keySize; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
154 |
this.random = random; |
2 | 155 |
} |
156 |
||
157 |
// see JCA spec |
|
158 |
public void initialize(AlgorithmParameterSpec params, SecureRandom random) |
|
159 |
throws InvalidAlgorithmParameterException { |
|
160 |
token.ensureValid(); |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
161 |
int tmpKeySize; |
2 | 162 |
if (algorithm.equals("DH")) { |
163 |
if (params instanceof DHParameterSpec == false) { |
|
164 |
throw new InvalidAlgorithmParameterException |
|
165 |
("DHParameterSpec required for Diffie-Hellman"); |
|
166 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
167 |
DHParameterSpec dhParams = (DHParameterSpec) params; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
168 |
tmpKeySize = dhParams.getP().bitLength(); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
169 |
checkKeySize(tmpKeySize, null); |
2 | 170 |
// XXX sanity check params |
171 |
} else if (algorithm.equals("RSA")) { |
|
172 |
if (params instanceof RSAKeyGenParameterSpec == false) { |
|
173 |
throw new InvalidAlgorithmParameterException |
|
174 |
("RSAKeyGenParameterSpec required for RSA"); |
|
175 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
176 |
RSAKeyGenParameterSpec rsaParams = |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
177 |
(RSAKeyGenParameterSpec) params; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
178 |
tmpKeySize = rsaParams.getKeysize(); |
2596 | 179 |
checkKeySize(tmpKeySize, rsaParams); |
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
180 |
// override the supplied params to null |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
181 |
params = null; |
2 | 182 |
this.rsaPublicExponent = rsaParams.getPublicExponent(); |
183 |
// XXX sanity check params |
|
184 |
} else if (algorithm.equals("DSA")) { |
|
185 |
if (params instanceof DSAParameterSpec == false) { |
|
186 |
throw new InvalidAlgorithmParameterException |
|
187 |
("DSAParameterSpec required for DSA"); |
|
188 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
189 |
DSAParameterSpec dsaParams = (DSAParameterSpec) params; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
190 |
tmpKeySize = dsaParams.getP().bitLength(); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
191 |
checkKeySize(tmpKeySize, null); |
2 | 192 |
// XXX sanity check params |
193 |
} else if (algorithm.equals("EC")) { |
|
194 |
ECParameterSpec ecParams; |
|
195 |
if (params instanceof ECParameterSpec) { |
|
2596 | 196 |
ecParams = P11ECKeyFactory.getECParameterSpec( |
197 |
(ECParameterSpec)params); |
|
2 | 198 |
if (ecParams == null) { |
199 |
throw new InvalidAlgorithmParameterException |
|
200 |
("Unsupported curve: " + params); |
|
201 |
} |
|
202 |
} else if (params instanceof ECGenParameterSpec) { |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
203 |
String name = ((ECGenParameterSpec) params).getName(); |
2 | 204 |
ecParams = P11ECKeyFactory.getECParameterSpec(name); |
205 |
if (ecParams == null) { |
|
206 |
throw new InvalidAlgorithmParameterException |
|
207 |
("Unknown curve name: " + name); |
|
208 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
209 |
// override the supplied params with the derived one |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
210 |
params = ecParams; |
2 | 211 |
} else { |
212 |
throw new InvalidAlgorithmParameterException |
|
213 |
("ECParameterSpec or ECGenParameterSpec required for EC"); |
|
214 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
215 |
tmpKeySize = ecParams.getCurve().getField().getFieldSize(); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
216 |
checkKeySize(tmpKeySize, null); |
2 | 217 |
} else { |
218 |
throw new ProviderException("Unknown algorithm: " + algorithm); |
|
219 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
220 |
this.keySize = tmpKeySize; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
221 |
this.params = params; |
2 | 222 |
this.random = random; |
223 |
} |
|
224 |
||
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
225 |
// NOTE: 'params' is only used for checking RSA keys currently. |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
226 |
private void checkKeySize(int keySize, RSAKeyGenParameterSpec params) |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
227 |
throws InvalidAlgorithmParameterException { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
228 |
// check native range first |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
229 |
if ((minKeySize != -1) && (keySize < minKeySize)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
230 |
throw new InvalidAlgorithmParameterException(algorithm + |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
231 |
" key must be at least " + minKeySize + " bits"); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
232 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
233 |
if ((maxKeySize != -1) && (keySize > maxKeySize)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
234 |
throw new InvalidAlgorithmParameterException(algorithm + |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
235 |
" key must be at most " + maxKeySize + " bits"); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
236 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
237 |
|
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
238 |
// check our own algorithm-specific limits also |
2 | 239 |
if (algorithm.equals("EC")) { |
240 |
if (keySize < 112) { |
|
241 |
throw new InvalidAlgorithmParameterException |
|
242 |
("Key size must be at least 112 bit"); |
|
243 |
} |
|
244 |
if (keySize > 2048) { |
|
245 |
// sanity check, nobody really wants keys this large |
|
246 |
throw new InvalidAlgorithmParameterException |
|
247 |
("Key size must be at most 2048 bit"); |
|
248 |
} |
|
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
249 |
} else { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
250 |
// RSA, DH, DSA |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
251 |
if (keySize < 512) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
252 |
throw new InvalidAlgorithmParameterException |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
253 |
("Key size must be at least 512 bit"); |
2596 | 254 |
} |
20751
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
255 |
if (algorithm.equals("RSA")) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
256 |
BigInteger tmpExponent = rsaPublicExponent; |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
257 |
if (params != null) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
258 |
tmpExponent = params.getPublicExponent(); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
259 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
260 |
try { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
261 |
// Reuse the checking in SunRsaSign provider. |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
262 |
// If maxKeySize is -1, then replace it with |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
263 |
// Integer.MAX_VALUE to indicate no limit. |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
264 |
RSAKeyFactory.checkKeyLengths(keySize, tmpExponent, |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
265 |
minKeySize, |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
266 |
(maxKeySize==-1? Integer.MAX_VALUE:maxKeySize)); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
267 |
} catch (InvalidKeyException e) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
268 |
throw new InvalidAlgorithmParameterException(e.getMessage()); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
269 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
270 |
} else { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
271 |
if (algorithm.equals("DH") && (params != null)) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
272 |
// sanity check, nobody really wants keys this large |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
273 |
if (keySize > 64 * 1024) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
274 |
throw new InvalidAlgorithmParameterException |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
275 |
("Key size must be at most 65536 bit"); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
276 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
277 |
} else { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
278 |
// this restriction is in the spec for DSA |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
279 |
// since we currently use DSA parameters for DH as well, |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
280 |
// it also applies to DH if no parameters are specified |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
281 |
if ((keySize != 2048) && |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
282 |
((keySize > 1024) || ((keySize & 0x3f) != 0))) { |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
283 |
throw new InvalidAlgorithmParameterException(algorithm + |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
284 |
" key must be multiples of 64 if less than 1024 bits" + |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
285 |
", or 2048 bits"); |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
286 |
} |
56898cefcda9
7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents:
5506
diff
changeset
|
287 |
} |
2 | 288 |
} |
289 |
} |
|
290 |
} |
|
291 |
||
292 |
// see JCA spec |
|
293 |
public KeyPair generateKeyPair() { |
|
294 |
token.ensureValid(); |
|
295 |
CK_ATTRIBUTE[] publicKeyTemplate; |
|
296 |
CK_ATTRIBUTE[] privateKeyTemplate; |
|
297 |
long keyType; |
|
298 |
if (algorithm.equals("RSA")) { |
|
299 |
keyType = CKK_RSA; |
|
300 |
publicKeyTemplate = new CK_ATTRIBUTE[] { |
|
301 |
new CK_ATTRIBUTE(CKA_MODULUS_BITS, keySize), |
|
302 |
new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, rsaPublicExponent), |
|
303 |
}; |
|
304 |
privateKeyTemplate = new CK_ATTRIBUTE[] { |
|
305 |
// empty |
|
306 |
}; |
|
307 |
} else if (algorithm.equals("DSA")) { |
|
308 |
keyType = CKK_DSA; |
|
309 |
DSAParameterSpec dsaParams; |
|
310 |
if (params == null) { |
|
311 |
try { |
|
312 |
dsaParams = ParameterCache.getDSAParameterSpec |
|
313 |
(keySize, random); |
|
314 |
} catch (GeneralSecurityException e) { |
|
315 |
throw new ProviderException |
|
316 |
("Could not generate DSA parameters", e); |
|
317 |
} |
|
318 |
} else { |
|
319 |
dsaParams = (DSAParameterSpec)params; |
|
320 |
} |
|
321 |
publicKeyTemplate = new CK_ATTRIBUTE[] { |
|
322 |
new CK_ATTRIBUTE(CKA_PRIME, dsaParams.getP()), |
|
323 |
new CK_ATTRIBUTE(CKA_SUBPRIME, dsaParams.getQ()), |
|
324 |
new CK_ATTRIBUTE(CKA_BASE, dsaParams.getG()), |
|
325 |
}; |
|
326 |
privateKeyTemplate = new CK_ATTRIBUTE[] { |
|
327 |
// empty |
|
328 |
}; |
|
329 |
} else if (algorithm.equals("DH")) { |
|
330 |
keyType = CKK_DH; |
|
331 |
DHParameterSpec dhParams; |
|
332 |
int privateBits; |
|
333 |
if (params == null) { |
|
334 |
try { |
|
335 |
dhParams = ParameterCache.getDHParameterSpec |
|
336 |
(keySize, random); |
|
337 |
} catch (GeneralSecurityException e) { |
|
338 |
throw new ProviderException |
|
339 |
("Could not generate DH parameters", e); |
|
340 |
} |
|
341 |
privateBits = 0; |
|
342 |
} else { |
|
343 |
dhParams = (DHParameterSpec)params; |
|
344 |
privateBits = dhParams.getL(); |
|
345 |
} |
|
346 |
if (privateBits <= 0) { |
|
347 |
// XXX find better defaults |
|
348 |
privateBits = (keySize >= 1024) ? 768 : 512; |
|
349 |
} |
|
350 |
publicKeyTemplate = new CK_ATTRIBUTE[] { |
|
351 |
new CK_ATTRIBUTE(CKA_PRIME, dhParams.getP()), |
|
352 |
new CK_ATTRIBUTE(CKA_BASE, dhParams.getG()) |
|
353 |
}; |
|
354 |
privateKeyTemplate = new CK_ATTRIBUTE[] { |
|
355 |
new CK_ATTRIBUTE(CKA_VALUE_BITS, privateBits), |
|
356 |
}; |
|
357 |
} else if (algorithm.equals("EC")) { |
|
358 |
keyType = CKK_EC; |
|
359 |
byte[] encodedParams = |
|
360 |
P11ECKeyFactory.encodeParameters((ECParameterSpec)params); |
|
361 |
publicKeyTemplate = new CK_ATTRIBUTE[] { |
|
362 |
new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams), |
|
363 |
}; |
|
364 |
privateKeyTemplate = new CK_ATTRIBUTE[] { |
|
365 |
// empty |
|
366 |
}; |
|
367 |
} else { |
|
368 |
throw new ProviderException("Unknown algorithm: " + algorithm); |
|
369 |
} |
|
370 |
Session session = null; |
|
371 |
try { |
|
372 |
session = token.getObjSession(); |
|
373 |
publicKeyTemplate = token.getAttributes |
|
374 |
(O_GENERATE, CKO_PUBLIC_KEY, keyType, publicKeyTemplate); |
|
375 |
privateKeyTemplate = token.getAttributes |
|
376 |
(O_GENERATE, CKO_PRIVATE_KEY, keyType, privateKeyTemplate); |
|
377 |
long[] keyIDs = token.p11.C_GenerateKeyPair |
|
378 |
(session.id(), new CK_MECHANISM(mechanism), |
|
379 |
publicKeyTemplate, privateKeyTemplate); |
|
380 |
PublicKey publicKey = P11Key.publicKey |
|
381 |
(session, keyIDs[0], algorithm, keySize, publicKeyTemplate); |
|
382 |
PrivateKey privateKey = P11Key.privateKey |
|
383 |
(session, keyIDs[1], algorithm, keySize, privateKeyTemplate); |
|
384 |
return new KeyPair(publicKey, privateKey); |
|
385 |
} catch (PKCS11Exception e) { |
|
386 |
throw new ProviderException(e); |
|
387 |
} finally { |
|
388 |
token.releaseSession(session); |
|
389 |
} |
|
390 |
} |
|
391 |
} |