Showing the changes to java/pkg/sun/security/pkcs11/P11KeyPairGenerator.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,329 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. | |
| 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 | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 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 | + * | |
| 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. | |
| 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 | + | |
| 41 | +import sun.security.rsa.RSAKeyFactory; | |
| 42 | + | |
| 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 | |
| 71 | + private BigInteger rsaPublicExponent = RSAKeyGenParameterSpec.F4; | |
| 72 | + | |
| 73 | + // SecureRandom instance, if specified in init | |
| 74 | + private SecureRandom random; | |
| 75 | + | |
| 76 | + P11KeyPairGenerator(Token token, String algorithm, long mechanism) | |
| 77 | + throws PKCS11Exception { | |
| 78 | + super(); | |
| 79 | + this.token = token; | |
| 80 | + this.algorithm = algorithm; | |
| 81 | + this.mechanism = mechanism; | |
| 82 | + if (algorithm.equals("EC")) { | |
| 83 | + initialize(256, null); | |
| 84 | + } else { | |
| 85 | + initialize(1024, null); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + // see JCA spec | |
| 90 | + public void initialize(int keySize, SecureRandom random) { | |
| 91 | + token.ensureValid(); | |
| 92 | + try { | |
| 93 | + checkKeySize(keySize, null); | |
| 94 | + } catch (InvalidAlgorithmParameterException e) { | |
| 95 | + throw new InvalidParameterException(e.getMessage()); | |
| 96 | + } | |
| 97 | + this.keySize = keySize; | |
| 98 | + this.params = null; | |
| 99 | + this.random = random; | |
| 100 | + if (algorithm.equals("EC")) { | |
| 101 | + params = P11ECKeyFactory.getECParameterSpec(keySize); | |
| 102 | + if (params == null) { | |
| 103 | + throw new InvalidParameterException( | |
| 104 | + "No EC parameters available for key size " | |
| 105 | + + keySize + " bits"); | |
| 106 | + } | |
| 107 | + } | |
| 108 | + } | |
| 109 | + | |
| 110 | + // see JCA spec | |
| 111 | + public void initialize(AlgorithmParameterSpec params, SecureRandom random) | |
| 112 | + throws InvalidAlgorithmParameterException { | |
| 113 | + token.ensureValid(); | |
| 114 | + if (algorithm.equals("DH")) { | |
| 115 | + if (params instanceof DHParameterSpec == false) { | |
| 116 | + throw new InvalidAlgorithmParameterException | |
| 117 | + ("DHParameterSpec required for Diffie-Hellman"); | |
| 118 | + } | |
| 119 | + DHParameterSpec dhParams = (DHParameterSpec)params; | |
| 120 | + int tmpKeySize = dhParams.getP().bitLength(); | |
| 121 | + checkKeySize(tmpKeySize, dhParams); | |
| 122 | + this.keySize = tmpKeySize; | |
| 123 | + this.params = dhParams; | |
| 124 | + // XXX sanity check params | |
| 125 | + } else if (algorithm.equals("RSA")) { | |
| 126 | + if (params instanceof RSAKeyGenParameterSpec == false) { | |
| 127 | + throw new InvalidAlgorithmParameterException | |
| 128 | + ("RSAKeyGenParameterSpec required for RSA"); | |
| 129 | + } | |
| 130 | + RSAKeyGenParameterSpec rsaParams = (RSAKeyGenParameterSpec)params; | |
| 131 | + int tmpKeySize = rsaParams.getKeysize(); | |
| 132 | + checkKeySize(tmpKeySize, rsaParams); | |
| 133 | + this.keySize = tmpKeySize; | |
| 134 | + this.params = null; | |
| 135 | + this.rsaPublicExponent = rsaParams.getPublicExponent(); | |
| 136 | + // XXX sanity check params | |
| 137 | + } else if (algorithm.equals("DSA")) { | |
| 138 | + if (params instanceof DSAParameterSpec == false) { | |
| 139 | + throw new InvalidAlgorithmParameterException | |
| 140 | + ("DSAParameterSpec required for DSA"); | |
| 141 | + } | |
| 142 | + DSAParameterSpec dsaParams = (DSAParameterSpec)params; | |
| 143 | + int tmpKeySize = dsaParams.getP().bitLength(); | |
| 144 | + checkKeySize(tmpKeySize, dsaParams); | |
| 145 | + this.keySize = tmpKeySize; | |
| 146 | + this.params = dsaParams; | |
| 147 | + // XXX sanity check params | |
| 148 | + } else if (algorithm.equals("EC")) { | |
| 149 | + ECParameterSpec ecParams; | |
| 150 | + if (params instanceof ECParameterSpec) { | |
| 151 | + ecParams = P11ECKeyFactory.getECParameterSpec( | |
| 152 | + (ECParameterSpec)params); | |
| 153 | + if (ecParams == null) { | |
| 154 | + throw new InvalidAlgorithmParameterException | |
| 155 | + ("Unsupported curve: " + params); | |
| 156 | + } | |
| 157 | + } else if (params instanceof ECGenParameterSpec) { | |
| 158 | + String name = ((ECGenParameterSpec)params).getName(); | |
| 159 | + ecParams = P11ECKeyFactory.getECParameterSpec(name); | |
| 160 | + if (ecParams == null) { | |
| 161 | + throw new InvalidAlgorithmParameterException | |
| 162 | + ("Unknown curve name: " + name); | |
| 163 | + } | |
| 164 | + } else { | |
| 165 | + throw new InvalidAlgorithmParameterException | |
| 166 | + ("ECParameterSpec or ECGenParameterSpec required for EC"); | |
| 167 | + } | |
| 168 | + int tmpKeySize = ecParams.getCurve().getField().getFieldSize(); | |
| 169 | + checkKeySize(tmpKeySize, ecParams); | |
| 170 | + this.keySize = tmpKeySize; | |
| 171 | + this.params = ecParams; | |
| 172 | + } else { | |
| 173 | + throw new ProviderException("Unknown algorithm: " + algorithm); | |
| 174 | + } | |
| 175 | + this.random = random; | |
| 176 | + } | |
| 177 | + | |
| 178 | + private void checkKeySize(int keySize, AlgorithmParameterSpec params) | |
| 179 | + throws InvalidAlgorithmParameterException { | |
| 180 | + if (algorithm.equals("EC")) { | |
| 181 | + if (keySize < 112) { | |
| 182 | + throw new InvalidAlgorithmParameterException | |
| 183 | + ("Key size must be at least 112 bit"); | |
| 184 | + } | |
| 185 | + if (keySize > 2048) { | |
| 186 | + // sanity check, nobody really wants keys this large | |
| 187 | + throw new InvalidAlgorithmParameterException | |
| 188 | + ("Key size must be at most 2048 bit"); | |
| 189 | + } | |
| 190 | + return; | |
| 191 | + } else if (algorithm.equals("RSA")) { | |
| 192 | + BigInteger tmpExponent = rsaPublicExponent; | |
| 193 | + if (params != null) { | |
| 194 | + // Already tested for instanceof RSAKeyGenParameterSpec above | |
| 195 | + tmpExponent = | |
| 196 | + ((RSAKeyGenParameterSpec)params).getPublicExponent(); | |
| 197 | + } | |
| 198 | + try { | |
| 199 | + // This provider supports 64K or less. | |
| 200 | + RSAKeyFactory.checkKeyLengths(keySize, tmpExponent, | |
| 201 | + 512, 64 * 1024); | |
| 202 | + } catch (InvalidKeyException e) { | |
| 203 | + throw new InvalidAlgorithmParameterException(e.getMessage()); | |
| 204 | + } | |
| 205 | + return; | |
| 206 | + } | |
| 207 | + | |
| 208 | + if (keySize < 512) { | |
| 209 | + throw new InvalidAlgorithmParameterException | |
| 210 | + ("Key size must be at least 512 bit"); | |
| 211 | + } | |
| 212 | + if (algorithm.equals("DH") && (params != null)) { | |
| 213 | + // sanity check, nobody really wants keys this large | |
| 214 | + if (keySize > 64 * 1024) { | |
| 215 | + throw new InvalidAlgorithmParameterException | |
| 216 | + ("Key size must be at most 65536 bit"); | |
| 217 | + } | |
| 218 | + } else { | |
| 219 | + // this restriction is in the spec for DSA | |
| 220 | + // since we currently use DSA parameters for DH as well, | |
| 221 | + // it also applies to DH if no parameters are specified | |
| 222 | + if ((keySize > 1024) || ((keySize & 0x3f) != 0)) { | |
| 223 | + throw new InvalidAlgorithmParameterException | |
| 224 | + ("Key size must be a multiple of 64 and at most 1024 bit"); | |
| 225 | + } | |
| 226 | + } | |
| 227 | + } | |
| 228 | + | |
| 229 | + // see JCA spec | |
| 230 | + public KeyPair generateKeyPair() { | |
| 231 | + token.ensureValid(); | |
| 232 | + CK_ATTRIBUTE[] publicKeyTemplate; | |
| 233 | + CK_ATTRIBUTE[] privateKeyTemplate; | |
| 234 | + long keyType; | |
| 235 | + if (algorithm.equals("RSA")) { | |
| 236 | + keyType = CKK_RSA; | |
| 237 | + publicKeyTemplate = new CK_ATTRIBUTE[] { | |
| 238 | + new CK_ATTRIBUTE(CKA_MODULUS_BITS, keySize), | |
| 239 | + new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, rsaPublicExponent), | |
| 240 | + }; | |
| 241 | + privateKeyTemplate = new CK_ATTRIBUTE[] { | |
| 242 | + // empty | |
| 243 | + }; | |
| 244 | + } else if (algorithm.equals("DSA")) { | |
| 245 | + keyType = CKK_DSA; | |
| 246 | + DSAParameterSpec dsaParams; | |
| 247 | + if (params == null) { | |
| 248 | + try { | |
| 249 | + dsaParams = ParameterCache.getDSAParameterSpec | |
| 250 | + (keySize, random); | |
| 251 | + } catch (GeneralSecurityException e) { | |
| 252 | + throw new ProviderException | |
| 253 | + ("Could not generate DSA parameters", e); | |
| 254 | + } | |
| 255 | + } else { | |
| 256 | + dsaParams = (DSAParameterSpec)params; | |
| 257 | + } | |
| 258 | + publicKeyTemplate = new CK_ATTRIBUTE[] { | |
| 259 | + new CK_ATTRIBUTE(CKA_PRIME, dsaParams.getP()), | |
| 260 | + new CK_ATTRIBUTE(CKA_SUBPRIME, dsaParams.getQ()), | |
| 261 | + new CK_ATTRIBUTE(CKA_BASE, dsaParams.getG()), | |
| 262 | + }; | |
| 263 | + privateKeyTemplate = new CK_ATTRIBUTE[] { | |
| 264 | + // empty | |
| 265 | + }; | |
| 266 | + } else if (algorithm.equals("DH")) { | |
| 267 | + keyType = CKK_DH; | |
| 268 | + DHParameterSpec dhParams; | |
| 269 | + int privateBits; | |
| 270 | + if (params == null) { | |
| 271 | + try { | |
| 272 | + dhParams = ParameterCache.getDHParameterSpec | |
| 273 | + (keySize, random); | |
| 274 | + } catch (GeneralSecurityException e) { | |
| 275 | + throw new ProviderException | |
| 276 | + ("Could not generate DH parameters", e); | |
| 277 | + } | |
| 278 | + privateBits = 0; | |
| 279 | + } else { | |
| 280 | + dhParams = (DHParameterSpec)params; | |
| 281 | + privateBits = dhParams.getL(); | |
| 282 | + } | |
| 283 | + if (privateBits <= 0) { | |
| 284 | + // XXX find better defaults | |
| 285 | + privateBits = (keySize >= 1024) ? 768 : 512; | |
| 286 | + } | |
| 287 | + publicKeyTemplate = new CK_ATTRIBUTE[] { | |
| 288 | + new CK_ATTRIBUTE(CKA_PRIME, dhParams.getP()), | |
| 289 | + new CK_ATTRIBUTE(CKA_BASE, dhParams.getG()) | |
| 290 | + }; | |
| 291 | + privateKeyTemplate = new CK_ATTRIBUTE[] { | |
| 292 | + new CK_ATTRIBUTE(CKA_VALUE_BITS, privateBits), | |
| 293 | + }; | |
| 294 | + } else if (algorithm.equals("EC")) { | |
| 295 | + keyType = CKK_EC; | |
| 296 | + byte[] encodedParams = | |
| 297 | + P11ECKeyFactory.encodeParameters((ECParameterSpec)params); | |
| 298 | + publicKeyTemplate = new CK_ATTRIBUTE[] { | |
| 299 | + new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams), | |
| 300 | + }; | |
| 301 | + privateKeyTemplate = new CK_ATTRIBUTE[] { | |
| 302 | + // empty | |
| 303 | + }; | |
| 304 | + } else { | |
| 305 | + throw new ProviderException("Unknown algorithm: " + algorithm); | |
| 306 | + } | |
| 307 | + Session session = null; | |
| 308 | + try { | |
| 309 | + session = token.getObjSession(); | |
| 310 | + publicKeyTemplate = token.getAttributes | |
| 311 | + (O_GENERATE, CKO_PUBLIC_KEY, keyType, publicKeyTemplate); | |
| 312 | + privateKeyTemplate = token.getAttributes | |
| 313 | + (O_GENERATE, CKO_PRIVATE_KEY, keyType, privateKeyTemplate); | |
| 314 | + long[] keyIDs = token.p11.C_GenerateKeyPair | |
| 315 | + (session.id(), new CK_MECHANISM(mechanism), | |
| 316 | + publicKeyTemplate, privateKeyTemplate); | |
| 317 | + PublicKey publicKey = P11Key.publicKey | |
| 318 | + (session, keyIDs[0], algorithm, keySize, publicKeyTemplate); | |
| 319 | + PrivateKey privateKey = P11Key.privateKey | |
| 320 | + (session, keyIDs[1], algorithm, keySize, privateKeyTemplate); | |
| 321 | + return new KeyPair(publicKey, privateKey); | |
| 322 | + } catch (PKCS11Exception e) { | |
| 323 | + throw new ProviderException(e); | |
| 324 | + } finally { | |
| 325 | + token.releaseSession(session); | |
| 326 | + } | |
| 327 | + } | |
| 328 | + | |
| 329 | +} | |