1 |
/* SecureRandom.java --- Secure Random class implmentation |
/* SecureRandom.java --- Secure Random class implmentation |
2 |
Copyright (C) 1999, 2001 Free Software Foundation, Inc. |
Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
53 |
|
|
54 |
//Serialized Field |
//Serialized Field |
55 |
long counter = 0; //Serialized |
long counter = 0; //Serialized |
|
MessageDigest digest = null; |
|
56 |
Provider provider = null; |
Provider provider = null; |
57 |
byte[] randomBytes = null; //Always null |
byte[] randomBytes = null; //Always null |
58 |
int randomBytesUsed = 0; |
int randomBytesUsed = 0; |
82 |
Enumeration e; |
Enumeration e; |
83 |
for (i = 0; i < p.length; i++) |
for (i = 0; i < p.length; i++) |
84 |
{ |
{ |
85 |
e = p[i].propertyNames(); |
e = p[i].propertyNames(); |
86 |
while (e.hasMoreElements()) |
while (e.hasMoreElements()) |
87 |
{ |
{ |
88 |
key = (String) e.nextElement(); |
key = (String) e.nextElement(); |
89 |
if (key.startsWith("SecureRandom.")) |
if (key.startsWith("SECURERANDOM.")) |
90 |
if ((classname = p[i].getProperty(key)) != null) |
{ |
91 |
break; |
if ((classname = p[i].getProperty(key)) != null) |
92 |
|
{ |
93 |
|
try |
94 |
|
{ |
95 |
|
secureRandomSpi = (SecureRandomSpi) Class. |
96 |
|
forName(classname).newInstance(); |
97 |
|
provider = p[i]; |
98 |
|
return; |
99 |
|
} |
100 |
|
catch (Throwable ignore) { } |
101 |
|
} |
102 |
|
} |
103 |
} |
} |
|
if (classname != null) |
|
|
break; |
|
104 |
} |
} |
105 |
|
|
106 |
//if( classname == null) |
// Nothing found. Fall back to SHA1PRNG |
107 |
// throw new NoSuchAlgorithmException(); |
secureRandomSpi = new gnu.java.security.provider.SHA1PRNG(); |
|
|
|
|
try |
|
|
{ |
|
|
this.secureRandomSpi = |
|
|
(SecureRandomSpi) Class.forName(classname).newInstance(); |
|
|
|
|
|
//s.algorithm = algorithm; |
|
|
this.provider = p[i]; |
|
|
} |
|
|
catch (ClassNotFoundException cnfe) |
|
|
{ |
|
|
//throw new NoSuchAlgorithmException("Class not found"); |
|
|
} |
|
|
catch (InstantiationException ie) |
|
|
{ |
|
|
//throw new NoSuchAlgorithmException("Class instantiation failed"); |
|
|
} |
|
|
catch (IllegalAccessException iae) |
|
|
{ |
|
|
//throw new NoSuchAlgorithmException("Illegal Access"); |
|
|
} |
|
108 |
} |
} |
109 |
|
|
110 |
/** |
/** |
154 |
NoSuchAlgorithmException |
NoSuchAlgorithmException |
155 |
{ |
{ |
156 |
Provider p[] = Security.getProviders(); |
Provider p[] = Security.getProviders(); |
157 |
|
for (int i = 0; i < p.length; i++) |
|
//Format of Key: SecureRandom.algname |
|
|
StringBuffer key = new StringBuffer("SecureRandom."); |
|
|
key.append(algorithm); |
|
|
|
|
|
String classname = null; |
|
|
int i; |
|
|
for (i = 0; i < p.length; i++) |
|
|
{ |
|
|
if ((classname = p[i].getProperty(key.toString())) != null) |
|
|
break; |
|
|
} |
|
|
|
|
|
if (classname == null) |
|
|
throw new NoSuchAlgorithmException(); |
|
|
|
|
|
try |
|
|
{ |
|
|
return new SecureRandom((SecureRandomSpi) Class.forName(classname). |
|
|
newInstance(), p[i]); |
|
|
} |
|
|
catch (ClassNotFoundException cnfe) |
|
|
{ |
|
|
throw new NoSuchAlgorithmException("Class not found"); |
|
|
} |
|
|
catch (InstantiationException ie) |
|
|
{ |
|
|
throw new NoSuchAlgorithmException("Class instantiation failed"); |
|
|
} |
|
|
catch (IllegalAccessException iae) |
|
158 |
{ |
{ |
159 |
throw new NoSuchAlgorithmException("Illegal Access"); |
try |
160 |
|
{ |
161 |
|
return getInstance(algorithm, p[i]); |
162 |
|
} |
163 |
|
catch (NoSuchAlgorithmException ignored) { } |
164 |
} |
} |
165 |
|
|
166 |
|
// None found. |
167 |
|
throw new NoSuchAlgorithmException(algorithm); |
168 |
} |
} |
169 |
|
|
170 |
/** |
/** |
186 |
Provider p = Security.getProvider(provider); |
Provider p = Security.getProvider(provider); |
187 |
if (p == null) |
if (p == null) |
188 |
throw new NoSuchProviderException(); |
throw new NoSuchProviderException(); |
189 |
|
|
190 |
|
return getInstance(algorithm, p); |
191 |
|
} |
192 |
|
|
193 |
//Format of Key: SecureRandom.algName |
/** |
194 |
StringBuffer key = new StringBuffer("SecureRandom."); |
Returns an instance of a SecureRandom. It creates the class for |
195 |
key.append(algorithm); |
the specified algorithm from the given provider. |
|
|
|
|
String classname = p.getProperty(key.toString()); |
|
|
if (classname == null) |
|
|
throw new NoSuchAlgorithmException(); |
|
196 |
|
|
197 |
try |
@param algorithm The SecureRandom algorithm to create. |
198 |
{ |
@param provider The provider to get the instance from. |
199 |
return new SecureRandom((SecureRandomSpi) Class.forName(classname). |
|
200 |
newInstance(), p); |
@throws NoSuchAlgorithmException If the algorithm cannot be found, or |
201 |
} |
if the class cannot be instantiated. |
202 |
catch (ClassNotFoundException cnfe) |
*/ |
203 |
{ |
public static SecureRandom getInstance(String algorithm, |
204 |
throw new NoSuchAlgorithmException("Class not found"); |
Provider provider) throws |
205 |
} |
NoSuchAlgorithmException |
206 |
catch (InstantiationException ie) |
{ |
207 |
{ |
return getInstance(algorithm, provider, true); |
208 |
throw new NoSuchAlgorithmException("Class instantiation failed"); |
} |
209 |
} |
|
210 |
catch (IllegalAccessException iae) |
/** |
211 |
|
Creates the instance of SecureRandom, recursing to resolve aliases. |
212 |
|
|
213 |
|
@param algorithm The SecureRandom algorithm to create. |
214 |
|
@param provider The provider to get the implementation from. |
215 |
|
@param recurse Whether or not to recurse to resolve aliases. |
216 |
|
|
217 |
|
@throws NoSuchAlgorithmException If the algorithm cannot be found, |
218 |
|
if there are too many aliases, or if the class cannot be |
219 |
|
instantiated. |
220 |
|
*/ |
221 |
|
private static SecureRandom getInstance(String algorithm, |
222 |
|
Provider provider, |
223 |
|
boolean recurse) |
224 |
|
throws NoSuchAlgorithmException |
225 |
|
{ |
226 |
|
String msg = algorithm; |
227 |
|
for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); ) |
228 |
{ |
{ |
229 |
throw new NoSuchAlgorithmException("Illegal Access"); |
// We could replace the boolean with an integer, incrementing it |
230 |
|
// every |
231 |
|
String key = (String) e.nextElement(); |
232 |
|
if (key.startsWith("SECURERANDOM.") |
233 |
|
&& key.substring(13).equalsIgnoreCase(algorithm)) |
234 |
|
{ |
235 |
|
try |
236 |
|
{ |
237 |
|
Class c = Class.forName(provider.getProperty(key)); |
238 |
|
return new SecureRandom((SecureRandomSpi) c.newInstance(), |
239 |
|
provider); |
240 |
|
} |
241 |
|
catch (Throwable ignored) { } |
242 |
|
} |
243 |
|
else if (key.startsWith("ALG.ALIAS.SECURERANDOM.") |
244 |
|
&& key.substring(23).equalsIgnoreCase(algorithm) && recurse) |
245 |
|
{ |
246 |
|
try |
247 |
|
{ |
248 |
|
// First see if this alias refers to a class in this |
249 |
|
// provider. |
250 |
|
return getInstance(provider.getProperty(key), provider, false); |
251 |
|
} |
252 |
|
catch (NoSuchAlgorithmException nsae) |
253 |
|
{ |
254 |
|
Provider[] provs = Security.getProviders(); |
255 |
|
for (int i = 0; i < provs.length; i++) |
256 |
|
{ |
257 |
|
if (provs[i] == provider) |
258 |
|
continue; |
259 |
|
// Now try other providers for the implementation |
260 |
|
try |
261 |
|
{ |
262 |
|
return getInstance(provider.getProperty(key), |
263 |
|
provs[i], false); |
264 |
|
} |
265 |
|
catch (NoSuchAlgorithmException nsae2) |
266 |
|
{ |
267 |
|
msg = nsae2.getMessage(); |
268 |
|
} |
269 |
|
} |
270 |
|
} |
271 |
|
} |
272 |
} |
} |
273 |
|
throw new NoSuchAlgorithmException(algorithm); |
274 |
} |
} |
275 |
|
|
276 |
/** |
/** |