src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSAKeyFactory.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/P11RSAKeyFactory.java@5e15de85a1a0
child 50204 3195a713e24d
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
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2003, 2013, 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.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
/**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10336
diff changeset
    41
 * RSA KeyFactory implementation.
2
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
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   133
        } catch (PKCS11Exception | InvalidKeyException e) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   134
            throw new InvalidKeySpecException
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   135
                ("Could not create RSA public key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                PrivateKey key =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        ("Could not create RSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (keySpec instanceof RSAPrivateCrtKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                RSAPrivateCrtKeySpec rs = (RSAPrivateCrtKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    rs.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    rs.getPublicExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    rs.getPrivateExponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    rs.getPrimeP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    rs.getPrimeQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    rs.getPrimeExponentP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    rs.getPrimeExponentQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    rs.getCrtCoefficient()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } else if (keySpec instanceof RSAPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                RSAPrivateKeySpec rs = (RSAPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    rs.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    rs.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    + "and PKCS8EncodedKeySpec supported for RSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   177
        } catch (PKCS11Exception | InvalidKeyException e) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   178
            throw new InvalidKeySpecException
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   179
                ("Could not create RSA private key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private PublicKey generatePublic(BigInteger n, BigInteger e)
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   184
            throws PKCS11Exception, InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   185
        RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            new CK_ATTRIBUTE(CKA_MODULUS, n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                (O_IMPORT, CKO_PUBLIC_KEY, CKK_RSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return P11Key.publicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                (session, keyID, "RSA", n.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private PrivateKey generatePrivate(BigInteger n, BigInteger d)
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   206
            throws PKCS11Exception, InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   207
        RSAKeyFactory.checkKeyLengths(n.bitLength(), null, -1, 64 * 1024);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            new CK_ATTRIBUTE(CKA_MODULUS, n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                (session,  keyID, "RSA", n.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private PrivateKey generatePrivate(BigInteger n, BigInteger e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            BigInteger d, BigInteger p, BigInteger q, BigInteger pe,
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   229
            BigInteger qe, BigInteger coeff) throws PKCS11Exception,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   230
            InvalidKeyException {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   231
        RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            new CK_ATTRIBUTE(CKA_MODULUS, n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            new CK_ATTRIBUTE(CKA_PRIME_1, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            new CK_ATTRIBUTE(CKA_PRIME_2, q),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            new CK_ATTRIBUTE(CKA_EXPONENT_1, pe),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            new CK_ATTRIBUTE(CKA_EXPONENT_2, qe),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            new CK_ATTRIBUTE(CKA_COEFFICIENT, coeff),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                (session, keyID, "RSA", n.bitLength(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   257
    <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   258
            Session[] session) throws PKCS11Exception, InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            KeySpec spec = new RSAPublicKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                attributes[1].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   270
            return keySpec.cast(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else { // X.509 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new InvalidKeySpecException("Only RSAPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                + "X509EncodedKeySpec supported for RSA public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   277
    <T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   278
            Session[] session) throws PKCS11Exception, InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                new CK_ATTRIBUTE(CKA_PRIME_1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                new CK_ATTRIBUTE(CKA_PRIME_2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                new CK_ATTRIBUTE(CKA_EXPONENT_1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                new CK_ATTRIBUTE(CKA_EXPONENT_2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                new CK_ATTRIBUTE(CKA_COEFFICIENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            KeySpec spec = new RSAPrivateCrtKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                attributes[2].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                attributes[3].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                attributes[4].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                attributes[5].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                attributes[6].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                attributes[7].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   302
            return keySpec.cast(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else if (RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            KeySpec spec = new RSAPrivateKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                attributes[0].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                attributes[1].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            );
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   314
            return keySpec.cast(spec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else { // PKCS#8 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new InvalidKeySpecException("Only RSAPrivate(Crt)KeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                + "and PKCS8EncodedKeySpec supported for RSA private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return KeyFactory.getInstance("RSA", P11Util.getSunRsaSignProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}