jdk/src/share/classes/java/security/cert/CertPath.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8152 94e5966bdf22
child 18551 882a3948c6e6
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8152
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security.cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.NotSerializableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * An immutable sequence of certificates (a certification path).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This is an abstract class that defines the methods common to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>CertPath</code>s. Subclasses can handle different kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * certificates (X.509, PGP, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * All <code>CertPath</code> objects have a type, a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>Certificate</code>s, and one or more supported encodings. Because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>CertPath</code> class is immutable, a <code>CertPath</code> cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * change in any externally visible way after being constructed. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * stipulation applies to all public fields and methods of this class and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * added or overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The type is a <code>String</code> that identifies the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>Certificate</code>s in the certification path. For each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * certificate <code>cert</code> in a certification path <code>certPath</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>cert.getType().equals(certPath.getType())</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The list of <code>Certificate</code>s is an ordered <code>List</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * zero or more <code>Certificate</code>s. This <code>List</code> and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * of the <code>Certificate</code>s contained in it must be immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Each <code>CertPath</code> object must support one or more encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * so that the object can be translated into a byte array for storage or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * transmission to other parties. Preferably, these encodings should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * well-documented standards (such as PKCS#7). One of the encodings supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * by a <code>CertPath</code> is considered the default encoding. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * encoding is used if no encoding is explicitly requested (for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@link #getEncoded() getEncoded()} method, for instance).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * All <code>CertPath</code> objects are also <code>Serializable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>CertPath</code> objects are resolved into an alternate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * {@link CertPathRep CertPathRep} object during serialization. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * a <code>CertPath</code> object to be serialized into an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * representation regardless of its underlying implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <code>CertPath</code> objects can be created with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <code>CertificateFactory</code> or they can be returned by other classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * such as a <code>CertPathBuilder</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * By convention, X.509 <code>CertPath</code>s (consisting of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <code>X509Certificate</code>s), are ordered starting with the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * certificate and ending with a certificate issued by the trust anchor. That
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * is, the issuer of one certificate is the subject of the following one. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * certificate representing the {@link TrustAnchor TrustAnchor} should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * included in the certification path. Unvalidated X.509 <code>CertPath</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * may not follow these conventions. PKIX <code>CertPathValidator</code>s will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * detect any departure from these conventions that cause the certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * path to be invalid and throw a <code>CertPathValidatorException</code>.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    86
 *
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    87
 * <p> Every implementation of the Java platform is required to support the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    88
 * following standard <code>CertPath</code> encodings:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    89
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    90
 * <li><tt>PKCS7</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    91
 * <li><tt>PkiPath</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    92
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    93
 * These encodings are described in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    94
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    95
 * CertPath Encodings section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    96
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    97
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    98
 * other encodings are supported.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * All <code>CertPath</code> objects must be thread-safe. That is, multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * threads may concurrently invoke the methods defined in this class on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * single <code>CertPath</code> object (or more than one) with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * ill effects. This is also true for the <code>List</code> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <code>CertPath.getCertificates</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Requiring <code>CertPath</code> objects to be immutable and thread-safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * allows them to be passed around to various pieces of code without worrying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * about coordinating access.  Providing this thread-safety is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * generally not difficult, since the <code>CertPath</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <code>List</code> objects in question are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @see CertificateFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @see CertPathBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
public abstract class CertPath implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final long serialVersionUID = 6068470306649138683L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private String type;        // the type of certificates in this chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates a <code>CertPath</code> of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * This constructor is protected because most users should use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>CertificateFactory</code> to create <code>CertPath</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param type the standard name of the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>Certificate</code>s in this path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected CertPath(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Returns the type of <code>Certificate</code>s in this certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * path. This is the same string that would be returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * {@link java.security.cert.Certificate#getType() cert.getType()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * for all <code>Certificate</code>s in the certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return the type of <code>Certificate</code>s in this certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * path (never null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns an iteration of the encodings supported by this certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * path, with the default encoding first. Attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code>Iterator</code> via its <code>remove</code> method result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>UnsupportedOperationException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return an <code>Iterator</code> over the names of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *         encodings (as Strings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public abstract Iterator<String> getEncodings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Compares this certification path for equality with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * object. Two <code>CertPath</code>s are equal if and only if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * types are equal and their certificate <code>List</code>s (and by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * implication the <code>Certificate</code>s in those <code>List</code>s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * are equal. A <code>CertPath</code> is never equal to an object that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * not a <code>CertPath</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * This algorithm is implemented by this method. If it is overridden,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the behavior specified here must be maintained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param other the object to test for equality with this certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return true if the specified object is equal to this certification path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (! (other instanceof CertPath))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        CertPath otherCP = (CertPath) other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (! otherCP.getType().equals(type))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        List<? extends Certificate> thisCertList = this.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        List<? extends Certificate> otherCertList = otherCP.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return(thisCertList.equals(otherCertList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns the hashcode for this certification path. The hash code of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * a certification path is defined to be the result of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * calculation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *  hashCode = path.getType().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *  hashCode = 31*hashCode + path.getCertificates().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This ensures that <code>path1.equals(path2)</code> implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <code>path1.hashCode()==path2.hashCode()</code> for any two certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * paths, <code>path1</code> and <code>path2</code>, as required by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * general contract of <code>Object.hashCode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the hashcode value for this certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int hashCode = type.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        hashCode = 31*hashCode + getCertificates().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns a string representation of this certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * This calls the <code>toString</code> method on each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <code>Certificate</code>s in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return a string representation of this certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Iterator<? extends Certificate> stringIterator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                        getCertificates().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        sb.append("\n" + type + " Cert Path: length = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            + getCertificates().size() + ".\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        sb.append("[\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        while (stringIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            sb.append("=========================================="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                + "===============Certificate " + i + " start.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            Certificate stringCert = stringIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            sb.append(stringCert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            sb.append("\n========================================"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                + "=================Certificate " + i + " end.\n\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        sb.append("\n]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns the encoded form of this certification path, using the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception CertificateEncodingException if an encoding error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public abstract byte[] getEncoded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        throws CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * specified encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param encoding the name of the encoding to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @exception CertificateEncodingException if an encoding error occurs or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *   the encoding requested is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public abstract byte[] getEncoded(String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        throws CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns the list of certificates in this certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * The <code>List</code> returned must be immutable and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return an immutable <code>List</code> of <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *         (may be empty, but not null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public abstract List<? extends Certificate> getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Replaces the <code>CertPath</code> to be serialized with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <code>CertPathRep</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return the <code>CertPathRep</code> to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws ObjectStreamException if a <code>CertPathRep</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * representing this certification path could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    protected Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return new CertPathRep(type, getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            NotSerializableException nse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    ("java.security.cert.CertPath: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            nse.initCause(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Alternate <code>CertPath</code> class for serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    protected static class CertPathRep implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        private static final long serialVersionUID = 3015633072427920915L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        /** The Certificate type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        /** The encoded form of the cert path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        private byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * Creates a <code>CertPathRep</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * type and encoded form of a certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * @param type the standard name of a <code>CertPath</code> type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * @param data the encoded form of the certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        protected CertPathRep(String type, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * Returns a <code>CertPath</code> constructed from the type and data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @return the resolved <code>CertPath</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * @throws ObjectStreamException if a <code>CertPath</code> could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        protected Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                CertificateFactory cf = CertificateFactory.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return cf.generateCertPath(new ByteArrayInputStream(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                NotSerializableException nse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        ("java.security.cert.CertPath: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                nse.initCause(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}