jdk/src/share/classes/sun/security/provider/certpath/X509CertPath.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10336 0bb1999251f8
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2006, 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 sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.pkcs.ContentInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.pkcs.PKCS7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.pkcs.SignerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * A {@link java.security.cert.CertPath CertPath} (certification path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * consisting exclusively of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link java.security.cert.X509Certificate X509Certificate}s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * By convention, X.509 <code>CertPath</code>s are stored from target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to trust anchor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * That is, the issuer of one certificate is the subject of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * one. However, unvalidated X.509 <code>CertPath</code>s may not follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * this convention. PKIX <code>CertPathValidator</code>s will detect any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * departure from this convention and throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>CertPathValidatorException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class X509CertPath extends CertPath {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final long serialVersionUID = 4989800333263052980L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * List of certificates in this chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private List<X509Certificate> certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The names of our encodings.  PkiPath is the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final String COUNT_ENCODING = "count";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final String PKCS7_ENCODING = "PKCS7";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final String PKIPATH_ENCODING = "PkiPath";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * List of supported encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final Collection<String> encodingList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        List<String> list = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        list.add(PKIPATH_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        list.add(PKCS7_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        encodingList = Collections.unmodifiableCollection(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates an <code>X509CertPath</code> from a <code>List</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <code>X509Certificate</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The certificates are copied out of the supplied <code>List</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param certs a <code>List</code> of <code>X509Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception CertificateException if <code>certs</code> contains an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *                      that is not an <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public X509CertPath(List<? extends Certificate> certs) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Ensure that the List contains only X509Certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (Object obj : (List<?>)certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (obj instanceof X509Certificate == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    ("List is not all X509Certificates: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    + obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Assumes that the resulting List is thread-safe. This is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // because we ensure that it cannot be modified after construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // and the methods in the Sun JDK 1.4 implementation of ArrayList that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // allow read-only access are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.certs = Collections.unmodifiableList(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                new ArrayList<X509Certificate>((List<X509Certificate>)certs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Creates an <code>X509CertPath</code>, reading the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * from an <code>InputStream</code>. The data is assumed to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the default encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @exception CertificateException if an exception occurs while decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public X509CertPath(InputStream is) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this(is, PKIPATH_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Creates an <code>X509CertPath</code>, reading the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * from an InputStream. The data is assumed to be in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param encoding the encoding used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception CertificateException if an exception occurs while decoding or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *   the encoding requested is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public X509CertPath(InputStream is, String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (PKIPATH_ENCODING.equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            certs = parsePKIPATH(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else if (PKCS7_ENCODING.equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            certs = parsePKCS7(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new CertificateException("unsupported encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Parse a PKIPATH format CertPath from an InputStream. Return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * unmodifiable List of the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return an unmodifiable List of the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @exception CertificateException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static List<X509Certificate> parsePKIPATH(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        List<X509Certificate> certList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        CertificateFactory certFac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new CertificateException("input stream is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            DerInputStream dis = new DerInputStream(readAllBytes(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            DerValue[] seq = dis.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (seq.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                return Collections.<X509Certificate>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            certFac = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            certList = new ArrayList<X509Certificate>(seq.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // append certs in reverse order (target to trust anchor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            for (int i = seq.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                certList.add((X509Certificate)certFac.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    (new ByteArrayInputStream(seq[i].toByteArray())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return Collections.unmodifiableList(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            CertificateException ce = new CertificateException("IOException" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                " parsing PkiPath data: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ce.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Parse a PKCS#7 format CertPath from an InputStream. Return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * unmodifiable List of the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return an unmodifiable List of the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception CertificateException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static List<X509Certificate> parsePKCS7(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        List<X509Certificate> certList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new CertificateException("input stream is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (is.markSupported() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // Copy the entire input stream into an InputStream that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                // support mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                is = new ByteArrayInputStream(readAllBytes(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            PKCS7 pkcs7 = new PKCS7(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            X509Certificate[] certArray = pkcs7.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // certs are optional in PKCS #7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (certArray != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                certList = Arrays.asList(certArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // no certs provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                certList = new ArrayList<X509Certificate>(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new CertificateException("IOException parsing PKCS7 data: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                        ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Assumes that the resulting List is thread-safe. This is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // because we ensure that it cannot be modified after construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // and the methods in the Sun JDK 1.4 implementation of ArrayList that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // allow read-only access are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return Collections.unmodifiableList(certList);
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
     * Reads the entire contents of an InputStream into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the bytes read from the InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private static byte[] readAllBytes(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        while ((n = is.read(buffer)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * default encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @exception CertificateEncodingException if an encoding error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public byte[] getEncoded() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // @@@ Should cache the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return encodePKIPATH();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Encode the CertPath using PKIPATH format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return a byte array containing the binary encoding of the PkiPath object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception CertificateEncodingException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private byte[] encodePKIPATH() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        ListIterator<X509Certificate> li = certs.listIterator(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // encode certs in reverse order (trust anchor to target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // according to PkiPath format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            while (li.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                X509Certificate cert = li.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                // check for duplicate cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (certs.lastIndexOf(cert) != certs.indexOf(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    throw new CertificateEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        ("Duplicate Certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                // get encoded certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                bytes.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // Wrap the data in a SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            derout.write(DerValue.tag_SequenceOf, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return derout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
           CertificateEncodingException ce = new CertificateEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                ("IOException encoding PkiPath data: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
           ce.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
           throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Encode the CertPath using PKCS#7 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return a byte array containing the binary encoding of the PKCS#7 object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception CertificateEncodingException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private byte[] encodePKCS7() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        PKCS7 p7 = new PKCS7(new AlgorithmId[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                             new ContentInfo(ContentInfo.DATA_OID, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                             certs.toArray(new X509Certificate[certs.size()]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                             new SignerInfo[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            p7.encodeSignedData(derout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new CertificateEncodingException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return derout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * specified encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param encoding the name of the encoding to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception CertificateEncodingException if an encoding error occurs or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *   the encoding requested is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public byte[] getEncoded(String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (PKIPATH_ENCODING.equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return encodePKIPATH();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else if (PKCS7_ENCODING.equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return encodePKCS7();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            throw new CertificateEncodingException("unsupported encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns the encodings supported by this certification path, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * default encoding first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return an <code>Iterator</code> over the names of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *         encodings (as Strings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public static Iterator<String> getEncodingsStatic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return encodingList.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Returns an iteration of the encodings supported by this certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * path, with the default encoding first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Attempts to modify the returned <code>Iterator</code> via its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <code>remove</code> method result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <code>UnsupportedOperationException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return an <code>Iterator</code> over the names of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *         encodings (as Strings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public Iterator<String> getEncodings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return getEncodingsStatic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns the list of certificates in this certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * The <code>List</code> returned must be immutable and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @return an immutable <code>List</code> of <code>X509Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         (may be empty, but not null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public List<X509Certificate> getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}