jdk/src/java.base/share/classes/sun/security/provider/certpath/PKIX.java
author juh
Fri, 17 Apr 2015 11:51:47 -0700
changeset 29973 188affdeeed2
parent 25859 3317bb8137f4
child 39465 8c06651d9138
permissions -rw-r--r--
7194452: Remove "Reverse" PKIX CertPathBuilder implementation 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
/*
29973
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 25859
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
package sun.security.provider.certpath;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    26
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    27
import java.security.InvalidAlgorithmParameterException;
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    28
import java.security.PublicKey;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    29
import java.security.cert.*;
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    30
import java.security.interfaces.DSAPublicKey;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    31
import java.util.*;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    32
import javax.security.auth.x500.X500Principal;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    33
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    34
import sun.security.util.Debug;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    35
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    36
/**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    37
 * Common utility methods and classes used by the PKIX CertPathValidator and
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    38
 * CertPathBuilder implementation.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    39
 */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    40
class PKIX {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    41
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    42
    private static final Debug debug = Debug.getInstance("certpath");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    43
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    44
    private PKIX() { }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    45
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    46
    static boolean isDSAPublicKeyWithoutParams(PublicKey publicKey) {
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    47
        return (publicKey instanceof DSAPublicKey &&
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    48
               ((DSAPublicKey)publicKey).getParams() == null);
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    49
    }
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13800
diff changeset
    50
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    51
    static ValidatorParams checkParams(CertPath cp, CertPathParameters params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    52
        throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    53
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    54
        if (!(params instanceof PKIXParameters)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    55
            throw new InvalidAlgorithmParameterException("inappropriate "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    56
                + "params, must be an instance of PKIXParameters");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    57
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    58
        return new ValidatorParams(cp, (PKIXParameters)params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    59
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    60
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    61
    static BuilderParams checkBuilderParams(CertPathParameters params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    62
        throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    63
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    64
        if (!(params instanceof PKIXBuilderParameters)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    65
            throw new InvalidAlgorithmParameterException("inappropriate "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    66
                + "params, must be an instance of PKIXBuilderParameters");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    67
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    68
        return new BuilderParams((PKIXBuilderParameters)params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    69
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    70
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    71
    /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    72
     * PKIXParameters that are shared by the PKIX CertPathValidator
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    73
     * implementation. Provides additional functionality and avoids
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    74
     * unnecessary cloning.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    75
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    76
    static class ValidatorParams {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    77
        private final PKIXParameters params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    78
        private CertPath certPath;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    79
        private List<PKIXCertPathChecker> checkers;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    80
        private List<CertStore> stores;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    81
        private boolean gotDate;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    82
        private Date date;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    83
        private Set<String> policies;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    84
        private boolean gotConstraints;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    85
        private CertSelector constraints;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    86
        private Set<TrustAnchor> anchors;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    87
        private List<X509Certificate> certs;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    88
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    89
        ValidatorParams(CertPath cp, PKIXParameters params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    90
            throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    91
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    92
            this(params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    93
            if (!cp.getType().equals("X.509") && !cp.getType().equals("X509")) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    94
                throw new InvalidAlgorithmParameterException("inappropriate "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    95
                    + "CertPath type specified, must be X.509 or X509");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    96
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    97
            this.certPath = cp;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    98
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
    99
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   100
        ValidatorParams(PKIXParameters params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   101
            throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   102
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   103
            this.anchors = params.getTrustAnchors();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   104
            // Make sure that none of the trust anchors include name constraints
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   105
            // (not supported).
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   106
            for (TrustAnchor anchor : this.anchors) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   107
                if (anchor.getNameConstraints() != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   108
                    throw new InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   109
                        ("name constraints in trust anchor not supported");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   110
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   111
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   112
            this.params = params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   113
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   114
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   115
        CertPath certPath() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   116
            return certPath;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   117
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   118
        // called by CertPathBuilder after path has been built
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   119
        void setCertPath(CertPath cp) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   120
            this.certPath = cp;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   121
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   122
        List<X509Certificate> certificates() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   123
            if (certs == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   124
                if (certPath == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   125
                    certs = Collections.emptyList();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   126
                } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   127
                    // Reverse the ordering for validation so that the target
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   128
                    // cert is the last certificate
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   129
                    @SuppressWarnings("unchecked")
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   130
                    List<X509Certificate> xc = new ArrayList<>
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   131
                        ((List<X509Certificate>)certPath.getCertificates());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   132
                    Collections.reverse(xc);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   133
                    certs = xc;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   134
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   135
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   136
            return certs;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   137
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   138
        List<PKIXCertPathChecker> certPathCheckers() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   139
            if (checkers == null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   140
                checkers = params.getCertPathCheckers();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   141
            return checkers;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   142
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   143
        List<CertStore> certStores() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   144
            if (stores == null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   145
                stores = params.getCertStores();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   146
            return stores;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   147
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   148
        Date date() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   149
            if (!gotDate) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   150
                date = params.getDate();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   151
                if (date == null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   152
                    date = new Date();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   153
                gotDate = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   154
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   155
            return date;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   156
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   157
        Set<String> initialPolicies() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   158
            if (policies == null)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   159
                policies = params.getInitialPolicies();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   160
            return policies;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   161
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   162
        CertSelector targetCertConstraints() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   163
            if (!gotConstraints) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   164
                constraints = params.getTargetCertConstraints();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   165
                gotConstraints = true;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   166
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   167
            return constraints;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   168
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   169
        Set<TrustAnchor> trustAnchors() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   170
            return anchors;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   171
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   172
        boolean revocationEnabled() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   173
            return params.isRevocationEnabled();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   174
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   175
        boolean policyMappingInhibited() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   176
            return params.isPolicyMappingInhibited();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   177
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   178
        boolean explicitPolicyRequired() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   179
            return params.isExplicitPolicyRequired();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   180
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   181
        boolean policyQualifiersRejected() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   182
            return params.getPolicyQualifiersRejected();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   183
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   184
        String sigProvider() { return params.getSigProvider(); }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   185
        boolean anyPolicyInhibited() { return params.isAnyPolicyInhibited(); }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   186
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   187
        // in rare cases we need access to the original params, for example
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   188
        // in order to clone CertPathCheckers before building a new chain
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   189
        PKIXParameters getPKIXParameters() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   190
            return params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   191
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   192
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   193
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   194
    static class BuilderParams extends ValidatorParams {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   195
        private PKIXBuilderParameters params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   196
        private List<CertStore> stores;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   197
        private X500Principal targetSubject;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   198
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   199
        BuilderParams(PKIXBuilderParameters params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   200
            throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   201
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   202
            super(params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   203
            checkParams(params);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   204
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   205
        private void checkParams(PKIXBuilderParameters params)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   206
            throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   207
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   208
            CertSelector sel = targetCertConstraints();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   209
            if (!(sel instanceof X509CertSelector)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   210
                throw new InvalidAlgorithmParameterException("the "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   211
                    + "targetCertConstraints parameter must be an "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   212
                    + "X509CertSelector");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   213
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   214
            this.params = params;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   215
            this.targetSubject = getTargetSubject(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   216
                certStores(), (X509CertSelector)targetCertConstraints());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   217
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   218
        @Override List<CertStore> certStores() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   219
            if (stores == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   220
                // reorder CertStores so that local CertStores are tried first
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   221
                stores = new ArrayList<>(params.getCertStores());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   222
                Collections.sort(stores, new CertStoreComparator());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   223
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   224
            return stores;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   225
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   226
        int maxPathLength() { return params.getMaxPathLength(); }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   227
        PKIXBuilderParameters params() { return params; }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   228
        X500Principal targetSubject() { return targetSubject; }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   229
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   230
        /**
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   231
         * Returns the target subject DN from the first X509Certificate that
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   232
         * is fetched that matches the specified X509CertSelector.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   233
         */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   234
        private static X500Principal getTargetSubject(List<CertStore> stores,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   235
                                                      X509CertSelector sel)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   236
            throws InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   237
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   238
            X500Principal subject = sel.getSubject();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   239
            if (subject != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   240
                return subject;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   241
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   242
            X509Certificate cert = sel.getCertificate();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   243
            if (cert != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   244
                subject = cert.getSubjectX500Principal();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   245
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   246
            if (subject != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   247
                return subject;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   248
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   249
            for (CertStore store : stores) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   250
                try {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   251
                    Collection<? extends Certificate> certs =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   252
                        (Collection<? extends Certificate>)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   253
                            store.getCertificates(sel);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   254
                    if (!certs.isEmpty()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   255
                        X509Certificate xc =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   256
                            (X509Certificate)certs.iterator().next();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   257
                        return xc.getSubjectX500Principal();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   258
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   259
                } catch (CertStoreException e) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   260
                    // ignore but log it
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   261
                    if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   262
                        debug.println("BuilderParams.getTargetSubjectDN: " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   263
                            "non-fatal exception retrieving certs: " + e);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   264
                        e.printStackTrace();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   265
                    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   266
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   267
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   268
            throw new InvalidAlgorithmParameterException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   269
                ("Could not determine unique target subject");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   270
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   271
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   272
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   273
    /**
13800
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   274
     * A CertStoreException with additional information about the type of
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   275
     * CertStore that generated the exception.
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   276
     */
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   277
    static class CertStoreTypeException extends CertStoreException {
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   278
        private static final long serialVersionUID = 7463352639238322556L;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   279
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   280
        private final String type;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   281
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   282
        CertStoreTypeException(String type, CertStoreException cse) {
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   283
            super(cse.getMessage(), cse.getCause());
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   284
            this.type = type;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   285
        }
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   286
        String getType() {
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   287
            return type;
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   288
        }
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   289
    }
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   290
2fd4a82efe9c 7176627: CertPath/jep124/PreferCRL_SoftFail test fails (Could not determine revocation status)
mullan
parents: 12860
diff changeset
   291
    /**
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   292
     * Comparator that orders CertStores so that local CertStores come before
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   293
     * remote CertStores.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   294
     */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   295
    private static class CertStoreComparator implements Comparator<CertStore> {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   296
        @Override
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   297
        public int compare(CertStore store1, CertStore store2) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   298
            if (store1.getType().equals("Collection") ||
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   299
                store1.getCertStoreParameters() instanceof
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   300
                CollectionCertStoreParameters) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   301
                return -1;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   302
            } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   303
                return 1;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   304
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   305
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   306
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
diff changeset
   307
}