1. /*
  2. * @(#)CertPath.java 1.6 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security.cert;
  8. import java.io.ByteArrayInputStream;
  9. import java.io.NotSerializableException;
  10. import java.io.ObjectStreamException;
  11. import java.io.Serializable;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. /**
  15. * An immutable sequence of certificates (a certification path).
  16. * <p>
  17. * This is an abstract class that defines the methods common to all
  18. * <code>CertPath</code>s. Subclasses can handle different kinds of
  19. * certificates (X.509, PGP, etc.).
  20. * <p>
  21. * All <code>CertPath</code> objects have a type, a list of
  22. * <code>Certificate</code>s, and one or more supported encodings. Because the
  23. * <code>CertPath</code> class is immutable, a <code>CertPath</code> cannot
  24. * change in any externally visible way after being constructed. This
  25. * stipulation applies to all public fields and methods of this class and any
  26. * added or overridden by subclasses.
  27. * <p>
  28. * The type is a <code>String</code> that identifies the type of
  29. * <code>Certificate</code>s in the certification path. For each
  30. * certificate <code>cert</code> in a certification path <code>certPath</code>,
  31. * <code>cert.getType().equals(certPath.getType())</code> must be
  32. * <code>true</code>.
  33. * <p>
  34. * The list of <code>Certificate</code>s is an ordered <code>List</code> of
  35. * zero or more <code>Certificate</code>s. This <code>List</code> and all
  36. * of the <code>Certificate</code>s contained in it must be immutable.
  37. * <p>
  38. * Each <code>CertPath</code> object must support one or more encodings
  39. * so that the object can be translated into a byte array for storage or
  40. * transmission to other parties. Preferably, these encodings should be
  41. * well-documented standards (such as PKCS#7). One of the encodings supported
  42. * by a <code>CertPath</code> is considered the default encoding. This
  43. * encoding is used if no encoding is explicitly requested (for the
  44. * {@link #getEncoded() getEncoded()} method, for instance).
  45. * <p>
  46. * All <code>CertPath</code> objects are also <code>Serializable</code>.
  47. * <code>CertPath</code> objects are resolved into an alternate
  48. * {@link CertPathRep CertPathRep} object during serialization. This allows
  49. * a <code>CertPath</code> object to be serialized into an equivalent
  50. * representation regardless of its underlying implementation.
  51. * <p>
  52. * <code>CertPath</code> objects can be created with a
  53. * <code>CertificateFactory</code> or they can be returned by other classes,
  54. * such as a <code>CertPathBuilder</code>.
  55. * <p>
  56. * By convention, X.509 <code>CertPath</code>s (consisting of
  57. * <code>X509Certificate</code>s), are ordered starting with the target
  58. * certificate and ending with a certificate issued by the trust anchor. That
  59. * is, the issuer of one certificate is the subject of the following one. The
  60. * certificate representing the {@link TrustAnchor TrustAnchor} should not be
  61. * included in the certification path. Unvalidated X.509 <code>CertPath</code>s
  62. * may not follow these conventions. PKIX <code>CertPathValidator</code>s will
  63. * detect any departure from these conventions that cause the certification
  64. * path to be invalid and throw a <code>CertPathValidatorException</code>.
  65. * <p>
  66. * <b>Concurrent Access</b>
  67. * <p>
  68. * All <code>CertPath</code> objects must be thread-safe. That is, multiple
  69. * threads may concurrently invoke the methods defined in this class on a
  70. * single <code>CertPath</code> object (or more than one) with no
  71. * ill effects. This is also true for the <code>List</code> returned by
  72. * <code>CertPath.getCertificates</code>.
  73. * <p>
  74. * Requiring <code>CertPath</code> objects to be immutable and thread-safe
  75. * allows them to be passed around to various pieces of code without worrying
  76. * about coordinating access. Providing this thread-safety is
  77. * generally not difficult, since the <code>CertPath</code> and
  78. * <code>List</code> objects in question are immutable.
  79. *
  80. * @see CertificateFactory
  81. * @see CertPathBuilder
  82. *
  83. * @version 1.6 01/23/03
  84. * @author Yassir Elley
  85. * @since 1.4
  86. */
  87. public abstract class CertPath implements Serializable {
  88. private String type; // the type of certificates in this chain
  89. /**
  90. * Creates a <code>CertPath</code> of the specified type.
  91. * <p>
  92. * This constructor is protected because most users should use a
  93. * <code>CertificateFactory</code> to create <code>CertPath</code>s.
  94. *
  95. * @param type the standard name of the type of
  96. * <code>Certificate</code>s in this path
  97. */
  98. protected CertPath(String type) {
  99. this.type = type;
  100. }
  101. /**
  102. * Returns the type of <code>Certificate</code>s in this certification
  103. * path. This is the same string that would be returned by
  104. * {@link java.security.cert.Certificate#getType() cert.getType()}
  105. * for all <code>Certificate</code>s in the certification path.
  106. *
  107. * @return the type of <code>Certificate</code>s in this certification
  108. * path (never null)
  109. */
  110. public String getType() {
  111. return type;
  112. }
  113. /**
  114. * Returns an iteration of the encodings supported by this certification
  115. * path, with the default encoding first. Attempts to modify the returned
  116. * <code>Iterator</code> via its <code>remove</code> method result in an
  117. * <code>UnsupportedOperationException</code>.
  118. *
  119. * @return an <code>Iterator</code> over the names of the supported
  120. * encodings (as Strings)
  121. */
  122. public abstract Iterator getEncodings();
  123. /**
  124. * Compares this certification path for equality with the specified
  125. * object. Two <code>CertPath</code>s are equal if and only if their
  126. * types are equal and their certificate <code>List</code>s (and by
  127. * implication the <code>Certificate</code>s in those <code>List</code>s)
  128. * are equal. A <code>CertPath</code> is never equal to an object that is
  129. * not a <code>CertPath</code>.
  130. * <p>
  131. * This algorithm is implemented by this method. If it is overridden,
  132. * the behavior specified here must be maintained.
  133. *
  134. * @param other the object to test for equality with this certification path
  135. * @return true if the specified object is equal to this certification path,
  136. * false otherwise
  137. */
  138. public boolean equals(Object other) {
  139. if (this == other)
  140. return true;
  141. if (! (other instanceof CertPath))
  142. return false;
  143. CertPath otherCP = (CertPath) other;
  144. if (! otherCP.getType().equals(type))
  145. return false;
  146. List thisCertList = this.getCertificates();
  147. List otherCertList = otherCP.getCertificates();
  148. return(thisCertList.equals(otherCertList));
  149. }
  150. /**
  151. * Returns the hashcode for this certification path. The hash code of
  152. * a certification path is defined to be the result of the following
  153. * calculation:
  154. * <pre><code>
  155. * hashCode = path.getType().hashCode();
  156. * hashCode = 31*hashCode + path.getCertificates().hashCode();
  157. * </code></pre>
  158. * This ensures that <code>path1.equals(path2)</code> implies that
  159. * <code>path1.hashCode()==path2.hashCode()</code> for any two certification
  160. * paths, <code>path1</code> and <code>path2</code>, as required by the
  161. * general contract of <code>Object.hashCode</code>.
  162. *
  163. * @return the hashcode value for this certification path
  164. */
  165. public int hashCode() {
  166. int hashCode = type.hashCode();
  167. hashCode = 31*hashCode + getCertificates().hashCode();
  168. return hashCode;
  169. }
  170. /**
  171. * Returns a string representation of this certification path.
  172. * This calls the <code>toString</code> method on each of the
  173. * <code>Certificate</code>s in the path.
  174. *
  175. * @return a string representation of this certification path
  176. */
  177. public String toString() {
  178. StringBuffer sb = new StringBuffer();
  179. Iterator stringIterator = getCertificates().iterator();
  180. sb.append("\n" + type + " Cert Path: length = "
  181. + getCertificates().size() + ".\n");
  182. sb.append("[\n");
  183. int i = 1;
  184. while (stringIterator.hasNext()) {
  185. sb.append("=========================================="
  186. + "===============Certificate " + i + " start.\n");
  187. Certificate stringCert = (Certificate) stringIterator.next();
  188. sb.append(stringCert.toString());
  189. sb.append("\n========================================"
  190. + "=================Certificate " + i + " end.\n\n\n");
  191. i++;
  192. }
  193. sb.append("\n]");
  194. return sb.toString();
  195. }
  196. /**
  197. * Returns the encoded form of this certification path, using the default
  198. * encoding.
  199. *
  200. * @return the encoded bytes
  201. * @exception CertificateEncodingException if an encoding error occurs
  202. */
  203. public abstract byte[] getEncoded()
  204. throws CertificateEncodingException;
  205. /**
  206. * Returns the encoded form of this certification path, using the
  207. * specified encoding.
  208. *
  209. * @param encoding the name of the encoding to use
  210. * @return the encoded bytes
  211. * @exception CertificateEncodingException if an encoding error occurs or
  212. * the encoding requested is not supported
  213. */
  214. public abstract byte[] getEncoded(String encoding)
  215. throws CertificateEncodingException;
  216. /**
  217. * Returns the list of certificates in this certification path.
  218. * The <code>List</code> returned must be immutable and thread-safe.
  219. *
  220. * @return an immutable <code>List</code> of <code>Certificate</code>s
  221. * (may be empty, but not null)
  222. */
  223. public abstract List getCertificates();
  224. /**
  225. * Replaces the <code>CertPath</code> to be serialized with a
  226. * <code>CertPathRep</code> object.
  227. *
  228. * @return the <code>CertPathRep</code> to be serialized
  229. *
  230. * @throws ObjectStreamException if a <code>CertPathRep</code> object
  231. * representing this certification path could not be created
  232. */
  233. protected Object writeReplace() throws ObjectStreamException {
  234. try {
  235. return new CertPathRep(type, getEncoded());
  236. } catch (CertificateException ce) {
  237. NotSerializableException nse =
  238. new NotSerializableException
  239. ("java.security.cert.CertPath: " + type);
  240. nse.initCause(ce);
  241. throw nse;
  242. }
  243. }
  244. /**
  245. * Alternate <code>CertPath</code> class for serialization.
  246. */
  247. protected static class CertPathRep implements Serializable {
  248. /** The Certificate type */
  249. private String type;
  250. /** The encoded form of the cert path */
  251. private byte[] data;
  252. /**
  253. * Creates a <code>CertPathRep</code> with the specified
  254. * type and encoded form of a certification path.
  255. *
  256. * @param type the standard name of a <code>CertPath</code> type
  257. * @param data the encoded form of the certification path
  258. */
  259. protected CertPathRep(String type, byte[] data) {
  260. this.type = type;
  261. this.data = data;
  262. }
  263. /**
  264. * Returns a <code>CertPath</code> constructed from the type and data.
  265. *
  266. * @return the resolved <code>CertPath</code> object
  267. *
  268. * @throws ObjectStreamException if a <code>CertPath</code> could not
  269. * be constructed
  270. */
  271. protected Object readResolve() throws ObjectStreamException {
  272. try {
  273. CertificateFactory cf = CertificateFactory.getInstance(type);
  274. return cf.generateCertPath(new ByteArrayInputStream(data));
  275. } catch (CertificateException ce) {
  276. NotSerializableException nse =
  277. new NotSerializableException
  278. ("java.security.cert.CertPath: " + type);
  279. nse.initCause(ce);
  280. throw nse;
  281. }
  282. }
  283. }
  284. }