📄 keystore.java
字号:
/* * @(#)KeyStore.java 1.37 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.security;import java.io.*;import java.security.cert.Certificate;import java.security.cert.CertificateException;import java.util.*;/** * This class represents an in-memory collection of keys and certificates. * It manages two types of entries: * * <ul> * <li><b>Key Entry</b> * <p>This type of keystore entry holds very sensitive cryptographic key * information, which is stored in a protected format to prevent unauthorized * access. * * <p>Typically, a key stored in this type of entry is a secret key, or a * private key accompanied by the certificate chain for the corresponding * public key. * * <p>Private keys and certificate chains are used by a given entity for * self-authentication. Applications for this authentication include software * distribution organizations which sign JAR files as part of releasing * and/or licensing software.<p> * * <li><b>Trusted Certificate Entry</b> * <p>This type of entry contains a single public key certificate belonging to * another party. It is called a <i>trusted certificate</i> because the * keystore owner trusts that the public key in the certificate indeed belongs * to the identity identified by the <i>subject</i> (owner) of the * certificate. * * <p>This type of entry can be used to authenticate other parties. * </ul> * * <p>Each entry in a keystore is identified by an "alias" string. In the * case of private keys and their associated certificate chains, 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>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 sensitive (e.g., private or * secret) keys. Smart cards or other integrated cryptographic engines * (SafeKeyper) are one option, and simpler mechanisms such as files may also * be used (in a variety of formats). * * <p>There are two ways to request a KeyStore object: by * specifying either just a keystore type, or both a keystore type * and a package provider. * * <ul> * <li>If just a keystore type is specified: * <pre> * KeyStore ks = KeyStore.getInstance("JKS"); * </pre> * the system will determine if there is an implementation of the keystore type * requested available in the environment, and if there is more than one, if * there is a preferred one.<p> * * <li>If both a keystore type and a package provider are specified: * <pre> * KeyStore ks = KeyStore.getInstance("JKS", "SUN"); * </pre> * the system will determine if there is an implementation of the * keystore type in the package requested, and throw an exception if there * is not. * * </ul> * * <p>Before a keystore can be accessed, it must be * {@link #load(java.io.InputStream, char[]) loaded}. In order to create * an empty keystore, you pass <code>null</code> * as the <code>InputStream</code> argument to the <code>load</code> method. * * @author Jan Luehe * * @version 1.29, 02/02/00 * * @see java.security.PrivateKey * @see java.security.cert.Certificate * * @since 1.2 */public class KeyStore { /* * Constant to lookup in the Security properties file to determine * the default keystore type. * In the Security properties file, the default keystore type is given as: * <pre> * keystore.type=jks * </pre> */ private static final String KEYSTORE_TYPE = "keystore.type"; // The keystore type private String type; // The provider private Provider provider; // The provider implementation private KeyStoreSpi keyStoreSpi; // Has this keystore been initialized (loaded)? private boolean initialized = false; /** * Creates a KeyStore object of the given type, and encapsulates the given * provider implementation (SPI object) in it. * * @param keyStoreSpi the provider implementation. * @param provider the provider. * @param type the keystore type. */ protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type) { this.keyStoreSpi = keyStoreSpi; this.provider = provider; this.type = type; } /** * Generates a keystore object of the given type. * * <p>If the default provider package provides a keystore implementation * of the given type, an instance of <code>KeyStore</code> containing that * implementation is returned. If the requested keystore type is not * available in the default package, other packages are searched. * * @param type the type of keystore. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard keystore types. * * @return a keystore object of the specified type. * * @exception KeyStoreException if the requested keystore type is * not available in the default provider package or any of the other * provider packages that were searched. */ public static KeyStore getInstance(String type) throws KeyStoreException { try { Object[] objs = Security.getImpl(type, "KeyStore", (String)null); return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type); } catch(NoSuchAlgorithmException nsae) { throw new KeyStoreException(type + " not found"); } catch(NoSuchProviderException nspe) { throw new KeyStoreException(type + " not found"); } } /** * Generates a keystore object for the specified keystore * type from the specified provider. * * @param type the type of keystore. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard keystore types. * * @param provider the name of the provider. * * @return a keystore object of the specified type, as * supplied by the specified provider. * * @exception KeyStoreException if the requested keystore type is not * available from the provider. * * @exception NoSuchProviderException if the provider has not been * configured. * * @exception IllegalArgumentException if the provider name is null * or empty. * * @see Provider */ public static KeyStore getInstance(String type, String provider) throws KeyStoreException, NoSuchProviderException { if (provider == null || provider.length() == 0) throw new IllegalArgumentException("missing provider"); try { Object[] objs = Security.getImpl(type, "KeyStore", provider); return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type); } catch(NoSuchAlgorithmException nsae) { throw new KeyStoreException(type + " not found"); } } /** * Generates a keystore object for the specified keystore * type from the specified provider. Note: the <code>provider</code> * doesn't have to be registered. * * @param type the type of keystore. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard keystore types. * * @param provider the provider. * * @return a keystore object of the specified type, as * supplied by the specified provider. * * @exception KeyStoreException if the requested keystore type is not * available from the provider. * * @exception IllegalArgumentException if the <code>provider</code> is * null. * * @see Provider * * @since 1.4 */ public static KeyStore getInstance(String type, Provider provider) throws KeyStoreException { if (provider == null) throw new IllegalArgumentException("missing provider"); try { Object[] objs = Security.getImpl(type, "KeyStore", provider); return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type); } catch(NoSuchAlgorithmException nsae) { throw new KeyStoreException(type + " not found"); } } /** * Returns the provider of this keystore. * * @return the provider of this keystore. */ public final Provider getProvider() { return this.provider; } /** * Returns the type of this keystore. * * @return the type of this keystore. */ public final String getType() { return this.type; } /** * Returns the key associated with the given alias, using the given * password to recover it. * * @param alias the alias name * @param password the password for recovering the key * * @return the requested key, or null if the given alias does not exist * or does not identify a <i>key entry</i>. * * @exception KeyStoreException if the keystore has not been initialized * (loaded). * @exception NoSuchAlgorithmException if the algorithm for recovering the * key cannot be found * @exception UnrecoverableKeyException if the key cannot be recovered * (e.g., the given password is wrong). */ public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineGetKey(alias, password); } /** * 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 or does not contain a certificate chain (i.e., the given * alias identifies either a <i>trusted certificate entry</i> or a * <i>key entry</i> without a certificate chain). * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final Certificate[] getCertificateChain(String alias) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineGetCertificateChain(alias); } /** * Returns the certificate associated with the given alias. * * <p>If the given alias name identifies a * <i>trusted certificate entry</i>, the certificate associated with that * entry is returned. If the given alias name identifies a * <i>key entry</i>, the first element of the certificate chain of that * entry is returned, or null if that entry does not have a certificate * chain. * * @param alias the alias name * * @return the certificate, or null if the given alias does not exist or * does not contain a certificate. * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final Certificate getCertificate(String alias) throws KeyStoreException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -