1 |
14 |
jlechner |
/* SecretKeyFactory.java -- Factory for creating secret keys.
|
2 |
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.crypto;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Engine;
|
42 |
|
|
|
43 |
|
|
import java.lang.reflect.InvocationTargetException;
|
44 |
|
|
import java.security.InvalidKeyException;
|
45 |
|
|
import java.security.NoSuchAlgorithmException;
|
46 |
|
|
import java.security.NoSuchProviderException;
|
47 |
|
|
import java.security.Provider;
|
48 |
|
|
import java.security.Security;
|
49 |
|
|
import java.security.spec.InvalidKeySpecException;
|
50 |
|
|
import java.security.spec.KeySpec;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* A secret key factory translates {@link SecretKey} objects to and from
|
54 |
|
|
* {@link java.security.spec.KeySpec} objects, and can translate between
|
55 |
|
|
* different vendors' representations of {@link SecretKey} objects (for
|
56 |
|
|
* security or semantics; whichever applies).
|
57 |
|
|
*
|
58 |
|
|
* @author Casey Marshall (csm@gnu.org)
|
59 |
|
|
* @since 1.4
|
60 |
|
|
* @see SecretKey
|
61 |
|
|
*/
|
62 |
|
|
public class SecretKeyFactory
|
63 |
|
|
{
|
64 |
|
|
|
65 |
|
|
// Constants and fields.
|
66 |
|
|
// ------------------------------------------------------------------------
|
67 |
|
|
|
68 |
|
|
private static final String SERVICE = "SecretKeyFactory";
|
69 |
|
|
|
70 |
|
|
/** The underlying factory implementation. */
|
71 |
|
|
private SecretKeyFactorySpi skfSpi;
|
72 |
|
|
|
73 |
|
|
/** The provider of the implementation. */
|
74 |
|
|
private Provider provider;
|
75 |
|
|
|
76 |
|
|
/** The name of the algorithm. */
|
77 |
|
|
private String algorithm;
|
78 |
|
|
|
79 |
|
|
// Constructor.
|
80 |
|
|
// ------------------------------------------------------------------------
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Create a new secret key factory.
|
84 |
|
|
*
|
85 |
|
|
* @param skfSpi The underlying factory implementation.
|
86 |
|
|
* @param provider The provider.
|
87 |
|
|
* @param algorithm The algorithm name.
|
88 |
|
|
*/
|
89 |
|
|
protected SecretKeyFactory(SecretKeyFactorySpi skfSpi, Provider provider,
|
90 |
|
|
String algorithm)
|
91 |
|
|
{
|
92 |
|
|
this.skfSpi = skfSpi;
|
93 |
|
|
this.provider = provider;
|
94 |
|
|
this.algorithm = algorithm;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
// Class methods.
|
98 |
|
|
// ------------------------------------------------------------------------
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* Create a new secret key factory from the first appropriate
|
102 |
|
|
* instance.
|
103 |
|
|
*
|
104 |
|
|
* @param algorithm The algorithm name.
|
105 |
|
|
* @return The appropriate key factory, if found.
|
106 |
|
|
* @throws java.security.NoSuchAlgorithmException If no provider
|
107 |
|
|
* implements the specified algorithm.
|
108 |
|
|
*/
|
109 |
|
|
public static final SecretKeyFactory getInstance(String algorithm)
|
110 |
|
|
throws NoSuchAlgorithmException
|
111 |
|
|
{
|
112 |
|
|
Provider[] provs = Security.getProviders();
|
113 |
|
|
for (int i = 0; i < provs.length; i++)
|
114 |
|
|
{
|
115 |
|
|
try
|
116 |
|
|
{
|
117 |
|
|
return getInstance(algorithm, provs[i]);
|
118 |
|
|
}
|
119 |
|
|
catch (NoSuchAlgorithmException nsae)
|
120 |
|
|
{
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
/**
|
127 |
|
|
* Create a new secret key factory from the named provider.
|
128 |
|
|
*
|
129 |
|
|
* @param algorithm The algorithm name.
|
130 |
|
|
* @param provider The provider name.
|
131 |
|
|
* @return The appropriate key factory, if found.
|
132 |
|
|
* @throws java.security.NoSuchAlgorithmException If the named
|
133 |
|
|
* provider does not implement the algorithm.
|
134 |
|
|
* @throws java.security.NoSuchProviderException If the named provider
|
135 |
|
|
* does not exist.
|
136 |
|
|
*/
|
137 |
|
|
public static final SecretKeyFactory getInstance(String algorithm,
|
138 |
|
|
String provider)
|
139 |
|
|
throws NoSuchAlgorithmException, NoSuchProviderException
|
140 |
|
|
{
|
141 |
|
|
Provider p = Security.getProvider(provider);
|
142 |
|
|
if (p == null)
|
143 |
|
|
{
|
144 |
|
|
throw new NoSuchProviderException(provider);
|
145 |
|
|
}
|
146 |
|
|
return getInstance(algorithm, p);
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/**
|
150 |
|
|
* Create a new secret key factory from the specified provider.
|
151 |
|
|
*
|
152 |
|
|
* @param algorithm The algorithm name.
|
153 |
|
|
* @param provider The provider.
|
154 |
|
|
* @return The appropriate key factory, if found.
|
155 |
|
|
* @throws java.security.NoSuchAlgorithmException If the provider
|
156 |
|
|
* does not implement the algorithm.
|
157 |
|
|
*/
|
158 |
|
|
public static final SecretKeyFactory getInstance(String algorithm,
|
159 |
|
|
Provider provider)
|
160 |
|
|
throws NoSuchAlgorithmException
|
161 |
|
|
{
|
162 |
|
|
try
|
163 |
|
|
{
|
164 |
|
|
return new SecretKeyFactory((SecretKeyFactorySpi)
|
165 |
|
|
Engine.getInstance(SERVICE, algorithm, provider),
|
166 |
|
|
provider, algorithm);
|
167 |
|
|
}
|
168 |
|
|
catch (InvocationTargetException ite)
|
169 |
|
|
{
|
170 |
|
|
if (ite.getCause() == null)
|
171 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
172 |
|
|
if (ite.getCause() instanceof NoSuchAlgorithmException)
|
173 |
|
|
throw (NoSuchAlgorithmException) ite.getCause();
|
174 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
175 |
|
|
}
|
176 |
|
|
catch (ClassCastException cce)
|
177 |
|
|
{
|
178 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
// Instance methods.
|
183 |
|
|
// ------------------------------------------------------------------------
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* Generate a secret key from a key specification, if possible.
|
187 |
|
|
*
|
188 |
|
|
* @param keySpec The key specification.
|
189 |
|
|
* @return The secret key.
|
190 |
|
|
* @throws java.security.InvalidKeySpecException If the key specification
|
191 |
|
|
* cannot be transformed into a secret key.
|
192 |
|
|
*/
|
193 |
|
|
public final SecretKey generateSecret(KeySpec keySpec)
|
194 |
|
|
throws InvalidKeySpecException
|
195 |
|
|
{
|
196 |
|
|
return skfSpi.engineGenerateSecret(keySpec);
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
/**
|
200 |
|
|
* Get the algorithm name.
|
201 |
|
|
*
|
202 |
|
|
* @return The algorithm name.
|
203 |
|
|
*/
|
204 |
|
|
public final String getAlgorithm()
|
205 |
|
|
{
|
206 |
|
|
return algorithm;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
/**
|
210 |
|
|
* Get the key specification from a secret key.
|
211 |
|
|
*
|
212 |
|
|
* @param key The secret key.
|
213 |
|
|
* @param keySpec The target key specification class.
|
214 |
|
|
* @return The key specification.
|
215 |
|
|
* @throws java.security.spec.InvalidKeySpecException If the secret key cannot
|
216 |
|
|
* be transformed into the specified key specification.
|
217 |
|
|
*/
|
218 |
|
|
public final KeySpec getKeySpec(SecretKey key, Class keySpec)
|
219 |
|
|
throws InvalidKeySpecException
|
220 |
|
|
{
|
221 |
|
|
return skfSpi.engineGetKeySpec(key, keySpec);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
/**
|
225 |
|
|
* Get the provider of this implementation.
|
226 |
|
|
*
|
227 |
|
|
* @return The provider.
|
228 |
|
|
*/
|
229 |
|
|
public final Provider getProvider()
|
230 |
|
|
{
|
231 |
|
|
return provider;
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
/**
|
235 |
|
|
* Translate a secret key into another form.
|
236 |
|
|
*
|
237 |
|
|
* @param key The key to translate.
|
238 |
|
|
* @return The translated key.
|
239 |
|
|
* @throws java.security.InvalidKeyException If the argument cannot be
|
240 |
|
|
* translated.
|
241 |
|
|
*/
|
242 |
|
|
public final SecretKey translateKey(SecretKey key)
|
243 |
|
|
throws InvalidKeyException
|
244 |
|
|
{
|
245 |
|
|
return skfSpi.engineTranslateKey(key);
|
246 |
|
|
}
|
247 |
|
|
}
|