jdk/src/java.base/share/classes/sun/security/provider/certpath/SunCertPathBuilder.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 38576 ccaac80108c5
child 45992 38bdf44057b9
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 38576
diff changeset
     2
 * Copyright (c) 2000, 2017, 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.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    43
import sun.security.provider.certpath.PKIX.BuilderParams;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    44
import static sun.security.x509.PKIXExtensions.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
29973
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 29264
diff changeset
    48
 * This class builds certification paths in the forward direction.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
    50
 * <p> If successful, it returns a certification path which has successfully
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * satisfied all the constraints and requirements specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * 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
    53
 * path validation algorithm defined in RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> This implementation uses a depth-first search approach to finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * certification paths. If it comes to a point in which it cannot find
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * any more certificates leading to the target OR the path length is too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * it backtracks to previous paths until the target has been found or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * all possible paths have been exhausted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> This implementation is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public final class SunCertPathBuilder extends CertPathBuilderSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * private objects shared by methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    74
    private BuilderParams buildParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private CertificateFactory cf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private boolean pathCompleted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private PolicyNode policyTreeResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private TrustAnchor trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private PublicKey finalPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Create an instance of <code>SunCertPathBuilder</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @throws CertPathBuilderException if an error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public SunCertPathBuilder() throws CertPathBuilderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new CertPathBuilderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    92
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    93
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    94
    @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    95
    public CertPathChecker engineGetRevocationChecker() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
    96
        return new RevocationChecker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Attempts to build a certification path using the Sun build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * algorithm from a trusted anchor(s) to a target subject, which must both
29973
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 29264
diff changeset
   102
     * be specified in the input parameter set. This method will
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 29264
diff changeset
   103
     * attempt to build in the forward direction: from the target to the CA.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>The certification path that is constructed is validated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * according to the PKIX specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param params the parameter set for building a path. Must be an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *  of <code>PKIXBuilderParameters</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return a certification path builder result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @exception CertPathBuilderException Exception thrown if builder is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *  unable to build a complete certification path from the trusted anchor(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *  to the target subject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws InvalidAlgorithmParameterException if the given parameters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *  inappropriate for this certification path builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   117
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public CertPathBuilderResult engineBuild(CertPathParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        throws CertPathBuilderException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            debug.println("SunCertPathBuilder.engineBuild(" + params + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   125
        buildParams = PKIX.checkBuilderParams(params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   126
        return build();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   127
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   129
    private PKIXCertPathBuilderResult build() throws CertPathBuilderException {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   130
        List<List<Vertex>> adjList = new ArrayList<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   131
        PKIXCertPathBuilderResult result = buildCertPath(false, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (debug != null) {
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   134
                debug.println("SunCertPathBuilder.engineBuild: 2nd pass; " +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   135
                              "try building again searching all certstores");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            adjList.clear();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   139
            result = buildCertPath(true, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new SunCertPathBuilderException("unable to find valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    + "certification path to requested target",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   149
    private PKIXCertPathBuilderResult buildCertPath(boolean searchAllCertStores,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   150
                                                    List<List<Vertex>> adjList)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   151
        throws CertPathBuilderException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   152
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Init shared variables and build certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        pathCompleted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        trustAnchor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        finalPublicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        policyTreeResult = null;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   158
        LinkedList<X509Certificate> certPathList = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
29973
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 29264
diff changeset
   160
            buildForward(adjList, certPathList, searchAllCertStores);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   161
        } catch (GeneralSecurityException | IOException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                debug.println("SunCertPathBuilder.engineBuild() exception in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    + "build");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new SunCertPathBuilderException("unable to find valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                + "certification path to requested target", e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // construct SunCertPathBuilderResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (pathCompleted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    debug.println("SunCertPathBuilder.engineBuild() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                  + "pathCompleted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // we must return a certpath which has the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // as the first cert in the certpath - i.e. reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                // the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                Collections.reverse(certPathList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return new SunCertPathBuilderResult(
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   185
                    cf.generateCertPath(certPathList), trustAnchor,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    policyTreeResult, finalPublicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   189
        } catch (CertificateException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                debug.println("SunCertPathBuilder.engineBuild() exception "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                              + "in wrap-up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new SunCertPathBuilderException("unable to find valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                + "certification path to requested target", e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                new AdjacencyList(adjList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Private build forward method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private void buildForward(List<List<Vertex>> adjacencyList,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   207
                              LinkedList<X509Certificate> certPathList,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   208
                              boolean searchAllCertStores)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throws GeneralSecurityException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            debug.println("SunCertPathBuilder.buildForward()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /* Initialize current state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        ForwardState currentState = new ForwardState();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   217
        currentState.initState(buildParams.certPathCheckers());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        /* Initialize adjacency list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        adjacencyList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        adjacencyList.add(new LinkedList<Vertex>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   223
        currentState.untrustedChecker = new UntrustedChecker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   225
        depthFirstSearchForward(buildParams.targetSubject(), currentState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   226
                                new ForwardBuilder(buildParams,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   227
                                                   searchAllCertStores),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   228
                                adjacencyList, certPathList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * This method performs a depth first search for a certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * path while building forward which meets the requirements set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the parameters object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * It uses an adjacency list to store all certificates which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * tried (i.e. at one time added to the path - they may not end up in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * the final path if backtracking occurs). This information can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * be used later to debug or demo the build.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * See "Data Structure and Algorithms, by Aho, Hopcroft, and Ullman"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * for an explanation of the DFS algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param dN the distinguished name being currently searched for certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param currentState the current PKIX validation state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   246
    private void depthFirstSearchForward(X500Principal dN,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   247
                                         ForwardState currentState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   248
                                         ForwardBuilder builder,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   249
                                         List<List<Vertex>> adjList,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   250
                                         LinkedList<X509Certificate> cpList)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        throws GeneralSecurityException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            debug.println("SunCertPathBuilder.depthFirstSearchForward(" + dN
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   255
                          + ", " + currentState.toString() + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * Find all the certificates issued to dN which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * satisfy the PKIX certification path constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   262
        Collection<X509Certificate> certs =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   263
            builder.getMatchingCerts(currentState, buildParams.certStores());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   264
        List<Vertex> vertices = addVertices(certs, adjList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            debug.println("SunCertPathBuilder.depthFirstSearchForward(): "
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   267
                          + "certs.size=" + vertices.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * For each cert in the collection, verify anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * that hasn't been checked yet (signature, revocation, etc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * and check for loops. Call depthFirstSearchForward()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * recursively for each good cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
               vertices:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        for (Vertex vertex : vertices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
             * Restore state to currentState each time through the loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
             * This is important because some of the user-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             * checkers modify the state, which MUST be restored if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
             * the cert eventually fails to lead to the target and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
             * the next matching cert is tried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            ForwardState nextState = (ForwardState) currentState.clone();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   287
            X509Certificate cert = vertex.getCertificate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   290
                builder.verifyCert(cert, nextState, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   294
                                  + ": validation failed: " + gse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    gse.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                vertex.setThrowable(gse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
             * Certificate is good.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
             * If cert completes the path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
             *    process userCheckers that don't support forward checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
             *    and process policies over whole path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
             *    and backtrack appropriately if there is a failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
             * else if cert does not complete the path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             *    add it to the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (builder.isPathCompleted(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   314
                                  + ": commencing final verification");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   316
                List<X509Certificate> appendedCerts = new ArrayList<>(cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                 * if the trust anchor selected is specified as a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                 * public key rather than a trusted cert, then verify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                 * cert (which is signed by the trusted public key), but
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   322
                 * don't add it yet to the cpList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (builder.trustAnchor.getTrustedCert() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    appendedCerts.add(0, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   328
                Set<String> initExpPolSet =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   329
                    Collections.singleton(PolicyChecker.ANY_POLICY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                PolicyNodeImpl rootNode = new PolicyNodeImpl(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    PolicyChecker.ANY_POLICY, null, false, initExpPolSet, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   334
                List<PKIXCertPathChecker> checkers = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                PolicyChecker policyChecker
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   336
                    = new PolicyChecker(buildParams.initialPolicies(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   337
                                        appendedCerts.size(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   338
                                        buildParams.explicitPolicyRequired(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   339
                                        buildParams.policyMappingInhibited(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   340
                                        buildParams.anyPolicyInhibited(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   341
                                        buildParams.policyQualifiersRejected(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   342
                                        rootNode);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   343
                checkers.add(policyChecker);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   345
                // add the algorithm checker
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 29973
diff changeset
   346
                checkers.add(new AlgorithmChecker(builder.trustAnchor,
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 38576
diff changeset
   347
                        buildParams.date(), null));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   348
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   349
                BasicChecker basicChecker = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (nextState.keyParamsNeeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    PublicKey rootKey = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (builder.trustAnchor.getTrustedCert() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        rootKey = builder.trustAnchor.getCAPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        if (debug != null)
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   355
                            debug.println(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   356
                                "SunCertPathBuilder.depthFirstSearchForward " +
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   357
                                "using buildParams public key: " +
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   358
                                rootKey.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    TrustAnchor anchor = new TrustAnchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        (cert.getSubjectX500Principal(), rootKey, null);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   362
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   363
                    // add the basic checker
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   364
                    basicChecker = new BasicChecker(anchor, buildParams.date(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   365
                                                    buildParams.sigProvider(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   366
                                                    true);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   367
                    checkers.add(basicChecker);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   368
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   369
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   370
                buildParams.setCertPath(cf.generateCertPath(appendedCerts));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   371
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   372
                boolean revCheckerAdded = false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   373
                List<PKIXCertPathChecker> ckrs = buildParams.certPathCheckers();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   374
                for (PKIXCertPathChecker ckr : ckrs) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   375
                    if (ckr instanceof PKIXRevocationChecker) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   376
                        if (revCheckerAdded) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   377
                            throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   378
                                "Only one PKIXRevocationChecker can be specified");
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   379
                        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   380
                        revCheckerAdded = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   381
                        // 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
   382
                        if (ckr instanceof RevocationChecker) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   383
                            ((RevocationChecker)ckr).init(builder.trustAnchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   384
                                                          buildParams);
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   385
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   388
                // only add a RevocationChecker if revocation is enabled and
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   389
                // a PKIXRevocationChecker has not already been added
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   390
                if (buildParams.revocationEnabled() && !revCheckerAdded) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   391
                    checkers.add(new RevocationChecker(builder.trustAnchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   392
                                                       buildParams));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   393
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   394
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   395
                checkers.addAll(ckrs);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   396
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   397
                // 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
   398
                // if nextState.keyParamsNeeded() is false?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   400
                for (int i = 0; i < appendedCerts.size(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    X509Certificate currCert = appendedCerts.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        debug.println("current subject = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                      + currCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    Set<String> unresCritExts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        currCert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    if (unresCritExts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        unresCritExts = Collections.<String>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   411
                    for (PKIXCertPathChecker currChecker : checkers) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   412
                        if (!currChecker.isForwardCheckingSupported()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                currChecker.init(false);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   415
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   416
                                // The user specified
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   417
                                // AlgorithmChecker may not be
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   418
                                // able to set the trust anchor until now.
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   419
                                if (currChecker instanceof AlgorithmChecker) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   420
                                    ((AlgorithmChecker)currChecker).
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   421
                                        trySetTrustAnchor(builder.trustAnchor);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   422
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                currChecker.check(currCert, unresCritExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            } catch (CertPathValidatorException cpve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                    debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                    ("SunCertPathBuilder.depthFirstSearchForward(): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                    "final verification failed: " + cpve);
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   432
                                // 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
   433
                                // 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
   434
                                if (buildParams.targetCertConstraints().match(currCert)
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   435
                                        && cpve.getReason() == BasicReason.REVOKED) {
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   436
                                    throw cpve;
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 19045
diff changeset
   437
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                vertex.setThrowable(cpve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                continue vertices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                     * Remove extensions from user checkers that support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                     * forward checking. After this step, we will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                     * removed all extensions that all user checkers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                     * are capable of processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    for (PKIXCertPathChecker checker :
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   451
                         buildParams.certPathCheckers())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        if (checker.isForwardCheckingSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            Set<String> suppExts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                checker.getSupportedExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                            if (suppExts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                unresCritExts.removeAll(suppExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    if (!unresCritExts.isEmpty()) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   463
                        unresCritExts.remove(BasicConstraints_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   464
                        unresCritExts.remove(NameConstraints_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   465
                        unresCritExts.remove(CertificatePolicies_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   466
                        unresCritExts.remove(PolicyMappings_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   467
                        unresCritExts.remove(PolicyConstraints_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   468
                        unresCritExts.remove(InhibitAnyPolicy_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   469
                        unresCritExts.remove(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                            SubjectAlternativeName_Id.toString());
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   471
                        unresCritExts.remove(KeyUsage_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   472
                        unresCritExts.remove(ExtendedKeyUsage_Id.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        if (!unresCritExts.isEmpty()) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   475
                            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   476
                                ("unrecognized critical extension(s)", null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   477
                                 null, -1, PKIXReason.UNRECOGNIZED_CRIT_EXT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        + ": final verification succeeded - path completed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                pathCompleted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                 * if the user specified a trusted public key rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                 * trusted certs, then add this cert (which is signed by
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   489
                 * the trusted public key) to the cpList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                if (builder.trustAnchor.getTrustedCert() == null)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   492
                    builder.addCertToPath(cert, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                // Save the trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                this.trustAnchor = builder.trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                 * Extract and save the final target public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (basicChecker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    finalPublicKey = basicChecker.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    Certificate finalCert;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   503
                    if (cpList.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        finalCert = builder.trustAnchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   506
                        finalCert = cpList.getLast();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    finalPublicKey = finalCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                policyTreeResult = policyChecker.getPolicyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   514
                builder.addCertToPath(cert, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            /* Update the PKIX state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            nextState.updateState(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             * Append an entry for cert in adjacency list and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
             * set index for current vertex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            adjList.add(new LinkedList<Vertex>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            vertex.setIndex(adjList.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            /* recursively search for matching certs at next dN */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   528
            depthFirstSearchForward(cert.getIssuerX500Principal(), nextState,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   529
                                    builder, adjList, cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
             * If path has been completed, return ASAP!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (pathCompleted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                 * If we get here, it means we have searched all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 * certs issued by the dN w/o finding any matching certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                 * This means we have to backtrack to the previous cert in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                 * the path and try some other paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    debug.println("SunCertPathBuilder.depthFirstSearchForward()"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   545
                                  + ": backtracking");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   546
                builder.removeFinalCertFromPath(cpList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Adds a collection of matching certificates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * adjacency list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   555
    private static List<Vertex> addVertices(Collection<X509Certificate> certs,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   556
                                            List<List<Vertex>> adjList)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   557
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        List<Vertex> l = adjList.get(adjList.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        for (X509Certificate cert : certs) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   561
            Vertex v = new Vertex(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   562
            l.add(v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Returns true if trust anchor certificate matches specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * certificate constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   572
    private static boolean anchorIsTarget(TrustAnchor anchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   573
                                          CertSelector sel)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 7040
diff changeset
   574
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        X509Certificate anchorCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (anchorCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            return sel.match(anchorCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
}