home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 10.7 KB | 308 lines |
- /*
- * @(#)KeyStore.java 1.6 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.security;
-
- import java.io.*;
- import java.security.cert.Certificate;
- import java.security.cert.CertificateException;
- import java.util.*;
-
- /**
- * This abstract class represents an in-memory collection of private keys and
- * associated certificate chains, for use in self-authentication.
- * These keys and certificate chains are used by a given entity when it
- * authenticates itself using public key certificates.
- *
- * <P> Applications for this authentication include software
- * distribution organizations which sign JAR files as part of releasing
- * and/or licensing software.
- *
- * <P> Each private key, and associated certificate chain, is
- * identified by an "alias" string. These strings distinguish among
- * the different ways in which the entity may authenticate itself.
- * For example, the entity may authenticate itself using different
- * certificate authorities, or using different public key algorithms.
- *
- * <P> This abstract class also manages trusted keys, which are used to
- * authenticate other parties.
- *
- * <P> Whether keystores are persistent, and the mechanisms used by the
- * keystore if it is persistent, are not specified here. This allows
- * use of a variety of techniques for protecting private keys. Smart
- * cards or other integrated cryptographic engines (SafeKeyper) are one
- * option, and simpler mechanisms such as files with encrypted private
- * keys may also be used (in a variety of formats).
- *
- * <P> In-memory instances of this class should be protected as strongly
- * as the private keys to which they provide access.
- *
- * @author Jan Luehe
- *
- * @version 1.6, 03/18/98
- *
- * @since JDK1.2
- */
-
- public abstract class KeyStore {
-
- /*
- * Keyword to look up in the security properties file.
- * In the security properties file, the default implementation
- * for KeyStore is given as:
- * <pre>
- * keystore=sun.security.tools.JavaKeyStore
- * </pre>
- */
- private static final String KEYSTORE_PROVIDER = "keystore";
-
- /**
- * Returns a new KeyStore object of the type configured in the
- * security properties file for <code>keystore</code>. If the security
- * properties file does not contain an entry for <code>keystore</code>,
- * an instance of the default KeyStore implementation
- * (sun.security.tools.JavaKeyStore) is returned.
- *
- * @return the new KeyStore object
- *
- * @exception KeyStoreException if the KeyStore object cannot be created
- */
- public static final KeyStore getInstance() throws KeyStoreException {
- String className = null;
-
- try {
- AccessController.beginPrivileged();
- className = Security.getProperty(KEYSTORE_PROVIDER);
- } finally {
- AccessController.endPrivileged();
- }
-
- if (className == null) {
- className = "sun.security.tools.JavaKeyStore";
- }
-
- try {
- Class cl = Class.forName(className);
- return (KeyStore)cl.newInstance();
- } catch (ClassNotFoundException e) {
- throw new KeyStoreException("Could not find class: " + e);
- } catch (IllegalAccessException e) {
- throw new KeyStoreException("Could not access class: " + e);
- } catch (InstantiationException e) {
- throw new KeyStoreException("Problems instantiating: " + e);
- }
- }
-
- /**
- * Returns the private key associated with the given alias. The private key
- * is recovered using the given password.
- *
- * @param alias the alias name
- * @param password the password for recovering the key
- *
- * @return the private key, or null if the given alias does not exist,
- * or the given alias does not have a private key
- *
- * @exception NoSuchAlgorithmException if the algorithm for recovering the
- * private key could not be found
- * @exception UnrecoverableKeyException if the private key could not be
- * recovered
- */
- public abstract PrivateKey getPrivateKey(String alias, String password)
- throws NoSuchAlgorithmException, UnrecoverableKeyException;
-
- /**
- * Returns the certificate chain associated with the given alias.
- *
- * @param alias the alias name
- *
- * @return the certificate chain (ordered with the user's certificate first
- * and the root certificate authority last), or null if the given alias
- * does not exist
- */
- public abstract Certificate[] getCertificateChain(String alias);
-
- /**
- * Returns the certificate associated with the given alias.
- *
- * @param alias the alias name
- *
- * @return the certificate, or null if the given alias does not exist
- */
- public abstract Certificate getCertificate(String alias);
-
- /**
- * Returns the creation date of the entry identified by the given alias.
- *
- * @param alias the alias name
- *
- * @return the creation date of this entry, or null if the given alias does
- * not exist
- */
- public abstract Date getCreationDate(String alias);
-
- /**
- * Assigns a private key and certificate chain to the given alias.
- *
- * The alias may already exist, in which case the private key and
- * certificate chain associated with it are replaced by the private key
- * and certificate chain provided in this call.
- * The private key is protected with the given password.
- *
- * @param alias the alias name
- * @param key the private key to be associated with the alias
- * @param password the password to protect the private key
- * @param chain the certificate chain to be associated with the alias
- *
- * @exception KeyStoreException if the private key cannot be protected, or
- * this operation failed for some other reason
- */
- public abstract void setKeyEntry(String alias, PrivateKey key,
- String password, Certificate chain[])
- throws KeyStoreException;
-
- /**
- * Assigns a protected private key and certificate chain to the given
- * alias.
- *
- * The alias may already exist, in which case the private key and
- * certificate chain associated with it are replaced by the private key
- * and certificate chain provided in this call.
- *
- * @param alias the alias name
- * @param key the protected private key to be associated with the alias
- * @param chain the certificate chain to be associated with the alias
- *
- * @exception KeyStoreException if this operation failed
- */
- public abstract void setKeyEntry(String alias, byte[] key,
- Certificate chain[])
- throws KeyStoreException;
-
- /**
- * Assigns a certificate to the given alias.
- *
- * The alias may already exist, in which case the certificate associated
- * with it is replaced by the certificate provided in this call.
- *
- * @param alias the alias name
- * @param cert the certificate to be added
- *
- * @exception KeyStoreException if the given alias identifies a private key
- * entry, or this operation failed for some other reason
- */
- public abstract void setCertificateEntry(String alias, Certificate cert)
- throws KeyStoreException;
-
- /**
- * Deletes the entry identified by alias.
- *
- * @param alias the alias name
- *
- * @exception KeyStoreException if the entry could not be removed
- */
- public abstract void deleteEntry(String alias)
- throws KeyStoreException;
-
- /**
- * Lists the alias names.
- *
- * @return enumeration of the alias names
- */
- public abstract Enumeration aliases();
-
- /**
- * Checks if alias exists.
- *
- * @param alias the alias name
- *
- * @return true if the alias exists, false otherwise
- */
- public abstract boolean containsAlias(String alias);
-
- /**
- * Retrieves the number of elements in this keystore.
- *
- * @return the number of elements in this keystore
- */
- public abstract int size();
-
- /**
- * Returns true if the entry identified by the given alias is a private
- * key entry, and false otherwise.
- *
- * @return true if the entry identified by the given alias is a private
- * key entry, false otherwise.
- */
- public abstract boolean isKeyEntry(String alias);
-
- /**
- * Returns true if the entry identified by the given alias is a
- * certificate entry, and false otherwise.
- *
- * @return true if the entry identified by the given alias is a
- * certificate entry, false otherwise.
- */
- public abstract boolean isCertificateEntry(String alias);
-
- /**
- * Returns the (alias) name of the first entry whose certificate matches
- * the given certificate.
- *
- * @param cert the certificate to compare against
- *
- * @return the (alias) name of the first entry with matching certificate,
- * or null if there is no such entry
- */
- public abstract String getCertificateAlias(Certificate cert);
-
- /**
- * Stores the keystore data into an output stream.
- *
- * An integrity check is created using the given password, and appended
- * to the stream. Such streams would typically come from a file or a URL.
- *
- * @param stream the stream to which an encrypted keystore will be written.
- * @param password the password to generate the integrity check
- *
- * @exception IOException if there was an I/O problem with data
- * @exception NoSuchAlgorithmException if the appropriate data integrity
- * algorithm could not be found
- * @exception CertificateException if any of the certificates included in
- * the keystore data could not be stored
- */
- public abstract void store(OutputStream stream, String password)
- throws IOException, NoSuchAlgorithmException, CertificateException;
-
- /**
- * Loads the keystore from an input stream.
- *
- * The integrity of the keystore data is checked using the given
- * password.
- * Such streams would typically come from a file or a URL.
- *
- * @param stream the input stream holding an encrypted keystore
- * @param password the password used to check the integrity of the
- * keystore data
- *
- * @exception IOException if there was an I/O or format problem with data
- * @exception NoSuchAlgorithmException if the appropriate data integrity
- * algorithm could not be found
- * @exception CertificateException if any of the certificates included in
- * the keystore data could not be loaded
- */
- public abstract void load(InputStream stream, String password)
- throws IOException, NoSuchAlgorithmException, CertificateException;
- }
-