Showing the changes to java/pkg/com/sun/crypto/provider/CipherWithWrappingSpi.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,261 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1999, 2007, 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 com.sun.crypto.provider; | ||
| 27 | |||
| 28 | import java.security.Key; | ||
| 29 | import java.security.PublicKey; | ||
| 30 | import java.security.PrivateKey; | ||
| 31 | import java.security.KeyFactory; | ||
| 32 | import java.security.InvalidKeyException; | ||
| 33 | import java.security.NoSuchProviderException; | ||
| 34 | import java.security.NoSuchAlgorithmException; | ||
| 35 | import java.security.spec.PKCS8EncodedKeySpec; | ||
| 36 | import java.security.spec.X509EncodedKeySpec; | ||
| 37 | import java.security.spec.InvalidKeySpecException; | ||
| 38 | |||
| 39 | import javax.crypto.Cipher; | ||
| 40 | import javax.crypto.CipherSpi; | ||
| 41 | import javax.crypto.SecretKey; | ||
| 42 | import javax.crypto.IllegalBlockSizeException; | ||
| 43 | import javax.crypto.BadPaddingException; | ||
| 44 | import javax.crypto.spec.SecretKeySpec; | ||
| 45 | |||
| 46 | /** | ||
| 47 | * This class entends the javax.crypto.CipherSpi class with a concrete | ||
| 48 | * implementation of the methods for wrapping and unwrapping | ||
| 49 | * keys. | ||
| 50 | * | ||
| 51 | * @author Sharon Liu | ||
| 52 | * | ||
| 53 | * | ||
| 54 | * @see javax.crypto.CipherSpi | ||
| 55 | * @see BlowfishCipher | ||
| 56 | * @see DESCipher | ||
| 57 | * @see PBEWithMD5AndDESCipher | ||
| 58 | */ | ||
| 59 | |||
| 60 | public abstract class CipherWithWrappingSpi extends CipherSpi { | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Wrap a key. | ||
| 64 | * | ||
| 65 | * @param key the key to be wrapped. | ||
| 66 | * | ||
| 67 | * @return the wrapped key. | ||
| 68 | * | ||
| 69 | * @exception IllegalBlockSizeException if this cipher is a block | ||
| 70 | * cipher, no padding has been requested, and the length of the | ||
| 71 | * encoding of the key to be wrapped is not a | ||
| 72 | * multiple of the block size. | ||
| 73 | * | ||
| 74 | * @exception InvalidKeyException if it is impossible or unsafe to | ||
| 75 | * wrap the key with this cipher (e.g., a hardware protected key is | ||
| 76 | * being passed to a software only cipher). | ||
| 77 | */ | ||
| 78 | protected final byte[] engineWrap(Key key) | ||
| 79 | throws IllegalBlockSizeException, InvalidKeyException | ||
| 80 | { | ||
| 81 | byte[] result = null; | ||
| 82 | |||
| 83 | try { | ||
| 84 | byte[] encodedKey = key.getEncoded(); | ||
| 85 | if ((encodedKey == null) || (encodedKey.length == 0)) { | ||
| 86 | throw new InvalidKeyException("Cannot get an encoding of " + | ||
| 87 | "the key to be wrapped"); | ||
| 88 | } | ||
| 89 | |||
| 90 | result = engineDoFinal(encodedKey, 0, encodedKey.length); | ||
| 91 | } catch (BadPaddingException e) { | ||
| 92 | // Should never happen | ||
| 93 | } | ||
| 94 | |||
| 95 | return result; | ||
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Unwrap a previously wrapped key. | ||
| 100 | * | ||
| 101 | * @param wrappedKey the key to be unwrapped. | ||
| 102 | * | ||
| 103 | * @param wrappedKeyAlgorithm the algorithm the wrapped key is for. | ||
| 104 | * | ||
| 105 | * @param wrappedKeyType the type of the wrapped key. | ||
| 106 | * This is one of <code>Cipher.SECRET_KEY</code>, | ||
| 107 | * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>. | ||
| 108 | * | ||
| 109 | * @return the unwrapped key. | ||
| 110 | * | ||
| 111 | * @exception InvalidKeyException if <code>wrappedKey</code> does not | ||
| 112 | * represent a wrapped key, or if the algorithm associated with the | ||
| 113 | * wrapped key is different from <code>wrappedKeyAlgorithm</code> | ||
| 114 | * and/or its key type is different from <code>wrappedKeyType</code>. | ||
| 115 | * | ||
| 116 | * @exception NoSuchAlgorithmException if no installed providers | ||
| 117 | * can create keys for the <code>wrappedKeyAlgorithm</code>. | ||
| 118 | */ | ||
| 119 | protected final Key engineUnwrap(byte[] wrappedKey, | ||
| 120 | String wrappedKeyAlgorithm, | ||
| 121 | int wrappedKeyType) | ||
| 122 | throws InvalidKeyException, NoSuchAlgorithmException | ||
| 123 | { | ||
| 124 | byte[] encodedKey; | ||
| 125 | Key result = null; | ||
| 126 | |||
| 127 | try { | ||
| 128 | encodedKey = engineDoFinal(wrappedKey, 0, | ||
| 129 | wrappedKey.length); | ||
| 130 | } catch (BadPaddingException ePadding) { | ||
| 131 | throw new InvalidKeyException(); | ||
| 132 | } catch (IllegalBlockSizeException eBlockSize) { | ||
| 133 | throw new InvalidKeyException(); | ||
| 134 | } | ||
| 135 | |||
| 136 | switch (wrappedKeyType) { | ||
| 137 | case Cipher.SECRET_KEY: | ||
| 138 | result = constructSecretKey(encodedKey, | ||
| 139 | wrappedKeyAlgorithm); | ||
| 140 | break; | ||
| 141 | case Cipher.PRIVATE_KEY: | ||
| 142 | result = constructPrivateKey(encodedKey, | ||
| 143 | wrappedKeyAlgorithm); | ||
| 144 | break; | ||
| 145 | case Cipher.PUBLIC_KEY: | ||
| 146 | result = constructPublicKey(encodedKey, | ||
| 147 | wrappedKeyAlgorithm); | ||
| 148 | break; | ||
| 149 | } | ||
| 150 | |||
| 151 | return result; | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Construct a public key from its encoding. | ||
| 157 | * | ||
| 158 | * @param encodedKey the encoding of a public key. | ||
| 159 | * | ||
| 160 | * @param encodedKeyAlgorithm the algorithm the encodedKey is for. | ||
| 161 | * | ||
| 162 | * @return a public key constructed from the encodedKey. | ||
| 163 | */ | ||
| 164 | private final PublicKey constructPublicKey(byte[] encodedKey, | ||
| 165 | String encodedKeyAlgorithm) | ||
| 166 | throws InvalidKeyException, NoSuchAlgorithmException | ||
| 167 | { | ||
| 168 | PublicKey key = null; | ||
| 169 | |||
| 170 | try { | ||
| 171 | KeyFactory keyFactory = | ||
| 172 | KeyFactory.getInstance(encodedKeyAlgorithm, "SunJCE"); | ||
| 173 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey); | ||
| 174 | key = keyFactory.generatePublic(keySpec); | ||
| 175 | } catch (NoSuchAlgorithmException nsae) { | ||
| 176 | // Try to see whether there is another | ||
| 177 | // provider which supports this algorithm | ||
| 178 | try { | ||
| 179 | KeyFactory keyFactory = | ||
| 180 | KeyFactory.getInstance(encodedKeyAlgorithm); | ||
| 181 | X509EncodedKeySpec keySpec = | ||
| 182 | new X509EncodedKeySpec(encodedKey); | ||
| 183 | key = keyFactory.generatePublic(keySpec); | ||
| 184 | } catch (NoSuchAlgorithmException nsae2) { | ||
| 185 | throw new NoSuchAlgorithmException("No installed providers " + | ||
| 186 | "can create keys for the " + | ||
| 187 | encodedKeyAlgorithm + | ||
| 188 | "algorithm"); | ||
| 189 | } catch (InvalidKeySpecException ikse2) { | ||
| 190 | // Should never happen. | ||
| 191 | } | ||
| 192 | } catch (InvalidKeySpecException ikse) { | ||
| 193 | // Should never happen. | ||
| 194 | } catch (NoSuchProviderException nspe) { | ||
| 195 | // Should never happen. | ||
| 196 | } | ||
| 197 | |||
| 198 | return key; | ||
| 199 | } | ||
| 200 | |||
| 201 | /** | ||
| 202 | * Construct a private key from its encoding. | ||
| 203 | * | ||
| 204 | * @param encodedKey the encoding of a private key. | ||
| 205 | * | ||
| 206 | * @param encodedKeyAlgorithm the algorithm the wrapped key is for. | ||
| 207 | * | ||
| 208 | * @return a private key constructed from the encodedKey. | ||
| 209 | */ | ||
| 210 | private final PrivateKey constructPrivateKey(byte[] encodedKey, | ||
| 211 | String encodedKeyAlgorithm) | ||
| 212 | throws InvalidKeyException, NoSuchAlgorithmException | ||
| 213 | { | ||
| 214 | PrivateKey key = null; | ||
| 215 | |||
| 216 | try { | ||
| 217 | KeyFactory keyFactory = | ||
| 218 | KeyFactory.getInstance(encodedKeyAlgorithm, "SunJCE"); | ||
| 219 | PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey); | ||
| 220 | return keyFactory.generatePrivate(keySpec); | ||
| 221 | } catch (NoSuchAlgorithmException nsae) { | ||
| 222 | // Try to see whether there is another | ||
| 223 | // provider which supports this algorithm | ||
| 224 | try { | ||
| 225 | KeyFactory keyFactory = | ||
| 226 | KeyFactory.getInstance(encodedKeyAlgorithm); | ||
| 227 | PKCS8EncodedKeySpec keySpec = | ||
| 228 | new PKCS8EncodedKeySpec(encodedKey); | ||
| 229 | key = keyFactory.generatePrivate(keySpec); | ||
| 230 | } catch (NoSuchAlgorithmException nsae2) { | ||
| 231 | throw new NoSuchAlgorithmException("No installed providers " + | ||
| 232 | "can create keys for the " + | ||
| 233 | encodedKeyAlgorithm + | ||
| 234 | "algorithm"); | ||
| 235 | } catch (InvalidKeySpecException ikse2) { | ||
| 236 | // Should never happen. | ||
| 237 | } | ||
| 238 | } catch (InvalidKeySpecException ikse) { | ||
| 239 | // Should never happen. | ||
| 240 | } catch (NoSuchProviderException nspe) { | ||
| 241 | // Should never happen. | ||
| 242 | } | ||
| 243 | |||
| 244 | return key; | ||
| 245 | } | ||
| 246 | |||
| 247 | /** | ||
| 248 | * Construct a secret key from its encoding. | ||
| 249 | * | ||
| 250 | * @param encodedKey the encoding of a secret key. | ||
| 251 | * | ||
| 252 | * @param encodedKeyAlgorithm the algorithm the secret key is for. | ||
| 253 | * | ||
| 254 | * @return a secret key constructed from the encodedKey. | ||
| 255 | */ | ||
| 256 | private final SecretKey constructSecretKey(byte[] encodedKey, | ||
| 257 | String encodedKeyAlgorithm) | ||
| 258 | { | ||
| 259 | return (new SecretKeySpec(encodedKey, encodedKeyAlgorithm)); | ||
| 260 | } | ||
| 261 | } | ||