1. /*
  2. * @(#)KeyStoreSpi.java 1.5 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.io.*;
  9. import java.security.cert.Certificate;
  10. import java.security.cert.CertificateException;
  11. import java.util.*;
  12. /**
  13. * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  14. * for the <code>KeyStore</code> class.
  15. * All the abstract methods in this class must be implemented by each
  16. * cryptographic service provider who wishes to supply the implementation
  17. * of a keystore for a particular keystore type.
  18. *
  19. * @author Jan Luehe
  20. *
  21. * @version 1.5, 11/29/01
  22. *
  23. * @see KeyStore
  24. *
  25. * @since JDK1.2
  26. */
  27. public abstract class KeyStoreSpi {
  28. /**
  29. * Returns the key associated with the given alias, using the given
  30. * password to recover it.
  31. *
  32. * @param alias the alias name
  33. * @param password the password for recovering the key
  34. *
  35. * @return the requested key, or null if the given alias does not exist
  36. * or does not identify a <i>key entry</i>.
  37. *
  38. * @exception NoSuchAlgorithmException if the algorithm for recovering the
  39. * key cannot be found
  40. * @exception UnrecoverableKeyException if the key cannot be recovered
  41. * (e.g., the given password is wrong).
  42. */
  43. public abstract Key engineGetKey(String alias, char[] password)
  44. throws NoSuchAlgorithmException, UnrecoverableKeyException;
  45. /**
  46. * Returns the certificate chain associated with the given alias.
  47. *
  48. * @param alias the alias name
  49. *
  50. * @return the certificate chain (ordered with the user's certificate first
  51. * and the root certificate authority last), or null if the given alias
  52. * does not exist or does not contain a certificate chain (i.e., the given
  53. * alias identifies either a <i>trusted certificate entry</i> or a
  54. * <i>key entry</i> without a certificate chain).
  55. */
  56. public abstract Certificate[] engineGetCertificateChain(String alias);
  57. /**
  58. * Returns the certificate associated with the given alias.
  59. *
  60. * <p>If the given alias name identifies a
  61. * <i>trusted certificate entry</i>, the certificate associated with that
  62. * entry is returned. If the given alias name identifies a
  63. * <i>key entry</i>, the first element of the certificate chain of that
  64. * entry is returned, or null if that entry does not have a certificate
  65. * chain.
  66. *
  67. * @param alias the alias name
  68. *
  69. * @return the certificate, or null if the given alias does not exist or
  70. * does not contain a certificate.
  71. */
  72. public abstract Certificate engineGetCertificate(String alias);
  73. /**
  74. * Returns the creation date of the entry identified by the given alias.
  75. *
  76. * @param alias the alias name
  77. *
  78. * @return the creation date of this entry, or null if the given alias does
  79. * not exist
  80. */
  81. public abstract Date engineGetCreationDate(String alias);
  82. /**
  83. * Assigns the given key to the given alias, protecting it with the given
  84. * password.
  85. *
  86. * <p>If the given key is of type <code>java.security.PrivateKey</code>,
  87. * it must be accompanied by a certificate chain certifying the
  88. * corresponding public key.
  89. *
  90. * <p>If the given alias already exists, the keystore information
  91. * associated with it is overridden by the given key (and possibly
  92. * certificate chain).
  93. *
  94. * @param alias the alias name
  95. * @param key the key to be associated with the alias
  96. * @param password the password to protect the key
  97. * @param chain the certificate chain for the corresponding public
  98. * key (only required if the given key is of type
  99. * <code>java.security.PrivateKey</code>).
  100. *
  101. * @exception KeyStoreException if the given key cannot be protected, or
  102. * this operation fails for some other reason
  103. */
  104. public abstract void engineSetKeyEntry(String alias, Key key,
  105. char[] password,
  106. Certificate[] chain)
  107. throws KeyStoreException;
  108. /**
  109. * Assigns the given key (that has already been protected) to the given
  110. * alias.
  111. *
  112. * <p>If the protected key is of type
  113. * <code>java.security.PrivateKey</code>,
  114. * it must be accompanied by a certificate chain certifying the
  115. * corresponding public key.
  116. *
  117. * <p>If the given alias already exists, the keystore information
  118. * associated with it is overridden by the given key (and possibly
  119. * certificate chain).
  120. *
  121. * @param alias the alias name
  122. * @param key the key (in protected format) to be associated with the alias
  123. * @param chain the certificate chain for the corresponding public
  124. * key (only useful if the protected key is of type
  125. * <code>java.security.PrivateKey</code>).
  126. *
  127. * @exception KeyStoreException if this operation fails.
  128. */
  129. public abstract void engineSetKeyEntry(String alias, byte[] key,
  130. Certificate[] chain)
  131. throws KeyStoreException;
  132. /**
  133. * Assigns the given certificate to the given alias.
  134. *
  135. * <p>If the given alias already exists in this keystore and identifies a
  136. * <i>trusted certificate entry</i>, the certificate associated with it is
  137. * overridden by the given certificate.
  138. *
  139. * @param alias the alias name
  140. * @param cert the certificate
  141. *
  142. * @exception KeyStoreException if the given alias already exists and does
  143. * not identify a <i>trusted certificate entry</i>, or this operation
  144. * fails for some other reason.
  145. */
  146. public abstract void engineSetCertificateEntry(String alias,
  147. Certificate cert)
  148. throws KeyStoreException;
  149. /**
  150. * Deletes the entry identified by the given alias from this keystore.
  151. *
  152. * @param alias the alias name
  153. *
  154. * @exception KeyStoreException if the entry cannot be removed.
  155. */
  156. public abstract void engineDeleteEntry(String alias)
  157. throws KeyStoreException;
  158. /**
  159. * Lists all the alias names of this keystore.
  160. *
  161. * @return enumeration of the alias names
  162. */
  163. public abstract Enumeration engineAliases();
  164. /**
  165. * Checks if the given alias exists in this keystore.
  166. *
  167. * @param alias the alias name
  168. *
  169. * @return true if the alias exists, false otherwise
  170. */
  171. public abstract boolean engineContainsAlias(String alias);
  172. /**
  173. * Retrieves the number of entries in this keystore.
  174. *
  175. * @return the number of entries in this keystore
  176. */
  177. public abstract int engineSize();
  178. /**
  179. * Returns true if the entry identified by the given alias is a
  180. * <i>key entry</i>, and false otherwise.
  181. *
  182. * @return true if the entry identified by the given alias is a
  183. * <i>key entry</i>, false otherwise.
  184. */
  185. public abstract boolean engineIsKeyEntry(String alias);
  186. /**
  187. * Returns true if the entry identified by the given alias is a
  188. * <i>trusted certificate entry</i>, and false otherwise.
  189. *
  190. * @return true if the entry identified by the given alias is a
  191. * <i>trusted certificate entry</i>, false otherwise.
  192. */
  193. public abstract boolean engineIsCertificateEntry(String alias);
  194. /**
  195. * Returns the (alias) name of the first keystore entry whose certificate
  196. * matches the given certificate.
  197. *
  198. * <p>This method attempts to match the given certificate with each
  199. * keystore entry. If the entry being considered
  200. * is a <i>trusted certificate entry</i>, the given certificate is
  201. * compared to that entry's certificate. If the entry being considered is
  202. * a <i>key entry</i>, the given certificate is compared to the first
  203. * element of that entry's certificate chain (if a chain exists).
  204. *
  205. * @param cert the certificate to match with.
  206. *
  207. * @return the (alias) name of the first entry with matching certificate,
  208. * or null if no such entry exists in this keystore.
  209. */
  210. public abstract String engineGetCertificateAlias(Certificate cert);
  211. /**
  212. * Stores this keystore to the given output stream, and protects its
  213. * integrity with the given password.
  214. *
  215. * @param stream the output stream to which this keystore is written.
  216. * @param password the password to generate the keystore integrity check
  217. *
  218. * @exception IOException if there was an I/O problem with data
  219. * @exception NoSuchAlgorithmException if the appropriate data integrity
  220. * algorithm could not be found
  221. * @exception CertificateException if any of the certificates included in
  222. * the keystore data could not be stored
  223. */
  224. public abstract void engineStore(OutputStream stream, char[] password)
  225. throws IOException, NoSuchAlgorithmException, CertificateException;
  226. /**
  227. * Loads the keystore from the given input stream.
  228. *
  229. * <p>If a password is given, it is used to check the integrity of the
  230. * keystore data. Otherwise, the integrity of the keystore is not checked.
  231. *
  232. * @param stream the input stream from which the keystore is loaded
  233. * @param password the (optional) password used to check the integrity of
  234. * the keystore.
  235. *
  236. * @exception IOException if there is an I/O or format problem with the
  237. * keystore data
  238. * @exception NoSuchAlgorithmException if the algorithm used to check
  239. * the integrity of the keystore cannot be found
  240. * @exception CertificateException if any of the certificates in the
  241. * keystore could not be loaded
  242. */
  243. public abstract void engineLoad(InputStream stream, char[] password)
  244. throws IOException, NoSuchAlgorithmException, CertificateException;
  245. }