src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyPairGenerator.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43248 jdk/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyPairGenerator.java@5e15de85a1a0
child 47421 f9e03aef3a49
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36249
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.spec.DHParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.provider.ParameterCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    41
import sun.security.rsa.RSAKeyFactory;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * KeyPairGenerator implementation class. This class currently supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * RSA, DSA, DH, and EC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Note that for DSA and DH we rely on the Sun and SunJCE providers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * obtain the parameters from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
final class P11KeyPairGenerator extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // selected or default key size, always valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // parameters specified via init, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private AlgorithmParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // for RSA, selected or default value of public exponent, always valid
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    71
    private BigInteger rsaPublicExponent = RSAKeyGenParameterSpec.F4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // SecureRandom instance, if specified in init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    P11KeyPairGenerator(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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 {
36249
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   109
            if (algorithm.equals("DSA")) {
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   110
                // keep default keysize at 1024 since larger keysizes may be
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   111
                // incompatible with SHA1withDSA and SHA-2 Signature algs
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   112
                // may not be supported by native pkcs11 implementations
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   113
                keySize = 1024;
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   114
            } else {
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   115
                // RSA and DH
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   116
                keySize = 2048;
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 25859
diff changeset
   117
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   118
            if ((minKeyLen == -1) || (minKeyLen < 512)) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   119
                minKeyLen = 512;
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
            if (algorithm.equals("RSA")) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   122
                if ((maxKeyLen == -1) || (maxKeyLen > 64 * 1024)) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   123
                    maxKeyLen = 64 * 1024;
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
            }
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   126
        }
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   127
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   128
        // 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
   129
        if ((minKeyLen != -1) && (keySize < minKeyLen)) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   130
            keySize = minKeyLen;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   131
        }
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   132
        if ((maxKeyLen != -1) && (keySize > maxKeyLen)) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   133
            keySize = maxKeyLen;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   134
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.mechanism = mechanism;
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   138
        this.minKeySize = minKeyLen;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   139
        this.maxKeySize = maxKeyLen;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   140
        initialize(keySize, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // see JCA spec
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   144
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void initialize(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   148
            checkKeySize(keySize, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new InvalidParameterException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            params = P11ECKeyFactory.getECParameterSpec(keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (params == null) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   156
                throw new InvalidParameterException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   157
                    "No EC parameters available for key size "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   158
                    + keySize + " bits");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   161
        this.keySize = keySize;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   162
        this.random = random;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // see JCA spec
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   166
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void initialize(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        token.ensureValid();
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   170
        int tmpKeySize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (algorithm.equals("DH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (params instanceof DHParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        ("DHParameterSpec required for Diffie-Hellman");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   176
            DHParameterSpec dhParams = (DHParameterSpec) params;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   177
            tmpKeySize = dhParams.getP().bitLength();
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   178
            checkKeySize(tmpKeySize, dhParams);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // XXX sanity check params
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } else if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (params instanceof RSAKeyGenParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        ("RSAKeyGenParameterSpec required for RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   185
            RSAKeyGenParameterSpec rsaParams =
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   186
                (RSAKeyGenParameterSpec) params;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   187
            tmpKeySize = rsaParams.getKeysize();
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   188
            checkKeySize(tmpKeySize, rsaParams);
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   189
            // override the supplied params to null
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   190
            params = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            this.rsaPublicExponent = rsaParams.getPublicExponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // XXX sanity check params
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (params instanceof DSAParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        ("DSAParameterSpec required for DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   198
            DSAParameterSpec dsaParams = (DSAParameterSpec) params;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   199
            tmpKeySize = dsaParams.getP().bitLength();
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   200
            checkKeySize(tmpKeySize, dsaParams);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // XXX sanity check params
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            ECParameterSpec ecParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (params instanceof ECParameterSpec) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   205
                ecParams = P11ECKeyFactory.getECParameterSpec(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   206
                    (ECParameterSpec)params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (ecParams == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        ("Unsupported curve: " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } else if (params instanceof ECGenParameterSpec) {
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   212
                String name = ((ECGenParameterSpec) params).getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                ecParams = P11ECKeyFactory.getECParameterSpec(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (ecParams == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        ("Unknown curve name: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   218
                // override the supplied params with the derived one
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   219
                params = ecParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    ("ECParameterSpec or ECGenParameterSpec required for EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   224
            tmpKeySize = ecParams.getCurve().getField().getFieldSize();
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   225
            checkKeySize(tmpKeySize, ecParams);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new ProviderException("Unknown algorithm: " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   229
        this.keySize = tmpKeySize;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   230
        this.params = params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   234
    private void checkKeySize(int keySize, AlgorithmParameterSpec params)
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   235
        throws InvalidAlgorithmParameterException {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   236
        // check native range first
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   237
        if ((minKeySize != -1) && (keySize < minKeySize)) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   238
            throw new InvalidAlgorithmParameterException(algorithm +
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   239
                " key must be at least " + minKeySize + " bits. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   240
                "The specific key size " + keySize + " is not supported");
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   241
        }
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   242
        if ((maxKeySize != -1) && (keySize > maxKeySize)) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   243
            throw new InvalidAlgorithmParameterException(algorithm +
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   244
                " key must be at most " + maxKeySize + " bits. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   245
                "The specific key size " + keySize + " is not supported");
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   246
        }
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   247
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   248
        // check our own algorithm-specific limits also
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (keySize < 112) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   251
                throw new InvalidAlgorithmParameterException(
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   252
                    "EC key size must be at least 112 bit. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   253
                    "The specific key size " + keySize + " is not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (keySize > 2048) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                // sanity check, nobody really wants keys this large
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   257
                throw new InvalidAlgorithmParameterException(
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   258
                    "EC key size must be at most 2048 bit. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   259
                    "The specific key size " + keySize + " is not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   261
        } else {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   262
            // RSA, DH, DSA
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   263
            if (keySize < 512) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   264
                throw new InvalidAlgorithmParameterException(algorithm +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   265
                    " key size must be at least 512 bit. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   266
                    "The specific key size " + keySize + " is not supported");
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   267
            }
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   268
            if (algorithm.equals("RSA")) {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   269
                BigInteger tmpExponent = rsaPublicExponent;
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   270
                if (params != null) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   271
                    tmpExponent =
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   272
                        ((RSAKeyGenParameterSpec)params).getPublicExponent();
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   273
                }
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   274
                try {
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   275
                    // Reuse the checking in SunRsaSign provider.
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   276
                    // If maxKeySize is -1, then replace it with
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   277
                    // Integer.MAX_VALUE to indicate no limit.
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   278
                    RSAKeyFactory.checkKeyLengths(keySize, tmpExponent,
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   279
                        minKeySize,
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   280
                        (maxKeySize==-1? Integer.MAX_VALUE:maxKeySize));
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   281
                } catch (InvalidKeyException e) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   282
                    throw new InvalidAlgorithmParameterException(e);
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   283
                }
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   284
            } else if (algorithm.equals("DH")) {
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   285
                if (params != null) {   // initialized with specified parameters
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   286
                    // sanity check, nobody really wants keys this large
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   287
                    if (keySize > 64 * 1024) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   288
                        throw new InvalidAlgorithmParameterException(
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   289
                            "DH key size must be at most 65536 bit. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   290
                            "The specific key size " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   291
                            keySize + " is not supported");
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   292
                    }
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   293
                } else {        // default parameters will be used.
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   294
                    // Range is based on the values in
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   295
                    // sun.security.provider.ParameterCache class.
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   296
                    if ((keySize > 8192) || (keySize < 512) ||
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   297
                            ((keySize & 0x3f) != 0)) {
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   298
                        throw new InvalidAlgorithmParameterException(
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   299
                            "DH key size must be multiple of 64, and can " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   300
                            "only range from 512 to 8192 (inclusive). " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   301
                            "The specific key size " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   302
                            keySize + " is not supported");
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   303
                    }
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   304
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   305
                    DHParameterSpec cache =
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   306
                            ParameterCache.getCachedDHParameterSpec(keySize);
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   307
                    // Except 2048 and 3072, not yet support generation of
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   308
                    // parameters bigger than 1024 bits.
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   309
                    if ((cache == null) && (keySize > 1024)) {
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   310
                        throw new InvalidAlgorithmParameterException(
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   311
                                "Unsupported " + keySize +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   312
                                "-bit DH parameter generation");
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   313
                    }
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   314
                }
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   315
            } else {
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   316
                // this restriction is in the spec for DSA
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   317
                if ((keySize != 3072) && (keySize != 2048) &&
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   318
                        ((keySize > 1024) || ((keySize & 0x3f) != 0))) {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   319
                    throw new InvalidAlgorithmParameterException(
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   320
                        "DSA key must be multiples of 64 if less than " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   321
                        "1024 bits, or 2048, 3072 bits. " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   322
                        "The specific key size " +
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   323
                        keySize + " is not supported");
20751
56898cefcda9 7196382: PKCS11 provider should support 2048-bit DH
valeriep
parents: 5506
diff changeset
   324
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    // see JCA spec
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 36249
diff changeset
   330
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        CK_ATTRIBUTE[] publicKeyTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        CK_ATTRIBUTE[] privateKeyTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        long keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            keyType = CKK_RSA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            publicKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                new CK_ATTRIBUTE(CKA_MODULUS_BITS, keySize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, rsaPublicExponent),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            privateKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            keyType = CKK_DSA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            DSAParameterSpec dsaParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    dsaParams = ParameterCache.getDSAParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                    (keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            ("Could not generate DSA parameters", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                dsaParams = (DSAParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            publicKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                new CK_ATTRIBUTE(CKA_PRIME, dsaParams.getP()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                new CK_ATTRIBUTE(CKA_SUBPRIME, dsaParams.getQ()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                new CK_ATTRIBUTE(CKA_BASE, dsaParams.getG()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            privateKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } else if (algorithm.equals("DH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            keyType = CKK_DH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            DHParameterSpec dhParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            int privateBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    dhParams = ParameterCache.getDHParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                                    (keySize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                            ("Could not generate DH parameters", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                privateBits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                dhParams = (DHParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                privateBits = dhParams.getL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (privateBits <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                // XXX find better defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                privateBits = (keySize >= 1024) ? 768 : 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            publicKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                new CK_ATTRIBUTE(CKA_PRIME, dhParams.getP()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                new CK_ATTRIBUTE(CKA_BASE, dhParams.getG())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            privateKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                new CK_ATTRIBUTE(CKA_VALUE_BITS, privateBits),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            keyType = CKK_EC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            byte[] encodedParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    P11ECKeyFactory.encodeParameters((ECParameterSpec)params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            publicKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            privateKeyTemplate = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new ProviderException("Unknown algorithm: " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            publicKeyTemplate = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                (O_GENERATE, CKO_PUBLIC_KEY, keyType, publicKeyTemplate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            privateKeyTemplate = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                (O_GENERATE, CKO_PRIVATE_KEY, keyType, privateKeyTemplate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            long[] keyIDs = token.p11.C_GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                (session.id(), new CK_MECHANISM(mechanism),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                publicKeyTemplate, privateKeyTemplate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            PublicKey publicKey = P11Key.publicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                (session, keyIDs[0], algorithm, keySize, publicKeyTemplate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            PrivateKey privateKey = P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                (session, keyIDs[1], algorithm, keySize, privateKeyTemplate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return new KeyPair(publicKey, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}