jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/P11KeyFactory.java
author katleman
Thu, 07 May 2015 10:19:33 -0700
changeset 30400 3901aa49fb70
parent 25859 3317bb8137f4
permissions -rw-r--r--
Added tag jdk9-b63 for changeset fe75ee04f0d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * KeyFactory base class. Provides common infrastructure for the RSA, DSA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * and DH implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The subclasses support conversion between keys and keyspecs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * using X.509, PKCS#8, and their individual algorithm specific formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * assuming keys are extractable.
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
abstract class P11KeyFactory extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // algorithm name, currently one of RSA, DSA, DH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    P11KeyFactory(Token token, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Convert an arbitrary key of algorithm into a P11Key of token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Used by P11Signature.init() and RSACipher.init().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static P11Key convertKey(Token token, Key key, String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return (P11Key)token.getKeyFactory(algorithm).engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected final <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if ((key == null) || (keySpec == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                ("key and keySpec must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // delegate to our Java based providers for PKCS#8 and X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                || X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    79
                return implGetSoftwareFactory().getKeySpec(key, keySpec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throw new InvalidKeySpecException("Could not encode key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // first translate into a key of this token, if it is not already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            p11Key = (P11Key)engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new InvalidKeySpecException("Could not convert key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Session[] session = new Session[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (p11Key.isPublic()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    94
                return implGetPublicKeySpec(p11Key, keySpec, session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    96
                return implGetPrivateKeySpec(p11Key, keySpec, session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new InvalidKeySpecException("Could not generate KeySpec", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            session[0] = token.releaseSession(session[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected final Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (key.getAlgorithm().equals(this.algorithm) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                ("Key algorithm must be " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (key instanceof P11Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            P11Key p11Key = (P11Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (p11Key.token == token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                // already a key of this token, no need to translate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        P11Key p11Key = token.privateCache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (p11Key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            PublicKey publicKey = implTranslatePublicKey((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            token.privateCache.put(key, (P11Key)publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            PrivateKey privateKey = implTranslatePrivateKey((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            token.privateCache.put(key, (P11Key)privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ("Key must be instance of PublicKey or PrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   140
    abstract <T extends KeySpec> T  implGetPublicKeySpec(P11Key key, Class<T> keySpec,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Session[] session) throws PKCS11Exception, InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   143
    abstract <T extends KeySpec> T  implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            Session[] session) throws PKCS11Exception, InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    abstract PublicKey implTranslatePublicKey(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    abstract PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    abstract KeyFactory implGetSoftwareFactory() throws GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}