jdk/src/share/classes/sun/security/pkcs11/P11KeyFactory.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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-2005 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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.pkcs11.wrapper.PKCS11Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * KeyFactory base class. Provides common infrastructure for the RSA, DSA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and DH implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The subclasses support conversion between keys and keyspecs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * using X.509, PKCS#8, and their individual algorithm specific formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * assuming keys are extractable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
abstract class P11KeyFactory extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // algorithm name, currently one of RSA, DSA, DH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    P11KeyFactory(Token token, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Convert an arbitrary key of algorithm into a P11Key of token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Used by P11Signature.init() and RSACipher.init().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static P11Key convertKey(Token token, Key key, String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return (P11Key)token.getKeyFactory(algorithm).engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected final <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if ((key == null) || (keySpec == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                ("key and keySpec must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // delegate to our Java based providers for PKCS#8 and X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                || X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                return (T)implGetSoftwareFactory().getKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                throw new InvalidKeySpecException("Could not encode key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // first translate into a key of this token, if it is not already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            p11Key = (P11Key)engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InvalidKeySpecException("Could not convert key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Session[] session = new Session[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (p11Key.isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                return (T)implGetPublicKeySpec(p11Key, keySpec, session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return (T)implGetPrivateKeySpec(p11Key, keySpec, session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new InvalidKeySpecException("Could not generate KeySpec", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            session[0] = token.releaseSession(session[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected final Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (key.getAlgorithm().equals(this.algorithm) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                ("Key algorithm must be " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (key instanceof P11Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            P11Key p11Key = (P11Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (p11Key.token == token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // already a key of this token, no need to translate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        P11Key p11Key = token.privateCache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (p11Key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            PublicKey publicKey = implTranslatePublicKey((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            token.privateCache.put(key, (P11Key)publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            PrivateKey privateKey = implTranslatePrivateKey((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            token.privateCache.put(key, (P11Key)privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                ("Key must be instance of PublicKey or PrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    abstract KeySpec implGetPublicKeySpec(P11Key key, Class keySpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Session[] session) throws PKCS11Exception, InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    abstract KeySpec implGetPrivateKeySpec(P11Key key, Class keySpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Session[] session) throws PKCS11Exception, InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    abstract PublicKey implTranslatePublicKey(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    abstract PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    abstract KeyFactory implGetSoftwareFactory() throws GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}