GnuPublicKeyring.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* GnuPublicKeyring.java --
  2. Copyright (C) 2003, 2006, 2010 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.cert.Certificate;
  40. import java.util.Date;
  41. import java.util.Iterator;
  42. import java.util.logging.Logger;
  43. public class GnuPublicKeyring
  44. extends BaseKeyring
  45. implements IPublicKeyring
  46. {
  47. private static final Logger log = Configuration.DEBUG ?
  48. Logger.getLogger(GnuPublicKeyring.class.getName()) : null;
  49. public static final int USAGE = Registry.GKR_CERTIFICATES;
  50. public GnuPublicKeyring(String mac, int macLen)
  51. {
  52. keyring = new PasswordAuthenticatedEntry(mac, macLen, new Properties());
  53. keyring2 = new CompressedEntry(new Properties());
  54. keyring.add(keyring2);
  55. }
  56. public GnuPublicKeyring()
  57. {
  58. }
  59. public boolean containsCertificate(String alias)
  60. {
  61. if (Configuration.DEBUG)
  62. log.entering(this.getClass().getName(), "containsCertificate", alias);
  63. boolean result = false;
  64. if (containsAlias(alias))
  65. for (Iterator it = get(alias).iterator(); it.hasNext();)
  66. if (it.next() instanceof CertificateEntry)
  67. {
  68. result = true;
  69. break;
  70. }
  71. if (Configuration.DEBUG)
  72. log.exiting(this.getClass().getName(), "containsCertificate",
  73. Boolean.valueOf(result));
  74. return result;
  75. }
  76. public Certificate getCertificate(String alias)
  77. {
  78. if (Configuration.DEBUG)
  79. log.entering(this.getClass().getName(), "getCertificate", alias);
  80. Certificate result = null;
  81. if (containsAlias(alias))
  82. for (Iterator it = get(alias).iterator(); it.hasNext();)
  83. {
  84. Entry e = (Entry) it.next();
  85. if (e instanceof CertificateEntry)
  86. {
  87. result = ((CertificateEntry) e).getCertificate();
  88. break;
  89. }
  90. }
  91. if (Configuration.DEBUG)
  92. log.exiting(this.getClass().getName(), "getCertificate", result);
  93. return result;
  94. }
  95. public void putCertificate(String alias, Certificate cert)
  96. {
  97. if (Configuration.DEBUG)
  98. log.entering(this.getClass().getName(), "putCertificate",
  99. new Object[] { alias, cert });
  100. if (! containsCertificate(alias))
  101. {
  102. Properties p = new Properties();
  103. p.put("alias", fixAlias(alias));
  104. add(new CertificateEntry(cert, new Date(), p));
  105. }
  106. else if (Configuration.DEBUG)
  107. log.fine("Keyring already contains alias: " + alias);
  108. if (Configuration.DEBUG)
  109. log.exiting(this.getClass().getName(), "putCertificate");
  110. }
  111. protected void load(InputStream in, char[] password) throws IOException
  112. {
  113. if (Configuration.DEBUG)
  114. log.entering(this.getClass().getName(), "load");
  115. if (in.read() != USAGE)
  116. throw new MalformedKeyringException("incompatible keyring usage");
  117. if (in.read() != PasswordAuthenticatedEntry.TYPE)
  118. throw new MalformedKeyringException(
  119. "expecting password-authenticated entry tag");
  120. DataInputStream dis = new DataInputStream(in);
  121. keyring = PasswordAuthenticatedEntry.decode(dis, password);
  122. if (Configuration.DEBUG)
  123. log.exiting(this.getClass().getName(), "load");
  124. }
  125. protected void store(OutputStream out, char[] password) throws IOException
  126. {
  127. if (Configuration.DEBUG)
  128. log.entering(this.getClass().getName(), "store");
  129. out.write(USAGE);
  130. keyring.encode(new DataOutputStream(out), password);
  131. if (Configuration.DEBUG)
  132. log.exiting(this.getClass().getName(), "store");
  133. }
  134. }