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