Showing the changes to java/pkg/sun/security/pkcs11/P11KeyFactory.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,155 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.security.pkcs11; | ||
| 27 | |||
| 28 | import java.security.*; | ||
| 29 | import java.security.spec.*; | ||
| 30 | |||
| 31 | import sun.security.pkcs11.wrapper.PKCS11Exception; | ||
| 32 | import static sun.security.pkcs11.wrapper.PKCS11Constants.*; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * KeyFactory base class. Provides common infrastructure for the RSA, DSA, | ||
| 36 | * and DH implementations. | ||
| 37 | * | ||
| 38 | * The subclasses support conversion between keys and keyspecs | ||
| 39 | * using X.509, PKCS#8, and their individual algorithm specific formats, | ||
| 40 | * assuming keys are extractable. | ||
| 41 | * | ||
| 42 | * @author Andreas Sterbenz | ||
| 43 | * @since 1.5 | ||
| 44 | */ | ||
| 45 | abstract class P11KeyFactory extends KeyFactorySpi { | ||
| 46 | |||
| 47 | // token instance | ||
| 48 | final Token token; | ||
| 49 | |||
| 50 | // algorithm name, currently one of RSA, DSA, DH | ||
| 51 | final String algorithm; | ||
| 52 | |||
| 53 | P11KeyFactory(Token token, String algorithm) { | ||
| 54 | super(); | ||
| 55 | this.token = token; | ||
| 56 | this.algorithm = algorithm; | ||
| 57 | } | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Convert an arbitrary key of algorithm into a P11Key of token. | ||
| 61 | * Used by P11Signature.init() and RSACipher.init(). | ||
| 62 | */ | ||
| 63 | static P11Key convertKey(Token token, Key key, String algorithm) | ||
| 64 | throws InvalidKeyException { | ||
| 65 | return (P11Key)token.getKeyFactory(algorithm).engineTranslateKey(key); | ||
| 66 | } | ||
| 67 | |||
| 68 | // see JCA spec | ||
| 69 | protected final <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) | ||
| 70 | throws InvalidKeySpecException { | ||
| 71 | token.ensureValid(); | ||
| 72 | if ((key == null) || (keySpec == null)) { | ||
| 73 | throw new InvalidKeySpecException | ||
| 74 | ("key and keySpec must not be null"); | ||
| 75 | } | ||
| 76 | // delegate to our Java based providers for PKCS#8 and X.509 | ||
| 77 | if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec) | ||
| 78 | || X509EncodedKeySpec.class.isAssignableFrom(keySpec)) { | ||
| 79 | try { | ||
| 80 | return (T)implGetSoftwareFactory().getKeySpec(key, keySpec); | ||
| 81 | } catch (GeneralSecurityException e) { | ||
| 82 | throw new InvalidKeySpecException("Could not encode key", e); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | // first translate into a key of this token, if it is not already | ||
| 86 | P11Key p11Key; | ||
| 87 | try { | ||
| 88 | p11Key = (P11Key)engineTranslateKey(key); | ||
| 89 | } catch (InvalidKeyException e) { | ||
| 90 | throw new InvalidKeySpecException("Could not convert key", e); | ||
| 91 | } | ||
| 92 | Session[] session = new Session[1]; | ||
| 93 | try { | ||
| 94 | if (p11Key.isPublic()) { | ||
| 95 | return (T)implGetPublicKeySpec(p11Key, keySpec, session); | ||
| 96 | } else { | ||
| 97 | return (T)implGetPrivateKeySpec(p11Key, keySpec, session); | ||
| 98 | } | ||
| 99 | } catch (PKCS11Exception e) { | ||
| 100 | throw new InvalidKeySpecException("Could not generate KeySpec", e); | ||
| 101 | } finally { | ||
| 102 | session[0] = token.releaseSession(session[0]); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | // see JCA spec | ||
| 107 | protected final Key engineTranslateKey(Key key) throws InvalidKeyException { | ||
| 108 | token.ensureValid(); | ||
| 109 | if (key == null) { | ||
| 110 | throw new InvalidKeyException("Key must not be null"); | ||
| 111 | } | ||
| 112 | if (key.getAlgorithm().equals(this.algorithm) == false) { | ||
| 113 | throw new InvalidKeyException | ||
| 114 | ("Key algorithm must be " + algorithm); | ||
| 115 | } | ||
| 116 | if (key instanceof P11Key) { | ||
| 117 | P11Key p11Key = (P11Key)key; | ||
| 118 | if (p11Key.token == token) { | ||
| 119 | // already a key of this token, no need to translate | ||
| 120 | return key; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | P11Key p11Key = token.privateCache.get(key); | ||
| 124 | if (p11Key != null) { | ||
| 125 | return p11Key; | ||
| 126 | } | ||
| 127 | if (key instanceof PublicKey) { | ||
| 128 | PublicKey publicKey = implTranslatePublicKey((PublicKey)key); | ||
| 129 | token.privateCache.put(key, (P11Key)publicKey); | ||
| 130 | return publicKey; | ||
| 131 | } else if (key instanceof PrivateKey) { | ||
| 132 | PrivateKey privateKey = implTranslatePrivateKey((PrivateKey)key); | ||
| 133 | token.privateCache.put(key, (P11Key)privateKey); | ||
| 134 | return privateKey; | ||
| 135 | } else { | ||
| 136 | throw new InvalidKeyException | ||
| 137 | ("Key must be instance of PublicKey or PrivateKey"); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | abstract KeySpec implGetPublicKeySpec(P11Key key, Class keySpec, | ||
| 142 | Session[] session) throws PKCS11Exception, InvalidKeySpecException; | ||
| 143 | |||
| 144 | abstract KeySpec implGetPrivateKeySpec(P11Key key, Class keySpec, | ||
| 145 | Session[] session) throws PKCS11Exception, InvalidKeySpecException; | ||
| 146 | |||
| 147 | abstract PublicKey implTranslatePublicKey(PublicKey key) | ||
| 148 | throws InvalidKeyException; | ||
| 149 | |||
| 150 | abstract PrivateKey implTranslatePrivateKey(PrivateKey key) | ||
| 151 | throws InvalidKeyException; | ||
| 152 | |||
| 153 | abstract KeyFactory implGetSoftwareFactory() throws GeneralSecurityException; | ||
| 154 | |||
| 155 | } | ||