FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / security / KeyStore.java
blob1627bc5610a06fc292b8dfad7e166a1693bc81e2
1 /* KeyStore.java --- Key Store Class
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
38 package java.security;
39 import java.io.InputStream;
40 import java.io.IOException;
41 import java.io.OutputStream;
42 import java.security.cert.CertificateException;
43 import java.util.Date;
44 import java.util.Enumeration;
46 /**
47 Keystore represents an in-memory collection of keys and
48 certificates. There are two types of entries:
50 * Key Entry
52 This type of keystore entry store sensitive crytographic key
53 information in a protected format.Typically this is a secret
54 key or a private key with a certificate chain.
57 * Trusted Ceritificate Entry
59 This type of keystore entry contains a single public key
60 certificate belonging to annother entity. It is called trusted
61 because the keystore owner trusts that the certificates
62 belongs to the subject (owner) of the certificate.
64 The keystore contains an "alias" string for each entry.
66 The structure and persistentence of the key store is not
67 specified. Any method could be used to protect sensitive
68 (private or secret) keys. Smart cards or integrated
69 cryptographic engines could be used or the keystore could
70 be simply stored in a file.
72 public class KeyStore
74 private KeyStoreSpi keyStoreSpi;
75 private Provider provider;
76 private String type;
78 /**
79 Creates an instance of KeyStore
81 @param keyStoreSpi A KeyStore engine to use
82 @param provider A provider to use
83 @param type The type of KeyStore
85 protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
87 this.keyStoreSpi = keyStoreSpi;
88 this.provider = provider;
89 this.type = type;
92 /**
93 Gets an instance of the KeyStore class representing
94 the specified keystore. If the type is not
95 found then, it throws KeyStoreException.
97 @param type the type of keystore to choose
99 @return a KeyStore repesenting the desired type
101 @throws KeyStoreException if the type of keystore is not implemented by providers
103 public static KeyStore getInstance(String type) throws KeyStoreException
105 Provider[] p = Security.getProviders();
107 for (int i = 0; i < p.length; i++)
109 String classname = p[i].getProperty("KeyStore." + type);
110 if (classname != null)
111 return getInstance(classname, type, p[i]);
114 throw new KeyStoreException(type);
117 /**
118 Gets an instance of the KeyStore class representing
119 the specified key store from the specified provider.
120 If the type is not found then, it throws KeyStoreException.
121 If the provider is not found, then it throws
122 NoSuchProviderException.
124 @param type the type of keystore to choose
125 @param provider the provider name
127 @return a KeyStore repesenting the desired type
129 @throws KeyStoreException if the type of keystore is not
130 implemented by the given provider
131 @throws NoSuchProviderException if the provider is not found
132 @throws IllegalArgumentException if the provider string is
133 null or empty
135 public static KeyStore getInstance(String type, String provider)
136 throws KeyStoreException, NoSuchProviderException
138 if (provider == null || provider.length() == 0)
139 throw new IllegalArgumentException("Illegal provider");
140 Provider p = Security.getProvider(provider);
141 if (p == null)
142 throw new NoSuchProviderException();
144 return getInstance(p.getProperty("KeyStore." + type), type, p);
147 /**
148 Gets an instance of the KeyStore class representing
149 the specified key store from the specified provider.
150 If the type is not found then, it throws KeyStoreException.
151 If the provider is not found, then it throws
152 NoSuchProviderException.
154 @param type the type of keystore to choose
155 @param provider the keystore provider
157 @return a KeyStore repesenting the desired type
159 @throws KeyStoreException if the type of keystore is not
160 implemented by the given provider
161 @throws IllegalArgumentException if the provider object is null
162 @since 1.4
164 public static KeyStore getInstance(String type, Provider provider)
165 throws KeyStoreException
167 if (provider == null)
168 throw new IllegalArgumentException("Illegal provider");
170 return getInstance(provider.getProperty("KeyStore." + type),
171 type, provider);
174 private static KeyStore getInstance(String classname,
175 String type,
176 Provider provider)
177 throws KeyStoreException
181 return new KeyStore((KeyStoreSpi) Class.forName(classname).
182 newInstance(), provider, type);
184 catch (ClassNotFoundException cnfe)
186 throw new KeyStoreException("Class not found");
188 catch (InstantiationException ie)
190 throw new KeyStoreException("Class instantiation failed");
192 catch (IllegalAccessException iae)
194 throw new KeyStoreException("Illegal Access");
200 Gets the provider that the class is from.
202 @return the provider of this class
204 public final Provider getProvider()
206 return provider;
210 Returns the type of the KeyStore supported
212 @return A string with the type of KeyStore
214 public final String getType()
216 return type;
220 Returns the key associated with given alias using the
221 supplied password.
223 @param alias an alias for the key to get
224 @param password password to access key with
226 @return the requested key, or null otherwise
228 @throws NoSuchAlgorithmException if there is no algorithm
229 for recovering the key
230 @throws UnrecoverableKeyException key cannot be reocovered
231 (wrong password).
233 public final Key getKey(String alias, char[]password)
234 throws KeyStoreException, NoSuchAlgorithmException,
235 UnrecoverableKeyException
237 return keyStoreSpi.engineGetKey(alias, password);
241 Gets a Certificate chain for the specified alias.
243 @param alias the alias name
245 @return a chain of Certificates ( ordered from the user's
246 certificate to the Certificate Authority's ) or
247 null if the alias does not exist or there is no
248 certificate chain for the alias ( the alias refers
249 to a trusted certificate entry or there is no entry).
251 public final java.security.cert.
252 Certificate[] getCertificateChain(String alias) throws KeyStoreException
254 return keyStoreSpi.engineGetCertificateChain(alias);
258 Gets a Certificate for the specified alias.
260 If there is a trusted certificate entry then that is returned.
261 it there is a key entry with a certificate chain then the
262 first certificate is return or else null.
264 @param alias the alias name
266 @return a Certificate or null if the alias does not exist
267 or there is no certificate for the alias
269 public final java.security.cert.Certificate getCertificate(String alias)
270 throws KeyStoreException
272 return keyStoreSpi.engineGetCertificate(alias);
276 Gets entry creation date for the specified alias.
278 @param alias the alias name
280 @returns the entry creation date or null
282 public final Date getCreationDate(String alias) throws KeyStoreException
284 return keyStoreSpi.engineGetCreationDate(alias);
288 Assign the key to the alias in the keystore, protecting it
289 with the given password. It will overwrite an existing
290 entry and if the key is a PrivateKey, also add the
291 certificate chain representing the corresponding public key.
293 @param alias the alias name
294 @param key the key to add
295 @password the password to protect with
296 @param chain the certificate chain for the corresponding
297 public key
299 @throws KeyStoreException if it fails
301 public final void setKeyEntry(String alias, Key key, char[]password,
302 java.security.cert.
303 Certificate[]chain) throws KeyStoreException
305 keyStoreSpi.engineSetKeyEntry(alias, key, password, chain);
309 Assign the key to the alias in the keystore. It will overwrite
310 an existing entry and if the key is a PrivateKey, also
311 add the certificate chain representing the corresponding
312 public key.
314 @param alias the alias name
315 @param key the key to add
316 @param chain the certificate chain for the corresponding
317 public key
319 @throws KeyStoreException if it fails
321 public final void setKeyEntry(String alias, byte[]key,
322 java.security.cert.
323 Certificate[]chain) throws KeyStoreException
325 keyStoreSpi.engineSetKeyEntry(alias, key, chain);
329 Assign the certificate to the alias in the keystore. It
330 will overwrite an existing entry.
332 @param alias the alias name
333 @param cert the certificate to add
335 @throws KeyStoreException if it fails
337 public final void setCertificateEntry(String alias,
338 java.security.cert.
339 Certificate cert) throws
340 KeyStoreException
342 keyStoreSpi.engineSetCertificateEntry(alias, cert);
346 Deletes the entry for the specified entry.
348 @param alias the alias name
350 @throws KeyStoreException if it fails
352 public final void deleteEntry(String alias) throws KeyStoreException
354 keyStoreSpi.engineDeleteEntry(alias);
358 Generates a list of all the aliases in the keystore.
360 @return an Enumeration of the aliases
362 public final Enumeration aliases() throws KeyStoreException
364 return keyStoreSpi.engineAliases();
368 Determines if the keystore contains the specified alias.
370 @param alias the alias name
372 @return true if it contains the alias, false otherwise
374 public final boolean containsAlias(String alias) throws KeyStoreException
376 return keyStoreSpi.engineContainsAlias(alias);
380 Returns the number of entries in the keystore.
382 @returns the number of keystore entries.
384 public final int size() throws KeyStoreException
386 return keyStoreSpi.engineSize();
390 Determines if the keystore contains a key entry for
391 the specified alias.
393 @param alias the alias name
395 @return true if it is a key entry, false otherwise
397 public final boolean isKeyEntry(String alias) throws KeyStoreException
399 return keyStoreSpi.engineIsKeyEntry(alias);
404 Determines if the keystore contains a certificate entry for
405 the specified alias.
407 @param alias the alias name
409 @return true if it is a certificate entry, false otherwise
411 public final boolean isCertificateEntry(String alias)
412 throws KeyStoreException
414 return keyStoreSpi.engineIsCertificateEntry(alias);
418 Determines if the keystore contains the specified certificate
419 entry and returns the alias.
421 It checks every entry and for a key entry checks only the
422 first certificate in the chain.
424 @param cert Certificate to look for
426 @return alias of first matching certificate, null if it
427 does not exist.
429 public final String getCertificateAlias(java.security.cert.Certificate cert)
430 throws KeyStoreException
432 return keyStoreSpi.engineGetCertificateAlias(cert);
436 Stores the keystore in the specified output stream and it
437 uses the specified key it keep it secure.
439 @param stream the output stream to save the keystore to
440 @param password the password to protect the keystore integrity with
442 @throws IOException if an I/O error occurs.
443 @throws NoSuchAlgorithmException the data integrity algorithm
444 used cannot be found.
445 @throws CertificateException if any certificates could not be
446 stored in the output stream.
448 public final void store(OutputStream stream, char[]password)
449 throws KeyStoreException, IOException, NoSuchAlgorithmException,
450 CertificateException
452 keyStoreSpi.engineStore(stream, password);
456 Loads the keystore from the specified input stream and it
457 uses the specified password to check for integrity if supplied.
459 @param stream the input stream to load the keystore from
460 @param password the password to check the keystore integrity with
462 @throws IOException if an I/O error occurs.
463 @throws NoSuchAlgorithmException the data integrity algorithm
464 used cannot be found.
465 @throws CertificateException if any certificates could not be
466 stored in the output stream.
468 public final void load(InputStream stream, char[]password)
469 throws IOException, NoSuchAlgorithmException, CertificateException
471 keyStoreSpi.engineLoad(stream, password);
475 Returns the default KeyStore type. This method looks up the
476 type in <JAVA_HOME>/lib/security/java.security with the
477 property "keystore.type" or if that fails then "jks" .
479 public static final String getDefaultType()
481 String tmp;
482 //Security reads every property in java.security so it
483 //will return this property if it exists.
484 tmp = Security.getProperty("keystore.type");
486 if (tmp == null)
487 tmp = "jks";
489 return tmp;