jdk/src/share/classes/sun/security/pkcs11/P11RSAKeyFactory.java
author wetmore
Fri, 22 Aug 2008 18:48:00 -0700
changeset 2596 a1964c157e68
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6497740: Limit the size of RSA public keys Reviewed-by: andreas, valeriep, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    38
import sun.security.rsa.RSAKeyFactory;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * RSA KeyFactory implemenation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class P11RSAKeyFactory extends P11KeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    P11RSAKeyFactory(Token token, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        super(token, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                RSAPublicKey rsaKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    rsaKey.getPublicExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                // let SunRsaSign provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                key = new sun.security.rsa.RSAPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                throw new InvalidKeyException("PublicKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        + "of RSAPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new InvalidKeyException("Could not create RSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    rsaKey.getPublicExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    rsaKey.getPrivateExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    rsaKey.getPrimeP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    rsaKey.getPrimeQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    rsaKey.getPrimeExponentP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    rsaKey.getPrimeExponentQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    rsaKey.getCrtCoefficient()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else if (key instanceof RSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                RSAPrivateKey rsaKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    rsaKey.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    rsaKey.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // let SunRsaSign provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                key = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new InvalidKeyException("Private key must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        + "of RSAPrivate(Crt)Key or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new InvalidKeyException("Could not create RSA private key", e);
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
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                PublicKey key = new sun.security.rsa.RSAPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        ("Could not create RSA public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (keySpec instanceof RSAPublicKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                + "X509EncodedKeySpec supported for RSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            RSAPublicKeySpec rs = (RSAPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                rs.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                rs.getPublicExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                ("Could not create RSA public key", e);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   136
        } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   137
            throw new InvalidKeySpecException
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   138
                ("Could not create RSA public key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                PrivateKey key =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        ("Could not create RSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (keySpec instanceof RSAPrivateCrtKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                RSAPrivateCrtKeySpec rs = (RSAPrivateCrtKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    rs.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    rs.getPublicExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    rs.getPrivateExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    rs.getPrimeP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    rs.getPrimeQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    rs.getPrimeExponentP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    rs.getPrimeExponentQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    rs.getCrtCoefficient()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else if (keySpec instanceof RSAPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                RSAPrivateKeySpec rs = (RSAPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    rs.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    rs.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    + "and PKCS8EncodedKeySpec supported for RSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                ("Could not create RSA private key", e);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   183
        } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   184
            throw new InvalidKeySpecException
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   185
                ("Could not create RSA private key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private PublicKey generatePublic(BigInteger n, BigInteger e)
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   190
            throws PKCS11Exception, InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   191
        RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            new CK_ATTRIBUTE(CKA_MODULUS, n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                (O_IMPORT, CKO_PUBLIC_KEY, CKK_RSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return P11Key.publicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                (session, keyID, "RSA", n.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private PrivateKey generatePrivate(BigInteger n, BigInteger d)
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   212
            throws PKCS11Exception, InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   213
        RSAKeyFactory.checkKeyLengths(n.bitLength(), null, -1, 64 * 1024);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            new CK_ATTRIBUTE(CKA_MODULUS, n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                (session,  keyID, "RSA", n.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private PrivateKey generatePrivate(BigInteger n, BigInteger e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            BigInteger d, BigInteger p, BigInteger q, BigInteger pe,
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   235
            BigInteger qe, BigInteger coeff) throws PKCS11Exception,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   236
            InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   237
        RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            new CK_ATTRIBUTE(CKA_MODULUS, n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            new CK_ATTRIBUTE(CKA_PRIME_1, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            new CK_ATTRIBUTE(CKA_PRIME_2, q),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            new CK_ATTRIBUTE(CKA_EXPONENT_1, pe),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            new CK_ATTRIBUTE(CKA_EXPONENT_2, qe),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            new CK_ATTRIBUTE(CKA_COEFFICIENT, coeff),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                (session, keyID, "RSA", n.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    KeySpec implGetPublicKeySpec(P11Key key, Class keySpec, Session[] session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throws PKCS11Exception, InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            KeySpec spec = new RSAPublicKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                attributes[1].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else { // X.509 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                + "X509EncodedKeySpec supported for RSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    KeySpec implGetPrivateKeySpec(P11Key key, Class keySpec, Session[] session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throws PKCS11Exception, InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                new CK_ATTRIBUTE(CKA_PRIME_1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                new CK_ATTRIBUTE(CKA_PRIME_2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                new CK_ATTRIBUTE(CKA_EXPONENT_1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                new CK_ATTRIBUTE(CKA_EXPONENT_2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                new CK_ATTRIBUTE(CKA_COEFFICIENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            KeySpec spec = new RSAPrivateCrtKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                attributes[2].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                attributes[3].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                attributes[4].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                attributes[5].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                attributes[6].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                attributes[7].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else if (RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            KeySpec spec = new RSAPrivateKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                attributes[1].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else { // PKCS#8 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                + "and PKCS8EncodedKeySpec supported for RSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return KeyFactory.getInstance("RSA", P11Util.getSunRsaSignProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}