jdk/src/java.base/share/classes/sun/security/provider/certpath/RevocationChecker.java
author juh
Tue, 03 Mar 2015 14:16:49 -0800
changeset 29264 5172066a2da6
parent 26967 c182469301ee
child 30506 1998a5644f50
permissions -rw-r--r--
8054037: Improve tracing for java.security.debug=certpath 8055207: keystore and truststore debug output could be much better Reviewed-by: mullan, coffeys, jnimeh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     1
/*
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     4
 *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    10
 *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    15
 * accompanied this code).
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    16
 *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    20
 *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    23
 * questions.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    24
 */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    25
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    26
package sun.security.provider.certpath;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    27
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    28
import java.io.IOException;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    29
import java.math.BigInteger;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    30
import java.net.URI;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    31
import java.net.URISyntaxException;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    32
import java.security.AccessController;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    33
import java.security.InvalidAlgorithmParameterException;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    35
import java.security.PrivilegedAction;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    36
import java.security.PublicKey;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    37
import java.security.Security;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    38
import java.security.cert.CertPathValidatorException.BasicReason;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    39
import java.security.cert.Extension;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    40
import java.security.cert.*;
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
    41
import java.util.*;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    42
import javax.security.auth.x500.X500Principal;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    43
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    44
import static sun.security.provider.certpath.OCSP.*;
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
    45
import static sun.security.provider.certpath.PKIX.*;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    46
import sun.security.action.GetPropertyAction;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    47
import sun.security.x509.*;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    48
import static sun.security.x509.PKIXExtensions.*;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    49
import sun.security.util.Debug;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    50
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    51
class RevocationChecker extends PKIXRevocationChecker {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    52
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    53
    private static final Debug debug = Debug.getInstance("certpath");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    54
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    55
    private TrustAnchor anchor;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    56
    private ValidatorParams params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    57
    private boolean onlyEE;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    58
    private boolean softFail;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    59
    private boolean crlDP;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    60
    private URI responderURI;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    61
    private X509Certificate responderCert;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    62
    private List<CertStore> certStores;
14402
424f3f23f1da 7171570: JEP 124 Potential API Changes
mullan
parents: 13806
diff changeset
    63
    private Map<X509Certificate, byte[]> ocspResponses;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    64
    private List<Extension> ocspExtensions;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    65
    private boolean legacy;
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
    66
    private LinkedList<CertPathValidatorException> softFailExceptions =
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
    67
        new LinkedList<>();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    68
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    69
    // state variables
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    70
    private X509Certificate issuerCert;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    71
    private PublicKey prevPubKey;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    72
    private boolean crlSignFlag;
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
    73
    private int certIndex;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    74
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
    75
    private enum Mode { PREFER_OCSP, PREFER_CRLS, ONLY_CRLS, ONLY_OCSP };
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    76
    private Mode mode = Mode.PREFER_OCSP;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    77
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    78
    private static class RevocationProperties {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    79
        boolean onlyEE;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    80
        boolean ocspEnabled;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    81
        boolean crlDPEnabled;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    82
        String ocspUrl;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    83
        String ocspSubject;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    84
        String ocspIssuer;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    85
        String ocspSerial;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    86
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    87
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    88
    RevocationChecker() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    89
        legacy = false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    90
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    91
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    92
    RevocationChecker(TrustAnchor anchor, ValidatorParams params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    93
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    94
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    95
        legacy = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    96
        init(anchor, params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    97
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    98
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    99
    void init(TrustAnchor anchor, ValidatorParams params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   100
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   101
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   102
        RevocationProperties rp = getRevocationProperties();
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   103
        URI uri = getOcspResponder();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   104
        responderURI = (uri == null) ? toURI(rp.ocspUrl) : uri;
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   105
        X509Certificate cert = getOcspResponderCert();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   106
        responderCert = (cert == null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   107
                        ? getResponderCert(rp, params.trustAnchors(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   108
                                           params.certStores())
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   109
                        : cert;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   110
        Set<Option> options = getOptions();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   111
        for (Option option : options) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   112
            switch (option) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   113
            case ONLY_END_ENTITY:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   114
            case PREFER_CRLS:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   115
            case SOFT_FAIL:
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   116
            case NO_FALLBACK:
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   117
                break;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   118
            default:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   119
                throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   120
                    "Unrecognized revocation parameter option: " + option);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   121
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   122
        }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   123
        softFail = options.contains(Option.SOFT_FAIL);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   124
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   125
        // set mode, only end entity flag
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   126
        if (legacy) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   127
            mode = (rp.ocspEnabled) ? Mode.PREFER_OCSP : Mode.ONLY_CRLS;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   128
            onlyEE = rp.onlyEE;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   129
        } else {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   130
            if (options.contains(Option.NO_FALLBACK)) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   131
                if (options.contains(Option.PREFER_CRLS)) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   132
                    mode = Mode.ONLY_CRLS;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   133
                } else {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   134
                    mode = Mode.ONLY_OCSP;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   135
                }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   136
            } else if (options.contains(Option.PREFER_CRLS)) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   137
                mode = Mode.PREFER_CRLS;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   138
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   139
            onlyEE = options.contains(Option.ONLY_END_ENTITY);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   140
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   141
        if (legacy) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   142
            crlDP = rp.crlDPEnabled;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   143
        } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   144
            crlDP = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   145
        }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   146
        ocspResponses = getOcspResponses();
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   147
        ocspExtensions = getOcspExtensions();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   148
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   149
        this.anchor = anchor;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   150
        this.params = params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   151
        this.certStores = new ArrayList<>(params.certStores());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   152
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   153
            this.certStores.add(CertStore.getInstance("Collection",
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   154
                new CollectionCertStoreParameters(params.certificates())));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   155
        } catch (InvalidAlgorithmParameterException |
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   156
                 NoSuchAlgorithmException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   157
            // should never occur but not necessarily fatal, so log it,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   158
            // ignore and continue
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   159
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   160
                debug.println("RevocationChecker: " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   161
                              "error creating Collection CertStore: " + e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   162
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   163
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   164
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   165
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   166
    private static URI toURI(String uriString)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   167
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   168
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   169
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   170
            if (uriString != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   171
                return new URI(uriString);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   172
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   173
            return null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   174
        } catch (URISyntaxException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   175
            throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   176
                "cannot parse ocsp.responderURL property", e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   177
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   178
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   179
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   180
    private static RevocationProperties getRevocationProperties() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   181
        return AccessController.doPrivileged(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   182
            new PrivilegedAction<RevocationProperties>() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   183
                public RevocationProperties run() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   184
                    RevocationProperties rp = new RevocationProperties();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   185
                    String onlyEE = Security.getProperty(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   186
                        "com.sun.security.onlyCheckRevocationOfEECert");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   187
                    rp.onlyEE = onlyEE != null
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   188
                                && onlyEE.equalsIgnoreCase("true");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   189
                    String ocspEnabled = Security.getProperty("ocsp.enable");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   190
                    rp.ocspEnabled = ocspEnabled != null
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   191
                                     && ocspEnabled.equalsIgnoreCase("true");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   192
                    rp.ocspUrl = Security.getProperty("ocsp.responderURL");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   193
                    rp.ocspSubject
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   194
                        = Security.getProperty("ocsp.responderCertSubjectName");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   195
                    rp.ocspIssuer
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   196
                        = Security.getProperty("ocsp.responderCertIssuerName");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   197
                    rp.ocspSerial
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   198
                        = Security.getProperty("ocsp.responderCertSerialNumber");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   199
                    rp.crlDPEnabled
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   200
                        = Boolean.getBoolean("com.sun.security.enableCRLDP");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   201
                    return rp;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   202
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   203
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   204
        );
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   205
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   206
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   207
    private static X509Certificate getResponderCert(RevocationProperties rp,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   208
                                                    Set<TrustAnchor> anchors,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   209
                                                    List<CertStore> stores)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   210
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   211
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   212
        if (rp.ocspSubject != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   213
            return getResponderCert(rp.ocspSubject, anchors, stores);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   214
        } else if (rp.ocspIssuer != null && rp.ocspSerial != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   215
            return getResponderCert(rp.ocspIssuer, rp.ocspSerial,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   216
                                    anchors, stores);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   217
        } else if (rp.ocspIssuer != null || rp.ocspSerial != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   218
            throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   219
                "Must specify both ocsp.responderCertIssuerName and " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   220
                "ocsp.responderCertSerialNumber properties");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   221
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   222
        return null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   223
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   224
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   225
    private static X509Certificate getResponderCert(String subject,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   226
                                                    Set<TrustAnchor> anchors,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   227
                                                    List<CertStore> stores)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   228
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   229
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   230
        X509CertSelector sel = new X509CertSelector();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   231
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   232
            sel.setSubject(new X500Principal(subject));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   233
        } catch (IllegalArgumentException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   234
            throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   235
                "cannot parse ocsp.responderCertSubjectName property", e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   236
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   237
        return getResponderCert(sel, anchors, stores);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   238
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   239
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   240
    private static X509Certificate getResponderCert(String issuer,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   241
                                                    String serial,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   242
                                                    Set<TrustAnchor> anchors,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   243
                                                    List<CertStore> stores)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   244
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   245
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   246
        X509CertSelector sel = new X509CertSelector();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   247
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   248
            sel.setIssuer(new X500Principal(issuer));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   249
        } catch (IllegalArgumentException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   250
            throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   251
                "cannot parse ocsp.responderCertIssuerName property", e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   252
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   253
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   254
            sel.setSerialNumber(new BigInteger(stripOutSeparators(serial), 16));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   255
        } catch (NumberFormatException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   256
            throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   257
                "cannot parse ocsp.responderCertSerialNumber property", e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   258
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   259
        return getResponderCert(sel, anchors, stores);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   260
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   261
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   262
    private static X509Certificate getResponderCert(X509CertSelector sel,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   263
                                                    Set<TrustAnchor> anchors,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   264
                                                    List<CertStore> stores)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   265
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   266
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   267
        // first check TrustAnchors
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   268
        for (TrustAnchor anchor : anchors) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   269
            X509Certificate cert = anchor.getTrustedCert();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   270
            if (cert == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   271
                continue;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   272
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   273
            if (sel.match(cert)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   274
                return cert;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   275
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   276
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   277
        // now check CertStores
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   278
        for (CertStore store : stores) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   279
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   280
                Collection<? extends Certificate> certs =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   281
                    store.getCertificates(sel);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   282
                if (!certs.isEmpty()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   283
                    return (X509Certificate)certs.iterator().next();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   284
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   285
            } catch (CertStoreException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   286
                // ignore and try next CertStore
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   287
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   288
                    debug.println("CertStore exception:" + e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   289
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   290
                continue;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   291
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   292
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   293
        throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   294
            "Cannot find the responder's certificate " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   295
            "(set using the OCSP security properties).");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   296
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   297
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   298
    @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   299
    public void init(boolean forward) throws CertPathValidatorException {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   300
        if (forward) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   301
            throw new
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   302
                CertPathValidatorException("forward checking not supported");
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   303
        }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   304
        if (anchor != null) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   305
            issuerCert = anchor.getTrustedCert();
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   306
            prevPubKey = (issuerCert != null) ? issuerCert.getPublicKey()
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   307
                                              : anchor.getCAPublicKey();
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   308
        }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   309
        crlSignFlag = true;
21329
c8ed09765b9a 8025287: NPE in api/java_security/cert/PKIXRevocationChecker/GeneralTests_GeneralTests
juh
parents: 19046
diff changeset
   310
        if (params != null && params.certPath() != null) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   311
            certIndex = params.certPath().getCertificates().size() - 1;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   312
        } else {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   313
            certIndex = -1;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   314
        }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   315
        softFailExceptions.clear();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   316
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   317
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   318
    @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   319
    public boolean isForwardCheckingSupported() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   320
        return false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   321
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   322
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   323
    @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   324
    public Set<String> getSupportedExtensions() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   325
        return null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   326
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   327
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   328
    @Override
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   329
    public List<CertPathValidatorException> getSoftFailExceptions() {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   330
        return Collections.unmodifiableList(softFailExceptions);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   331
    }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   332
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   333
    @Override
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   334
    public void check(Certificate cert, Collection<String> unresolvedCritExts)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   335
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   336
    {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   337
        check((X509Certificate)cert, unresolvedCritExts,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   338
              prevPubKey, crlSignFlag);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   339
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   340
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   341
    private void check(X509Certificate xcert,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   342
                       Collection<String> unresolvedCritExts,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   343
                       PublicKey pubKey, boolean crlSignFlag)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   344
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   345
    {
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   346
        if (debug != null) {
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   347
            debug.println("RevocationChecker.check: checking cert" +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   348
                "\n  SN: " + Debug.toHexString(xcert.getSerialNumber()) +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   349
                "\n  Subject: " + xcert.getSubjectX500Principal() +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   350
                "\n  Issuer: " + xcert.getIssuerX500Principal());
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   351
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   352
        try {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   353
            if (onlyEE && xcert.getBasicConstraints() != -1) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   354
                if (debug != null) {
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   355
                    debug.println("Skipping revocation check; cert is not " +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 26967
diff changeset
   356
                                  "an end entity cert");
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   357
                }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   358
                return;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   359
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   360
            switch (mode) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   361
                case PREFER_OCSP:
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   362
                case ONLY_OCSP:
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   363
                    checkOCSP(xcert, unresolvedCritExts);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   364
                    break;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   365
                case PREFER_CRLS:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   366
                case ONLY_CRLS:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   367
                    checkCRLs(xcert, unresolvedCritExts, null,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   368
                              pubKey, crlSignFlag);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   369
                    break;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   370
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   371
        } catch (CertPathValidatorException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   372
            if (e.getReason() == BasicReason.REVOKED) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   373
                throw e;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   374
            }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   375
            boolean eSoftFail = isSoftFailException(e);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   376
            if (eSoftFail) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   377
                if (mode == Mode.ONLY_OCSP || mode == Mode.ONLY_CRLS) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   378
                    return;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   379
                }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   380
            } else {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   381
                if (mode == Mode.ONLY_OCSP || mode == Mode.ONLY_CRLS) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   382
                    throw e;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   383
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   384
            }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   385
            CertPathValidatorException cause = e;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   386
            // Otherwise, failover
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   387
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   388
                debug.println("RevocationChecker.check() " + e.getMessage());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   389
                debug.println("RevocationChecker.check() preparing to failover");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   390
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   391
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   392
                switch (mode) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   393
                    case PREFER_OCSP:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   394
                        checkCRLs(xcert, unresolvedCritExts, null,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   395
                                  pubKey, crlSignFlag);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   396
                        break;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   397
                    case PREFER_CRLS:
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   398
                        checkOCSP(xcert, unresolvedCritExts);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   399
                        break;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   400
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   401
            } catch (CertPathValidatorException x) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   402
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   403
                    debug.println("RevocationChecker.check() failover failed");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   404
                    debug.println("RevocationChecker.check() " + x.getMessage());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   405
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   406
                if (x.getReason() == BasicReason.REVOKED) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   407
                    throw x;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   408
                }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   409
                if (!isSoftFailException(x)) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   410
                    cause.addSuppressed(x);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   411
                    throw cause;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   412
                } else {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   413
                    // only pass if both exceptions were soft failures
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   414
                    if (!eSoftFail) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   415
                        throw cause;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   416
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   417
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   418
            }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   419
        } finally {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   420
            updateState(xcert);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   421
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   422
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   423
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   424
    private boolean isSoftFailException(CertPathValidatorException e) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   425
        if (softFail &&
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   426
            e.getReason() == BasicReason.UNDETERMINED_REVOCATION_STATUS)
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   427
        {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   428
            // recreate exception with correct index
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   429
            CertPathValidatorException e2 = new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   430
                e.getMessage(), e.getCause(), params.certPath(), certIndex,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   431
                e.getReason());
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   432
            softFailExceptions.addFirst(e2);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   433
            return true;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   434
        }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   435
        return false;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   436
    }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   437
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   438
    private void updateState(X509Certificate cert)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   439
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   440
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   441
        issuerCert = cert;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   442
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   443
        // Make new public key if parameters are missing
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   444
        PublicKey pubKey = cert.getPublicKey();
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
   445
        if (PKIX.isDSAPublicKeyWithoutParams(pubKey)) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   446
            // pubKey needs to inherit DSA parameters from prev key
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   447
            pubKey = BasicChecker.makeInheritedParamsKey(pubKey, prevPubKey);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   448
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   449
        prevPubKey = pubKey;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   450
        crlSignFlag = certCanSignCrl(cert);
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   451
        if (certIndex > 0) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   452
            certIndex--;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   453
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   454
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   455
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   456
    // Maximum clock skew in milliseconds (15 minutes) allowed when checking
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   457
    // validity of CRLs
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   458
    private static final long MAX_CLOCK_SKEW = 900000;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   459
    private void checkCRLs(X509Certificate cert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   460
                           Collection<String> unresolvedCritExts,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   461
                           Set<X509Certificate> stackedCerts,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   462
                           PublicKey pubKey, boolean signFlag)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   463
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   464
    {
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
   465
        checkCRLs(cert, pubKey, null, signFlag, true,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   466
                  stackedCerts, params.trustAnchors());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   467
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   468
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   469
    private void checkCRLs(X509Certificate cert, PublicKey prevKey,
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
   470
                           X509Certificate prevCert, boolean signFlag,
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
   471
                           boolean allowSeparateKey,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   472
                           Set<X509Certificate> stackedCerts,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   473
                           Set<TrustAnchor> anchors)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   474
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   475
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   476
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   477
            debug.println("RevocationChecker.checkCRLs()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   478
                          " ---checking revocation status ...");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   479
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   480
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   481
        // Reject circular dependencies - RFC 5280 is not explicit on how
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   482
        // to handle this, but does suggest that they can be a security
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   483
        // risk and can create unresolvable dependencies
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   484
        if (stackedCerts != null && stackedCerts.contains(cert)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   485
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   486
                debug.println("RevocationChecker.checkCRLs()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   487
                              " circular dependency");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   488
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   489
            throw new CertPathValidatorException
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   490
                 ("Could not determine revocation status", null, null, -1,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   491
                  BasicReason.UNDETERMINED_REVOCATION_STATUS);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   492
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   493
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   494
        Set<X509CRL> possibleCRLs = new HashSet<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   495
        Set<X509CRL> approvedCRLs = new HashSet<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   496
        X509CRLSelector sel = new X509CRLSelector();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   497
        sel.setCertificateChecking(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   498
        CertPathHelper.setDateAndTime(sel, params.date(), MAX_CLOCK_SKEW);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   499
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   500
        // First, check user-specified CertStores
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   501
        CertPathValidatorException networkFailureException = null;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   502
        for (CertStore store : certStores) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   503
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   504
                for (CRL crl : store.getCRLs(sel)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   505
                    possibleCRLs.add((X509CRL)crl);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   506
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   507
            } catch (CertStoreException e) {
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   508
                if (debug != null) {
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   509
                    debug.println("RevocationChecker.checkCRLs() " +
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   510
                                  "CertStoreException: " + e.getMessage());
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   511
                }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   512
                if (networkFailureException == null &&
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   513
                    CertStoreHelper.isCausedByNetworkIssue(store.getType(),e)) {
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   514
                    // save this exception, we may need to throw it later
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   515
                    networkFailureException = new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   516
                        "Unable to determine revocation status due to " +
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   517
                        "network error", e, null, -1,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   518
                        BasicReason.UNDETERMINED_REVOCATION_STATUS);
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   519
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   520
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   521
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   522
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   523
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   524
            debug.println("RevocationChecker.checkCRLs() " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   525
                          "possible crls.size() = " + possibleCRLs.size());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   526
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   527
        boolean[] reasonsMask = new boolean[9];
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   528
        if (!possibleCRLs.isEmpty()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   529
            // Now that we have a list of possible CRLs, see which ones can
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   530
            // be approved
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   531
            approvedCRLs.addAll(verifyPossibleCRLs(possibleCRLs, cert, prevKey,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   532
                                                   signFlag, reasonsMask,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   533
                                                   anchors));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   534
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   535
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   536
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   537
            debug.println("RevocationChecker.checkCRLs() " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   538
                          "approved crls.size() = " + approvedCRLs.size());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   539
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   540
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   541
        // make sure that we have at least one CRL that _could_ cover
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   542
        // the certificate in question and all reasons are covered
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   543
        if (!approvedCRLs.isEmpty() &&
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   544
            Arrays.equals(reasonsMask, ALL_REASONS))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   545
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   546
            checkApprovedCRLs(cert, approvedCRLs);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   547
        } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   548
            // Check Distribution Points
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   549
            // all CRLs returned by the DP Fetcher have also been verified
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   550
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   551
                if (crlDP) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   552
                    approvedCRLs.addAll(DistributionPointFetcher.getCRLs(
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
   553
                                        sel, signFlag, prevKey, prevCert,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   554
                                        params.sigProvider(), certStores,
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   555
                                        reasonsMask, anchors, null));
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   556
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   557
            } catch (CertStoreException e) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   558
                if (e instanceof CertStoreTypeException) {
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   559
                    CertStoreTypeException cste = (CertStoreTypeException)e;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   560
                    if (CertStoreHelper.isCausedByNetworkIssue(cste.getType(),
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   561
                                                               e)) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   562
                        throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   563
                            "Unable to determine revocation status due to " +
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   564
                            "network error", e, null, -1,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   565
                            BasicReason.UNDETERMINED_REVOCATION_STATUS);
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   566
                    }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   567
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   568
                throw new CertPathValidatorException(e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   569
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   570
            if (!approvedCRLs.isEmpty() &&
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   571
                Arrays.equals(reasonsMask, ALL_REASONS))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   572
            {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   573
                checkApprovedCRLs(cert, approvedCRLs);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   574
            } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   575
                if (allowSeparateKey) {
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   576
                    try {
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   577
                        verifyWithSeparateSigningKey(cert, prevKey, signFlag,
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   578
                                                     stackedCerts);
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   579
                        return;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   580
                    } catch (CertPathValidatorException cpve) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   581
                        if (networkFailureException != null) {
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   582
                            // if a network issue previously prevented us from
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   583
                            // retrieving a CRL from one of the user-specified
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   584
                            // CertStores, throw it now so it can be handled
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   585
                            // appropriately
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   586
                            throw networkFailureException;
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   587
                        }
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   588
                        throw cpve;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   589
                    }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   590
                } else {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   591
                    if (networkFailureException != null) {
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   592
                        // if a network issue previously prevented us from
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   593
                        // retrieving a CRL from one of the user-specified
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   594
                        // CertStores, throw it now so it can be handled
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   595
                        // appropriately
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   596
                        throw networkFailureException;
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   597
                    }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   598
                    throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   599
                        "Could not determine revocation status", null, null, -1,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   600
                        BasicReason.UNDETERMINED_REVOCATION_STATUS);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   601
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   602
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   603
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   604
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   605
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   606
    private void checkApprovedCRLs(X509Certificate cert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   607
                                   Set<X509CRL> approvedCRLs)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   608
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   609
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   610
        // See if the cert is in the set of approved crls.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   611
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   612
            BigInteger sn = cert.getSerialNumber();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   613
            debug.println("RevocationChecker.checkApprovedCRLs() " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   614
                          "starting the final sweep...");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   615
            debug.println("RevocationChecker.checkApprovedCRLs()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   616
                          " cert SN: " + sn.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   617
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   618
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   619
        CRLReason reasonCode = CRLReason.UNSPECIFIED;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   620
        X509CRLEntryImpl entry = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   621
        for (X509CRL crl : approvedCRLs) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   622
            X509CRLEntry e = crl.getRevokedCertificate(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   623
            if (e != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   624
                try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   625
                    entry = X509CRLEntryImpl.toImpl(e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   626
                } catch (CRLException ce) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   627
                    throw new CertPathValidatorException(ce);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   628
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   629
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   630
                    debug.println("RevocationChecker.checkApprovedCRLs()"
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   631
                        + " CRL entry: " + entry.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   632
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   633
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   634
                /*
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   635
                 * Abort CRL validation and throw exception if there are any
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   636
                 * unrecognized critical CRL entry extensions (see section
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   637
                 * 5.3 of RFC 5280).
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   638
                 */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   639
                Set<String> unresCritExts = entry.getCriticalExtensionOIDs();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   640
                if (unresCritExts != null && !unresCritExts.isEmpty()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   641
                    /* remove any that we will process */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   642
                    unresCritExts.remove(ReasonCode_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   643
                    unresCritExts.remove(CertificateIssuer_Id.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   644
                    if (!unresCritExts.isEmpty()) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   645
                        throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   646
                            "Unrecognized critical extension(s) in revoked " +
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   647
                            "CRL entry");
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   648
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   649
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   650
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   651
                reasonCode = entry.getRevocationReason();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   652
                if (reasonCode == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   653
                    reasonCode = CRLReason.UNSPECIFIED;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   654
                }
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   655
                Date revocationDate = entry.getRevocationDate();
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   656
                if (revocationDate.before(params.date())) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   657
                    Throwable t = new CertificateRevokedException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   658
                        revocationDate, reasonCode,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   659
                        crl.getIssuerX500Principal(), entry.getExtensions());
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   660
                    throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   661
                        t.getMessage(), t, null, -1, BasicReason.REVOKED);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   662
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   663
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   664
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   665
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   666
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   667
    private void checkOCSP(X509Certificate cert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   668
                           Collection<String> unresolvedCritExts)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   669
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   670
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   671
        X509CertImpl currCert = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   672
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   673
            currCert = X509CertImpl.toImpl(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   674
        } catch (CertificateException ce) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   675
            throw new CertPathValidatorException(ce);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   676
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   677
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   678
        // The algorithm constraints of the OCSP trusted responder certificate
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   679
        // does not need to be checked in this code. The constraints will be
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   680
        // checked when the responder's certificate is validated.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   681
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   682
        OCSPResponse response = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   683
        CertId certId = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   684
        try {
16492
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   685
            if (issuerCert != null) {
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   686
                certId = new CertId(issuerCert,
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   687
                                    currCert.getSerialNumberObject());
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   688
            } else {
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   689
                // must be an anchor name and key
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   690
                certId = new CertId(anchor.getCA(), anchor.getCAPublicKey(),
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   691
                                    currCert.getSerialNumberObject());
4cd9aa345503 8010112: NullPointerException in sun.security.provider.certpath.CertId()
mullan
parents: 14402
diff changeset
   692
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   693
14402
424f3f23f1da 7171570: JEP 124 Potential API Changes
mullan
parents: 13806
diff changeset
   694
            // check if there is a cached OCSP response available
424f3f23f1da 7171570: JEP 124 Potential API Changes
mullan
parents: 13806
diff changeset
   695
            byte[] responseBytes = ocspResponses.get(cert);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   696
            if (responseBytes != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   697
                if (debug != null) {
14402
424f3f23f1da 7171570: JEP 124 Potential API Changes
mullan
parents: 13806
diff changeset
   698
                    debug.println("Found cached OCSP response");
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   699
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   700
                response = new OCSPResponse(responseBytes);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   701
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   702
                // verify the response
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   703
                byte[] nonce = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   704
                for (Extension ext : ocspExtensions) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   705
                    if (ext.getId().equals("1.3.6.1.5.5.7.48.1.2")) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   706
                        nonce = ext.getValue();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   707
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   708
                }
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 21329
diff changeset
   709
                response.verify(Collections.singletonList(certId), issuerCert,
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 21329
diff changeset
   710
                                responderCert, params.date(), nonce);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   711
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   712
            } else {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   713
                URI responderURI = (this.responderURI != null)
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   714
                                   ? this.responderURI
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   715
                                   : OCSP.getResponderURI(currCert);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   716
                if (responderURI == null) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   717
                    throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   718
                        "Certificate does not specify OCSP responder", null,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   719
                        null, -1);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   720
                }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   721
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   722
                response = OCSP.check(Collections.singletonList(certId),
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 21329
diff changeset
   723
                                      responderURI, issuerCert, responderCert,
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 21329
diff changeset
   724
                                      null, ocspExtensions);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   725
            }
18823
6c67701507df 8019627: RuntimeException gets obscured during OCSP cert revocation checking
vinnie
parents: 18587
diff changeset
   726
        } catch (IOException e) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   727
            throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   728
                "Unable to determine revocation status due to network error",
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   729
                e, null, -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   730
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   731
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   732
        RevocationStatus rs =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   733
            (RevocationStatus)response.getSingleResponse(certId);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   734
        RevocationStatus.CertStatus certStatus = rs.getCertStatus();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   735
        if (certStatus == RevocationStatus.CertStatus.REVOKED) {
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   736
            Date revocationTime = rs.getRevocationTime();
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   737
            if (revocationTime.before(params.date())) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   738
                Throwable t = new CertificateRevokedException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   739
                    revocationTime, rs.getRevocationReason(),
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 21329
diff changeset
   740
                    response.getSignerCertificate().getSubjectX500Principal(),
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   741
                    rs.getSingleExtensions());
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   742
                throw new CertPathValidatorException(t.getMessage(), t, null,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   743
                                                     -1, BasicReason.REVOKED);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   744
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   745
        } else if (certStatus == RevocationStatus.CertStatus.UNKNOWN) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   746
            throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   747
                "Certificate's revocation status is unknown", null,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   748
                params.certPath(), -1,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   749
                BasicReason.UNDETERMINED_REVOCATION_STATUS);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   750
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   751
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   752
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   753
    /*
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   754
     * Removes any non-hexadecimal characters from a string.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   755
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   756
    private static final String HEX_DIGITS = "0123456789ABCDEFabcdef";
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   757
    private static String stripOutSeparators(String value) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   758
        char[] chars = value.toCharArray();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   759
        StringBuilder hexNumber = new StringBuilder();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   760
        for (int i = 0; i < chars.length; i++) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   761
            if (HEX_DIGITS.indexOf(chars[i]) != -1) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   762
                hexNumber.append(chars[i]);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   763
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   764
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   765
        return hexNumber.toString();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   766
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   767
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   768
    /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   769
     * Checks that a cert can be used to verify a CRL.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   770
     *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   771
     * @param cert an X509Certificate to check
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   772
     * @return a boolean specifying if the cert is allowed to vouch for the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   773
     *         validity of a CRL
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   774
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   775
    static boolean certCanSignCrl(X509Certificate cert) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   776
        // if the cert doesn't include the key usage ext, or
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   777
        // the key usage ext asserts cRLSigning, return true,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   778
        // otherwise return false.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   779
        boolean[] keyUsage = cert.getKeyUsage();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   780
        if (keyUsage != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   781
            return keyUsage[6];
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   782
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   783
        return false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   784
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   785
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   786
    /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   787
     * Internal method that verifies a set of possible_crls,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   788
     * and sees if each is approved, based on the cert.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   789
     *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   790
     * @param crls a set of possible CRLs to test for acceptability
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   791
     * @param cert the certificate whose revocation status is being checked
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   792
     * @param signFlag <code>true</code> if prevKey was trusted to sign CRLs
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   793
     * @param prevKey the public key of the issuer of cert
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   794
     * @param reasonsMask the reason code mask
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   795
     * @param trustAnchors a <code>Set</code> of <code>TrustAnchor</code>s>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   796
     * @return a collection of approved crls (or an empty collection)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   797
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   798
    private static final boolean[] ALL_REASONS =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   799
        {true, true, true, true, true, true, true, true, true};
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   800
    private Collection<X509CRL> verifyPossibleCRLs(Set<X509CRL> crls,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   801
                                                   X509Certificate cert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   802
                                                   PublicKey prevKey,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   803
                                                   boolean signFlag,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   804
                                                   boolean[] reasonsMask,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   805
                                                   Set<TrustAnchor> anchors)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   806
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   807
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   808
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   809
            X509CertImpl certImpl = X509CertImpl.toImpl(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   810
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   811
                debug.println("RevocationChecker.verifyPossibleCRLs: " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   812
                              "Checking CRLDPs for "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   813
                              + certImpl.getSubjectX500Principal());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   814
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   815
            CRLDistributionPointsExtension ext =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   816
                certImpl.getCRLDistributionPointsExtension();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   817
            List<DistributionPoint> points = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   818
            if (ext == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   819
                // assume a DP with reasons and CRLIssuer fields omitted
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   820
                // and a DP name of the cert issuer.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   821
                // TODO add issuerAltName too
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   822
                X500Name certIssuer = (X500Name)certImpl.getIssuerDN();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   823
                DistributionPoint point = new DistributionPoint(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   824
                     new GeneralNames().add(new GeneralName(certIssuer)),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   825
                     null, null);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   826
                points = Collections.singletonList(point);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   827
            } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   828
                points = ext.get(CRLDistributionPointsExtension.POINTS);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   829
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   830
            Set<X509CRL> results = new HashSet<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   831
            for (DistributionPoint point : points) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   832
                for (X509CRL crl : crls) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   833
                    if (DistributionPointFetcher.verifyCRL(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   834
                            certImpl, point, crl, reasonsMask, signFlag,
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
   835
                            prevKey, null, params.sigProvider(), anchors,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   836
                            certStores, params.date()))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   837
                    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   838
                        results.add(crl);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   839
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   840
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   841
                if (Arrays.equals(reasonsMask, ALL_REASONS))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   842
                    break;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   843
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   844
            return results;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   845
        } catch (CertificateException | CRLException | IOException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   846
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   847
                debug.println("Exception while verifying CRL: "+e.getMessage());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   848
                e.printStackTrace();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   849
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   850
            return Collections.emptySet();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   851
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   852
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   853
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   854
    /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   855
     * We have a cert whose revocation status couldn't be verified by
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   856
     * a CRL issued by the cert that issued the CRL. See if we can
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   857
     * find a valid CRL issued by a separate key that can verify the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   858
     * revocation status of this certificate.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   859
     * <p>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   860
     * Note that this does not provide support for indirect CRLs,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   861
     * only CRLs signed with a different key (but the same issuer
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   862
     * name) as the certificate being checked.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   863
     *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   864
     * @param currCert the <code>X509Certificate</code> to be checked
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   865
     * @param prevKey the <code>PublicKey</code> that failed
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   866
     * @param signFlag <code>true</code> if that key was trusted to sign CRLs
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   867
     * @param stackedCerts a <code>Set</code> of <code>X509Certificate</code>s>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   868
     *                     whose revocation status depends on the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   869
     *                     non-revoked status of this cert. To avoid
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   870
     *                     circular dependencies, we assume they're
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   871
     *                     revoked while checking the revocation
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   872
     *                     status of this cert.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   873
     * @throws CertPathValidatorException if the cert's revocation status
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   874
     *         cannot be verified successfully with another key
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   875
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   876
    private void verifyWithSeparateSigningKey(X509Certificate cert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   877
                                              PublicKey prevKey,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   878
                                              boolean signFlag,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   879
                                              Set<X509Certificate> stackedCerts)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   880
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   881
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   882
        String msg = "revocation status";
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   883
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   884
            debug.println(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   885
                "RevocationChecker.verifyWithSeparateSigningKey()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   886
                " ---checking " + msg + "...");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   887
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   888
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   889
        // Reject circular dependencies - RFC 5280 is not explicit on how
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   890
        // to handle this, but does suggest that they can be a security
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   891
        // risk and can create unresolvable dependencies
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   892
        if ((stackedCerts != null) && stackedCerts.contains(cert)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   893
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   894
                debug.println(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   895
                    "RevocationChecker.verifyWithSeparateSigningKey()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   896
                    " circular dependency");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   897
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   898
            throw new CertPathValidatorException
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   899
                ("Could not determine revocation status", null, null, -1,
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
   900
                 BasicReason.UNDETERMINED_REVOCATION_STATUS);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   901
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   902
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   903
        // Try to find another key that might be able to sign
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   904
        // CRLs vouching for this cert.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   905
        // If prevKey wasn't trusted, maybe we just didn't have the right
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   906
        // path to it. Don't rule that key out.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   907
        if (!signFlag) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   908
            buildToNewKey(cert, null, stackedCerts);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   909
        } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   910
            buildToNewKey(cert, prevKey, stackedCerts);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   911
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   912
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   913
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   914
    /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   915
     * Tries to find a CertPath that establishes a key that can be
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   916
     * used to verify the revocation status of a given certificate.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   917
     * Ignores keys that have previously been tried. Throws a
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   918
     * CertPathValidatorException if no such key could be found.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   919
     *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   920
     * @param currCert the <code>X509Certificate</code> to be checked
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   921
     * @param prevKey the <code>PublicKey</code> of the certificate whose key
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   922
     *    cannot be used to vouch for the CRL and should be ignored
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   923
     * @param stackedCerts a <code>Set</code> of <code>X509Certificate</code>s>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   924
     *                     whose revocation status depends on the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   925
     *                     establishment of this path.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   926
     * @throws CertPathValidatorException on failure
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   927
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   928
    private static final boolean [] CRL_SIGN_USAGE =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   929
        { false, false, false, false, false, false, true };
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   930
    private void buildToNewKey(X509Certificate currCert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   931
                               PublicKey prevKey,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   932
                               Set<X509Certificate> stackedCerts)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   933
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   934
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   935
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   936
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   937
            debug.println("RevocationChecker.buildToNewKey()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   938
                          " starting work");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   939
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   940
        Set<PublicKey> badKeys = new HashSet<>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   941
        if (prevKey != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   942
            badKeys.add(prevKey);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   943
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   944
        X509CertSelector certSel = new RejectKeySelector(badKeys);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   945
        certSel.setSubject(currCert.getIssuerX500Principal());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   946
        certSel.setKeyUsage(CRL_SIGN_USAGE);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   947
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   948
        Set<TrustAnchor> newAnchors = anchor == null ?
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   949
                                      params.trustAnchors() :
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   950
                                      Collections.singleton(anchor);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   951
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   952
        PKIXBuilderParameters builderParams;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   953
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   954
            builderParams = new PKIXBuilderParameters(newAnchors, certSel);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   955
        } catch (InvalidAlgorithmParameterException iape) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   956
            throw new RuntimeException(iape); // should never occur
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   957
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   958
        builderParams.setInitialPolicies(params.initialPolicies());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   959
        builderParams.setCertStores(certStores);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   960
        builderParams.setExplicitPolicyRequired
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   961
            (params.explicitPolicyRequired());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   962
        builderParams.setPolicyMappingInhibited
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   963
            (params.policyMappingInhibited());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   964
        builderParams.setAnyPolicyInhibited(params.anyPolicyInhibited());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   965
        // Policy qualifiers must be rejected, since we don't have
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   966
        // any way to convey them back to the application.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   967
        // That's the default, so no need to write code.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   968
        builderParams.setDate(params.date());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   969
        // CertPathCheckers need to be cloned to start from fresh state
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   970
        builderParams.setCertPathCheckers(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   971
            params.getPKIXParameters().getCertPathCheckers());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   972
        builderParams.setSigProvider(params.sigProvider());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   973
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   974
        // Skip revocation during this build to detect circular
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   975
        // references. But check revocation afterwards, using the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   976
        // key (or any other that works).
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   977
        builderParams.setRevocationEnabled(false);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   978
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   979
        // check for AuthorityInformationAccess extension
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   980
        if (Builder.USE_AIA == true) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   981
            X509CertImpl currCertImpl = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   982
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   983
                currCertImpl = X509CertImpl.toImpl(currCert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   984
            } catch (CertificateException ce) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   985
                // ignore but log it
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   986
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   987
                    debug.println("RevocationChecker.buildToNewKey: " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   988
                                  "error decoding cert: " + ce);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   989
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   990
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   991
            AuthorityInfoAccessExtension aiaExt = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   992
            if (currCertImpl != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   993
                aiaExt = currCertImpl.getAuthorityInfoAccessExtension();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   994
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   995
            if (aiaExt != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   996
                List<AccessDescription> adList = aiaExt.getAccessDescriptions();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   997
                if (adList != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   998
                    for (AccessDescription ad : adList) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   999
                        CertStore cs = URICertStore.getInstance(ad);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1000
                        if (cs != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1001
                            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1002
                                debug.println("adding AIAext CertStore");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1003
                            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1004
                            builderParams.addCertStore(cs);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1005
                        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1006
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1007
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1008
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1009
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1010
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1011
        CertPathBuilder builder = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1012
        try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1013
            builder = CertPathBuilder.getInstance("PKIX");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1014
        } catch (NoSuchAlgorithmException nsae) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1015
            throw new CertPathValidatorException(nsae);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1016
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1017
        while (true) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1018
            try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1019
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1020
                    debug.println("RevocationChecker.buildToNewKey()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1021
                                  " about to try build ...");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1022
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1023
                PKIXCertPathBuilderResult cpbr =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1024
                    (PKIXCertPathBuilderResult)builder.build(builderParams);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1025
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1026
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1027
                    debug.println("RevocationChecker.buildToNewKey()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1028
                                  " about to check revocation ...");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1029
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1030
                // Now check revocation of all certs in path, assuming that
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1031
                // the stackedCerts are revoked.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1032
                if (stackedCerts == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1033
                    stackedCerts = new HashSet<X509Certificate>();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1034
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1035
                stackedCerts.add(currCert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1036
                TrustAnchor ta = cpbr.getTrustAnchor();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1037
                PublicKey prevKey2 = ta.getCAPublicKey();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1038
                if (prevKey2 == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1039
                    prevKey2 = ta.getTrustedCert().getPublicKey();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1040
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1041
                boolean signFlag = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1042
                List<? extends Certificate> cpList =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1043
                    cpbr.getCertPath().getCertificates();
22968
cf587f317be6 8031025: SQE test CertPath/CertPathBuilderTest/* failed with java.lang.IndexOutOfBoundsException
juh
parents: 22107
diff changeset
  1044
                if (cpList.isEmpty()) {
cf587f317be6 8031025: SQE test CertPath/CertPathBuilderTest/* failed with java.lang.IndexOutOfBoundsException
juh
parents: 22107
diff changeset
  1045
                    return;
cf587f317be6 8031025: SQE test CertPath/CertPathBuilderTest/* failed with java.lang.IndexOutOfBoundsException
juh
parents: 22107
diff changeset
  1046
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1047
                try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1048
                    for (int i = cpList.size()-1; i >= 0; i-- ) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1049
                        X509Certificate cert = (X509Certificate)cpList.get(i);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1050
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1051
                        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1052
                            debug.println("RevocationChecker.buildToNewKey()"
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1053
                                          + " index " + i + " checking "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1054
                                          + cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1055
                        }
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
  1056
                        checkCRLs(cert, prevKey2, null, signFlag, true,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1057
                                  stackedCerts, newAnchors);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1058
                        signFlag = certCanSignCrl(cert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1059
                        prevKey2 = cert.getPublicKey();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1060
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1061
                } catch (CertPathValidatorException cpve) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1062
                    // ignore it and try to get another key
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1063
                    badKeys.add(cpbr.getPublicKey());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1064
                    continue;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1065
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1066
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1067
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1068
                    debug.println("RevocationChecker.buildToNewKey()" +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1069
                                  " got key " + cpbr.getPublicKey());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1070
                }
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
  1071
                // Now check revocation on the current cert using that key and
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
  1072
                // the corresponding certificate.
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1073
                // If it doesn't check out, try to find a different key.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1074
                // And if we can't find a key, then return false.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1075
                PublicKey newKey = cpbr.getPublicKey();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1076
                try {
22107
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
  1077
                    checkCRLs(currCert, newKey, (X509Certificate) cpList.get(0),
3e6b0718041e 8007967: Infinite loop can happen in sun.security.provider.certpath.SunCertPathBuilder.depthFirstSearchForward()
juh
parents: 21819
diff changeset
  1078
                              true, false, null, params.trustAnchors());
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1079
                    // If that passed, the cert is OK!
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1080
                    return;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1081
                } catch (CertPathValidatorException cpve) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1082
                    // If it is revoked, rethrow exception
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1083
                    if (cpve.getReason() == BasicReason.REVOKED) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1084
                        throw cpve;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1085
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1086
                    // Otherwise, ignore the exception and
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1087
                    // try to get another key.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1088
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1089
                badKeys.add(newKey);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1090
            } catch (InvalidAlgorithmParameterException iape) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1091
                throw new CertPathValidatorException(iape);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1092
            } catch (CertPathBuilderException cpbe) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1093
                throw new CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1094
                    ("Could not determine revocation status", null, null,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1095
                     -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1096
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1097
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1098
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1099
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1100
    @Override
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1101
    public RevocationChecker clone() {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1102
        RevocationChecker copy = (RevocationChecker)super.clone();
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1103
        // we don't deep-copy the exceptions, but that is ok because they
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1104
        // are never modified after they are instantiated
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1105
        copy.softFailExceptions = new LinkedList<>(softFailExceptions);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1106
        return copy;
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1107
    }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 16492
diff changeset
  1108
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1109
    /*
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1110
     * This inner class extends the X509CertSelector to add an additional
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1111
     * check to make sure the subject public key isn't on a particular list.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1112
     * This class is used by buildToNewKey() to make sure the builder doesn't
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1113
     * end up with a CertPath to a public key that has already been rejected.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1114
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1115
    private static class RejectKeySelector extends X509CertSelector {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1116
        private final Set<PublicKey> badKeySet;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1117
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1118
        /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1119
         * Creates a new <code>RejectKeySelector</code>.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1120
         *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1121
         * @param badPublicKeys a <code>Set</code> of
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1122
         *                      <code>PublicKey</code>s that
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1123
         *                      should be rejected (or <code>null</code>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1124
         *                      if no such check should be done)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1125
         */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1126
        RejectKeySelector(Set<PublicKey> badPublicKeys) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1127
            this.badKeySet = badPublicKeys;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1128
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1129
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1130
        /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1131
         * Decides whether a <code>Certificate</code> should be selected.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1132
         *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1133
         * @param cert the <code>Certificate</code> to be checked
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1134
         * @return <code>true</code> if the <code>Certificate</code> should be
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1135
         *         selected, <code>false</code> otherwise
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1136
         */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1137
        @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1138
        public boolean match(Certificate cert) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1139
            if (!super.match(cert))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1140
                return(false);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1141
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1142
            if (badKeySet.contains(cert.getPublicKey())) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1143
                if (debug != null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1144
                    debug.println("RejectKeySelector.match: bad key");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1145
                return false;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1146
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1147
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1148
            if (debug != null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1149
                debug.println("RejectKeySelector.match: returning true");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1150
            return true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1151
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1152
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1153
        /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1154
         * Return a printable representation of the <code>CertSelector</code>.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1155
         *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1156
         * @return a <code>String</code> describing the contents of the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1157
         *         <code>CertSelector</code>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1158
         */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1159
        @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1160
        public String toString() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1161
            StringBuilder sb = new StringBuilder();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1162
            sb.append("RejectKeySelector: [\n");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1163
            sb.append(super.toString());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1164
            sb.append(badKeySet);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1165
            sb.append("]");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1166
            return sb.toString();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1167
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1168
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
  1169
}