KeyStore.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* KeyStore.java --- Key Store Class
  2. Copyright (C) 1999, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. import java.io.InputStream;
  33. import java.io.IOException;
  34. import java.io.OutputStream;
  35. import java.security.cert.CertificateException;
  36. import java.util.Date;
  37. import java.util.Enumeration;
  38. /**
  39. Keystore represents an in-memory collection of keys and
  40. certificates. There are two types of entries:
  41. * Key Entry
  42. This type of keystore entry store sensitive crytographic key
  43. information in a protected format.Typically this is a secret
  44. key or a private key with a certificate chain.
  45. * Trusted Ceritificate Entry
  46. This type of keystore entry contains a single public key
  47. certificate belonging to annother entity. It is called trusted
  48. because the keystore owner trusts that the certificates
  49. belongs to the subject (owner) of the certificate.
  50. The keystore contains an "alias" string for each entry.
  51. The structure and persistentence of the key store is not
  52. specified. Any method could be used to protect sensitive
  53. (private or secret) keys. Smart cards or integrated
  54. cryptographic engines could be used or the keystore could
  55. be simply stored in a file.
  56. */
  57. public class KeyStore
  58. {
  59. private KeyStoreSpi keyStoreSpi;
  60. private Provider provider;
  61. private String type;
  62. /**
  63. Creates an instance of KeyStore
  64. @param keyStoreSpi A KeyStore engine to use
  65. @param provider A provider to use
  66. @param type The type of KeyStore
  67. */
  68. protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
  69. {
  70. this.keyStoreSpi = keyStoreSpi;
  71. this.provider = provider;
  72. this.type = type;
  73. }
  74. /**
  75. Gets an instance of the KeyStore class representing
  76. the specified keystore. If the type is not
  77. found then, it throws KeyStoreException.
  78. @param type the type of keystore to choose
  79. @return a KeyStore repesenting the desired type
  80. @throws KeyStoreException if the type of keystore is not implemented by providers
  81. */
  82. public static KeyStore getInstance(String type) throws KeyStoreException
  83. {
  84. Provider[] p = Security.getProviders();
  85. for (int i = 0; i < p.length; i++)
  86. {
  87. String classname = p[i].getProperty("KeyStore." + type);
  88. if (classname != null)
  89. return getInstance(classname, type, p[i]);
  90. }
  91. throw new KeyStoreException(type);
  92. }
  93. /**
  94. Gets an instance of the KeyStore class representing
  95. the specified key store from the specified provider.
  96. If the type is not found then, it throws KeyStoreException.
  97. If the provider is not found, then it throws
  98. NoSuchProviderException.
  99. @param type the type of keystore to choose
  100. @param provider the provider name
  101. @return a KeyStore repesenting the desired type
  102. @throws KeyStoreException if the type of keystore is not
  103. implemented by the given provider
  104. @throws NoSuchProviderException if the provider is not found
  105. @throws IllegalArgumentException if the provider string is
  106. null or empty
  107. */
  108. public static KeyStore getInstance(String type, String provider)
  109. throws KeyStoreException, NoSuchProviderException
  110. {
  111. if (provider == null || provider.length() == 0)
  112. throw new IllegalArgumentException("Illegal provider");
  113. Provider p = Security.getProvider(provider);
  114. if (p == null)
  115. throw new NoSuchProviderException();
  116. return getInstance(p.getProperty("KeyStore." + type), type, p);
  117. }
  118. /**
  119. Gets an instance of the KeyStore class representing
  120. the specified key store from the specified provider.
  121. If the type is not found then, it throws KeyStoreException.
  122. If the provider is not found, then it throws
  123. NoSuchProviderException.
  124. @param type the type of keystore to choose
  125. @param provider the keystore provider
  126. @return a KeyStore repesenting the desired type
  127. @throws KeyStoreException if the type of keystore is not
  128. implemented by the given provider
  129. @throws IllegalArgumentException if the provider object is null
  130. @since 1.4
  131. */
  132. public static KeyStore getInstance(String type, Provider provider)
  133. throws KeyStoreException
  134. {
  135. if (provider == null)
  136. throw new IllegalArgumentException("Illegal provider");
  137. return getInstance(provider.getProperty("KeyStore." + type),
  138. type, provider);
  139. }
  140. private static KeyStore getInstance(String classname,
  141. String type,
  142. Provider provider)
  143. throws KeyStoreException
  144. {
  145. try
  146. {
  147. return new KeyStore((KeyStoreSpi) Class.forName(classname).
  148. newInstance(), provider, type);
  149. }
  150. catch (ClassNotFoundException cnfe)
  151. {
  152. throw new KeyStoreException("Class not found");
  153. }
  154. catch (InstantiationException ie)
  155. {
  156. throw new KeyStoreException("Class instantiation failed");
  157. }
  158. catch (IllegalAccessException iae)
  159. {
  160. throw new KeyStoreException("Illegal Access");
  161. }
  162. }
  163. /**
  164. Gets the provider that the class is from.
  165. @return the provider of this class
  166. */
  167. public final Provider getProvider()
  168. {
  169. return provider;
  170. }
  171. /**
  172. Returns the type of the KeyStore supported
  173. @return A string with the type of KeyStore
  174. */
  175. public final String getType()
  176. {
  177. return type;
  178. }
  179. /**
  180. Returns the key associated with given alias using the
  181. supplied password.
  182. @param alias an alias for the key to get
  183. @param password password to access key with
  184. @return the requested key, or null otherwise
  185. @throws NoSuchAlgorithmException if there is no algorithm
  186. for recovering the key
  187. @throws UnrecoverableKeyException key cannot be reocovered
  188. (wrong password).
  189. */
  190. public final Key getKey(String alias, char[]password)
  191. throws KeyStoreException, NoSuchAlgorithmException,
  192. UnrecoverableKeyException
  193. {
  194. return keyStoreSpi.engineGetKey(alias, password);
  195. }
  196. /**
  197. Gets a Certificate chain for the specified alias.
  198. @param alias the alias name
  199. @return a chain of Certificates ( ordered from the user's
  200. certificate to the Certificate Authority's ) or
  201. null if the alias does not exist or there is no
  202. certificate chain for the alias ( the alias refers
  203. to a trusted certificate entry or there is no entry).
  204. */
  205. public final java.security.cert.
  206. Certificate[] getCertificateChain(String alias) throws KeyStoreException
  207. {
  208. return keyStoreSpi.engineGetCertificateChain(alias);
  209. }
  210. /**
  211. Gets a Certificate for the specified alias.
  212. If there is a trusted certificate entry then that is returned.
  213. it there is a key entry with a certificate chain then the
  214. first certificate is return or else null.
  215. @param alias the alias name
  216. @return a Certificate or null if the alias does not exist
  217. or there is no certificate for the alias
  218. */
  219. public final java.security.cert.Certificate getCertificate(String alias)
  220. throws KeyStoreException
  221. {
  222. return keyStoreSpi.engineGetCertificate(alias);
  223. }
  224. /**
  225. Gets entry creation date for the specified alias.
  226. @param alias the alias name
  227. @returns the entry creation date or null
  228. */
  229. public final Date getCreationDate(String alias) throws KeyStoreException
  230. {
  231. return keyStoreSpi.engineGetCreationDate(alias);
  232. }
  233. /**
  234. Assign the key to the alias in the keystore, protecting it
  235. with the given password. It will overwrite an existing
  236. entry and if the key is a PrivateKey, also add the
  237. certificate chain representing the corresponding public key.
  238. @param alias the alias name
  239. @param key the key to add
  240. @password the password to protect with
  241. @param chain the certificate chain for the corresponding
  242. public key
  243. @throws KeyStoreException if it fails
  244. */
  245. public final void setKeyEntry(String alias, Key key, char[]password,
  246. java.security.cert.
  247. Certificate[]chain) throws KeyStoreException
  248. {
  249. keyStoreSpi.engineSetKeyEntry(alias, key, password, chain);
  250. }
  251. /**
  252. Assign the key to the alias in the keystore. It will overwrite
  253. an existing entry and if the key is a PrivateKey, also
  254. add the certificate chain representing the corresponding
  255. public key.
  256. @param alias the alias name
  257. @param key the key to add
  258. @param chain the certificate chain for the corresponding
  259. public key
  260. @throws KeyStoreException if it fails
  261. */
  262. public final void setKeyEntry(String alias, byte[]key,
  263. java.security.cert.
  264. Certificate[]chain) throws KeyStoreException
  265. {
  266. keyStoreSpi.engineSetKeyEntry(alias, key, chain);
  267. }
  268. /**
  269. Assign the certificate to the alias in the keystore. It
  270. will overwrite an existing entry.
  271. @param alias the alias name
  272. @param cert the certificate to add
  273. @throws KeyStoreException if it fails
  274. */
  275. public final void setCertificateEntry(String alias,
  276. java.security.cert.
  277. Certificate cert) throws
  278. KeyStoreException
  279. {
  280. keyStoreSpi.engineSetCertificateEntry(alias, cert);
  281. }
  282. /**
  283. Deletes the entry for the specified entry.
  284. @param alias the alias name
  285. @throws KeyStoreException if it fails
  286. */
  287. public final void deleteEntry(String alias) throws KeyStoreException
  288. {
  289. keyStoreSpi.engineDeleteEntry(alias);
  290. }
  291. /**
  292. Generates a list of all the aliases in the keystore.
  293. @return an Enumeration of the aliases
  294. */
  295. public final Enumeration aliases() throws KeyStoreException
  296. {
  297. return keyStoreSpi.engineAliases();
  298. }
  299. /**
  300. Determines if the keystore contains the specified alias.
  301. @param alias the alias name
  302. @return true if it contains the alias, false otherwise
  303. */
  304. public final boolean containsAlias(String alias) throws KeyStoreException
  305. {
  306. return keyStoreSpi.engineContainsAlias(alias);
  307. }
  308. /**
  309. Returns the number of entries in the keystore.
  310. @returns the number of keystore entries.
  311. */
  312. public final int size() throws KeyStoreException
  313. {
  314. return keyStoreSpi.engineSize();
  315. }
  316. /**
  317. Determines if the keystore contains a key entry for
  318. the specified alias.
  319. @param alias the alias name
  320. @return true if it is a key entry, false otherwise
  321. */
  322. public final boolean isKeyEntry(String alias) throws KeyStoreException
  323. {
  324. return keyStoreSpi.engineIsKeyEntry(alias);
  325. }
  326. /**
  327. Determines if the keystore contains a certificate entry for
  328. the specified alias.
  329. @param alias the alias name
  330. @return true if it is a certificate entry, false otherwise
  331. */
  332. public final boolean isCertificateEntry(String alias)
  333. throws KeyStoreException
  334. {
  335. return keyStoreSpi.engineIsCertificateEntry(alias);
  336. }
  337. /**
  338. Determines if the keystore contains the specified certificate
  339. entry and returns the alias.
  340. It checks every entry and for a key entry checks only the
  341. first certificate in the chain.
  342. @param cert Certificate to look for
  343. @return alias of first matching certificate, null if it
  344. does not exist.
  345. */
  346. public final String getCertificateAlias(java.security.cert.Certificate cert)
  347. throws KeyStoreException
  348. {
  349. return keyStoreSpi.engineGetCertificateAlias(cert);
  350. }
  351. /**
  352. Stores the keystore in the specified output stream and it
  353. uses the specified key it keep it secure.
  354. @param stream the output stream to save the keystore to
  355. @param password the password to protect the keystore integrity with
  356. @throws IOException if an I/O error occurs.
  357. @throws NoSuchAlgorithmException the data integrity algorithm
  358. used cannot be found.
  359. @throws CertificateException if any certificates could not be
  360. stored in the output stream.
  361. */
  362. public final void store(OutputStream stream, char[]password)
  363. throws KeyStoreException, IOException, NoSuchAlgorithmException,
  364. CertificateException
  365. {
  366. keyStoreSpi.engineStore(stream, password);
  367. }
  368. /**
  369. Loads the keystore from the specified input stream and it
  370. uses the specified password to check for integrity if supplied.
  371. @param stream the input stream to load the keystore from
  372. @param password the password to check the keystore integrity with
  373. @throws IOException if an I/O error occurs.
  374. @throws NoSuchAlgorithmException the data integrity algorithm
  375. used cannot be found.
  376. @throws CertificateException if any certificates could not be
  377. stored in the output stream.
  378. */
  379. public final void load(InputStream stream, char[]password)
  380. throws IOException, NoSuchAlgorithmException, CertificateException
  381. {
  382. keyStoreSpi.engineLoad(stream, password);
  383. }
  384. /**
  385. Returns the default KeyStore type. This method looks up the
  386. type in <JAVA_HOME>/lib/security/java.security with the
  387. property "keystore.type" or if that fails then "jks" .
  388. */
  389. public static final String getDefaultType()
  390. {
  391. String tmp;
  392. //Security reads every property in java.security so it
  393. //will return this property if it exists.
  394. tmp = Security.getProperty("keystore.type");
  395. if (tmp == null)
  396. tmp = "jks";
  397. return tmp;
  398. }
  399. }