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