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