jdk/src/java.base/share/classes/sun/security/provider/certpath/SunCertPathBuilder.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 29264 5172066a2da6
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2014, 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: 3841
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: 3841
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: 3841
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PublicKey;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    32
import java.security.cert.*;
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
    33
import java.security.cert.CertPathValidatorException.BasicReason;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    34
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    45
import sun.security.provider.certpath.PKIX.BuilderParams;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    46
import static sun.security.x509.PKIXExtensions.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class is able to build certification paths in either the forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * or reverse directions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
    53
 * <p> If successful, it returns a certification path which has successfully
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * satisfied all the constraints and requirements specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * PKIXBuilderParameters object and has been validated according to the PKIX
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    56
 * path validation algorithm defined in RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> This implementation uses a depth-first search approach to finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * certification paths. If it comes to a point in which it cannot find
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * any more certificates leading to the target OR the path length is too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * it backtracks to previous paths until the target has been found or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * all possible paths have been exhausted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p> This implementation is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public final class SunCertPathBuilder extends CertPathBuilderSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * private objects shared by methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    77
    private BuilderParams buildParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private CertificateFactory cf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean pathCompleted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private PolicyNode policyTreeResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private TrustAnchor trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private PublicKey finalPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Create an instance of <code>SunCertPathBuilder</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @throws CertPathBuilderException if an error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public SunCertPathBuilder() throws CertPathBuilderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new CertPathBuilderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    95
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    96
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    97
    @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    98
    public CertPathChecker engineGetRevocationChecker() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    99
        return new RevocationChecker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Attempts to build a certification path using the Sun build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * algorithm from a trusted anchor(s) to a target subject, which must both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * be specified in the input parameter set. By default, this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * attempt to build in the forward direction. In order to build in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * reverse direction, the caller needs to pass in an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * SunCertPathBuilderParameters with the buildForward flag set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>The certification path that is constructed is validated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * according to the PKIX specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param params the parameter set for building a path. Must be an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  of <code>PKIXBuilderParameters</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return a certification path builder result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception CertPathBuilderException Exception thrown if builder is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *  unable to build a complete certification path from the trusted anchor(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *  to the target subject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws InvalidAlgorithmParameterException if the given parameters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  inappropriate for this certification path builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   122
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public CertPathBuilderResult engineBuild(CertPathParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws CertPathBuilderException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            debug.println("SunCertPathBuilder.engineBuild(" + params + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   130
        buildParams = PKIX.checkBuilderParams(params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   131
        return build();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   132
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   134
    private PKIXCertPathBuilderResult build() throws CertPathBuilderException {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   135
        List<List<Vertex>> adjList = new ArrayList<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   136
        PKIXCertPathBuilderResult result = buildCertPath(false, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                debug.println("SunCertPathBuilder.engineBuild: 2nd pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            adjList.clear();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   143
            result = buildCertPath(true, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                throw new SunCertPathBuilderException("unable to find valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    + "certification path to requested target",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   153
    private PKIXCertPathBuilderResult buildCertPath(boolean searchAllCertStores,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   154
                                                    List<List<Vertex>> adjList)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   155
        throws CertPathBuilderException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   156
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Init shared variables and build certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        pathCompleted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        trustAnchor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        finalPublicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        policyTreeResult = null;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   162
        LinkedList<X509Certificate> certPathList = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   164
            if (buildParams.buildForward()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                buildForward(adjList, certPathList, searchAllCertStores);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                buildReverse(adjList, certPathList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   169
        } catch (GeneralSecurityException | IOException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                debug.println("SunCertPathBuilder.engineBuild() exception in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    + "build");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new SunCertPathBuilderException("unable to find valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                + "certification path to requested target", e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // construct SunCertPathBuilderResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (pathCompleted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    debug.println("SunCertPathBuilder.engineBuild() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                  + "pathCompleted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                // we must return a certpath which has the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // as the first cert in the certpath - i.e. reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                Collections.reverse(certPathList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                return new SunCertPathBuilderResult(
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   193
                    cf.generateCertPath(certPathList), trustAnchor,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    policyTreeResult, finalPublicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   197
        } catch (CertificateException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                debug.println("SunCertPathBuilder.engineBuild() exception "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                              + "in wrap-up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new SunCertPathBuilderException("unable to find valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                + "certification path to requested target", e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return null;
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
     * Private build reverse method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void buildReverse(List<List<Vertex>> adjacencyList,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   215
                              LinkedList<X509Certificate> certPathList)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   216
        throws GeneralSecurityException, IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            debug.println("SunCertPathBuilder.buildReverse()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            debug.println("SunCertPathBuilder.buildReverse() InitialPolicies: "
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   221
                + buildParams.initialPolicies());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        ReverseState currentState = new ReverseState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        /* Initialize adjacency list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        adjacencyList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        adjacencyList.add(new LinkedList<Vertex>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * Perform a search using each trust anchor, until a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * path is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   233
        Iterator<TrustAnchor> iter = buildParams.trustAnchors().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            TrustAnchor anchor = iter.next();
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   236
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            /* check if anchor satisfies target constraints */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   238
            if (anchorIsTarget(anchor, buildParams.targetCertConstraints())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                this.trustAnchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                this.pathCompleted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                this.finalPublicKey = anchor.getTrustedCert().getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   245
            // skip anchor if it contains a DSA key with no DSA params
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   246
            X509Certificate trustedCert = anchor.getTrustedCert();
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   247
            PublicKey pubKey = trustedCert != null ? trustedCert.getPublicKey()
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   248
                                                   : anchor.getCAPublicKey();
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   249
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   250
            if (PKIX.isDSAPublicKeyWithoutParams(pubKey)) {
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   251
                continue;
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   252
            }
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   253
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            /* Initialize current state */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   255
            currentState.initState(buildParams);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   256
            currentState.updateState(anchor, buildParams);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   257
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   258
            currentState.algorithmChecker = new AlgorithmChecker(anchor);
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   259
            currentState.untrustedChecker = new UntrustedChecker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                depthFirstSearchReverse(null, currentState,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   262
                                        new ReverseBuilder(buildParams),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   263
                                        adjacencyList, certPathList);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   264
            } catch (GeneralSecurityException | IOException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // continue on error if more anchors to try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (iter.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // break out of loop if search is successful
12693
e3c056161305 7167988: PKIX CertPathBuilder in reverse mode doesn't work if more than one trust anchor is specified
xuelei
parents: 11900
diff changeset
   273
            if (pathCompleted) {
e3c056161305 7167988: PKIX CertPathBuilder in reverse mode doesn't work if more than one trust anchor is specified
xuelei
parents: 11900
diff changeset
   274
                break;
e3c056161305 7167988: PKIX CertPathBuilder in reverse mode doesn't work if more than one trust anchor is specified
xuelei
parents: 11900
diff changeset
   275
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            debug.println("SunCertPathBuilder.buildReverse() returned from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                + "depthFirstSearchReverse()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            debug.println("SunCertPathBuilder.buildReverse() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                + "certPathList.size: " + certPathList.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Private build forward method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private void buildForward(List<List<Vertex>> adjacencyList,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   290
                              LinkedList<X509Certificate> certPathList,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   291
                              boolean searchAllCertStores)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        throws GeneralSecurityException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            debug.println("SunCertPathBuilder.buildForward()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /* Initialize current state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        ForwardState currentState = new ForwardState();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   300
        currentState.initState(buildParams.certPathCheckers());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        /* Initialize adjacency list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        adjacencyList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        adjacencyList.add(new LinkedList<Vertex>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   306
        currentState.untrustedChecker = new UntrustedChecker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   308
        depthFirstSearchForward(buildParams.targetSubject(), currentState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   309
                                new ForwardBuilder(buildParams,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   310
                                                   searchAllCertStores),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   311
                                adjacencyList, certPathList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * This method performs a depth first search for a certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * path while building forward which meets the requirements set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the parameters object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * It uses an adjacency list to store all certificates which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * tried (i.e. at one time added to the path - they may not end up in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * the final path if backtracking occurs). This information can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * be used later to debug or demo the build.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * See "Data Structure and Algorithms, by Aho, Hopcroft, and Ullman"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * for an explanation of the DFS algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param dN the distinguished name being currently searched for certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param currentState the current PKIX validation state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   329
    private void depthFirstSearchForward(X500Principal dN,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   330
                                         ForwardState currentState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   331
                                         ForwardBuilder builder,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   332
                                         List<List<Vertex>> adjList,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   333
                                         LinkedList<X509Certificate> cpList)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        throws GeneralSecurityException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            debug.println("SunCertPathBuilder.depthFirstSearchForward(" + dN
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   338
                          + ", " + currentState.toString() + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * Find all the certificates issued to dN which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * satisfy the PKIX certification path constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   345
        Collection<X509Certificate> certs =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   346
            builder.getMatchingCerts(currentState, buildParams.certStores());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   347
        List<Vertex> vertices = addVertices(certs, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            debug.println("SunCertPathBuilder.depthFirstSearchForward(): "
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   350
                          + "certs.size=" + vertices.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         * For each cert in the collection, verify anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * that hasn't been checked yet (signature, revocation, etc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * and check for loops. Call depthFirstSearchForward()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * recursively for each good cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
               vertices:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        for (Vertex vertex : vertices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
             * Restore state to currentState each time through the loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
             * This is important because some of the user-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
             * checkers modify the state, which MUST be restored if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
             * the cert eventually fails to lead to the target and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
             * the next matching cert is tried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            ForwardState nextState = (ForwardState) currentState.clone();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   370
            X509Certificate cert = vertex.getCertificate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   373
                builder.verifyCert(cert, nextState, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   377
                                  + ": validation failed: " + gse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    gse.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                vertex.setThrowable(gse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
             * Certificate is good.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
             * If cert completes the path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
             *    process userCheckers that don't support forward checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
             *    and process policies over whole path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
             *    and backtrack appropriately if there is a failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
             * else if cert does not complete the path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
             *    add it to the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (builder.isPathCompleted(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   397
                                  + ": commencing final verification");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   399
                List<X509Certificate> appendedCerts = new ArrayList<>(cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                 * if the trust anchor selected is specified as a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                 * public key rather than a trusted cert, then verify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                 * cert (which is signed by the trusted public key), but
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   405
                 * don't add it yet to the cpList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (builder.trustAnchor.getTrustedCert() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    appendedCerts.add(0, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   411
                Set<String> initExpPolSet =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   412
                    Collections.singleton(PolicyChecker.ANY_POLICY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                PolicyNodeImpl rootNode = new PolicyNodeImpl(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    PolicyChecker.ANY_POLICY, null, false, initExpPolSet, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   417
                List<PKIXCertPathChecker> checkers = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                PolicyChecker policyChecker
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   419
                    = new PolicyChecker(buildParams.initialPolicies(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   420
                                        appendedCerts.size(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   421
                                        buildParams.explicitPolicyRequired(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   422
                                        buildParams.policyMappingInhibited(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   423
                                        buildParams.anyPolicyInhibited(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   424
                                        buildParams.policyQualifiersRejected(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   425
                                        rootNode);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   426
                checkers.add(policyChecker);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   428
                // add the algorithm checker
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   429
                checkers.add(new AlgorithmChecker(builder.trustAnchor));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   430
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   431
                BasicChecker basicChecker = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                if (nextState.keyParamsNeeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    PublicKey rootKey = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    if (builder.trustAnchor.getTrustedCert() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        rootKey = builder.trustAnchor.getCAPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        if (debug != null)
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   437
                            debug.println(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   438
                                "SunCertPathBuilder.depthFirstSearchForward " +
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   439
                                "using buildParams public key: " +
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   440
                                rootKey.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    TrustAnchor anchor = new TrustAnchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        (cert.getSubjectX500Principal(), rootKey, null);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   444
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   445
                    // add the basic checker
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   446
                    basicChecker = new BasicChecker(anchor, buildParams.date(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   447
                                                    buildParams.sigProvider(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   448
                                                    true);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   449
                    checkers.add(basicChecker);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   450
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   451
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   452
                buildParams.setCertPath(cf.generateCertPath(appendedCerts));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   453
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   454
                boolean revCheckerAdded = false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   455
                List<PKIXCertPathChecker> ckrs = buildParams.certPathCheckers();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   456
                for (PKIXCertPathChecker ckr : ckrs) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   457
                    if (ckr instanceof PKIXRevocationChecker) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   458
                        if (revCheckerAdded) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   459
                            throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   460
                                "Only one PKIXRevocationChecker can be specified");
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   461
                        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   462
                        revCheckerAdded = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   463
                        // if it's our own, initialize it
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   464
                        if (ckr instanceof RevocationChecker) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   465
                            ((RevocationChecker)ckr).init(builder.trustAnchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   466
                                                          buildParams);
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   467
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   470
                // only add a RevocationChecker if revocation is enabled and
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   471
                // a PKIXRevocationChecker has not already been added
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   472
                if (buildParams.revocationEnabled() && !revCheckerAdded) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   473
                    checkers.add(new RevocationChecker(builder.trustAnchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   474
                                                       buildParams));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   475
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   476
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   477
                checkers.addAll(ckrs);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   478
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   479
                // Why we don't need BasicChecker and RevocationChecker
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   480
                // if nextState.keyParamsNeeded() is false?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   482
                for (int i = 0; i < appendedCerts.size(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    X509Certificate currCert = appendedCerts.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        debug.println("current subject = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                      + currCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    Set<String> unresCritExts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        currCert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    if (unresCritExts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        unresCritExts = Collections.<String>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   493
                    for (PKIXCertPathChecker currChecker : checkers) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   494
                        if (!currChecker.isForwardCheckingSupported()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                currChecker.init(false);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   497
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   498
                                // The user specified
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   499
                                // AlgorithmChecker may not be
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   500
                                // able to set the trust anchor until now.
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   501
                                if (currChecker instanceof AlgorithmChecker) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   502
                                    ((AlgorithmChecker)currChecker).
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   503
                                        trySetTrustAnchor(builder.trustAnchor);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   504
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                currChecker.check(currCert, unresCritExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            } catch (CertPathValidatorException cpve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                    debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                    ("SunCertPathBuilder.depthFirstSearchForward(): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                    "final verification failed: " + cpve);
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   514
                                // If the target cert itself is revoked, we
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   515
                                // cannot trust it. We can bail out here.
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   516
                                if (buildParams.targetCertConstraints().match(currCert)
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   517
                                        && cpve.getReason() == BasicReason.REVOKED) {
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   518
                                    throw cpve;
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   519
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                vertex.setThrowable(cpve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                                continue vertices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                     * Remove extensions from user checkers that support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                     * forward checking. After this step, we will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                     * removed all extensions that all user checkers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                     * are capable of processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    for (PKIXCertPathChecker checker :
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   533
                         buildParams.certPathCheckers())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        if (checker.isForwardCheckingSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            Set<String> suppExts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                checker.getSupportedExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                            if (suppExts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                                unresCritExts.removeAll(suppExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    if (!unresCritExts.isEmpty()) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   545
                        unresCritExts.remove(BasicConstraints_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   546
                        unresCritExts.remove(NameConstraints_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   547
                        unresCritExts.remove(CertificatePolicies_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   548
                        unresCritExts.remove(PolicyMappings_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   549
                        unresCritExts.remove(PolicyConstraints_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   550
                        unresCritExts.remove(InhibitAnyPolicy_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   551
                        unresCritExts.remove(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                            SubjectAlternativeName_Id.toString());
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   553
                        unresCritExts.remove(KeyUsage_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   554
                        unresCritExts.remove(ExtendedKeyUsage_Id.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        if (!unresCritExts.isEmpty()) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   557
                            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   558
                                ("unrecognized critical extension(s)", null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   559
                                 null, -1, PKIXReason.UNRECOGNIZED_CRIT_EXT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        + ": final verification succeeded - path completed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                pathCompleted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                 * if the user specified a trusted public key rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                 * trusted certs, then add this cert (which is signed by
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   571
                 * the trusted public key) to the cpList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if (builder.trustAnchor.getTrustedCert() == null)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   574
                    builder.addCertToPath(cert, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                // Save the trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                this.trustAnchor = builder.trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                 * Extract and save the final target public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                if (basicChecker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    finalPublicKey = basicChecker.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    Certificate finalCert;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   585
                    if (cpList.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        finalCert = builder.trustAnchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   588
                        finalCert = cpList.getLast();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    finalPublicKey = finalCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                policyTreeResult = policyChecker.getPolicyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   596
                builder.addCertToPath(cert, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            /* Update the PKIX state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            nextState.updateState(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
             * Append an entry for cert in adjacency list and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
             * set index for current vertex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            adjList.add(new LinkedList<Vertex>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            vertex.setIndex(adjList.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            /* recursively search for matching certs at next dN */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   610
            depthFirstSearchForward(cert.getIssuerX500Principal(), nextState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   611
                                    builder, adjList, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
             * If path has been completed, return ASAP!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (pathCompleted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                 * If we get here, it means we have searched all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                 * certs issued by the dN w/o finding any matching certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                 * This means we have to backtrack to the previous cert in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                 * the path and try some other paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   627
                                  + ": backtracking");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   628
                builder.removeFinalCertFromPath(cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * This method performs a depth first search for a certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * path while building reverse which meets the requirements set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * the parameters object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * It uses an adjacency list to store all certificates which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * tried (i.e. at one time added to the path - they may not end up in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * the final path if backtracking occurs). This information can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * be used later to debug or demo the build.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * See "Data Structure and Algorithms, by Aho, Hopcroft, and Ullman"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * for an explanation of the DFS algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @param dN the distinguished name being currently searched for certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param currentState the current PKIX validation state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   648
    private void depthFirstSearchReverse(X500Principal dN,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   649
                                         ReverseState currentState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   650
                                         ReverseBuilder builder,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   651
                                         List<List<Vertex>> adjList,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   652
                                         LinkedList<X509Certificate> cpList)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        throws GeneralSecurityException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            debug.println("SunCertPathBuilder.depthFirstSearchReverse(" + dN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                + ", " + currentState.toString() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * Find all the certificates issued by dN which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * satisfy the PKIX certification path constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   663
        Collection<X509Certificate> certs =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   664
            builder.getMatchingCerts(currentState, buildParams.certStores());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   665
        List<Vertex> vertices = addVertices(certs, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            debug.println("SunCertPathBuilder.depthFirstSearchReverse(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                + "certs.size=" + vertices.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * For each cert in the collection, verify anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * that hasn't been checked yet (signature, revocation, etc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * and check for loops. Call depthFirstSearchReverse()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * recursively for each good cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        for (Vertex vertex : vertices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
             * Restore state to currentState each time through the loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
             * This is important because some of the user-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
             * checkers modify the state, which MUST be restored if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
             * the cert eventually fails to lead to the target and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
             * the next matching cert is tried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            ReverseState nextState = (ReverseState) currentState.clone();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   685
            X509Certificate cert = vertex.getCertificate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   687
                builder.verifyCert(cert, nextState, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    debug.println("SunCertPathBuilder.depthFirstSearchReverse()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        + ": validation failed: " + gse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                vertex.setThrowable(gse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
             * Certificate is good, add it to the path (if it isn't a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
             * self-signed cert) and update state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (!currentState.isInitial())
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   701
                builder.addCertToPath(cert, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            // save trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            this.trustAnchor = currentState.trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
             * Check if path is completed, return ASAP if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (builder.isPathCompleted(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    debug.println("SunCertPathBuilder.depthFirstSearchReverse()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        + ": path completed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                pathCompleted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                PolicyNodeImpl rootNode = nextState.rootNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                if (rootNode == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    policyTreeResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    policyTreeResult = rootNode.copyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    ((PolicyNodeImpl)policyTreeResult).setImmutable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                 * Extract and save the final target public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                finalPublicKey = cert.getPublicKey();
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   727
                if (PKIX.isDSAPublicKeyWithoutParams(finalPublicKey)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    finalPublicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        BasicChecker.makeInheritedParamsKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                            (finalPublicKey, currentState.pubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            /* Update the PKIX state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            nextState.updateState(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
             * Append an entry for cert in adjacency list and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
             * set index for current vertex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            adjList.add(new LinkedList<Vertex>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            vertex.setIndex(adjList.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            /* recursively search for matching certs at next dN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            depthFirstSearchReverse(cert.getSubjectX500Principal(), nextState,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   748
                                    builder, adjList, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
             * If path has been completed, return ASAP!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            if (pathCompleted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                 * If we get here, it means we have searched all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                 * certs issued by the dN w/o finding any matching certs. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                 * means we have to backtrack to the previous cert in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                 * and try some other paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    debug.println("SunCertPathBuilder.depthFirstSearchReverse()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        + ": backtracking");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                if (!currentState.isInitial())
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   766
                    builder.removeFinalCertFromPath(cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            debug.println("SunCertPathBuilder.depthFirstSearchReverse() all "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                + "certs in this adjacency list checked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Adds a collection of matching certificates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * adjacency list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   778
    private static List<Vertex> addVertices(Collection<X509Certificate> certs,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   779
                                            List<List<Vertex>> adjList)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   780
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        List<Vertex> l = adjList.get(adjList.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        for (X509Certificate cert : certs) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   784
            Vertex v = new Vertex(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   785
            l.add(v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Returns true if trust anchor certificate matches specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * certificate constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   795
    private static boolean anchorIsTarget(TrustAnchor anchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   796
                                          CertSelector sel)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   797
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        X509Certificate anchorCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (anchorCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            return sel.match(anchorCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
}