src/java.base/share/classes/sun/security/provider/certpath/X509CertPath.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/sun/security/provider/certpath/X509CertPath.java@3317bb8137f4
child 57950 4612a3cfb927
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
     2
 * Copyright (c) 2000, 2012, 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;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
    36
import java.security.cert.CertPath;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
    39
2
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
 * 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 {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
    86
        List<String> list = new ArrayList<>(2);
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
10358
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   108
        //
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   109
        // Note; The certs parameter is not necessarily to be of Certificate
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   110
        // for some old code. For compatibility, to make sure the exception
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   111
        // is CertificateException, rather than ClassCastException, please
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   112
        // don't use
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   113
        //     for (Certificate obj : certs)
5a4ec0a5f7ee 7081817: test/sun/security/provider/certpath/X509CertPath/IllegalCertiticates.java failing
xuelei
parents: 10336
diff changeset
   114
        for (Object obj : certs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (obj instanceof X509Certificate == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    ("List is not all X509Certificates: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    + obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Assumes that the resulting List is thread-safe. This is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // because we ensure that it cannot be modified after construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // and the methods in the Sun JDK 1.4 implementation of ArrayList that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // allow read-only access are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.certs = Collections.unmodifiableList(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                new ArrayList<X509Certificate>((List<X509Certificate>)certs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Creates an <code>X509CertPath</code>, reading the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * from an <code>InputStream</code>. The data is assumed to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * the default encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception CertificateException if an exception occurs while decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public X509CertPath(InputStream is) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this(is, PKIPATH_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates an <code>X509CertPath</code>, reading the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * from an InputStream. The data is assumed to be in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param encoding the encoding used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception CertificateException if an exception occurs while decoding or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *   the encoding requested is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public X509CertPath(InputStream is, String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   156
        switch (encoding) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   157
            case PKIPATH_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   158
                certs = parsePKIPATH(is);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   159
                break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   160
            case PKCS7_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   161
                certs = parsePKCS7(is);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   162
                break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   163
            default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   164
                throw new CertificateException("unsupported encoding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Parse a PKIPATH format CertPath from an InputStream. Return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * unmodifiable List of the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return an unmodifiable List of the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception CertificateException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static List<X509Certificate> parsePKIPATH(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        List<X509Certificate> certList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        CertificateFactory certFac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new CertificateException("input stream is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            DerInputStream dis = new DerInputStream(readAllBytes(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            DerValue[] seq = dis.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (seq.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return Collections.<X509Certificate>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            certFac = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            certList = new ArrayList<X509Certificate>(seq.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // append certs in reverse order (target to trust anchor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (int i = seq.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                certList.add((X509Certificate)certFac.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    (new ByteArrayInputStream(seq[i].toByteArray())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return Collections.unmodifiableList(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   204
            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
   205
                    + ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Parse a PKCS#7 format CertPath from an InputStream. Return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * unmodifiable List of the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param is the <code>InputStream</code> to read the data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return an unmodifiable List of the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception CertificateException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private static List<X509Certificate> parsePKCS7(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        List<X509Certificate> certList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new CertificateException("input stream is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (is.markSupported() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                // Copy the entire input stream into an InputStream that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                // support mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                is = new ByteArrayInputStream(readAllBytes(is));
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   230
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            PKCS7 pkcs7 = new PKCS7(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            X509Certificate[] certArray = pkcs7.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // certs are optional in PKCS #7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (certArray != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                certList = Arrays.asList(certArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // no certs provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                certList = new ArrayList<X509Certificate>(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new CertificateException("IOException parsing PKCS7 data: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                        ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // Assumes that the resulting List is thread-safe. This is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // because we ensure that it cannot be modified after construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // and the methods in the Sun JDK 1.4 implementation of ArrayList that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // allow read-only access are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return Collections.unmodifiableList(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Reads the entire contents of an InputStream into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return the bytes read from the InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private static byte[] readAllBytes(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        while ((n = is.read(buffer)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * default encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @exception CertificateEncodingException if an encoding error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
   275
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public byte[] getEncoded() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // @@@ Should cache the encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return encodePKIPATH();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Encode the CertPath using PKIPATH format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return a byte array containing the binary encoding of the PkiPath object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception CertificateEncodingException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private byte[] encodePKIPATH() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        ListIterator<X509Certificate> li = certs.listIterator(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // encode certs in reverse order (trust anchor to target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // according to PkiPath format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            while (li.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                X509Certificate cert = li.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // check for duplicate cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (certs.lastIndexOf(cert) != certs.indexOf(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    throw new CertificateEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        ("Duplicate Certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // get encoded certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                bytes.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // Wrap the data in a SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            derout.write(DerValue.tag_SequenceOf, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return derout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   312
           throw new CertificateEncodingException("IOException encoding " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   313
                   "PkiPath data: " + ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Encode the CertPath using PKCS#7 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return a byte array containing the binary encoding of the PKCS#7 object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @exception CertificateEncodingException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private byte[] encodePKCS7() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        PKCS7 p7 = new PKCS7(new AlgorithmId[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                             new ContentInfo(ContentInfo.DATA_OID, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                             certs.toArray(new X509Certificate[certs.size()]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                             new SignerInfo[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            p7.encodeSignedData(derout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new CertificateEncodingException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return derout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Returns the encoded form of this certification path, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * specified encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param encoding the name of the encoding to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return the encoded bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @exception CertificateEncodingException if an encoding error occurs or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *   the encoding requested is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
   346
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public byte[] getEncoded(String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throws CertificateEncodingException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   349
        switch (encoding) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   350
            case PKIPATH_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   351
                return encodePKIPATH();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   352
            case PKCS7_ENCODING:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   353
                return encodePKCS7();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   354
            default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   355
                throw new CertificateEncodingException("unsupported encoding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns the encodings supported by this certification path, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * default encoding first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return an <code>Iterator</code> over the names of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *         encodings (as Strings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public static Iterator<String> getEncodingsStatic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return encodingList.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Returns an iteration of the encodings supported by this certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * path, with the default encoding first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Attempts to modify the returned <code>Iterator</code> via its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <code>remove</code> method result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>UnsupportedOperationException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return an <code>Iterator</code> over the names of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *         encodings (as Strings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
   381
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public Iterator<String> getEncodings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return getEncodingsStatic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Returns the list of certificates in this certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * The <code>List</code> returned must be immutable and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return an immutable <code>List</code> of <code>X509Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *         (may be empty, but not null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10358
diff changeset
   393
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public List<X509Certificate> getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
}