1 /* KeyStore.java --- Key Store Class
2 Copyright (C) 1999, 2002, 2003, 2004 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
39 package java
.security
;
41 import gnu
.java
.security
.Engine
;
43 import java
.io
.IOException
;
44 import java
.io
.InputStream
;
45 import java
.io
.OutputStream
;
46 import java
.lang
.reflect
.InvocationTargetException
;
47 import java
.security
.cert
.CertificateException
;
48 import java
.util
.Date
;
49 import java
.util
.Enumeration
;
52 * Keystore represents an in-memory collection of keys and
53 * certificates. There are two types of entries:
58 * <dd><p>This type of keystore entry store sensitive crytographic key
59 * information in a protected format.Typically this is a secret
60 * key or a private key with a certificate chain.</p></dd>
62 * <dt>Trusted Ceritificate Entry</dt>
64 * <dd><p>This type of keystore entry contains a single public key
65 * certificate belonging to annother entity. It is called trusted
66 * because the keystore owner trusts that the certificates
67 * belongs to the subject (owner) of the certificate.</p></dd>
70 * <p>Entries in a key store are referred to by their "alias": a simple
73 * <p>The structure and persistentence of the key store is not
74 * specified. Any method could be used to protect sensitive
75 * (private or secret) keys. Smart cards or integrated
76 * cryptographic engines could be used or the keystore could
77 * be simply stored in a file.</p>
79 * @see java.security.cert.Certificate
85 // Constants and fields.
86 // ------------------------------------------------------------------------
88 /** Service name for key stores. */
89 private static final String KEY_STORE
= "KeyStore";
91 private KeyStoreSpi keyStoreSpi
;
92 private Provider provider
;
96 // ------------------------------------------------------------------------
99 Creates an instance of KeyStore
101 @param keyStoreSpi A KeyStore engine to use
102 @param provider A provider to use
103 @param type The type of KeyStore
105 protected KeyStore(KeyStoreSpi keyStoreSpi
, Provider provider
, String type
)
107 this.keyStoreSpi
= keyStoreSpi
;
108 this.provider
= provider
;
113 * Returns an instance of a <code>KeyStore</code> representing the specified
114 * type, from the first provider that implements it.
116 * @param type the type of keystore to create.
117 * @return a <code>KeyStore</code> repesenting the desired type.
118 * @throws KeyStoreException if the designated type of is not implemented by
119 * any provider, or the implementation could not be instantiated.
120 * @throws IllegalArgumentException if <code>type</code> is
121 * <code>null</code> or is an empty string.
123 public static KeyStore
getInstance(String type
) throws KeyStoreException
125 Provider
[] p
= Security
.getProviders();
126 KeyStoreException lastException
= null;
127 for (int i
= 0; i
< p
.length
; i
++)
130 return getInstance(type
, p
[i
]);
132 catch (KeyStoreException x
)
136 if (lastException
!= null)
138 throw new KeyStoreException(type
);
142 * Returns an instance of a <code>KeyStore</code> representing the specified
143 * type, from the named provider.
145 * @param type the type of keystore to create.
146 * @param provider the name of the provider to use.
147 * @return a <code>KeyStore</code> repesenting the desired type.
148 * @throws KeyStoreException if the designated type is not implemented by the
150 * @throws NoSuchProviderException if the provider is not found.
151 * @throws IllegalArgumentException if either <code>type</code> or
152 * <code>provider</code> is <code>null</code> or empty.
154 public static KeyStore
getInstance(String type
, String provider
)
155 throws KeyStoreException
, NoSuchProviderException
157 if (provider
== null)
158 throw new IllegalArgumentException("provider MUST NOT be null");
159 provider
= provider
.trim();
160 if (provider
.length() == 0)
161 throw new IllegalArgumentException("provider MUST NOT be empty");
162 Provider p
= Security
.getProvider(provider
);
164 throw new NoSuchProviderException(provider
);
165 return getInstance(type
, p
);
169 * Returns an instance of a <code>KeyStore</code> representing the specified
170 * type, from the specified provider.
172 * @param type the type of keystore to create.
173 * @param provider the provider to use.
174 * @return a <code>KeyStore</code> repesenting the desired type.
175 * @throws KeyStoreException if the designated type is not implemented by the
177 * @throws IllegalArgumentException if either <code>type</code> or
178 * <code>provider</code> is <code>null</code>, or if
179 * <code>type</code> is an empty string.
182 public static KeyStore
getInstance(String type
, Provider provider
)
183 throws KeyStoreException
188 Object spi
= Engine
.getInstance(KEY_STORE
, type
, provider
);
189 return new KeyStore((KeyStoreSpi
) spi
, provider
, type
);
191 catch (NoSuchAlgorithmException x
)
195 catch (InvocationTargetException x
)
197 cause
= x
.getCause() != null ? x
.getCause() : x
;
199 catch (ClassCastException x
)
203 KeyStoreException x
= new KeyStoreException(type
);
209 * Returns the default KeyStore type. This method looks up the
210 * type in <JAVA_HOME>/lib/security/java.security with the
211 * property "keystore.type" or if that fails then "gkr" .
213 public static final String
getDefaultType()
215 // Security reads every property in java.security so it
216 // will return this property if it exists.
217 String tmp
= AccessController
.doPrivileged(new PrivilegedAction
<String
> () {
220 return Security
.getProperty("keystore.type");
231 // ------------------------------------------------------------------------
234 Gets the provider that the class is from.
236 @return the provider of this class
238 public final Provider
getProvider()
244 Returns the type of the KeyStore supported
246 @return A string with the type of KeyStore
248 public final String
getType()
254 Returns the key associated with given alias using the
257 @param alias an alias for the key to get
258 @param password password to access key with
260 @return the requested key, or null otherwise
262 @throws NoSuchAlgorithmException if there is no algorithm
263 for recovering the key
264 @throws UnrecoverableKeyException key cannot be reocovered
267 public final Key
getKey(String alias
, char[]password
)
268 throws KeyStoreException
, NoSuchAlgorithmException
,
269 UnrecoverableKeyException
271 return keyStoreSpi
.engineGetKey(alias
, password
);
275 Gets a Certificate chain for the specified alias.
277 @param alias the alias name
279 @return a chain of Certificates ( ordered from the user's
280 certificate to the Certificate Authority's ) or
281 null if the alias does not exist or there is no
282 certificate chain for the alias ( the alias refers
283 to a trusted certificate entry or there is no entry).
285 public final java
.security
.cert
.
286 Certificate
[] getCertificateChain(String alias
) throws KeyStoreException
288 return keyStoreSpi
.engineGetCertificateChain(alias
);
292 Gets a Certificate for the specified alias.
294 If there is a trusted certificate entry then that is returned.
295 it there is a key entry with a certificate chain then the
296 first certificate is return or else null.
298 @param alias the alias name
300 @return a Certificate or null if the alias does not exist
301 or there is no certificate for the alias
303 public final java
.security
.cert
.Certificate
getCertificate(String alias
)
304 throws KeyStoreException
306 return keyStoreSpi
.engineGetCertificate(alias
);
310 Gets entry creation date for the specified alias.
312 @param alias the alias name
314 @returns the entry creation date or null
316 public final Date
getCreationDate(String alias
) throws KeyStoreException
318 return keyStoreSpi
.engineGetCreationDate(alias
);
322 Assign the key to the alias in the keystore, protecting it
323 with the given password. It will overwrite an existing
324 entry and if the key is a PrivateKey, also add the
325 certificate chain representing the corresponding public key.
327 @param alias the alias name
328 @param key the key to add
329 @password the password to protect with
330 @param chain the certificate chain for the corresponding
333 @throws KeyStoreException if it fails
335 public final void setKeyEntry(String alias
, Key key
, char[]password
,
337 Certificate
[]chain
) throws KeyStoreException
339 keyStoreSpi
.engineSetKeyEntry(alias
, key
, password
, chain
);
343 Assign the key to the alias in the keystore. It will overwrite
344 an existing entry and if the key is a PrivateKey, also
345 add the certificate chain representing the corresponding
348 @param alias the alias name
349 @param key the key to add
350 @param chain the certificate chain for the corresponding
353 @throws KeyStoreException if it fails
355 public final void setKeyEntry(String alias
, byte[]key
,
357 Certificate
[]chain
) throws KeyStoreException
359 keyStoreSpi
.engineSetKeyEntry(alias
, key
, chain
);
363 Assign the certificate to the alias in the keystore. It
364 will overwrite an existing entry.
366 @param alias the alias name
367 @param cert the certificate to add
369 @throws KeyStoreException if it fails
371 public final void setCertificateEntry(String alias
,
373 Certificate cert
) throws
376 keyStoreSpi
.engineSetCertificateEntry(alias
, cert
);
380 Deletes the entry for the specified entry.
382 @param alias the alias name
384 @throws KeyStoreException if it fails
386 public final void deleteEntry(String alias
) throws KeyStoreException
388 keyStoreSpi
.engineDeleteEntry(alias
);
392 Generates a list of all the aliases in the keystore.
394 @return an Enumeration of the aliases
396 public final Enumeration
<String
> aliases() throws KeyStoreException
398 return keyStoreSpi
.engineAliases();
402 Determines if the keystore contains the specified alias.
404 @param alias the alias name
406 @return true if it contains the alias, false otherwise
408 public final boolean containsAlias(String alias
) throws KeyStoreException
410 return keyStoreSpi
.engineContainsAlias(alias
);
414 Returns the number of entries in the keystore.
416 @returns the number of keystore entries.
418 public final int size() throws KeyStoreException
420 return keyStoreSpi
.engineSize();
424 Determines if the keystore contains a key entry for
427 @param alias the alias name
429 @return true if it is a key entry, false otherwise
431 public final boolean isKeyEntry(String alias
) throws KeyStoreException
433 return keyStoreSpi
.engineIsKeyEntry(alias
);
438 Determines if the keystore contains a certificate entry for
441 @param alias the alias name
443 @return true if it is a certificate entry, false otherwise
445 public final boolean isCertificateEntry(String alias
)
446 throws KeyStoreException
448 return keyStoreSpi
.engineIsCertificateEntry(alias
);
452 Determines if the keystore contains the specified certificate
453 entry and returns the alias.
455 It checks every entry and for a key entry checks only the
456 first certificate in the chain.
458 @param cert Certificate to look for
460 @return alias of first matching certificate, null if it
463 public final String
getCertificateAlias(java
.security
.cert
.Certificate cert
)
464 throws KeyStoreException
466 return keyStoreSpi
.engineGetCertificateAlias(cert
);
470 Stores the keystore in the specified output stream and it
471 uses the specified key it keep it secure.
473 @param stream the output stream to save the keystore to
474 @param password the password to protect the keystore integrity with
476 @throws IOException if an I/O error occurs.
477 @throws NoSuchAlgorithmException the data integrity algorithm
478 used cannot be found.
479 @throws CertificateException if any certificates could not be
480 stored in the output stream.
482 public final void store(OutputStream stream
, char[]password
)
483 throws KeyStoreException
, IOException
, NoSuchAlgorithmException
,
486 keyStoreSpi
.engineStore(stream
, password
);
490 Loads the keystore from the specified input stream and it
491 uses the specified password to check for integrity if supplied.
493 @param stream the input stream to load the keystore from
494 @param password the password to check the keystore integrity with
496 @throws IOException if an I/O error occurs.
497 @throws NoSuchAlgorithmException the data integrity algorithm
498 used cannot be found.
499 @throws CertificateException if any certificates could not be
500 stored in the output stream.
502 public final void load(InputStream stream
, char[]password
)
503 throws IOException
, NoSuchAlgorithmException
, CertificateException
505 keyStoreSpi
.engineLoad(stream
, password
);