📄 keystore.java
字号:
{ if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineGetCertificate(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 * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final Date getCreationDate(String alias) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineGetCreationDate(alias); } /** * Assigns the given key to the given alias, protecting it with the given * password. * * <p>If the given key is of type <code>java.security.PrivateKey</code>, * it must be accompanied by a certificate chain certifying the * corresponding public key. * * <p>If the given alias already exists, the keystore information * associated with it is overridden by the given key (and possibly * certificate chain). * * @param alias the alias name * @param key the key to be associated with the alias * @param password the password to protect the key * @param chain the certificate chain for the corresponding public * key (only required if the given key is of type * <code>java.security.PrivateKey</code>). * * @exception KeyStoreException if the keystore has not been initialized * (loaded), the given key cannot be protected, or this operation fails * for some other reason */ public final void setKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } if ((key instanceof PrivateKey) && (chain==null || chain.length==0)) { throw new IllegalArgumentException("Private key must be " + "accompanied by certificate " + "chain"); } keyStoreSpi.engineSetKeyEntry(alias, key, password, chain); } /** * Assigns the given key (that has already been protected) to the given * alias. * * <p>If the protected key is of type * <code>java.security.PrivateKey</code>, it must be accompanied by a * certificate chain certifying the corresponding public key. * * <p>If the given alias already exists, the keystore information * associated with it is overridden by the given key (and possibly * certificate chain). * * @param alias the alias name * @param key the key (in protected format) to be associated with the alias * @param chain the certificate chain for the corresponding public * key (only useful if the protected key is of type * <code>java.security.PrivateKey</code>). * * @exception KeyStoreException if the keystore has not been initialized * (loaded), or if this operation fails for some other reason. */ public final void setKeyEntry(String alias, byte[] key, Certificate[] chain) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } keyStoreSpi.engineSetKeyEntry(alias, key, chain); } /** * Assigns the given certificate to the given alias. * * <p>If the given alias already exists in this keystore and identifies a * <i>trusted certificate entry</i>, the certificate associated with it is * overridden by the given certificate. * * @param alias the alias name * @param cert the certificate * * @exception KeyStoreException if the keystore has not been initialized, * or the given alias already exists and does not identify a * <i>trusted certificate entry</i>, or this operation fails for some * other reason. */ public final void setCertificateEntry(String alias, Certificate cert) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } keyStoreSpi.engineSetCertificateEntry(alias, cert); } /** * Deletes the entry identified by the given alias from this keystore. * * @param alias the alias name * * @exception KeyStoreException if the keystore has not been initialized, * or if the entry cannot be removed. */ public final void deleteEntry(String alias) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } keyStoreSpi.engineDeleteEntry(alias); } /** * Lists all the alias names of this keystore. * * @return enumeration of the alias names * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final Enumeration aliases() throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineAliases(); } /** * Checks if the given alias exists in this keystore. * * @param alias the alias name * * @return true if the alias exists, false otherwise * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final boolean containsAlias(String alias) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineContainsAlias(alias); } /** * Retrieves the number of entries in this keystore. * * @return the number of entries in this keystore * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final int size() throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineSize(); } /** * Returns true if the entry identified by the given alias is a * <i>key entry</i>, and false otherwise. * * @param alias the alias for the keystore entry to be checked * * @return true if the entry identified by the given alias is a * <i>key entry</i>, false otherwise. * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final boolean isKeyEntry(String alias) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineIsKeyEntry(alias); } /** * Returns true if the entry identified by the given alias is a * <i>trusted certificate entry</i>, and false otherwise. * * @param alias the alias for the keystore entry to be checked * * @return true if the entry identified by the given alias is a * <i>trusted certificate entry</i>, false otherwise. * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final boolean isCertificateEntry(String alias) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineIsCertificateEntry(alias); } /** * Returns the (alias) name of the first keystore entry whose certificate * matches the given certificate. * * <p>This method attempts to match the given certificate with each * keystore entry. If the entry being considered * is a <i>trusted certificate entry</i>, the given certificate is * compared to that entry's certificate. If the entry being considered is * a <i>key entry</i>, the given certificate is compared to the first * element of that entry's certificate chain (if a chain exists). * * @param cert the certificate to match with. * * @return the (alias) name of the first entry with matching certificate, * or null if no such entry exists in this keystore. * * @exception KeyStoreException if the keystore has not been initialized * (loaded). */ public final String getCertificateAlias(Certificate cert) throws KeyStoreException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } return keyStoreSpi.engineGetCertificateAlias(cert); } /** * Stores this keystore to the given output stream, and protects its * integrity with the given password. * * @param stream the output stream to which this keystore is written. * @param password the password to generate the keystore integrity check * * @exception KeyStoreException if the keystore has not been initialized * (loaded). * @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 final void store(OutputStream stream, char[] password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { if (!initialized) { throw new KeyStoreException("Uninitialized keystore"); } keyStoreSpi.engineStore(stream, password); } /** * Loads this KeyStore from the given input stream. * * <p>If a password is given, it is used to check the integrity of the * keystore data. Otherwise, the integrity of the keystore is not checked. * * <p>In order to create an empty keystore, or if the keystore cannot * be initialized from a stream (e.g., because it is stored on a hardware * token device), you pass <code>null</code> * as the <code>stream</code> argument. * * <p> Note that if this KeyStore has already been loaded, it is * reinitialized and loaded again from the given input stream. * * @param stream the input stream from which the keystore is loaded, or * null if an empty keystore is to be created. * @param password the (optional) password used to check the integrity of * the keystore. * * @exception IOException if there is an I/O or format problem with the * keystore data * @exception NoSuchAlgorithmException if the algorithm used to check * the integrity of the keystore cannot be found * @exception CertificateException if any of the certificates in the * keystore could not be loaded */ public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException { keyStoreSpi.engineLoad(stream, password); initialized = true; } /** * Returns the default keystore type as specified in the Java security * properties file, or the string "jks" (acronym for "Java keystore") * if no such property exists. * The Java security properties file is located in the file named * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME> * refers to the directory where the SDK was installed. * * <p>The default keystore type can be used by applications that do not * want to use a hard-coded keystore type when calling one of the * <code>getInstance</code> methods, and want to provide a default keystore * type in case a user does not specify its own. * * <p>The default keystore type can be changed by setting the value of the * "keystore.type" security property (in the Java security properties * file) to the desired keystore type. * * @return the default keystore type as specified in the * Java security properties file, or the string "jks" * if no such property exists. */ public final static String getDefaultType() { String kstype; kstype = (String)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return Security.getProperty(KEYSTORE_TYPE); } }); if (kstype == null) { kstype = "jks"; } return kstype; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -