0N/A/*
2362N/A * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.KeyStore.*;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.security.cert.CertificateException;
0N/A
0N/Aimport javax.crypto.SecretKey;
0N/A
0N/Aimport javax.security.auth.callback.*;
0N/A
0N/A/**
0N/A * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
0N/A * for the <code>KeyStore</code> class.
0N/A * All the abstract methods in this class must be implemented by each
0N/A * cryptographic service provider who wishes to supply the implementation
0N/A * of a keystore for a particular keystore type.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A *
0N/A * @see KeyStore
0N/A *
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic abstract class KeyStoreSpi {
0N/A
0N/A /**
0N/A * Returns the key associated with the given alias, using the given
0N/A * password to recover it. The key must have been associated with
0N/A * the alias by a call to <code>setKeyEntry</code>,
0N/A * or by a call to <code>setEntry</code> with a
0N/A * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>.
0N/A *
0N/A * @param alias the alias name
0N/A * @param password the password for recovering the key
0N/A *
0N/A * @return the requested key, or null if the given alias does not exist
0N/A * or does not identify a key-related entry.
0N/A *
0N/A * @exception NoSuchAlgorithmException if the algorithm for recovering the
0N/A * key cannot be found
0N/A * @exception UnrecoverableKeyException if the key cannot be recovered
0N/A * (e.g., the given password is wrong).
0N/A */
0N/A public abstract Key engineGetKey(String alias, char[] password)
0N/A throws NoSuchAlgorithmException, UnrecoverableKeyException;
0N/A
0N/A /**
0N/A * Returns the certificate chain associated with the given alias.
0N/A * The certificate chain must have been associated with the alias
0N/A * by a call to <code>setKeyEntry</code>,
0N/A * or by a call to <code>setEntry</code> with a
0N/A * <code>PrivateKeyEntry</code>.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return the certificate chain (ordered with the user's certificate first
0N/A * and the root certificate authority last), or null if the given alias
0N/A * does not exist or does not contain a certificate chain
0N/A */
0N/A public abstract Certificate[] engineGetCertificateChain(String alias);
0N/A
0N/A /**
0N/A * Returns the certificate associated with the given alias.
0N/A *
0N/A * <p> If the given alias name identifies an entry
0N/A * created by a call to <code>setCertificateEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>TrustedCertificateEntry</code>,
0N/A * then the trusted certificate contained in that entry is returned.
0N/A *
0N/A * <p> If the given alias name identifies an entry
0N/A * created by a call to <code>setKeyEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>PrivateKeyEntry</code>,
0N/A * then the first element of the certificate chain in that entry
0N/A * (if a chain exists) is returned.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return the certificate, or null if the given alias does not exist or
0N/A * does not contain a certificate.
0N/A */
0N/A public abstract Certificate engineGetCertificate(String alias);
0N/A
0N/A /**
0N/A * Returns the creation date of the entry identified by the given alias.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return the creation date of this entry, or null if the given alias does
0N/A * not exist
0N/A */
0N/A public abstract Date engineGetCreationDate(String alias);
0N/A
0N/A /**
0N/A * Assigns the given key to the given alias, protecting it with the given
0N/A * password.
0N/A *
0N/A * <p>If the given key is of type <code>java.security.PrivateKey</code>,
0N/A * it must be accompanied by a certificate chain certifying the
0N/A * corresponding public key.
0N/A *
0N/A * <p>If the given alias already exists, the keystore information
0N/A * associated with it is overridden by the given key (and possibly
0N/A * certificate chain).
0N/A *
0N/A * @param alias the alias name
0N/A * @param key the key to be associated with the alias
0N/A * @param password the password to protect the key
0N/A * @param chain the certificate chain for the corresponding public
0N/A * key (only required if the given key is of type
0N/A * <code>java.security.PrivateKey</code>).
0N/A *
0N/A * @exception KeyStoreException if the given key cannot be protected, or
0N/A * this operation fails for some other reason
0N/A */
0N/A public abstract void engineSetKeyEntry(String alias, Key key,
0N/A char[] password,
0N/A Certificate[] chain)
0N/A throws KeyStoreException;
0N/A
0N/A /**
0N/A * Assigns the given key (that has already been protected) to the given
0N/A * alias.
0N/A *
0N/A * <p>If the protected key is of type
0N/A * <code>java.security.PrivateKey</code>,
0N/A * it must be accompanied by a certificate chain certifying the
0N/A * corresponding public key.
0N/A *
0N/A * <p>If the given alias already exists, the keystore information
0N/A * associated with it is overridden by the given key (and possibly
0N/A * certificate chain).
0N/A *
0N/A * @param alias the alias name
0N/A * @param key the key (in protected format) to be associated with the alias
0N/A * @param chain the certificate chain for the corresponding public
0N/A * key (only useful if the protected key is of type
0N/A * <code>java.security.PrivateKey</code>).
0N/A *
0N/A * @exception KeyStoreException if this operation fails.
0N/A */
0N/A public abstract void engineSetKeyEntry(String alias, byte[] key,
0N/A Certificate[] chain)
0N/A throws KeyStoreException;
0N/A
0N/A /**
0N/A * Assigns the given certificate to the given alias.
0N/A *
0N/A * <p> If the given alias identifies an existing entry
0N/A * created by a call to <code>setCertificateEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>TrustedCertificateEntry</code>,
0N/A * the trusted certificate in the existing entry
0N/A * is overridden by the given certificate.
0N/A *
0N/A * @param alias the alias name
0N/A * @param cert the certificate
0N/A *
0N/A * @exception KeyStoreException if the given alias already exists and does
0N/A * not identify an entry containing a trusted certificate,
0N/A * or this operation fails for some other reason.
0N/A */
0N/A public abstract void engineSetCertificateEntry(String alias,
0N/A Certificate cert)
0N/A throws KeyStoreException;
0N/A
0N/A /**
0N/A * Deletes the entry identified by the given alias from this keystore.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @exception KeyStoreException if the entry cannot be removed.
0N/A */
0N/A public abstract void engineDeleteEntry(String alias)
0N/A throws KeyStoreException;
0N/A
0N/A /**
0N/A * Lists all the alias names of this keystore.
0N/A *
0N/A * @return enumeration of the alias names
0N/A */
0N/A public abstract Enumeration<String> engineAliases();
0N/A
0N/A /**
0N/A * Checks if the given alias exists in this keystore.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return true if the alias exists, false otherwise
0N/A */
0N/A public abstract boolean engineContainsAlias(String alias);
0N/A
0N/A /**
0N/A * Retrieves the number of entries in this keystore.
0N/A *
0N/A * @return the number of entries in this keystore
0N/A */
0N/A public abstract int engineSize();
0N/A
0N/A /**
0N/A * Returns true if the entry identified by the given alias
0N/A * was created by a call to <code>setKeyEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>PrivateKeyEntry</code> or a <code>SecretKeyEntry</code>.
0N/A *
0N/A * @param alias the alias for the keystore entry to be checked
0N/A *
0N/A * @return true if the entry identified by the given alias is a
0N/A * key-related, false otherwise.
0N/A */
0N/A public abstract boolean engineIsKeyEntry(String alias);
0N/A
0N/A /**
0N/A * Returns true if the entry identified by the given alias
0N/A * was created by a call to <code>setCertificateEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>TrustedCertificateEntry</code>.
0N/A *
0N/A * @param alias the alias for the keystore entry to be checked
0N/A *
0N/A * @return true if the entry identified by the given alias contains a
0N/A * trusted certificate, false otherwise.
0N/A */
0N/A public abstract boolean engineIsCertificateEntry(String alias);
0N/A
0N/A /**
0N/A * Returns the (alias) name of the first keystore entry whose certificate
0N/A * matches the given certificate.
0N/A *
0N/A * <p>This method attempts to match the given certificate with each
0N/A * keystore entry. If the entry being considered was
0N/A * created by a call to <code>setCertificateEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>TrustedCertificateEntry</code>,
0N/A * then the given certificate is compared to that entry's certificate.
0N/A *
0N/A * <p> If the entry being considered was
0N/A * created by a call to <code>setKeyEntry</code>,
0N/A * or created by a call to <code>setEntry</code> with a
0N/A * <code>PrivateKeyEntry</code>,
0N/A * then the given certificate is compared to the first
0N/A * element of that entry's certificate chain.
0N/A *
0N/A * @param cert the certificate to match with.
0N/A *
0N/A * @return the alias name of the first entry with matching certificate,
0N/A * or null if no such entry exists in this keystore.
0N/A */
0N/A public abstract String engineGetCertificateAlias(Certificate cert);
0N/A
0N/A /**
0N/A * Stores this keystore to the given output stream, and protects its
0N/A * integrity with the given password.
0N/A *
0N/A * @param stream the output stream to which this keystore is written.
0N/A * @param password the password to generate the keystore integrity check
0N/A *
0N/A * @exception IOException if there was an I/O problem with data
0N/A * @exception NoSuchAlgorithmException if the appropriate data integrity
0N/A * algorithm could not be found
0N/A * @exception CertificateException if any of the certificates included in
0N/A * the keystore data could not be stored
0N/A */
0N/A public abstract void engineStore(OutputStream stream, char[] password)
0N/A throws IOException, NoSuchAlgorithmException, CertificateException;
0N/A
0N/A /**
0N/A * Stores this keystore using the given
0N/A * <code>KeyStore.LoadStoreParmeter</code>.
0N/A *
0N/A * @param param the <code>KeyStore.LoadStoreParmeter</code>
0N/A * that specifies how to store the keystore,
0N/A * which may be <code>null</code>
0N/A *
0N/A * @exception IllegalArgumentException if the given
0N/A * <code>KeyStore.LoadStoreParmeter</code>
0N/A * input is not recognized
0N/A * @exception IOException if there was an I/O problem with data
0N/A * @exception NoSuchAlgorithmException if the appropriate data integrity
0N/A * algorithm could not be found
0N/A * @exception CertificateException if any of the certificates included in
0N/A * the keystore data could not be stored
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public void engineStore(KeyStore.LoadStoreParameter param)
0N/A throws IOException, NoSuchAlgorithmException,
0N/A CertificateException {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * Loads the keystore from the given input stream.
0N/A *
0N/A * <p>A password may be given to unlock the keystore
0N/A * (e.g. the keystore resides on a hardware token device),
0N/A * or to check the integrity of the keystore data.
0N/A * If a password is not given for integrity checking,
0N/A * then integrity checking is not performed.
0N/A *
0N/A * @param stream the input stream from which the keystore is loaded,
0N/A * or <code>null</code>
0N/A * @param password the password used to check the integrity of
0N/A * the keystore, the password used to unlock the keystore,
0N/A * or <code>null</code>
0N/A *
0N/A * @exception IOException if there is an I/O or format problem with the
0N/A * keystore data, if a password is required but not given,
0N/A * or if the given password was incorrect. If the error is due to a
0N/A * wrong password, the {@link Throwable#getCause cause} of the
0N/A * <code>IOException</code> should be an
0N/A * <code>UnrecoverableKeyException</code>
0N/A * @exception NoSuchAlgorithmException if the algorithm used to check
0N/A * the integrity of the keystore cannot be found
0N/A * @exception CertificateException if any of the certificates in the
0N/A * keystore could not be loaded
0N/A */
0N/A public abstract void engineLoad(InputStream stream, char[] password)
0N/A throws IOException, NoSuchAlgorithmException, CertificateException;
0N/A
0N/A /**
0N/A * Loads the keystore using the given
0N/A * <code>KeyStore.LoadStoreParameter</code>.
0N/A *
0N/A * <p> Note that if this KeyStore has already been loaded, it is
0N/A * reinitialized and loaded again from the given parameter.
0N/A *
0N/A * @param param the <code>KeyStore.LoadStoreParameter</code>
0N/A * that specifies how to load the keystore,
0N/A * which may be <code>null</code>
0N/A *
0N/A * @exception IllegalArgumentException if the given
0N/A * <code>KeyStore.LoadStoreParameter</code>
0N/A * input is not recognized
0N/A * @exception IOException if there is an I/O or format problem with the
0N/A * keystore data. If the error is due to an incorrect
0N/A * <code>ProtectionParameter</code> (e.g. wrong password)
0N/A * the {@link Throwable#getCause cause} of the
0N/A * <code>IOException</code> should be an
0N/A * <code>UnrecoverableKeyException</code>
0N/A * @exception NoSuchAlgorithmException if the algorithm used to check
0N/A * the integrity of the keystore cannot be found
0N/A * @exception CertificateException if any of the certificates in the
0N/A * keystore could not be loaded
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public void engineLoad(KeyStore.LoadStoreParameter param)
0N/A throws IOException, NoSuchAlgorithmException,
0N/A CertificateException {
0N/A
0N/A if (param == null) {
0N/A engineLoad((InputStream)null, (char[])null);
0N/A return;
0N/A }
0N/A
0N/A if (param instanceof KeyStore.SimpleLoadStoreParameter) {
0N/A ProtectionParameter protection = param.getProtectionParameter();
0N/A char[] password;
0N/A if (protection instanceof PasswordProtection) {
0N/A password = ((PasswordProtection)protection).getPassword();
0N/A } else if (protection instanceof CallbackHandlerProtection) {
0N/A CallbackHandler handler =
0N/A ((CallbackHandlerProtection)protection).getCallbackHandler();
0N/A PasswordCallback callback =
0N/A new PasswordCallback("Password: ", false);
0N/A try {
0N/A handler.handle(new Callback[] {callback});
0N/A } catch (UnsupportedCallbackException e) {
0N/A throw new NoSuchAlgorithmException
0N/A ("Could not obtain password", e);
0N/A }
0N/A password = callback.getPassword();
0N/A callback.clearPassword();
0N/A if (password == null) {
0N/A throw new NoSuchAlgorithmException
0N/A ("No password provided");
0N/A }
0N/A } else {
0N/A throw new NoSuchAlgorithmException("ProtectionParameter must"
0N/A + " be PasswordProtection or CallbackHandlerProtection");
0N/A }
0N/A engineLoad(null, password);
0N/A return;
0N/A }
0N/A
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * Gets a <code>KeyStore.Entry</code> for the specified alias
0N/A * with the specified protection parameter.
0N/A *
0N/A * @param alias get the <code>KeyStore.Entry</code> for this alias
0N/A * @param protParam the <code>ProtectionParameter</code>
0N/A * used to protect the <code>Entry</code>,
0N/A * which may be <code>null</code>
0N/A *
0N/A * @return the <code>KeyStore.Entry</code> for the specified alias,
0N/A * or <code>null</code> if there is no such entry
0N/A *
0N/A * @exception KeyStoreException if the operation failed
0N/A * @exception NoSuchAlgorithmException if the algorithm for recovering the
0N/A * entry cannot be found
0N/A * @exception UnrecoverableEntryException if the specified
0N/A * <code>protParam</code> were insufficient or invalid
0N/A * @exception UnrecoverableKeyException if the entry is a
0N/A * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>
0N/A * and the specified <code>protParam</code> does not contain
0N/A * the information needed to recover the key (e.g. wrong password)
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public KeyStore.Entry engineGetEntry(String alias,
0N/A KeyStore.ProtectionParameter protParam)
0N/A throws KeyStoreException, NoSuchAlgorithmException,
0N/A UnrecoverableEntryException {
0N/A
0N/A if (!engineContainsAlias(alias)) {
0N/A return null;
0N/A }
0N/A
0N/A if (protParam == null) {
0N/A if (engineIsCertificateEntry(alias)) {
0N/A return new KeyStore.TrustedCertificateEntry
0N/A (engineGetCertificate(alias));
0N/A } else {
0N/A throw new UnrecoverableKeyException
0N/A ("requested entry requires a password");
0N/A }
0N/A }
0N/A
0N/A if (protParam instanceof KeyStore.PasswordProtection) {
0N/A if (engineIsCertificateEntry(alias)) {
0N/A throw new UnsupportedOperationException
0N/A ("trusted certificate entries are not password-protected");
0N/A } else if (engineIsKeyEntry(alias)) {
0N/A KeyStore.PasswordProtection pp =
0N/A (KeyStore.PasswordProtection)protParam;
0N/A char[] password = pp.getPassword();
0N/A
0N/A Key key = engineGetKey(alias, password);
0N/A if (key instanceof PrivateKey) {
0N/A Certificate[] chain = engineGetCertificateChain(alias);
0N/A return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain);
0N/A } else if (key instanceof SecretKey) {
0N/A return new KeyStore.SecretKeyEntry((SecretKey)key);
0N/A }
0N/A }
0N/A }
0N/A
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * Saves a <code>KeyStore.Entry</code> under the specified alias.
0N/A * The specified protection parameter is used to protect the
0N/A * <code>Entry</code>.
0N/A *
0N/A * <p> If an entry already exists for the specified alias,
0N/A * it is overridden.
0N/A *
0N/A * @param alias save the <code>KeyStore.Entry</code> under this alias
0N/A * @param entry the <code>Entry</code> to save
0N/A * @param protParam the <code>ProtectionParameter</code>
0N/A * used to protect the <code>Entry</code>,
0N/A * which may be <code>null</code>
0N/A *
0N/A * @exception KeyStoreException if this operation fails
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public void engineSetEntry(String alias, KeyStore.Entry entry,
0N/A KeyStore.ProtectionParameter protParam)
0N/A throws KeyStoreException {
0N/A
0N/A // get password
0N/A if (protParam != null &&
0N/A !(protParam instanceof KeyStore.PasswordProtection)) {
0N/A throw new KeyStoreException("unsupported protection parameter");
0N/A }
0N/A KeyStore.PasswordProtection pProtect = null;
0N/A if (protParam != null) {
0N/A pProtect = (KeyStore.PasswordProtection)protParam;
0N/A }
0N/A
0N/A // set entry
0N/A if (entry instanceof KeyStore.TrustedCertificateEntry) {
0N/A if (protParam != null && pProtect.getPassword() != null) {
0N/A // pre-1.5 style setCertificateEntry did not allow password
0N/A throw new KeyStoreException
0N/A ("trusted certificate entries are not password-protected");
0N/A } else {
0N/A KeyStore.TrustedCertificateEntry tce =
0N/A (KeyStore.TrustedCertificateEntry)entry;
0N/A engineSetCertificateEntry(alias, tce.getTrustedCertificate());
0N/A return;
0N/A }
0N/A } else if (entry instanceof KeyStore.PrivateKeyEntry) {
0N/A if (pProtect == null || pProtect.getPassword() == null) {
0N/A // pre-1.5 style setKeyEntry required password
0N/A throw new KeyStoreException
0N/A ("non-null password required to create PrivateKeyEntry");
0N/A } else {
0N/A engineSetKeyEntry
0N/A (alias,
0N/A ((KeyStore.PrivateKeyEntry)entry).getPrivateKey(),
0N/A pProtect.getPassword(),
0N/A ((KeyStore.PrivateKeyEntry)entry).getCertificateChain());
0N/A return;
0N/A }
0N/A } else if (entry instanceof KeyStore.SecretKeyEntry) {
0N/A if (pProtect == null || pProtect.getPassword() == null) {
0N/A // pre-1.5 style setKeyEntry required password
0N/A throw new KeyStoreException
0N/A ("non-null password required to create SecretKeyEntry");
0N/A } else {
0N/A engineSetKeyEntry
0N/A (alias,
0N/A ((KeyStore.SecretKeyEntry)entry).getSecretKey(),
0N/A pProtect.getPassword(),
0N/A (Certificate[])null);
0N/A return;
0N/A }
0N/A }
0N/A
0N/A throw new KeyStoreException
0N/A ("unsupported entry type: " + entry.getClass().getName());
0N/A }
0N/A
0N/A /**
0N/A * Determines if the keystore <code>Entry</code> for the specified
0N/A * <code>alias</code> is an instance or subclass of the specified
0N/A * <code>entryClass</code>.
0N/A *
0N/A * @param alias the alias name
0N/A * @param entryClass the entry class
0N/A *
0N/A * @return true if the keystore <code>Entry</code> for the specified
0N/A * <code>alias</code> is an instance or subclass of the
0N/A * specified <code>entryClass</code>, false otherwise
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public boolean
0N/A engineEntryInstanceOf(String alias,
0N/A Class<? extends KeyStore.Entry> entryClass)
0N/A {
0N/A if (entryClass == KeyStore.TrustedCertificateEntry.class) {
0N/A return engineIsCertificateEntry(alias);
0N/A }
0N/A if (entryClass == KeyStore.PrivateKeyEntry.class) {
0N/A return engineIsKeyEntry(alias) &&
0N/A engineGetCertificate(alias) != null;
0N/A }
0N/A if (entryClass == KeyStore.SecretKeyEntry.class) {
0N/A return engineIsKeyEntry(alias) &&
0N/A engineGetCertificate(alias) == null;
0N/A }
0N/A return false;
0N/A }
0N/A}