The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package javax.crypto; |
| 19 | |
| 20 | import java.security.InvalidKeyException; |
| 21 | import java.security.NoSuchAlgorithmException; |
| 22 | import java.security.NoSuchProviderException; |
| 23 | import java.security.Provider; |
| 24 | import java.security.Security; |
| 25 | import java.security.spec.InvalidKeySpecException; |
| 26 | import java.security.spec.KeySpec; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | import org.apache.harmony.security.fortress.Engine; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * The public API for {@code SecretKeyFactory} implementations. |
| 32 | * <p> |
| 33 | * Secret key factories provide the following functionality: |
| 34 | * <ul> |
| 35 | * <li>convert {@link SecretKey} objects to and from {@link KeySpec} objects</li> |
| 36 | * <li>translate {@link SecretKey} objects from one provider implementation to |
| 37 | * another</li> |
| 38 | * </ul> |
| 39 | * Which key specifications are supported by the {@link #generateSecret} and |
| 40 | * {@link #getKeySpec} is provider dependent. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | */ |
| 42 | public class SecretKeyFactory { |
| 43 | |
| 44 | // Used to access common engine functionality |
Brian Carlstrom | 0a48084 | 2010-10-18 23:00:51 -0700 | [diff] [blame] | 45 | private static final Engine ENGINE = new Engine("SecretKeyFactory"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | |
| 47 | // Store used provider |
| 48 | private final Provider provider; |
| 49 | |
| 50 | // Store used spi implementation |
| 51 | private final SecretKeyFactorySpi spiImpl; |
| 52 | |
| 53 | // Store used algorithm name |
| 54 | private final String algorithm; |
| 55 | |
| 56 | /** |
| 57 | * Creates a new {@code SecretKeyFactory} |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 58 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | * @param keyFacSpi |
| 60 | * the SPI delegate. |
| 61 | * @param provider |
| 62 | * the provider providing this key factory. |
| 63 | * @param algorithm |
| 64 | * the algorithm name for the secret key. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 65 | */ |
| 66 | protected SecretKeyFactory(SecretKeyFactorySpi keyFacSpi, |
| 67 | Provider provider, String algorithm) { |
| 68 | this.provider = provider; |
| 69 | this.algorithm = algorithm; |
| 70 | this.spiImpl = keyFacSpi; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the name of the secret key algorithm. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 75 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | * @return the name of the secret key algorithm. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 77 | */ |
| 78 | public final String getAlgorithm() { |
| 79 | return algorithm; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the provider for this {@code SecretKeyFactory} instance. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 84 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | * @return the provider for this {@code SecretKeyFactory} instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 86 | */ |
| 87 | public final Provider getProvider() { |
| 88 | return provider; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates a new {@code SecretKeyFactory} instance for the specified key |
| 93 | * algorithm. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 94 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | * @param algorithm |
| 96 | * the name of the key algorithm. |
| 97 | * @return a secret key factory for the specified key algorithm. |
| 98 | * @throws NoSuchAlgorithmException |
| 99 | * if no installed provider can provide the requested algorithm. |
| 100 | * @throws NullPointerException |
| 101 | * if the specified algorithm is {@code null}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | */ |
| 103 | public static final SecretKeyFactory getInstance(String algorithm) |
| 104 | throws NoSuchAlgorithmException { |
| 105 | if (algorithm == null) { |
Kenny Root | 86acc04 | 2012-09-12 10:32:58 -0700 | [diff] [blame] | 106 | throw new NullPointerException("algorithm == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 107 | } |
Brian Carlstrom | 6cdb6b7 | 2010-10-19 11:27:15 -0700 | [diff] [blame] | 108 | Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null); |
| 109 | return new SecretKeyFactory((SecretKeyFactorySpi) sap.spi, sap.provider, algorithm); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates a new {@code SecretKeyFactory} instance for the specified key |
| 114 | * algorithm from the specified {@code provider}. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 115 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | * @param algorithm |
| 117 | * the name of the key algorithm. |
| 118 | * @param provider |
| 119 | * the name of the provider that provides the requested |
| 120 | * algorithm. |
| 121 | * @return a secret key factory for the specified key algorithm from the |
| 122 | * specified provider. |
| 123 | * @throws NoSuchAlgorithmException |
| 124 | * if the specified provider cannot provide the requested |
| 125 | * algorithm. |
| 126 | * @throws NoSuchProviderException |
| 127 | * if the specified provider does not exist. |
| 128 | * @throws IllegalArgumentException |
| 129 | * if the specified provider name is {@code null} or empty. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 130 | */ |
| 131 | public static final SecretKeyFactory getInstance(String algorithm, |
| 132 | String provider) throws NoSuchAlgorithmException, |
| 133 | NoSuchProviderException { |
Elliott Hughes | 80a7fba | 2010-05-21 16:58:35 -0700 | [diff] [blame] | 134 | if (provider == null || provider.isEmpty()) { |
| 135 | throw new IllegalArgumentException("Provider is null or empty"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 136 | } |
| 137 | Provider impProvider = Security.getProvider(provider); |
| 138 | if (impProvider == null) { |
| 139 | throw new NoSuchProviderException(provider); |
| 140 | } |
| 141 | return getInstance(algorithm, impProvider); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Creates a new {@code SecretKeyFactory} instance for the specified key |
Kenny Root | 0a64887 | 2014-02-05 10:09:13 -0800 | [diff] [blame] | 146 | * algorithm from the specified provider. The {@code provider} supplied |
| 147 | * does not have to be registered. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 148 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 149 | * @param algorithm |
| 150 | * the name of the key algorithm. |
| 151 | * @param provider |
| 152 | * the provider that provides the requested algorithm. |
| 153 | * @return a secret key factory for the specified key algorithm from the |
| 154 | * specified provider. |
| 155 | * @throws NoSuchAlgorithmException |
| 156 | * if the specified provider cannot provider the requested |
| 157 | * algorithm. |
| 158 | * @throws IllegalArgumentException |
| 159 | * if the specified provider is {@code null}. |
| 160 | * @throws NullPointerException |
| 161 | * is the specified algorithm name is {@code null}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 162 | */ |
| 163 | public static final SecretKeyFactory getInstance(String algorithm, |
| 164 | Provider provider) throws NoSuchAlgorithmException { |
| 165 | if (provider == null) { |
Elliott Hughes | 80a7fba | 2010-05-21 16:58:35 -0700 | [diff] [blame] | 166 | throw new IllegalArgumentException("provider == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 167 | } |
| 168 | if (algorithm == null) { |
Kenny Root | 86acc04 | 2012-09-12 10:32:58 -0700 | [diff] [blame] | 169 | throw new NullPointerException("algorithm == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 170 | } |
Brian Carlstrom | 6cdb6b7 | 2010-10-19 11:27:15 -0700 | [diff] [blame] | 171 | Object spi = ENGINE.getInstance(algorithm, provider, null); |
| 172 | return new SecretKeyFactory((SecretKeyFactorySpi) spi, provider, algorithm); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Generate a secret key from the specified key specification. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 177 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 178 | * @param keySpec |
| 179 | * the key specification. |
| 180 | * @return a secret key. |
| 181 | * @throws InvalidKeySpecException |
| 182 | * if the specified key specification cannot be used to generate |
| 183 | * a secret key. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 184 | */ |
| 185 | public final SecretKey generateSecret(KeySpec keySpec) |
| 186 | throws InvalidKeySpecException { |
| 187 | return spiImpl.engineGenerateSecret(keySpec); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns the key specification of the specified secret key. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 192 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 193 | * @param key |
| 194 | * the secret key to get the specification from. |
| 195 | * @param keySpec |
| 196 | * the target key specification class. |
| 197 | * @return an instance of the specified key specification class. |
| 198 | * @throws InvalidKeySpecException |
| 199 | * if the specified secret key cannot be transformed into the |
| 200 | * requested key specification. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 201 | */ |
| 202 | @SuppressWarnings("unchecked") |
| 203 | public final KeySpec getKeySpec(SecretKey key, Class keySpec) |
| 204 | throws InvalidKeySpecException { |
| 205 | return spiImpl.engineGetKeySpec(key, keySpec); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Translates the specified secret key into an instance of the corresponding |
| 210 | * key from the provider of this key factory. |
Jesse Wilson | ce9ec01 | 2009-08-31 15:37:14 -0700 | [diff] [blame] | 211 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 212 | * @param key |
| 213 | * the secret key to translate. |
| 214 | * @return the corresponding translated key. |
| 215 | * @throws InvalidKeyException |
| 216 | * if the specified key cannot be translated using this key |
| 217 | * factory. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 218 | */ |
| 219 | public final SecretKey translateKey(SecretKey key) |
| 220 | throws InvalidKeyException { |
| 221 | return spiImpl.engineTranslateKey(key); |
| 222 | |
| 223 | } |
Elliott Hughes | 80a7fba | 2010-05-21 16:58:35 -0700 | [diff] [blame] | 224 | } |