Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/KeyStoreSpi.java
38829 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.io.*;28import java.util.*;2930import java.security.KeyStore.*;31import java.security.cert.Certificate;32import java.security.cert.CertificateException;3334import javax.crypto.SecretKey;3536import javax.security.auth.callback.*;3738/**39* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)40* for the {@code KeyStore} class.41* All the abstract methods in this class must be implemented by each42* cryptographic service provider who wishes to supply the implementation43* of a keystore for a particular keystore type.44*45* @author Jan Luehe46*47*48* @see KeyStore49*50* @since 1.251*/5253public abstract class KeyStoreSpi {5455/**56* Returns the key associated with the given alias, using the given57* password to recover it. The key must have been associated with58* the alias by a call to {@code setKeyEntry},59* or by a call to {@code setEntry} with a60* {@code PrivateKeyEntry} or {@code SecretKeyEntry}.61*62* @param alias the alias name63* @param password the password for recovering the key64*65* @return the requested key, or null if the given alias does not exist66* or does not identify a key-related entry.67*68* @exception NoSuchAlgorithmException if the algorithm for recovering the69* key cannot be found70* @exception UnrecoverableKeyException if the key cannot be recovered71* (e.g., the given password is wrong).72*/73public abstract Key engineGetKey(String alias, char[] password)74throws NoSuchAlgorithmException, UnrecoverableKeyException;7576/**77* Returns the certificate chain associated with the given alias.78* The certificate chain must have been associated with the alias79* by a call to {@code setKeyEntry},80* or by a call to {@code setEntry} with a81* {@code PrivateKeyEntry}.82*83* @param alias the alias name84*85* @return the certificate chain (ordered with the user's certificate first86* and the root certificate authority last), or null if the given alias87* does not exist or does not contain a certificate chain88*/89public abstract Certificate[] engineGetCertificateChain(String alias);9091/**92* Returns the certificate associated with the given alias.93*94* <p> If the given alias name identifies an entry95* created by a call to {@code setCertificateEntry},96* or created by a call to {@code setEntry} with a97* {@code TrustedCertificateEntry},98* then the trusted certificate contained in that entry is returned.99*100* <p> If the given alias name identifies an entry101* created by a call to {@code setKeyEntry},102* or created by a call to {@code setEntry} with a103* {@code PrivateKeyEntry},104* then the first element of the certificate chain in that entry105* (if a chain exists) is returned.106*107* @param alias the alias name108*109* @return the certificate, or null if the given alias does not exist or110* does not contain a certificate.111*/112public abstract Certificate engineGetCertificate(String alias);113114/**115* Returns the creation date of the entry identified by the given alias.116*117* @param alias the alias name118*119* @return the creation date of this entry, or null if the given alias does120* not exist121*/122public abstract Date engineGetCreationDate(String alias);123124/**125* Assigns the given key to the given alias, protecting it with the given126* password.127*128* <p>If the given key is of type {@code java.security.PrivateKey},129* it must be accompanied by a certificate chain certifying the130* corresponding public key.131*132* <p>If the given alias already exists, the keystore information133* associated with it is overridden by the given key (and possibly134* certificate chain).135*136* @param alias the alias name137* @param key the key to be associated with the alias138* @param password the password to protect the key139* @param chain the certificate chain for the corresponding public140* key (only required if the given key is of type141* {@code java.security.PrivateKey}).142*143* @exception KeyStoreException if the given key cannot be protected, or144* this operation fails for some other reason145*/146public abstract void engineSetKeyEntry(String alias, Key key,147char[] password,148Certificate[] chain)149throws KeyStoreException;150151/**152* Assigns the given key (that has already been protected) to the given153* alias.154*155* <p>If the protected key is of type156* {@code java.security.PrivateKey},157* it must be accompanied by a certificate chain certifying the158* corresponding public key.159*160* <p>If the given alias already exists, the keystore information161* associated with it is overridden by the given key (and possibly162* certificate chain).163*164* @param alias the alias name165* @param key the key (in protected format) to be associated with the alias166* @param chain the certificate chain for the corresponding public167* key (only useful if the protected key is of type168* {@code java.security.PrivateKey}).169*170* @exception KeyStoreException if this operation fails.171*/172public abstract void engineSetKeyEntry(String alias, byte[] key,173Certificate[] chain)174throws KeyStoreException;175176/**177* Assigns the given certificate to the given alias.178*179* <p> If the given alias identifies an existing entry180* created by a call to {@code setCertificateEntry},181* or created by a call to {@code setEntry} with a182* {@code TrustedCertificateEntry},183* the trusted certificate in the existing entry184* is overridden by the given certificate.185*186* @param alias the alias name187* @param cert the certificate188*189* @exception KeyStoreException if the given alias already exists and does190* not identify an entry containing a trusted certificate,191* or this operation fails for some other reason.192*/193public abstract void engineSetCertificateEntry(String alias,194Certificate cert)195throws KeyStoreException;196197/**198* Deletes the entry identified by the given alias from this keystore.199*200* @param alias the alias name201*202* @exception KeyStoreException if the entry cannot be removed.203*/204public abstract void engineDeleteEntry(String alias)205throws KeyStoreException;206207/**208* Lists all the alias names of this keystore.209*210* @return enumeration of the alias names211*/212public abstract Enumeration<String> engineAliases();213214/**215* Checks if the given alias exists in this keystore.216*217* @param alias the alias name218*219* @return true if the alias exists, false otherwise220*/221public abstract boolean engineContainsAlias(String alias);222223/**224* Retrieves the number of entries in this keystore.225*226* @return the number of entries in this keystore227*/228public abstract int engineSize();229230/**231* Returns true if the entry identified by the given alias232* was created by a call to {@code setKeyEntry},233* or created by a call to {@code setEntry} with a234* {@code PrivateKeyEntry} or a {@code SecretKeyEntry}.235*236* @param alias the alias for the keystore entry to be checked237*238* @return true if the entry identified by the given alias is a239* key-related, false otherwise.240*/241public abstract boolean engineIsKeyEntry(String alias);242243/**244* Returns true if the entry identified by the given alias245* was created by a call to {@code setCertificateEntry},246* or created by a call to {@code setEntry} with a247* {@code TrustedCertificateEntry}.248*249* @param alias the alias for the keystore entry to be checked250*251* @return true if the entry identified by the given alias contains a252* trusted certificate, false otherwise.253*/254public abstract boolean engineIsCertificateEntry(String alias);255256/**257* Returns the (alias) name of the first keystore entry whose certificate258* matches the given certificate.259*260* <p>This method attempts to match the given certificate with each261* keystore entry. If the entry being considered was262* created by a call to {@code setCertificateEntry},263* or created by a call to {@code setEntry} with a264* {@code TrustedCertificateEntry},265* then the given certificate is compared to that entry's certificate.266*267* <p> If the entry being considered was268* created by a call to {@code setKeyEntry},269* or created by a call to {@code setEntry} with a270* {@code PrivateKeyEntry},271* then the given certificate is compared to the first272* element of that entry's certificate chain.273*274* @param cert the certificate to match with.275*276* @return the alias name of the first entry with matching certificate,277* or null if no such entry exists in this keystore.278*/279public abstract String engineGetCertificateAlias(Certificate cert);280281/**282* Stores this keystore to the given output stream, and protects its283* integrity with the given password.284*285* @param stream the output stream to which this keystore is written.286* @param password the password to generate the keystore integrity check287*288* @exception IOException if there was an I/O problem with data289* @exception NoSuchAlgorithmException if the appropriate data integrity290* algorithm could not be found291* @exception CertificateException if any of the certificates included in292* the keystore data could not be stored293*/294public abstract void engineStore(OutputStream stream, char[] password)295throws IOException, NoSuchAlgorithmException, CertificateException;296297/**298* Stores this keystore using the given299* {@code KeyStore.LoadStoreParmeter}.300*301* @param param the {@code KeyStore.LoadStoreParmeter}302* that specifies how to store the keystore,303* which may be {@code null}304*305* @exception IllegalArgumentException if the given306* {@code KeyStore.LoadStoreParmeter}307* input is not recognized308* @exception IOException if there was an I/O problem with data309* @exception NoSuchAlgorithmException if the appropriate data integrity310* algorithm could not be found311* @exception CertificateException if any of the certificates included in312* the keystore data could not be stored313*314* @since 1.5315*/316public void engineStore(KeyStore.LoadStoreParameter param)317throws IOException, NoSuchAlgorithmException,318CertificateException {319throw new UnsupportedOperationException();320}321322/**323* Loads the keystore from the given input stream.324*325* <p>A password may be given to unlock the keystore326* (e.g. the keystore resides on a hardware token device),327* or to check the integrity of the keystore data.328* If a password is not given for integrity checking,329* then integrity checking is not performed.330*331* @param stream the input stream from which the keystore is loaded,332* or {@code null}333* @param password the password used to check the integrity of334* the keystore, the password used to unlock the keystore,335* or {@code null}336*337* @exception IOException if there is an I/O or format problem with the338* keystore data, if a password is required but not given,339* or if the given password was incorrect. If the error is due to a340* wrong password, the {@link Throwable#getCause cause} of the341* {@code IOException} should be an342* {@code UnrecoverableKeyException}343* @exception NoSuchAlgorithmException if the algorithm used to check344* the integrity of the keystore cannot be found345* @exception CertificateException if any of the certificates in the346* keystore could not be loaded347*/348public abstract void engineLoad(InputStream stream, char[] password)349throws IOException, NoSuchAlgorithmException, CertificateException;350351/**352* Loads the keystore using the given353* {@code KeyStore.LoadStoreParameter}.354*355* <p> Note that if this KeyStore has already been loaded, it is356* reinitialized and loaded again from the given parameter.357*358* @param param the {@code KeyStore.LoadStoreParameter}359* that specifies how to load the keystore,360* which may be {@code null}361*362* @exception IllegalArgumentException if the given363* {@code KeyStore.LoadStoreParameter}364* input is not recognized365* @exception IOException if there is an I/O or format problem with the366* keystore data. If the error is due to an incorrect367* {@code ProtectionParameter} (e.g. wrong password)368* the {@link Throwable#getCause cause} of the369* {@code IOException} should be an370* {@code UnrecoverableKeyException}371* @exception NoSuchAlgorithmException if the algorithm used to check372* the integrity of the keystore cannot be found373* @exception CertificateException if any of the certificates in the374* keystore could not be loaded375*376* @since 1.5377*/378public void engineLoad(KeyStore.LoadStoreParameter param)379throws IOException, NoSuchAlgorithmException,380CertificateException {381382if (param == null) {383engineLoad((InputStream)null, (char[])null);384return;385}386387if (param instanceof KeyStore.SimpleLoadStoreParameter) {388ProtectionParameter protection = param.getProtectionParameter();389char[] password;390if (protection instanceof PasswordProtection) {391password = ((PasswordProtection)protection).getPassword();392} else if (protection instanceof CallbackHandlerProtection) {393CallbackHandler handler =394((CallbackHandlerProtection)protection).getCallbackHandler();395PasswordCallback callback =396new PasswordCallback("Password: ", false);397try {398handler.handle(new Callback[] {callback});399} catch (UnsupportedCallbackException e) {400throw new NoSuchAlgorithmException401("Could not obtain password", e);402}403password = callback.getPassword();404callback.clearPassword();405if (password == null) {406throw new NoSuchAlgorithmException407("No password provided");408}409} else {410throw new NoSuchAlgorithmException("ProtectionParameter must"411+ " be PasswordProtection or CallbackHandlerProtection");412}413engineLoad(null, password);414return;415}416417throw new UnsupportedOperationException();418}419420/**421* Gets a {@code KeyStore.Entry} for the specified alias422* with the specified protection parameter.423*424* @param alias get the {@code KeyStore.Entry} for this alias425* @param protParam the {@code ProtectionParameter}426* used to protect the {@code Entry},427* which may be {@code null}428*429* @return the {@code KeyStore.Entry} for the specified alias,430* or {@code null} if there is no such entry431*432* @exception KeyStoreException if the operation failed433* @exception NoSuchAlgorithmException if the algorithm for recovering the434* entry cannot be found435* @exception UnrecoverableEntryException if the specified436* {@code protParam} were insufficient or invalid437* @exception UnrecoverableKeyException if the entry is a438* {@code PrivateKeyEntry} or {@code SecretKeyEntry}439* and the specified {@code protParam} does not contain440* the information needed to recover the key (e.g. wrong password)441*442* @since 1.5443*/444public KeyStore.Entry engineGetEntry(String alias,445KeyStore.ProtectionParameter protParam)446throws KeyStoreException, NoSuchAlgorithmException,447UnrecoverableEntryException {448449if (!engineContainsAlias(alias)) {450return null;451}452453if (protParam == null) {454if (engineIsCertificateEntry(alias)) {455return new KeyStore.TrustedCertificateEntry456(engineGetCertificate(alias));457} else {458throw new UnrecoverableKeyException459("requested entry requires a password");460}461}462463if (protParam instanceof KeyStore.PasswordProtection) {464if (engineIsCertificateEntry(alias)) {465throw new UnsupportedOperationException466("trusted certificate entries are not password-protected");467} else if (engineIsKeyEntry(alias)) {468KeyStore.PasswordProtection pp =469(KeyStore.PasswordProtection)protParam;470char[] password = pp.getPassword();471472Key key = engineGetKey(alias, password);473if (key instanceof PrivateKey) {474Certificate[] chain = engineGetCertificateChain(alias);475return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain);476} else if (key instanceof SecretKey) {477return new KeyStore.SecretKeyEntry((SecretKey)key);478}479}480}481482throw new UnsupportedOperationException();483}484485/**486* Saves a {@code KeyStore.Entry} under the specified alias.487* The specified protection parameter is used to protect the488* {@code Entry}.489*490* <p> If an entry already exists for the specified alias,491* it is overridden.492*493* @param alias save the {@code KeyStore.Entry} under this alias494* @param entry the {@code Entry} to save495* @param protParam the {@code ProtectionParameter}496* used to protect the {@code Entry},497* which may be {@code null}498*499* @exception KeyStoreException if this operation fails500*501* @since 1.5502*/503public void engineSetEntry(String alias, KeyStore.Entry entry,504KeyStore.ProtectionParameter protParam)505throws KeyStoreException {506507// get password508if (protParam != null &&509!(protParam instanceof KeyStore.PasswordProtection)) {510throw new KeyStoreException("unsupported protection parameter");511}512KeyStore.PasswordProtection pProtect = null;513if (protParam != null) {514pProtect = (KeyStore.PasswordProtection)protParam;515}516517// set entry518if (entry instanceof KeyStore.TrustedCertificateEntry) {519if (protParam != null && pProtect.getPassword() != null) {520// pre-1.5 style setCertificateEntry did not allow password521throw new KeyStoreException522("trusted certificate entries are not password-protected");523} else {524KeyStore.TrustedCertificateEntry tce =525(KeyStore.TrustedCertificateEntry)entry;526engineSetCertificateEntry(alias, tce.getTrustedCertificate());527return;528}529} else if (entry instanceof KeyStore.PrivateKeyEntry) {530if (pProtect == null || pProtect.getPassword() == null) {531// pre-1.5 style setKeyEntry required password532throw new KeyStoreException533("non-null password required to create PrivateKeyEntry");534} else {535engineSetKeyEntry536(alias,537((KeyStore.PrivateKeyEntry)entry).getPrivateKey(),538pProtect.getPassword(),539((KeyStore.PrivateKeyEntry)entry).getCertificateChain());540return;541}542} else if (entry instanceof KeyStore.SecretKeyEntry) {543if (pProtect == null || pProtect.getPassword() == null) {544// pre-1.5 style setKeyEntry required password545throw new KeyStoreException546("non-null password required to create SecretKeyEntry");547} else {548engineSetKeyEntry549(alias,550((KeyStore.SecretKeyEntry)entry).getSecretKey(),551pProtect.getPassword(),552(Certificate[])null);553return;554}555}556557throw new KeyStoreException558("unsupported entry type: " + entry.getClass().getName());559}560561/**562* Determines if the keystore {@code Entry} for the specified563* {@code alias} is an instance or subclass of the specified564* {@code entryClass}.565*566* @param alias the alias name567* @param entryClass the entry class568*569* @return true if the keystore {@code Entry} for the specified570* {@code alias} is an instance or subclass of the571* specified {@code entryClass}, false otherwise572*573* @since 1.5574*/575public boolean576engineEntryInstanceOf(String alias,577Class<? extends KeyStore.Entry> entryClass)578{579if (entryClass == KeyStore.TrustedCertificateEntry.class) {580return engineIsCertificateEntry(alias);581}582if (entryClass == KeyStore.PrivateKeyEntry.class) {583return engineIsKeyEntry(alias) &&584engineGetCertificate(alias) != null;585}586if (entryClass == KeyStore.SecretKeyEntry.class) {587return engineIsKeyEntry(alias) &&588engineGetCertificate(alias) == null;589}590return false;591}592}593594595