Showing the changes to java/pkg/com/sun/crypto/provider/ConstructKeys.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,191 @@ | |||
| 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.SecretKey; | ||
| 40 | import javax.crypto.Cipher; | ||
| 41 | import javax.crypto.spec.SecretKeySpec; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * This class is a helper class which construct key objects | ||
| 45 | * from encoded keys. | ||
| 46 | * | ||
| 47 | * @author Sharon Liu | ||
| 48 | * | ||
| 49 | */ | ||
| 50 | |||
| 51 | final class ConstructKeys { | ||
| 52 | /** | ||
| 53 | * Construct a public key from its encoding. | ||
| 54 | * | ||
| 55 | * @param encodedKey the encoding of a public key. | ||
| 56 | * | ||
| 57 | * @param encodedKeyAlgorithm the algorithm the encodedKey is for. | ||
| 58 | * | ||
| 59 | * @return a public key constructed from the encodedKey. | ||
| 60 | */ | ||
| 61 | private static final PublicKey constructPublicKey(byte[] encodedKey, | ||
| 62 | String encodedKeyAlgorithm) | ||
| 63 | throws InvalidKeyException, NoSuchAlgorithmException | ||
| 64 | { | ||
| 65 | PublicKey key = null; | ||
| 66 | |||
| 67 | try { | ||
| 68 | KeyFactory keyFactory = | ||
| 69 | KeyFactory.getInstance(encodedKeyAlgorithm, "SunJCE"); | ||
| 70 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey); | ||
| 71 | key = keyFactory.generatePublic(keySpec); | ||
| 72 | } catch (NoSuchAlgorithmException nsae) { | ||
| 73 | // Try to see whether there is another | ||
| 74 | // provider which supports this algorithm | ||
| 75 | try { | ||
| 76 | KeyFactory keyFactory = | ||
| 77 | KeyFactory.getInstance(encodedKeyAlgorithm); | ||
| 78 | X509EncodedKeySpec keySpec = | ||
| 79 | new X509EncodedKeySpec(encodedKey); | ||
| 80 | key = keyFactory.generatePublic(keySpec); | ||
| 81 | } catch (NoSuchAlgorithmException nsae2) { | ||
| 82 | throw new NoSuchAlgorithmException("No installed providers " + | ||
| 83 | "can create keys for the " + | ||
| 84 | encodedKeyAlgorithm + | ||
| 85 | "algorithm"); | ||
| 86 | } catch (InvalidKeySpecException ikse2) { | ||
| 87 | InvalidKeyException ike = | ||
| 88 | new InvalidKeyException("Cannot construct public key"); | ||
| 89 | ike.initCause(ikse2); | ||
| 90 | throw ike; | ||
| 91 | } | ||
| 92 | } catch (InvalidKeySpecException ikse) { | ||
| 93 | InvalidKeyException ike = | ||
| 94 | new InvalidKeyException("Cannot construct public key"); | ||
| 95 | ike.initCause(ikse); | ||
| 96 | throw ike; | ||
| 97 | } catch (NoSuchProviderException nspe) { | ||
| 98 | // Should never happen. | ||
| 99 | } | ||
| 100 | |||
| 101 | return key; | ||
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Construct a private key from its encoding. | ||
| 106 | * | ||
| 107 | * @param encodedKey the encoding of a private key. | ||
| 108 | * | ||
| 109 | * @param encodedKeyAlgorithm the algorithm the wrapped key is for. | ||
| 110 | * | ||
| 111 | * @return a private key constructed from the encodedKey. | ||
| 112 | */ | ||
| 113 | private static final PrivateKey constructPrivateKey(byte[] encodedKey, | ||
| 114 | String encodedKeyAlgorithm) | ||
| 115 | throws InvalidKeyException, NoSuchAlgorithmException | ||
| 116 | { | ||
| 117 | PrivateKey key = null; | ||
| 118 | |||
| 119 | try { | ||
| 120 | KeyFactory keyFactory = | ||
| 121 | KeyFactory.getInstance(encodedKeyAlgorithm, "SunJCE"); | ||
| 122 | PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey); | ||
| 123 | return keyFactory.generatePrivate(keySpec); | ||
| 124 | } catch (NoSuchAlgorithmException nsae) { | ||
| 125 | // Try to see whether there is another | ||
| 126 | // provider which supports this algorithm | ||
| 127 | try { | ||
| 128 | KeyFactory keyFactory = | ||
| 129 | KeyFactory.getInstance(encodedKeyAlgorithm); | ||
| 130 | PKCS8EncodedKeySpec keySpec = | ||
| 131 | new PKCS8EncodedKeySpec(encodedKey); | ||
| 132 | key = keyFactory.generatePrivate(keySpec); | ||
| 133 | } catch (NoSuchAlgorithmException nsae2) { | ||
| 134 | throw new NoSuchAlgorithmException("No installed providers " + | ||
| 135 | "can create keys for the " + | ||
| 136 | encodedKeyAlgorithm + | ||
| 137 | "algorithm"); | ||
| 138 | } catch (InvalidKeySpecException ikse2) { | ||
| 139 | InvalidKeyException ike = | ||
| 140 | new InvalidKeyException("Cannot construct private key"); | ||
| 141 | ike.initCause(ikse2); | ||
| 142 | throw ike; | ||
| 143 | } | ||
| 144 | } catch (InvalidKeySpecException ikse) { | ||
| 145 | InvalidKeyException ike = | ||
| 146 | new InvalidKeyException("Cannot construct private key"); | ||
| 147 | ike.initCause(ikse); | ||
| 148 | throw ike; | ||
| 149 | } catch (NoSuchProviderException nspe) { | ||
| 150 | // Should never happen. | ||
| 151 | } | ||
| 152 | |||
| 153 | return key; | ||
| 154 | } | ||
| 155 | |||
| 156 | /** | ||
| 157 | * Construct a secret key from its encoding. | ||
| 158 | * | ||
| 159 | * @param encodedKey the encoding of a secret key. | ||
| 160 | * | ||
| 161 | * @param encodedKeyAlgorithm the algorithm the secret key is for. | ||
| 162 | * | ||
| 163 | * @return a secret key constructed from the encodedKey. | ||
| 164 | */ | ||
| 165 | private static final SecretKey constructSecretKey(byte[] encodedKey, | ||
| 166 | String encodedKeyAlgorithm) | ||
| 167 | { | ||
| 168 | return (new SecretKeySpec(encodedKey, encodedKeyAlgorithm)); | ||
| 169 | } | ||
| 170 | |||
| 171 | static final Key constructKey(byte[] encoding, String keyAlgorithm, | ||
| 172 | int keyType) | ||
| 173 | throws InvalidKeyException, NoSuchAlgorithmException { | ||
| 174 | Key result = null; | ||
| 175 | switch (keyType) { | ||
| 176 | case Cipher.SECRET_KEY: | ||
| 177 | result = ConstructKeys.constructSecretKey(encoding, | ||
| 178 | keyAlgorithm); | ||
| 179 | break; | ||
| 180 | case Cipher.PRIVATE_KEY: | ||
| 181 | result = ConstructKeys.constructPrivateKey(encoding, | ||
| 182 | keyAlgorithm); | ||
| 183 | break; | ||
| 184 | case Cipher.PUBLIC_KEY: | ||
| 185 | result = ConstructKeys.constructPublicKey(encoding, | ||
| 186 | keyAlgorithm); | ||
| 187 | break; | ||
| 188 | } | ||
| 189 | return result; | ||
| 190 | } | ||
| 191 | } | ||