CertPath.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* CertPath.java -- a sequence of certificates
  2. Copyright (C) 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.cert;
  32. import java.io.ByteArrayInputStream;
  33. import java.io.NotSerializableException;
  34. import java.io.ObjectStreamException;
  35. import java.io.Serializable;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. /**
  39. * This class represents an immutable sequence, or path, of security
  40. * certificates. The path type must match the type of each certificate in the
  41. * path, or in other words, for all instances of cert in a certpath object,
  42. * <code>cert.getType().equals(certpath.getType())</code> will return true.
  43. *
  44. * <p>Since this class is immutable, it is thread-safe. During serialization,
  45. * the path is consolidated into a {@link CertPathRep}, which preserves the
  46. * data regardless of the underlying implementation of the path.
  47. *
  48. * @author Eric Blake <ebb9@email.byu.edu>
  49. * @since 1.4
  50. * @status updated to 1.4
  51. */
  52. public abstract class CertPath implements Serializable
  53. {
  54. /**
  55. * The serialized representation of a path.
  56. *
  57. * @author Eric Blake <ebb9@email.byu.edu>
  58. */
  59. protected static class CertPathRep implements Serializable
  60. {
  61. /**
  62. * Compatible with JDK 1.4+.
  63. */
  64. private static final long serialVersionUID = 3015633072427920915L;
  65. /**
  66. * The certificate type.
  67. *
  68. * @serial the type of the certificate path
  69. */
  70. private final String type;
  71. /**
  72. * The encoded form of the path.
  73. *
  74. * @serial the encoded form
  75. */
  76. private final byte[] data;
  77. /**
  78. * Create the new serial representation.
  79. *
  80. * @param type the path type
  81. * @param data the encoded path data
  82. */
  83. protected CertPathRep(String type, byte[] data)
  84. {
  85. this.type = type;
  86. this.data = data;
  87. }
  88. /**
  89. * Decode the data into an actual {@link CertPath} upon deserialization.
  90. *
  91. * @return the replacement object
  92. * @throws ObjectStreamException if replacement fails
  93. */
  94. protected Object readResolve() throws ObjectStreamException
  95. {
  96. try
  97. {
  98. return CertificateFactory.getInstance(type)
  99. .generateCertPath(new ByteArrayInputStream(data));
  100. }
  101. catch (CertificateException e)
  102. {
  103. throw (ObjectStreamException)
  104. new NotSerializableException("java.security.cert.CertPath: "
  105. + type).initCause(e);
  106. }
  107. }
  108. } // class CertPathRep
  109. /**
  110. * Compatible with JDK 1.4+.
  111. */
  112. private static final long serialVersionUID = 6068470306649138683L;
  113. /**
  114. * The path type.
  115. *
  116. * @serial the type of all certificates in this path
  117. */
  118. private final String type;
  119. /**
  120. * Create a certificate path with the given type. Most code should use
  121. * {@link CertificateFactory} to create CertPaths.
  122. *
  123. * @param type the type of the path
  124. */
  125. protected CertPath(String type)
  126. {
  127. this.type = type;
  128. }
  129. /**
  130. * Get the (non-null) type of all certificates in the path.
  131. *
  132. * @return the path certificate type
  133. */
  134. public String getType()
  135. {
  136. return type;
  137. }
  138. /**
  139. * Get an immutable iterator over the path encodings (all String names),
  140. * starting with the default encoding. The iterator will throw an
  141. * <code>UnsupportedOperationException</code> if an attempt is made to
  142. * remove items from the list.
  143. *
  144. * @return the iterator of supported encodings in the path
  145. */
  146. public abstract Iterator getEncodings();
  147. /**
  148. * Compares this path to another for semantic equality. To be equal, both
  149. * must be instances of CertPath, with the same type, and identical
  150. * certificate lists. Overriding classes must not change this behavior.
  151. *
  152. * @param o the object to compare to
  153. * @return true if the two are equal
  154. */
  155. public boolean equals(Object o)
  156. {
  157. if (! (o instanceof CertPath))
  158. return false;
  159. CertPath cp = (CertPath) o;
  160. return type.equals(cp.type)
  161. && getCertificates().equals(cp.getCertificates());
  162. }
  163. /**
  164. * Returns the hashcode of this certificate path. This is defined as:<br>
  165. * <code>31 * getType().hashCode() + getCertificates().hashCode()</code>.
  166. *
  167. * @return the hashcode
  168. */
  169. public int hashCode()
  170. {
  171. return 31 * type.hashCode() + getCertificates().hashCode();
  172. }
  173. public String toString()
  174. {
  175. List l = getCertificates();
  176. int size = l.size();
  177. int i = 0;
  178. StringBuffer result = new StringBuffer(type);
  179. result.append(" Cert Path: length = ").append(size).append(".\n[\n");
  180. while (--size >= 0)
  181. result.append(l.get(i++)).append('\n');
  182. return result.append("\n]").toString();
  183. }
  184. /**
  185. * Returns the encoded form of this path, via the default encoding.
  186. *
  187. * @return the encoded form
  188. * @throws CertificateEncodingException if encoding fails
  189. */
  190. public abstract byte[] getEncoded() throws CertificateEncodingException;
  191. /**
  192. * Returns the encoded form of this path, via the specified encoding.
  193. *
  194. * @param encoding the encoding to use
  195. * @return the encoded form
  196. * @throws CertificateEncodingException if encoding fails or does not exist
  197. */
  198. public abstract byte[] getEncoded(String encoding)
  199. throws CertificateEncodingException;
  200. /**
  201. * Returns the immutable, thread-safe list of certificates in this path.
  202. *
  203. * @return the list of certificates, non-null but possibly empty
  204. */
  205. public abstract List getCertificates();
  206. /**
  207. * Serializes the path in its encoded form, to ensure reserialization with
  208. * the appropriate factory object without worrying about list implementation.
  209. * The result will always be an instance of {@link CertPathRep}.
  210. *
  211. * @return the replacement object
  212. * @throws ObjectStreamException if the replacement creation fails
  213. */
  214. protected Object writeReplace() throws ObjectStreamException
  215. {
  216. try
  217. {
  218. return new CertPathRep(type, getEncoded());
  219. }
  220. catch (CertificateEncodingException e)
  221. {
  222. throw (ObjectStreamException)
  223. new NotSerializableException("java.security.cert.CertPath: "
  224. + type).initCause(e);
  225. }
  226. }
  227. } // class CertPath