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