GnuPrivateKeyring.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* GnuPrivateKeyring.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a 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 of the License, or (at
  7. your option) 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; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. 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 gnu.javax.crypto.keyring;
  32. import gnu.java.security.Configuration;
  33. import gnu.java.security.Registry;
  34. import java.io.DataInputStream;
  35. import java.io.DataOutputStream;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.io.OutputStream;
  39. import java.security.Key;
  40. import java.security.PublicKey;
  41. import java.security.UnrecoverableKeyException;
  42. import java.security.cert.Certificate;
  43. import java.util.Date;
  44. import java.util.Iterator;
  45. import java.util.logging.Level;
  46. import java.util.logging.Logger;
  47. /**
  48. *
  49. */
  50. public class GnuPrivateKeyring
  51. extends BaseKeyring
  52. implements IPrivateKeyring
  53. {
  54. private static final Logger log = Logger.getLogger(GnuPrivateKeyring.class.getName());
  55. public static final int USAGE = Registry.GKR_PRIVATE_KEYS
  56. | Registry.GKR_PUBLIC_CREDENTIALS;
  57. protected String mac;
  58. protected int maclen;
  59. protected String cipher;
  60. protected String mode;
  61. protected int keylen;
  62. public GnuPrivateKeyring(String mac, int maclen, String cipher, String mode,
  63. int keylen)
  64. {
  65. keyring = new PasswordAuthenticatedEntry(mac, maclen, new Properties());
  66. keyring2 = new CompressedEntry(new Properties());
  67. keyring.add(keyring2);
  68. this.mac = mac;
  69. this.maclen = maclen;
  70. this.cipher = cipher;
  71. this.mode = mode;
  72. this.keylen = keylen;
  73. }
  74. public GnuPrivateKeyring()
  75. {
  76. this("HMAC-SHA-1", 20, "AES", "OFB", 16);
  77. }
  78. public boolean containsPrivateKey(String alias)
  79. {
  80. if (Configuration.DEBUG)
  81. log.entering(this.getClass().getName(), "containsPrivateKey", alias);
  82. boolean result = false;
  83. if (containsAlias(alias))
  84. for (Iterator it = get(alias).iterator(); it.hasNext();)
  85. if (it.next() instanceof PasswordAuthenticatedEntry)
  86. {
  87. result = true;
  88. break;
  89. }
  90. if (Configuration.DEBUG)
  91. log.exiting(this.getClass().getName(), "containsPrivateKey",
  92. Boolean.valueOf(result));
  93. return result;
  94. }
  95. public Key getPrivateKey(String alias, char[] password)
  96. throws UnrecoverableKeyException
  97. {
  98. if (Configuration.DEBUG)
  99. log.entering(this.getClass().getName(), "getPrivateKey", alias);
  100. Key result = null;
  101. if (containsAlias(alias))
  102. {
  103. PasswordAuthenticatedEntry e1 = null;
  104. for (Iterator it = get(alias).iterator(); it.hasNext();)
  105. {
  106. Entry e = (Entry) it.next();
  107. if (Configuration.DEBUG)
  108. log.finest("Entry: " + e);
  109. if (e instanceof PasswordAuthenticatedEntry)
  110. {
  111. e1 = (PasswordAuthenticatedEntry) e;
  112. break;
  113. }
  114. }
  115. if (Configuration.DEBUG)
  116. log.fine("e1 = " + e1);
  117. if (e1 != null)
  118. {
  119. try
  120. {
  121. e1.verify(password);
  122. }
  123. catch (Exception e)
  124. {
  125. if (Configuration.DEBUG)
  126. log.throwing(this.getClass().getName(), "getPrivateKey", e);
  127. throw new UnrecoverableKeyException("authentication failed");
  128. }
  129. PasswordEncryptedEntry e2 = null;
  130. for (Iterator it = e1.getEntries().iterator(); it.hasNext();)
  131. {
  132. Entry e = (Entry) it.next();
  133. if (e instanceof PasswordEncryptedEntry)
  134. {
  135. e2 = (PasswordEncryptedEntry) e;
  136. break;
  137. }
  138. }
  139. if (e2 != null)
  140. {
  141. try
  142. {
  143. e2.decrypt(password);
  144. }
  145. catch (Exception e)
  146. {
  147. log.throwing(this.getClass().getName(), "getPrivateKey", e);
  148. throw new UnrecoverableKeyException("decryption failed");
  149. }
  150. for (Iterator it = e2.get(alias).iterator(); it.hasNext();)
  151. {
  152. Entry e = (Entry) it.next();
  153. if (e instanceof PrivateKeyEntry)
  154. {
  155. result = ((PrivateKeyEntry) e).getKey();
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. if (Configuration.DEBUG)
  163. log.exiting(this.getClass().getName(), "getPrivateKey",
  164. result == null ? "null" : result.getClass().getName());
  165. return result;
  166. }
  167. public void putPrivateKey(String alias, Key key, char[] password)
  168. {
  169. if (Configuration.DEBUG)
  170. log.entering(this.getClass().getName(), "putPrivateKey",
  171. new Object[] { alias, key.getClass().getName() });
  172. if (! containsPrivateKey(alias))
  173. {
  174. alias = fixAlias(alias);
  175. Properties p = new Properties();
  176. p.put("alias", alias);
  177. PrivateKeyEntry pke = new PrivateKeyEntry(key, new Date(), p);
  178. if (Configuration.DEBUG)
  179. log.fine("About to encrypt the key...");
  180. PasswordEncryptedEntry enc;
  181. enc = new PasswordEncryptedEntry(cipher, mode, keylen, new Properties());
  182. enc.add(pke);
  183. try
  184. {
  185. enc.encode(null, password);
  186. }
  187. catch (IOException x)
  188. {
  189. if (Configuration.DEBUG)
  190. log.log(Level.FINE, "Exception while encrypting the key. "
  191. + "Rethrow as IllegalArgumentException", x);
  192. throw new IllegalArgumentException(x.toString());
  193. }
  194. if (Configuration.DEBUG)
  195. log.fine("About to authenticate the encrypted key...");
  196. PasswordAuthenticatedEntry auth;
  197. auth = new PasswordAuthenticatedEntry(mac, maclen, new Properties());
  198. auth.add(enc);
  199. try
  200. {
  201. auth.encode(null, password);
  202. }
  203. catch (IOException x)
  204. {
  205. if (Configuration.DEBUG)
  206. log.log(Level.FINE, "Exception while authenticating the encrypted "
  207. + "key. Rethrow as IllegalArgumentException", x);
  208. throw new IllegalArgumentException(x.toString());
  209. }
  210. keyring.add(auth);
  211. }
  212. else if (Configuration.DEBUG)
  213. log.fine("Keyring already contains alias: " + alias);
  214. if (Configuration.DEBUG)
  215. log.exiting(this.getClass().getName(), "putPrivateKey");
  216. }
  217. public boolean containsPublicKey(String alias)
  218. {
  219. if (Configuration.DEBUG)
  220. log.entering(this.getClass().getName(), "containsPublicKey", alias);
  221. boolean result = false;
  222. if (containsAlias(alias))
  223. for (Iterator it = get(alias).iterator(); it.hasNext();)
  224. if (it.next() instanceof PublicKeyEntry)
  225. {
  226. result = true;
  227. break;
  228. }
  229. if (Configuration.DEBUG)
  230. log.exiting(this.getClass().getName(), "containsPublicKey",
  231. Boolean.valueOf(result));
  232. return result;
  233. }
  234. public PublicKey getPublicKey(String alias)
  235. {
  236. if (Configuration.DEBUG)
  237. log.entering(this.getClass().getName(), "getPublicKey", alias);
  238. PublicKey result = null;
  239. if (containsAlias(alias))
  240. for (Iterator it = get(alias).iterator(); it.hasNext();)
  241. {
  242. Entry e = (Entry) it.next();
  243. if (e instanceof PublicKeyEntry)
  244. {
  245. result = ((PublicKeyEntry) e).getKey();
  246. break;
  247. }
  248. }
  249. if (Configuration.DEBUG)
  250. log.exiting(this.getClass().getName(), "getPublicKey",
  251. result == null ? "null" : result.getClass().getName());
  252. return result;
  253. }
  254. public void putPublicKey(String alias, PublicKey key)
  255. {
  256. if (Configuration.DEBUG)
  257. log.entering(this.getClass().getName(), "putPublicKey",
  258. new Object[] { alias, key.getClass().getName() });
  259. if (! containsPublicKey(alias))
  260. {
  261. Properties p = new Properties();
  262. p.put("alias", fixAlias(alias));
  263. add(new PublicKeyEntry(key, new Date(), p));
  264. }
  265. else if (Configuration.DEBUG)
  266. log.fine("Keyring already contains alias: " + alias);
  267. if (Configuration.DEBUG)
  268. log.exiting(this.getClass().getName(), "putPublicKey");
  269. }
  270. public boolean containsCertPath(String alias)
  271. {
  272. if (Configuration.DEBUG)
  273. log.entering(this.getClass().getName(), "containsCertPath", alias);
  274. boolean result = false;
  275. if (containsAlias(alias))
  276. for (Iterator it = get(alias).iterator(); it.hasNext();)
  277. if (it.next() instanceof CertPathEntry)
  278. {
  279. result = true;
  280. break;
  281. }
  282. if (Configuration.DEBUG)
  283. log.exiting(this.getClass().getName(), "containsCertPath",
  284. Boolean.valueOf(result));
  285. return result;
  286. }
  287. public Certificate[] getCertPath(String alias)
  288. {
  289. if (Configuration.DEBUG)
  290. log.entering(this.getClass().getName(), "getCertPath", alias);
  291. Certificate[] result = null;
  292. if (containsAlias(alias))
  293. for (Iterator it = get(alias).iterator(); it.hasNext();)
  294. {
  295. Entry e = (Entry) it.next();
  296. if (e instanceof CertPathEntry)
  297. {
  298. result = ((CertPathEntry) e).getCertPath();
  299. break;
  300. }
  301. }
  302. if (Configuration.DEBUG)
  303. log.exiting(this.getClass().getName(), "getCertPath", result);
  304. return result;
  305. }
  306. public void putCertPath(String alias, Certificate[] path)
  307. {
  308. if (Configuration.DEBUG)
  309. log.entering(this.getClass().getName(), "putCertPath",
  310. new Object[] { alias, path });
  311. if (! containsCertPath(alias))
  312. {
  313. Properties p = new Properties();
  314. p.put("alias", fixAlias(alias));
  315. add(new CertPathEntry(path, new Date(), p));
  316. }
  317. else if (Configuration.DEBUG)
  318. log.fine("Keyring already contains alias: " + alias);
  319. if (Configuration.DEBUG)
  320. log.exiting(this.getClass().getName(), "putCertPath");
  321. }
  322. protected void load(InputStream in, char[] password) throws IOException
  323. {
  324. if (Configuration.DEBUG)
  325. log.entering(this.getClass().getName(), "load");
  326. if (in.read() != USAGE)
  327. throw new MalformedKeyringException("incompatible keyring usage");
  328. if (in.read() != PasswordAuthenticatedEntry.TYPE)
  329. throw new MalformedKeyringException(
  330. "expecting password-authenticated entry tag");
  331. keyring = PasswordAuthenticatedEntry.decode(new DataInputStream(in), password);
  332. if (Configuration.DEBUG)
  333. log.exiting(this.getClass().getName(), "load");
  334. }
  335. protected void store(OutputStream out, char[] password) throws IOException
  336. {
  337. if (Configuration.DEBUG)
  338. log.entering(this.getClass().getName(), "store");
  339. out.write(USAGE);
  340. keyring.encode(new DataOutputStream(out), password);
  341. if (Configuration.DEBUG)
  342. log.exiting(this.getClass().getName(), "store");
  343. }
  344. }