2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / KeyStore.java
blob5f0c159d7d255eda81f54fb8b513183347b65d44
1 /* KeyStore.java --- Key Store Class
2 Copyright (C) 1999, 2002, 2003 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 import gnu.java.security.Engine;
48 /**
49 * Keystore represents an in-memory collection of keys and
50 * certificates. There are two types of entries:
52 * <dl>
53 * <dt>Key Entry</dt>
55 * <dd><p>This type of keystore entry store sensitive crytographic key
56 * information in a protected format.Typically this is a secret
57 * key or a private key with a certificate chain.</p></dd>
59 * <dt>Trusted Ceritificate Entry</dt>
61 * <dd><p>This type of keystore entry contains a single public key
62 * certificate belonging to annother entity. It is called trusted
63 * because the keystore owner trusts that the certificates
64 * belongs to the subject (owner) of the certificate.</p></dd>
65 * </dl>
67 * <p>Entries in a key store are referred to by their "alias": a simple
68 * unique string.
70 * <p>The structure and persistentence of the key store is not
71 * specified. Any method could be used to protect sensitive
72 * (private or secret) keys. Smart cards or integrated
73 * cryptographic engines could be used or the keystore could
74 * be simply stored in a file.</p>
76 * @see java.security.cert.Certificate
77 * @see Key
79 public class KeyStore
82 // Constants and fields.
83 // ------------------------------------------------------------------------
85 /** Service name for key stores. */
86 private static final String KEY_STORE = "KeyStore";
88 private KeyStoreSpi keyStoreSpi;
89 private Provider provider;
90 private String type;
92 // Constructors.
93 // ------------------------------------------------------------------------
95 /**
96 Creates an instance of KeyStore
98 @param keyStoreSpi A KeyStore engine to use
99 @param provider A provider to use
100 @param type The type of KeyStore
102 protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
104 this.keyStoreSpi = keyStoreSpi;
105 this.provider = provider;
106 this.type = type;
109 // Class methods.
110 // ------------------------------------------------------------------------
112 /**
113 * Gets an instance of the KeyStore class representing
114 * the specified keystore. If the type is not
115 * found then, it throws KeyStoreException.
117 * @param type the type of keystore to choose
118 * @return a KeyStore repesenting the desired type
119 * @throws KeyStoreException if the type of keystore is not implemented
120 * by providers or the implementation cannot be instantiated.
122 public static KeyStore getInstance(String type) throws KeyStoreException
124 Provider[] p = Security.getProviders();
126 for (int i = 0; i < p.length; i++)
130 return getInstance(type, p[i]);
132 catch (KeyStoreException ignore)
137 throw new KeyStoreException(type);
140 /**
141 * Gets an instance of the KeyStore class representing
142 * the specified key store from the specified provider.
143 * If the type is not found then, it throws KeyStoreException.
144 * If the provider is not found, then it throws
145 * NoSuchProviderException.
147 * @param type the type of keystore to choose
148 * @param provider the provider name
149 * @return a KeyStore repesenting the desired type
150 * @throws KeyStoreException if the type of keystore is not
151 * implemented by the given provider
152 * @throws NoSuchProviderException if the provider is not found
153 * @throws IllegalArgumentException if the provider string is
154 * null or empty
156 public static KeyStore getInstance(String type, String provider)
157 throws KeyStoreException, NoSuchProviderException
159 if (provider == null || provider.length() == 0)
160 throw new IllegalArgumentException("Illegal provider");
162 Provider p = Security.getProvider(provider);
163 if (p == null)
164 throw new NoSuchProviderException();
166 return getInstance(type, p);
169 /**
170 * Gets an instance of the KeyStore class representing
171 * the specified key store from the specified provider.
172 * If the type is not found then, it throws KeyStoreException.
173 * If the provider is not found, then it throws
174 * NoSuchProviderException.
176 * @param type the type of keystore to choose
177 * @param provider the keystore provider
178 * @return a KeyStore repesenting the desired type
179 * @throws KeyStoreException if the type of keystore is not
180 * implemented by the given provider
181 * @throws IllegalArgumentException if the provider object is null
182 * @since 1.4
184 public static KeyStore getInstance(String type, Provider provider)
185 throws KeyStoreException
187 if (provider == null)
188 throw new IllegalArgumentException("Illegal provider");
191 return new KeyStore(
192 (KeyStoreSpi) Engine.getInstance(KEY_STORE, type, provider),
193 provider, type);
195 catch (NoSuchAlgorithmException nsae)
197 throw new KeyStoreException(type);
199 catch (java.lang.reflect.InvocationTargetException ite)
201 throw new KeyStoreException(type);
203 catch (ClassCastException cce)
205 throw new KeyStoreException(type);
210 * Returns the default KeyStore type. This method looks up the
211 * type in <JAVA_HOME>/lib/security/java.security with the
212 * property "keystore.type" or if that fails then "jks" .
214 public static final String getDefaultType()
216 // Security reads every property in java.security so it
217 // will return this property if it exists.
218 String tmp = Security.getProperty("keystore.type");
220 if (tmp == null)
221 tmp = "jks";
223 return tmp;
226 // Instance methods.
227 // ------------------------------------------------------------------------
230 Gets the provider that the class is from.
232 @return the provider of this class
234 public final Provider getProvider()
236 return provider;
240 Returns the type of the KeyStore supported
242 @return A string with the type of KeyStore
244 public final String getType()
246 return type;
250 Returns the key associated with given alias using the
251 supplied password.
253 @param alias an alias for the key to get
254 @param password password to access key with
256 @return the requested key, or null otherwise
258 @throws NoSuchAlgorithmException if there is no algorithm
259 for recovering the key
260 @throws UnrecoverableKeyException key cannot be reocovered
261 (wrong password).
263 public final Key getKey(String alias, char[]password)
264 throws KeyStoreException, NoSuchAlgorithmException,
265 UnrecoverableKeyException
267 return keyStoreSpi.engineGetKey(alias, password);
271 Gets a Certificate chain for the specified alias.
273 @param alias the alias name
275 @return a chain of Certificates ( ordered from the user's
276 certificate to the Certificate Authority's ) or
277 null if the alias does not exist or there is no
278 certificate chain for the alias ( the alias refers
279 to a trusted certificate entry or there is no entry).
281 public final java.security.cert.
282 Certificate[] getCertificateChain(String alias) throws KeyStoreException
284 return keyStoreSpi.engineGetCertificateChain(alias);
288 Gets a Certificate for the specified alias.
290 If there is a trusted certificate entry then that is returned.
291 it there is a key entry with a certificate chain then the
292 first certificate is return or else null.
294 @param alias the alias name
296 @return a Certificate or null if the alias does not exist
297 or there is no certificate for the alias
299 public final java.security.cert.Certificate getCertificate(String alias)
300 throws KeyStoreException
302 return keyStoreSpi.engineGetCertificate(alias);
306 Gets entry creation date for the specified alias.
308 @param alias the alias name
310 @returns the entry creation date or null
312 public final Date getCreationDate(String alias) throws KeyStoreException
314 return keyStoreSpi.engineGetCreationDate(alias);
318 Assign the key to the alias in the keystore, protecting it
319 with the given password. It will overwrite an existing
320 entry and if the key is a PrivateKey, also add the
321 certificate chain representing the corresponding public key.
323 @param alias the alias name
324 @param key the key to add
325 @password the password to protect with
326 @param chain the certificate chain for the corresponding
327 public key
329 @throws KeyStoreException if it fails
331 public final void setKeyEntry(String alias, Key key, char[]password,
332 java.security.cert.
333 Certificate[]chain) throws KeyStoreException
335 keyStoreSpi.engineSetKeyEntry(alias, key, password, chain);
339 Assign the key to the alias in the keystore. It will overwrite
340 an existing entry and if the key is a PrivateKey, also
341 add the certificate chain representing the corresponding
342 public key.
344 @param alias the alias name
345 @param key the key to add
346 @param chain the certificate chain for the corresponding
347 public key
349 @throws KeyStoreException if it fails
351 public final void setKeyEntry(String alias, byte[]key,
352 java.security.cert.
353 Certificate[]chain) throws KeyStoreException
355 keyStoreSpi.engineSetKeyEntry(alias, key, chain);
359 Assign the certificate to the alias in the keystore. It
360 will overwrite an existing entry.
362 @param alias the alias name
363 @param cert the certificate to add
365 @throws KeyStoreException if it fails
367 public final void setCertificateEntry(String alias,
368 java.security.cert.
369 Certificate cert) throws
370 KeyStoreException
372 keyStoreSpi.engineSetCertificateEntry(alias, cert);
376 Deletes the entry for the specified entry.
378 @param alias the alias name
380 @throws KeyStoreException if it fails
382 public final void deleteEntry(String alias) throws KeyStoreException
384 keyStoreSpi.engineDeleteEntry(alias);
388 Generates a list of all the aliases in the keystore.
390 @return an Enumeration of the aliases
392 public final Enumeration aliases() throws KeyStoreException
394 return keyStoreSpi.engineAliases();
398 Determines if the keystore contains the specified alias.
400 @param alias the alias name
402 @return true if it contains the alias, false otherwise
404 public final boolean containsAlias(String alias) throws KeyStoreException
406 return keyStoreSpi.engineContainsAlias(alias);
410 Returns the number of entries in the keystore.
412 @returns the number of keystore entries.
414 public final int size() throws KeyStoreException
416 return keyStoreSpi.engineSize();
420 Determines if the keystore contains a key entry for
421 the specified alias.
423 @param alias the alias name
425 @return true if it is a key entry, false otherwise
427 public final boolean isKeyEntry(String alias) throws KeyStoreException
429 return keyStoreSpi.engineIsKeyEntry(alias);
434 Determines if the keystore contains a certificate entry for
435 the specified alias.
437 @param alias the alias name
439 @return true if it is a certificate entry, false otherwise
441 public final boolean isCertificateEntry(String alias)
442 throws KeyStoreException
444 return keyStoreSpi.engineIsCertificateEntry(alias);
448 Determines if the keystore contains the specified certificate
449 entry and returns the alias.
451 It checks every entry and for a key entry checks only the
452 first certificate in the chain.
454 @param cert Certificate to look for
456 @return alias of first matching certificate, null if it
457 does not exist.
459 public final String getCertificateAlias(java.security.cert.Certificate cert)
460 throws KeyStoreException
462 return keyStoreSpi.engineGetCertificateAlias(cert);
466 Stores the keystore in the specified output stream and it
467 uses the specified key it keep it secure.
469 @param stream the output stream to save the keystore to
470 @param password the password to protect the keystore integrity with
472 @throws IOException if an I/O error occurs.
473 @throws NoSuchAlgorithmException the data integrity algorithm
474 used cannot be found.
475 @throws CertificateException if any certificates could not be
476 stored in the output stream.
478 public final void store(OutputStream stream, char[]password)
479 throws KeyStoreException, IOException, NoSuchAlgorithmException,
480 CertificateException
482 keyStoreSpi.engineStore(stream, password);
486 Loads the keystore from the specified input stream and it
487 uses the specified password to check for integrity if supplied.
489 @param stream the input stream to load the keystore from
490 @param password the password to check the keystore integrity with
492 @throws IOException if an I/O error occurs.
493 @throws NoSuchAlgorithmException the data integrity algorithm
494 used cannot be found.
495 @throws CertificateException if any certificates could not be
496 stored in the output stream.
498 public final void load(InputStream stream, char[]password)
499 throws IOException, NoSuchAlgorithmException, CertificateException
501 keyStoreSpi.engineLoad(stream, password);