src/java.base/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java
author coffeys
Tue, 20 Nov 2018 13:12:48 +0000
changeset 52621 f7309a1491d9
parent 49776 40a012dc4cee
permissions -rw-r--r--
8148188: Enhance the security libraries to record events of interest Reviewed-by: egahlin, mullan, weijun, 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: 39465
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: 4209
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: 4209
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: 4209
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
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
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    28
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.InvalidAlgorithmParameterException;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    30
import java.security.cert.*;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    31
import java.util.*;
52621
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
    32
import java.util.concurrent.atomic.AtomicLong;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    33
52621
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
    34
import jdk.internal.event.X509ValidationEvent;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
    35
import jdk.internal.event.EventHelper;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    36
import sun.security.provider.certpath.PKIX.ValidatorParams;
49776
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
    37
import sun.security.validator.Validator;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    38
import sun.security.x509.X509CertImpl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class implements the PKIX validation algorithm for certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * paths consisting exclusively of <code>X509Certificates</code>. It uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the specified input parameter set (which must be a
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    45
 * <code>PKIXParameters</code> object).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    50
public final class PKIXCertPathValidator extends CertPathValidatorSpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final Debug debug = Debug.getInstance("certpath");
52621
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
    53
    private static final AtomicLong validationCounter = new AtomicLong();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public PKIXCertPathValidator() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    60
    @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    61
    public CertPathChecker engineGetRevocationChecker() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    62
        return new RevocationChecker();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    63
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Validates a certification path consisting exclusively of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>X509Certificate</code>s using the PKIX validation algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * which uses the specified input parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The input parameter set must be a <code>PKIXParameters</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param cp the X509 certification path
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    72
     * @param params the input PKIX parameter set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return the result
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    74
     * @throws CertPathValidatorException if cert path does not validate.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    75
     * @throws InvalidAlgorithmParameterException if the specified
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    76
     *         parameters are inappropriate for this CertPathValidator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    78
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public CertPathValidatorResult engineValidate(CertPath cp,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    80
                                                  CertPathParameters params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        throws CertPathValidatorException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    83
        ValidatorParams valParams = PKIX.checkParams(cp, params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    84
        return validate(valParams);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    85
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    86
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    87
    private static PKIXCertPathValidatorResult validate(ValidatorParams params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    88
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    89
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            debug.println("PKIXCertPathValidator.engineValidate()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Retrieve the first certificate in the certpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // (to be used later in pre-screening)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    95
        AdaptableX509CertSelector selector = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    96
        List<X509Certificate> certList = params.certificates();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (!certList.isEmpty()) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    98
            selector = new AdaptableX509CertSelector();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    99
            X509Certificate firstCert = certList.get(0);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   100
            // check trusted certificate's subject
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   101
            selector.setSubject(firstCert.getIssuerX500Principal());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   102
            /*
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   103
             * Facilitate certification path construction with authority
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   104
             * key identifier and subject key identifier.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   105
             */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   106
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   107
                X509CertImpl firstCertImpl = X509CertImpl.toImpl(firstCert);
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 19045
diff changeset
   108
                selector.setSkiAndSerialNumber(
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   109
                            firstCertImpl.getAuthorityKeyIdentifierExtension());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   110
            } catch (CertificateException | IOException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   111
                // ignore
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   112
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        CertPathValidatorException lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // We iterate through the set of trust anchors until we find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // one that works at which time we stop iterating
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   119
        for (TrustAnchor anchor : params.trustAnchors()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // if this trust anchor is not worth trying,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // we move on to the next one
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   124
                if (selector != null && !selector.match(trustedCert)) {
44920
5b66112437ba 8176457: Add verbose option to java.security.debug
ascarpino
parents: 44158
diff changeset
   125
                    if (debug != null && Debug.isVerbose()) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   126
                        debug.println("NO - don't try this trustedCert");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   127
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (debug != null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   132
                    debug.println("YES - try this trustedCert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    debug.println("anchor.getTrustedCert()."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        + "getSubjectX500Principal() = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        + trustedCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    debug.println("PKIXCertPathValidator.engineValidate(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        + "anchor.getTrustedCert() == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   145
                return validate(anchor, params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } catch (CertPathValidatorException cpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // remember this exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                lastException = cpe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // could not find a trust anchor that verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // (a) if we did a validation and it failed, use that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // (b) otherwise, generate new exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   159
            ("Path does not chain with any of the trust anchors",
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   160
             null, null, -1, PKIXReason.NO_TRUST_ANCHOR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   163
    private static PKIXCertPathValidatorResult validate(TrustAnchor anchor,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   164
                                                        ValidatorParams params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   165
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   166
    {
31688
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   167
        // check if anchor is untrusted
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   168
        UntrustedChecker untrustedChecker = new UntrustedChecker();
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   169
        X509Certificate anchorCert = anchor.getTrustedCert();
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   170
        if (anchorCert != null) {
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   171
            untrustedChecker.check(anchorCert);
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   172
        }
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   173
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   174
        int certPathLen = params.certificates().size();
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   175
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   176
        // create PKIXCertPathCheckers
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   177
        List<PKIXCertPathChecker> certPathCheckers = new ArrayList<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   178
        // add standard checkers that we will be using
31688
42c9b194a469 8073894: Getting to the root of certificate chains
mullan
parents: 25859
diff changeset
   179
        certPathCheckers.add(untrustedChecker);
44158
49deb8a1ed3f 8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents: 43701
diff changeset
   180
        certPathCheckers.add(new AlgorithmChecker(anchor, null, params.date(),
49deb8a1ed3f 8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents: 43701
diff changeset
   181
                params.timestamp(), params.variant()));
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   182
        certPathCheckers.add(new KeyChecker(certPathLen,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   183
                                            params.targetCertConstraints()));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   184
        certPathCheckers.add(new ConstraintsChecker(certPathLen));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   185
        PolicyNodeImpl rootNode =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   186
            new PolicyNodeImpl(null, PolicyChecker.ANY_POLICY, null, false,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   187
                               Collections.singleton(PolicyChecker.ANY_POLICY),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   188
                               false);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   189
        PolicyChecker pc = new PolicyChecker(params.initialPolicies(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   190
                                             certPathLen,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   191
                                             params.explicitPolicyRequired(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   192
                                             params.policyMappingInhibited(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   193
                                             params.anyPolicyInhibited(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   194
                                             params.policyQualifiersRejected(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   195
                                             rootNode);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   196
        certPathCheckers.add(pc);
49776
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   197
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   198
        // the time that the certificate validity period should be
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   199
        // checked against
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   200
        Date timeToCheck = null;
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   201
        // use timestamp if checking signed code that is timestamped, otherwise
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   202
        // use date parameter from PKIXParameters
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   203
        if ((params.variant() == Validator.VAR_CODE_SIGNING ||
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   204
             params.variant() == Validator.VAR_PLUGIN_CODE_SIGNING) &&
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   205
             params.timestamp() != null) {
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   206
            timeToCheck = params.timestamp().getTimestamp();
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   207
        } else {
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   208
            timeToCheck = params.date();
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   209
        }
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   210
        BasicChecker bc = new BasicChecker(anchor, timeToCheck,
40a012dc4cee 8191358: Restore TSA certificate expiration check
mullan
parents: 47216
diff changeset
   211
                                           params.sigProvider(), false);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   212
        certPathCheckers.add(bc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   214
        boolean revCheckerAdded = false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   215
        List<PKIXCertPathChecker> checkers = params.certPathCheckers();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   216
        for (PKIXCertPathChecker checker : checkers) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   217
            if (checker instanceof PKIXRevocationChecker) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 12861
diff changeset
   218
                if (revCheckerAdded) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 12861
diff changeset
   219
                    throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 12861
diff changeset
   220
                        "Only one PKIXRevocationChecker can be specified");
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 12861
diff changeset
   221
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   222
                revCheckerAdded = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   223
                // if it's our own, initialize it
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 12861
diff changeset
   224
                if (checker instanceof RevocationChecker) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   225
                    ((RevocationChecker)checker).init(anchor, params);
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 12861
diff changeset
   226
                }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   227
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   229
        // only add a RevocationChecker if revocation is enabled and
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   230
        // a PKIXRevocationChecker has not already been added
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   231
        if (params.revocationEnabled() && !revCheckerAdded) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   232
            certPathCheckers.add(new RevocationChecker(anchor, params));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   234
        // add user-specified checkers
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   235
        certPathCheckers.addAll(checkers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   237
        PKIXMasterCertPathValidator.validate(params.certPath(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   238
                                             params.certificates(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   239
                                             certPathCheckers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
52621
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   241
        X509ValidationEvent xve = new X509ValidationEvent();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   242
        if (xve.shouldCommit() || EventHelper.isLoggingSecurity()) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   243
            int[] certIds = params.certificates().stream()
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   244
                    .mapToInt(x -> x.hashCode())
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   245
                    .toArray();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   246
            int anchorCertId =
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   247
                    anchor.getTrustedCert().hashCode();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   248
            if (xve.shouldCommit()) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   249
                xve.certificateId = anchorCertId;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   250
                int certificatePos = 1; //anchor cert
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   251
                xve.certificatePosition = certificatePos;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   252
                xve.validationCounter = validationCounter.incrementAndGet();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   253
                xve.commit();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   254
                // now, iterate through remaining
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   255
                for (int id : certIds) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   256
                    xve.certificateId = id;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   257
                    xve.certificatePosition = ++certificatePos;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   258
                    xve.commit();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   259
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   260
                }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   261
            }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   262
            if (EventHelper.isLoggingSecurity()) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   263
                EventHelper.logX509ValidationEvent(anchorCertId, certIds);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   264
            }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   265
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   266
        return new PKIXCertPathValidatorResult(anchor, pc.getPolicyTree(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   267
                                               bc.getPublicKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
52621
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents: 49776
diff changeset
   269
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}