Path: blob/master/src/java.base/share/classes/java/security/KeyStoreSpi.java
41152 views
/*1* Copyright (c) 1998, 2020, 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* Constructor for subclasses to call.57*/58public KeyStoreSpi() {}5960/**61* Returns the key associated with the given alias, using the given62* password to recover it. The key must have been associated with63* the alias by a call to {@code setKeyEntry},64* or by a call to {@code setEntry} with a65* {@code PrivateKeyEntry} or {@code SecretKeyEntry}.66*67* @param alias the alias name68* @param password the password for recovering the key69*70* @return the requested key, or null if the given alias does not exist71* or does not identify a key-related entry.72*73* @throws NoSuchAlgorithmException if the algorithm for recovering the74* key cannot be found75* @throws UnrecoverableKeyException if the key cannot be recovered76* (e.g., the given password is wrong).77*/78public abstract Key engineGetKey(String alias, char[] password)79throws NoSuchAlgorithmException, UnrecoverableKeyException;8081/**82* Returns the certificate chain associated with the given alias.83* The certificate chain must have been associated with the alias84* by a call to {@code setKeyEntry},85* or by a call to {@code setEntry} with a86* {@code PrivateKeyEntry}.87*88* @param alias the alias name89*90* @return the certificate chain (ordered with the user's certificate first91* and the root certificate authority last), or null if the given alias92* does not exist or does not contain a certificate chain93*/94public abstract Certificate[] engineGetCertificateChain(String alias);9596/**97* Returns the certificate associated with the given alias.98*99* <p> If the given alias name identifies an entry100* created by a call to {@code setCertificateEntry},101* or created by a call to {@code setEntry} with a102* {@code TrustedCertificateEntry},103* then the trusted certificate contained in that entry is returned.104*105* <p> If the given alias name identifies an entry106* created by a call to {@code setKeyEntry},107* or created by a call to {@code setEntry} with a108* {@code PrivateKeyEntry},109* then the first element of the certificate chain in that entry110* (if a chain exists) is returned.111*112* @param alias the alias name113*114* @return the certificate, or null if the given alias does not exist or115* does not contain a certificate.116*/117public abstract Certificate engineGetCertificate(String alias);118119/**120* Returns the creation date of the entry identified by the given alias.121*122* @param alias the alias name123*124* @return the creation date of this entry, or null if the given alias does125* not exist126*/127public abstract Date engineGetCreationDate(String alias);128129/**130* Assigns the given key to the given alias, protecting it with the given131* password.132*133* <p>If the given key is of type {@code java.security.PrivateKey},134* it must be accompanied by a certificate chain certifying the135* corresponding public key.136*137* <p>If the given alias already exists, the keystore information138* associated with it is overridden by the given key (and possibly139* certificate chain).140*141* @param alias the alias name142* @param key the key to be associated with the alias143* @param password the password to protect the key144* @param chain the certificate chain for the corresponding public145* key (only required if the given key is of type146* {@code java.security.PrivateKey}).147*148* @throws KeyStoreException if the given key cannot be protected, or149* this operation fails for some other reason150*/151public abstract void engineSetKeyEntry(String alias, Key key,152char[] password,153Certificate[] chain)154throws KeyStoreException;155156/**157* Assigns the given key (that has already been protected) to the given158* alias.159*160* <p>If the protected key is of type161* {@code java.security.PrivateKey},162* it must be accompanied by a certificate chain certifying the163* corresponding public key.164*165* <p>If the given alias already exists, the keystore information166* associated with it is overridden by the given key (and possibly167* certificate chain).168*169* @param alias the alias name170* @param key the key (in protected format) to be associated with the alias171* @param chain the certificate chain for the corresponding public172* key (only useful if the protected key is of type173* {@code java.security.PrivateKey}).174*175* @throws KeyStoreException if this operation fails.176*/177public abstract void engineSetKeyEntry(String alias, byte[] key,178Certificate[] chain)179throws KeyStoreException;180181/**182* Assigns the given certificate to the given alias.183*184* <p> If the given alias identifies an existing entry185* created by a call to {@code setCertificateEntry},186* or created by a call to {@code setEntry} with a187* {@code TrustedCertificateEntry},188* the trusted certificate in the existing entry189* is overridden by the given certificate.190*191* @param alias the alias name192* @param cert the certificate193*194* @throws KeyStoreException if the given alias already exists and does195* not identify an entry containing a trusted certificate,196* or this operation fails for some other reason.197*/198public abstract void engineSetCertificateEntry(String alias,199Certificate cert)200throws KeyStoreException;201202/**203* Deletes the entry identified by the given alias from this keystore.204*205* @param alias the alias name206*207* @throws KeyStoreException if the entry cannot be removed.208*/209public abstract void engineDeleteEntry(String alias)210throws KeyStoreException;211212/**213* Lists all the alias names of this keystore.214*215* @return enumeration of the alias names216*/217public abstract Enumeration<String> engineAliases();218219/**220* Checks if the given alias exists in this keystore.221*222* @param alias the alias name223*224* @return true if the alias exists, false otherwise225*/226public abstract boolean engineContainsAlias(String alias);227228/**229* Retrieves the number of entries in this keystore.230*231* @return the number of entries in this keystore232*/233public abstract int engineSize();234235/**236* Returns true if the entry identified by the given alias237* was created by a call to {@code setKeyEntry},238* or created by a call to {@code setEntry} with a239* {@code PrivateKeyEntry} or a {@code SecretKeyEntry}.240*241* @param alias the alias for the keystore entry to be checked242*243* @return true if the entry identified by the given alias is a244* key-related, false otherwise.245*/246public abstract boolean engineIsKeyEntry(String alias);247248/**249* Returns true if the entry identified by the given alias250* was created by a call to {@code setCertificateEntry},251* or created by a call to {@code setEntry} with a252* {@code TrustedCertificateEntry}.253*254* @param alias the alias for the keystore entry to be checked255*256* @return true if the entry identified by the given alias contains a257* trusted certificate, false otherwise.258*/259public abstract boolean engineIsCertificateEntry(String alias);260261/**262* Returns the (alias) name of the first keystore entry whose certificate263* matches the given certificate.264*265* <p>This method attempts to match the given certificate with each266* keystore entry. If the entry being considered was267* created by a call to {@code setCertificateEntry},268* or created by a call to {@code setEntry} with a269* {@code TrustedCertificateEntry},270* then the given certificate is compared to that entry's certificate.271*272* <p> If the entry being considered was273* created by a call to {@code setKeyEntry},274* or created by a call to {@code setEntry} with a275* {@code PrivateKeyEntry},276* then the given certificate is compared to the first277* element of that entry's certificate chain.278*279* @param cert the certificate to match with.280*281* @return the alias name of the first entry with matching certificate,282* or null if no such entry exists in this keystore.283*/284public abstract String engineGetCertificateAlias(Certificate cert);285286/**287* Stores this keystore to the given output stream, and protects its288* integrity with the given password.289*290* @param stream the output stream to which this keystore is written.291* @param password the password to generate the keystore integrity check292*293* @throws IOException if there was an I/O problem with data294* @throws NoSuchAlgorithmException if the appropriate data integrity295* algorithm could not be found296* @throws CertificateException if any of the certificates included in297* the keystore data could not be stored298*/299public abstract void engineStore(OutputStream stream, char[] password)300throws IOException, NoSuchAlgorithmException, CertificateException;301302/**303* Stores this keystore using the given304* {@code KeyStore.LoadStoreParameter}.305*306* @implSpec The default implementation throws307* an {@link UnsupportedOperationException}.308*309* @param param the {@code KeyStore.LoadStoreParameter}310* that specifies how to store the keystore,311* which may be {@code null}312*313* @throws IllegalArgumentException if the given314* {@code KeyStore.LoadStoreParameter}315* input is not recognized316* @throws IOException if there was an I/O problem with data317* @throws NoSuchAlgorithmException if the appropriate data integrity318* algorithm could not be found319* @throws CertificateException if any of the certificates included in320* the keystore data could not be stored321* @throws UnsupportedOperationException if the implementation does322* not support this operation323*324* @since 1.5325*/326public void engineStore(KeyStore.LoadStoreParameter param)327throws IOException, NoSuchAlgorithmException,328CertificateException {329throw new UnsupportedOperationException();330}331332/**333* Loads the keystore from the given input stream.334*335* <p>A password may be given to unlock the keystore336* (e.g. the keystore resides on a hardware token device),337* or to check the integrity of the keystore data.338* If a password is not given for integrity checking,339* then integrity checking is not performed.340*341* @param stream the input stream from which the keystore is loaded,342* or {@code null}343* @param password the password used to check the integrity of344* the keystore, the password used to unlock the keystore,345* or {@code null}346*347* @throws IOException if there is an I/O or format problem with the348* keystore data, if a password is required but not given,349* or if the given password was incorrect. If the error is due to a350* wrong password, the {@link Throwable#getCause cause} of the351* {@code IOException} should be an352* {@code UnrecoverableKeyException}353* @throws NoSuchAlgorithmException if the algorithm used to check354* the integrity of the keystore cannot be found355* @throws CertificateException if any of the certificates in the356* keystore could not be loaded357*/358public abstract void engineLoad(InputStream stream, char[] password)359throws IOException, NoSuchAlgorithmException, CertificateException;360361/**362* Loads the keystore using the given363* {@code KeyStore.LoadStoreParameter}.364*365* <p> Note that if this KeyStore has already been loaded, it is366* reinitialized and loaded again from the given parameter.367*368* @param param the {@code KeyStore.LoadStoreParameter}369* that specifies how to load the keystore,370* which may be {@code null}371*372* @implSpec373* The default implementation examines {@code KeyStore.LoadStoreParameter}374* to extract its password and pass it to375* {@link KeyStoreSpi#engineLoad(InputStream, char[])} along with a376* {@code null} {@code InputStream}.377* <p>378* If {@code KeyStore.LoadStoreParameter} is {@code null} then379* the password parameter will also be {@code null}.380* Otherwise the {@code KeyStore.ProtectionParameter} of381* {@code KeyStore.LoadStoreParameter} must be either a382* {@code KeyStore.PasswordProtection} or a383* {@code KeyStore.CallbackHandlerProtection} that supports384* {@code PasswordCallback} so that the password parameter can be385* extracted. If the {@code KeyStore.ProtectionParameter} is neither386* of those classes then a {@code NoSuchAlgorithmException} is thrown.387*388* @throws IllegalArgumentException if the given389* {@code KeyStore.LoadStoreParameter}390* input is not recognized391* @throws IOException if there is an I/O or format problem with the392* keystore data. If the error is due to an incorrect393* {@code ProtectionParameter} (e.g. wrong password)394* the {@link Throwable#getCause cause} of the395* {@code IOException} should be an396* {@code UnrecoverableKeyException}397* @throws NoSuchAlgorithmException if the algorithm used to check398* the integrity of the keystore cannot be found399* @throws CertificateException if any of the certificates in the400* keystore could not be loaded401*402* @since 1.5403*/404public void engineLoad(KeyStore.LoadStoreParameter param)405throws IOException, NoSuchAlgorithmException,406CertificateException {407engineLoad(null, param);408}409410void engineLoad(InputStream stream, KeyStore.LoadStoreParameter param)411throws IOException, NoSuchAlgorithmException,412CertificateException {413414if (param == null) {415engineLoad((InputStream)null, (char[])null);416return;417}418419ProtectionParameter protection = param.getProtectionParameter();420char[] password;421if (protection instanceof PasswordProtection) {422password = ((PasswordProtection)protection).getPassword();423} else if (protection instanceof CallbackHandlerProtection) {424CallbackHandler handler =425((CallbackHandlerProtection)protection).getCallbackHandler();426PasswordCallback callback =427new PasswordCallback("Password: ", false);428try {429handler.handle(new Callback[] {callback});430} catch (UnsupportedCallbackException e) {431throw new NoSuchAlgorithmException432("Could not obtain password", e);433}434password = callback.getPassword();435callback.clearPassword();436if (password == null) {437throw new NoSuchAlgorithmException("No password provided");438}439} else {440throw new NoSuchAlgorithmException("ProtectionParameter must"441+ " be PasswordProtection or CallbackHandlerProtection");442}443engineLoad(stream, password);444return;445}446447/**448* Gets a {@code KeyStore.Entry} for the specified alias449* with the specified protection parameter.450*451* @param alias get the {@code KeyStore.Entry} for this alias452* @param protParam the {@code ProtectionParameter}453* used to protect the {@code Entry},454* which may be {@code null}455*456* @return the {@code KeyStore.Entry} for the specified alias,457* or {@code null} if there is no such entry458*459* @throws KeyStoreException if the operation failed460* @throws NoSuchAlgorithmException if the algorithm for recovering the461* entry cannot be found462* @throws UnrecoverableEntryException if the specified463* {@code protParam} were insufficient or invalid464* @throws UnrecoverableKeyException if the entry is a465* {@code PrivateKeyEntry} or {@code SecretKeyEntry}466* and the specified {@code protParam} does not contain467* the information needed to recover the key (e.g. wrong password)468*469* @since 1.5470*/471public KeyStore.Entry engineGetEntry(String alias,472KeyStore.ProtectionParameter protParam)473throws KeyStoreException, NoSuchAlgorithmException,474UnrecoverableEntryException {475476if (!engineContainsAlias(alias)) {477return null;478}479480if (protParam == null) {481if (engineIsCertificateEntry(alias)) {482return new KeyStore.TrustedCertificateEntry483(engineGetCertificate(alias));484} else {485throw new UnrecoverableKeyException486("requested entry requires a password");487}488}489490if (protParam instanceof KeyStore.PasswordProtection) {491if (engineIsCertificateEntry(alias)) {492throw new UnsupportedOperationException493("trusted certificate entries are not password-protected");494} else if (engineIsKeyEntry(alias)) {495KeyStore.PasswordProtection pp =496(KeyStore.PasswordProtection)protParam;497if (pp.getProtectionAlgorithm() != null) {498throw new KeyStoreException(499"unsupported password protection algorithm");500}501char[] password = pp.getPassword();502503Key key = engineGetKey(alias, password);504if (key instanceof PrivateKey) {505Certificate[] chain = engineGetCertificateChain(alias);506return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain);507} else if (key instanceof SecretKey) {508return new KeyStore.SecretKeyEntry((SecretKey)key);509}510}511}512513throw new UnsupportedOperationException();514}515516/**517* Saves a {@code KeyStore.Entry} under the specified alias.518* The specified protection parameter is used to protect the519* {@code Entry}.520*521* <p> If an entry already exists for the specified alias,522* it is overridden.523*524* @param alias save the {@code KeyStore.Entry} under this alias525* @param entry the {@code Entry} to save526* @param protParam the {@code ProtectionParameter}527* used to protect the {@code Entry},528* which may be {@code null}529*530* @throws KeyStoreException if this operation fails531*532* @since 1.5533*/534public void engineSetEntry(String alias, KeyStore.Entry entry,535KeyStore.ProtectionParameter protParam)536throws KeyStoreException {537538// get password539if (protParam != null &&540!(protParam instanceof KeyStore.PasswordProtection)) {541throw new KeyStoreException("unsupported protection parameter");542}543KeyStore.PasswordProtection pProtect = null;544if (protParam != null) {545pProtect = (KeyStore.PasswordProtection)protParam;546if (pProtect.getProtectionAlgorithm() != null) {547throw new KeyStoreException(548"unsupported password protection algorithm");549}550}551552// set entry553if (entry instanceof KeyStore.TrustedCertificateEntry) {554if (protParam != null && pProtect.getPassword() != null) {555// pre-1.5 style setCertificateEntry did not allow password556throw new KeyStoreException557("trusted certificate entries are not password-protected");558} else {559KeyStore.TrustedCertificateEntry tce =560(KeyStore.TrustedCertificateEntry)entry;561engineSetCertificateEntry(alias, tce.getTrustedCertificate());562return;563}564} else if (entry instanceof KeyStore.PrivateKeyEntry) {565if (pProtect == null || pProtect.getPassword() == null) {566// pre-1.5 style setKeyEntry required password567throw new KeyStoreException568("non-null password required to create PrivateKeyEntry");569} else {570engineSetKeyEntry571(alias,572((KeyStore.PrivateKeyEntry)entry).getPrivateKey(),573pProtect.getPassword(),574((KeyStore.PrivateKeyEntry)entry).getCertificateChain());575return;576}577} else if (entry instanceof KeyStore.SecretKeyEntry) {578if (pProtect == null || pProtect.getPassword() == null) {579// pre-1.5 style setKeyEntry required password580throw new KeyStoreException581("non-null password required to create SecretKeyEntry");582} else {583engineSetKeyEntry584(alias,585((KeyStore.SecretKeyEntry)entry).getSecretKey(),586pProtect.getPassword(),587(Certificate[])null);588return;589}590}591592throw new KeyStoreException593("unsupported entry type: " + entry.getClass().getName());594}595596/**597* Determines if the keystore {@code Entry} for the specified598* {@code alias} is an instance or subclass of the specified599* {@code entryClass}.600*601* @param alias the alias name602* @param entryClass the entry class603*604* @return true if the keystore {@code Entry} for the specified605* {@code alias} is an instance or subclass of the606* specified {@code entryClass}, false otherwise607*608* @since 1.5609*/610public boolean611engineEntryInstanceOf(String alias,612Class<? extends KeyStore.Entry> entryClass)613{614if (entryClass == KeyStore.TrustedCertificateEntry.class) {615return engineIsCertificateEntry(alias);616}617if (entryClass == KeyStore.PrivateKeyEntry.class) {618return engineIsKeyEntry(alias) &&619engineGetCertificate(alias) != null;620}621if (entryClass == KeyStore.SecretKeyEntry.class) {622return engineIsKeyEntry(alias) &&623engineGetCertificate(alias) == null;624}625return false;626}627628/**629* Probes the specified input stream to determine whether it contains a630* keystore that is supported by this implementation, or not.631*632* @implSpec633* This method returns false by default. Keystore implementations should634* override this method to peek at the data stream directly or to use other635* content detection mechanisms.636*637* @param stream the keystore data to be probed638*639* @return true if the keystore data is supported, otherwise false640*641* @throws IOException if there is an I/O problem with the keystore data.642* @throws NullPointerException if stream is {@code null}.643*644* @since 9645*/646public boolean engineProbe(InputStream stream) throws IOException {647if (stream == null) {648throw new NullPointerException("input stream must not be null");649}650return false;651}652}653654655