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