0N/A/*
3909N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security.cert;
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.NotSerializableException;
0N/Aimport java.io.ObjectStreamException;
0N/Aimport java.io.Serializable;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/A
0N/A/**
0N/A * An immutable sequence of certificates (a certification path).
0N/A * <p>
0N/A * This is an abstract class that defines the methods common to all
0N/A * <code>CertPath</code>s. Subclasses can handle different kinds of
0N/A * certificates (X.509, PGP, etc.).
0N/A * <p>
0N/A * All <code>CertPath</code> objects have a type, a list of
0N/A * <code>Certificate</code>s, and one or more supported encodings. Because the
0N/A * <code>CertPath</code> class is immutable, a <code>CertPath</code> cannot
0N/A * change in any externally visible way after being constructed. This
0N/A * stipulation applies to all public fields and methods of this class and any
0N/A * added or overridden by subclasses.
0N/A * <p>
0N/A * The type is a <code>String</code> that identifies the type of
0N/A * <code>Certificate</code>s in the certification path. For each
0N/A * certificate <code>cert</code> in a certification path <code>certPath</code>,
0N/A * <code>cert.getType().equals(certPath.getType())</code> must be
0N/A * <code>true</code>.
0N/A * <p>
0N/A * The list of <code>Certificate</code>s is an ordered <code>List</code> of
0N/A * zero or more <code>Certificate</code>s. This <code>List</code> and all
0N/A * of the <code>Certificate</code>s contained in it must be immutable.
0N/A * <p>
0N/A * Each <code>CertPath</code> object must support one or more encodings
0N/A * so that the object can be translated into a byte array for storage or
0N/A * transmission to other parties. Preferably, these encodings should be
0N/A * well-documented standards (such as PKCS#7). One of the encodings supported
0N/A * by a <code>CertPath</code> is considered the default encoding. This
0N/A * encoding is used if no encoding is explicitly requested (for the
0N/A * {@link #getEncoded() getEncoded()} method, for instance).
0N/A * <p>
0N/A * All <code>CertPath</code> objects are also <code>Serializable</code>.
0N/A * <code>CertPath</code> objects are resolved into an alternate
0N/A * {@link CertPathRep CertPathRep} object during serialization. This allows
0N/A * a <code>CertPath</code> object to be serialized into an equivalent
0N/A * representation regardless of its underlying implementation.
0N/A * <p>
0N/A * <code>CertPath</code> objects can be created with a
0N/A * <code>CertificateFactory</code> or they can be returned by other classes,
0N/A * such as a <code>CertPathBuilder</code>.
0N/A * <p>
0N/A * By convention, X.509 <code>CertPath</code>s (consisting of
0N/A * <code>X509Certificate</code>s), are ordered starting with the target
0N/A * certificate and ending with a certificate issued by the trust anchor. That
0N/A * is, the issuer of one certificate is the subject of the following one. The
0N/A * certificate representing the {@link TrustAnchor TrustAnchor} should not be
0N/A * included in the certification path. Unvalidated X.509 <code>CertPath</code>s
0N/A * may not follow these conventions. PKIX <code>CertPathValidator</code>s will
0N/A * detect any departure from these conventions that cause the certification
0N/A * path to be invalid and throw a <code>CertPathValidatorException</code>.
3465N/A *
3465N/A * <p> Every implementation of the Java platform is required to support the
3465N/A * following standard <code>CertPath</code> encodings:
3465N/A * <ul>
3465N/A * <li><tt>PKCS7</tt></li>
3465N/A * <li><tt>PkiPath</tt></li>
3465N/A * </ul>
3465N/A * These encodings are described in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
3465N/A * CertPath Encodings section</a> of the
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation.
3465N/A * Consult the release documentation for your implementation to see if any
3465N/A * other encodings are supported.
0N/A * <p>
0N/A * <b>Concurrent Access</b>
0N/A * <p>
0N/A * All <code>CertPath</code> objects must be thread-safe. That is, multiple
0N/A * threads may concurrently invoke the methods defined in this class on a
0N/A * single <code>CertPath</code> object (or more than one) with no
0N/A * ill effects. This is also true for the <code>List</code> returned by
0N/A * <code>CertPath.getCertificates</code>.
0N/A * <p>
0N/A * Requiring <code>CertPath</code> objects to be immutable and thread-safe
0N/A * allows them to be passed around to various pieces of code without worrying
0N/A * about coordinating access. Providing this thread-safety is
0N/A * generally not difficult, since the <code>CertPath</code> and
0N/A * <code>List</code> objects in question are immutable.
0N/A *
0N/A * @see CertificateFactory
0N/A * @see CertPathBuilder
0N/A *
0N/A * @author Yassir Elley
0N/A * @since 1.4
0N/A */
0N/Apublic abstract class CertPath implements Serializable {
0N/A
0N/A private static final long serialVersionUID = 6068470306649138683L;
0N/A
0N/A private String type; // the type of certificates in this chain
0N/A
0N/A /**
0N/A * Creates a <code>CertPath</code> of the specified type.
0N/A * <p>
0N/A * This constructor is protected because most users should use a
0N/A * <code>CertificateFactory</code> to create <code>CertPath</code>s.
0N/A *
0N/A * @param type the standard name of the type of
0N/A * <code>Certificate</code>s in this path
0N/A */
0N/A protected CertPath(String type) {
0N/A this.type = type;
0N/A }
0N/A
0N/A /**
0N/A * Returns the type of <code>Certificate</code>s in this certification
0N/A * path. This is the same string that would be returned by
0N/A * {@link java.security.cert.Certificate#getType() cert.getType()}
0N/A * for all <code>Certificate</code>s in the certification path.
0N/A *
0N/A * @return the type of <code>Certificate</code>s in this certification
0N/A * path (never null)
0N/A */
0N/A public String getType() {
0N/A return type;
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration of the encodings supported by this certification
0N/A * path, with the default encoding first. Attempts to modify the returned
0N/A * <code>Iterator</code> via its <code>remove</code> method result in an
0N/A * <code>UnsupportedOperationException</code>.
0N/A *
0N/A * @return an <code>Iterator</code> over the names of the supported
0N/A * encodings (as Strings)
0N/A */
0N/A public abstract Iterator<String> getEncodings();
0N/A
0N/A /**
0N/A * Compares this certification path for equality with the specified
0N/A * object. Two <code>CertPath</code>s are equal if and only if their
0N/A * types are equal and their certificate <code>List</code>s (and by
0N/A * implication the <code>Certificate</code>s in those <code>List</code>s)
0N/A * are equal. A <code>CertPath</code> is never equal to an object that is
0N/A * not a <code>CertPath</code>.
0N/A * <p>
0N/A * This algorithm is implemented by this method. If it is overridden,
0N/A * the behavior specified here must be maintained.
0N/A *
0N/A * @param other the object to test for equality with this certification path
0N/A * @return true if the specified object is equal to this certification path,
0N/A * false otherwise
0N/A */
0N/A public boolean equals(Object other) {
0N/A if (this == other)
0N/A return true;
0N/A
0N/A if (! (other instanceof CertPath))
0N/A return false;
0N/A
0N/A CertPath otherCP = (CertPath) other;
0N/A if (! otherCP.getType().equals(type))
0N/A return false;
0N/A
0N/A List<? extends Certificate> thisCertList = this.getCertificates();
0N/A List<? extends Certificate> otherCertList = otherCP.getCertificates();
0N/A return(thisCertList.equals(otherCertList));
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this certification path. The hash code of
0N/A * a certification path is defined to be the result of the following
0N/A * calculation:
0N/A * <pre><code>
0N/A * hashCode = path.getType().hashCode();
0N/A * hashCode = 31*hashCode + path.getCertificates().hashCode();
0N/A * </code></pre>
0N/A * This ensures that <code>path1.equals(path2)</code> implies that
0N/A * <code>path1.hashCode()==path2.hashCode()</code> for any two certification
0N/A * paths, <code>path1</code> and <code>path2</code>, as required by the
0N/A * general contract of <code>Object.hashCode</code>.
0N/A *
0N/A * @return the hashcode value for this certification path
0N/A */
0N/A public int hashCode() {
0N/A int hashCode = type.hashCode();
0N/A hashCode = 31*hashCode + getCertificates().hashCode();
0N/A return hashCode;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this certification path.
0N/A * This calls the <code>toString</code> method on each of the
0N/A * <code>Certificate</code>s in the path.
0N/A *
0N/A * @return a string representation of this certification path
0N/A */
0N/A public String toString() {
0N/A StringBuffer sb = new StringBuffer();
0N/A Iterator<? extends Certificate> stringIterator =
0N/A getCertificates().iterator();
0N/A
0N/A sb.append("\n" + type + " Cert Path: length = "
0N/A + getCertificates().size() + ".\n");
0N/A sb.append("[\n");
0N/A int i = 1;
0N/A while (stringIterator.hasNext()) {
0N/A sb.append("=========================================="
0N/A + "===============Certificate " + i + " start.\n");
0N/A Certificate stringCert = stringIterator.next();
0N/A sb.append(stringCert.toString());
0N/A sb.append("\n========================================"
0N/A + "=================Certificate " + i + " end.\n\n\n");
0N/A i++;
0N/A }
0N/A
0N/A sb.append("\n]");
0N/A return sb.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns the encoded form of this certification path, using the default
0N/A * encoding.
0N/A *
0N/A * @return the encoded bytes
0N/A * @exception CertificateEncodingException if an encoding error occurs
0N/A */
0N/A public abstract byte[] getEncoded()
0N/A throws CertificateEncodingException;
0N/A
0N/A /**
0N/A * Returns the encoded form of this certification path, using the
0N/A * specified encoding.
0N/A *
0N/A * @param encoding the name of the encoding to use
0N/A * @return the encoded bytes
0N/A * @exception CertificateEncodingException if an encoding error occurs or
0N/A * the encoding requested is not supported
0N/A */
0N/A public abstract byte[] getEncoded(String encoding)
0N/A throws CertificateEncodingException;
0N/A
0N/A /**
0N/A * Returns the list of certificates in this certification path.
0N/A * The <code>List</code> returned must be immutable and thread-safe.
0N/A *
0N/A * @return an immutable <code>List</code> of <code>Certificate</code>s
0N/A * (may be empty, but not null)
0N/A */
0N/A public abstract List<? extends Certificate> getCertificates();
0N/A
0N/A /**
0N/A * Replaces the <code>CertPath</code> to be serialized with a
0N/A * <code>CertPathRep</code> object.
0N/A *
0N/A * @return the <code>CertPathRep</code> to be serialized
0N/A *
0N/A * @throws ObjectStreamException if a <code>CertPathRep</code> object
0N/A * representing this certification path could not be created
0N/A */
0N/A protected Object writeReplace() throws ObjectStreamException {
0N/A try {
0N/A return new CertPathRep(type, getEncoded());
0N/A } catch (CertificateException ce) {
0N/A NotSerializableException nse =
0N/A new NotSerializableException
0N/A ("java.security.cert.CertPath: " + type);
0N/A nse.initCause(ce);
0N/A throw nse;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Alternate <code>CertPath</code> class for serialization.
0N/A * @since 1.4
0N/A */
0N/A protected static class CertPathRep implements Serializable {
0N/A
0N/A private static final long serialVersionUID = 3015633072427920915L;
0N/A
0N/A /** The Certificate type */
0N/A private String type;
0N/A /** The encoded form of the cert path */
0N/A private byte[] data;
0N/A
0N/A /**
0N/A * Creates a <code>CertPathRep</code> with the specified
0N/A * type and encoded form of a certification path.
0N/A *
0N/A * @param type the standard name of a <code>CertPath</code> type
0N/A * @param data the encoded form of the certification path
0N/A */
0N/A protected CertPathRep(String type, byte[] data) {
0N/A this.type = type;
0N/A this.data = data;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>CertPath</code> constructed from the type and data.
0N/A *
0N/A * @return the resolved <code>CertPath</code> object
0N/A *
0N/A * @throws ObjectStreamException if a <code>CertPath</code> could not
0N/A * be constructed
0N/A */
0N/A protected Object readResolve() throws ObjectStreamException {
0N/A try {
0N/A CertificateFactory cf = CertificateFactory.getInstance(type);
0N/A return cf.generateCertPath(new ByteArrayInputStream(data));
0N/A } catch (CertificateException ce) {
0N/A NotSerializableException nse =
0N/A new NotSerializableException
0N/A ("java.security.cert.CertPath: " + type);
0N/A nse.initCause(ce);
0N/A throw nse;
0N/A }
0N/A }
0N/A }
0N/A}