jdk/src/share/classes/sun/security/provider/certpath/X509CertPath.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 10358 5a4ec0a5f7ee
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
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 sun.security.provider.certpath;
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.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.pkcs.ContentInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.pkcs.PKCS7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.pkcs.SignerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * A {@link java.security.cert.CertPath CertPath} (certification path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * consisting exclusively of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link java.security.cert.X509Certificate X509Certificate}s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * By convention, X.509 <code>CertPath</code>s are stored from target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * to trust anchor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * That is, the issuer of one certificate is the subject of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * one. However, unvalidated X.509 <code>CertPath</code>s may not follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * this convention. PKIX <code>CertPathValidator</code>s will detect any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * departure from this convention and throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>CertPathValidatorException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class X509CertPath extends CertPath {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final long serialVersionUID = 4989800333263052980L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * List of certificates in this chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private List<X509Certificate> certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The names of our encodings.  PkiPath is the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final String COUNT_ENCODING = "count";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final String PKCS7_ENCODING = "PKCS7";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final String PKIPATH_ENCODING = "PkiPath";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * List of supported encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final Collection<String> encodingList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        List<String> list = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        list.add(PKIPATH_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        list.add(PKCS7_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        encodingList = Collections.unmodifiableCollection(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Creates an <code>X509CertPath</code> from a <code>List</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <code>X509Certificate</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The certificates are copied out of the supplied <code>List</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param certs a <code>List</code> of <code>X509Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @exception CertificateException if <code>certs</code> contains an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *                      that is not an <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   103
    @SuppressWarnings("unchecked")
2
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
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   108
        for (Certificate obj : certs) {
2
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
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   150
        switch (encoding) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   151
            case PKIPATH_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   152
                certs = parsePKIPATH(is);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   153
                break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   154
            case PKCS7_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   155
                certs = parsePKCS7(is);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   156
                break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   157
            default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   158
                throw new CertificateException("unsupported encoding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Parse a PKIPATH format CertPath from an InputStream. Return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * unmodifiable List of the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return an unmodifiable List of the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception CertificateException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static List<X509Certificate> parsePKIPATH(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        List<X509Certificate> certList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        CertificateFactory certFac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new CertificateException("input stream is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            DerInputStream dis = new DerInputStream(readAllBytes(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            DerValue[] seq = dis.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (seq.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return Collections.<X509Certificate>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            certFac = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            certList = new ArrayList<X509Certificate>(seq.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // append certs in reverse order (target to trust anchor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            for (int i = seq.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                certList.add((X509Certificate)certFac.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    (new ByteArrayInputStream(seq[i].toByteArray())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return Collections.unmodifiableList(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   198
            throw new CertificateException("IOException parsing PkiPath data: "
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   199
                    + ioe, ioe);
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Parse a PKCS#7 format CertPath from an InputStream. Return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * unmodifiable List of the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return an unmodifiable List of the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception CertificateException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private static List<X509Certificate> parsePKCS7(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        List<X509Certificate> certList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new CertificateException("input stream is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (is.markSupported() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                // Copy the entire input stream into an InputStream that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // support mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                is = new ByteArrayInputStream(readAllBytes(is));
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   224
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            PKCS7 pkcs7 = new PKCS7(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            X509Certificate[] certArray = pkcs7.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            // certs are optional in PKCS #7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (certArray != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                certList = Arrays.asList(certArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                // no certs provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                certList = new ArrayList<X509Certificate>(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new CertificateException("IOException parsing PKCS7 data: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                        ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Assumes that the resulting List is thread-safe. This is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // because we ensure that it cannot be modified after construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // and the methods in the Sun JDK 1.4 implementation of ArrayList that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // allow read-only access are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return Collections.unmodifiableList(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Reads the entire contents of an InputStream into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return the bytes read from the InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static byte[] readAllBytes(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        while ((n = is.read(buffer)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * default encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception CertificateEncodingException if an encoding error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public byte[] getEncoded() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // @@@ Should cache the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return encodePKIPATH();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Encode the CertPath using PKIPATH format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return a byte array containing the binary encoding of the PkiPath object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception CertificateEncodingException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private byte[] encodePKIPATH() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        ListIterator<X509Certificate> li = certs.listIterator(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // encode certs in reverse order (trust anchor to target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // according to PkiPath format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            while (li.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                X509Certificate cert = li.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                // check for duplicate cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (certs.lastIndexOf(cert) != certs.indexOf(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    throw new CertificateEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        ("Duplicate Certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                // get encoded certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                bytes.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // Wrap the data in a SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            derout.write(DerValue.tag_SequenceOf, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return derout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   305
           throw new CertificateEncodingException("IOException encoding " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   306
                   "PkiPath data: " + ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
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
     * Encode the CertPath using PKCS#7 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return a byte array containing the binary encoding of the PKCS#7 object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @exception CertificateEncodingException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private byte[] encodePKCS7() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        PKCS7 p7 = new PKCS7(new AlgorithmId[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                             new ContentInfo(ContentInfo.DATA_OID, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                             certs.toArray(new X509Certificate[certs.size()]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                             new SignerInfo[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            p7.encodeSignedData(derout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new CertificateEncodingException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return derout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * specified encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param encoding the name of the encoding to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @exception CertificateEncodingException if an encoding error occurs or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *   the encoding requested is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public byte[] getEncoded(String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throws CertificateEncodingException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   341
        switch (encoding) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   342
            case PKIPATH_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   343
                return encodePKIPATH();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   344
            case PKCS7_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   345
                return encodePKCS7();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   346
            default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   347
                throw new CertificateEncodingException("unsupported encoding");
2
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
}