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