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