jdk/src/share/classes/sun/security/provider/certpath/ReverseState.java
author mullan
Mon, 17 Feb 2014 11:36:40 -0500
changeset 22977 f8f315760941
parent 19045 bc9a25fff6c5
permissions -rw-r--r--
8025708: Certificate Path Building problem with AKI serial number Reviewed-by: xuelei, juh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.PKIXCertPathChecker;
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
    33
import java.security.cert.PKIXRevocationChecker;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
    43
import sun.security.provider.certpath.PKIX.BuilderParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.x509.NameConstraintsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.x509.SubjectKeyIdentifierExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * A specification of a reverse PKIX validation state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * which is initialized by each build and updated each time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * certificate is added to the current path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
class ReverseState implements State {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* The subject DN of the last cert in the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    X500Principal subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* The subject public key of the last cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    PublicKey pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* The subject key identifier extension (if any) of the last cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    SubjectKeyIdentifierExtension subjKeyId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* The PKIX constrained/excluded subtrees state variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    NameConstraintsExtension nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* The PKIX explicit policy, policy mapping, and inhibit_any-policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       state variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    int explicitPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int policyMapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    int inhibitAnyPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int certIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    PolicyNodeImpl rootNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /* The number of remaining CA certs which may follow in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * -1: previous cert was an EE cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * 0: only EE certs may follow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * >0 and <Integer.MAX_VALUE:no more than this number of CA certs may follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Integer.MAX_VALUE: unlimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    int remainingCACerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* The list of user-defined checkers retrieved from the PKIXParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * instance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    ArrayList<PKIXCertPathChecker> userCheckers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /* Flag indicating if state is initial (path is just starting) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private boolean init = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /* the checker used for revocation status */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
    98
    RevocationChecker revChecker;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   100
    /* the algorithm checker */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   101
    AlgorithmChecker algorithmChecker;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   102
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   103
    /* the untrusted certificates checker */
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   104
    UntrustedChecker untrustedChecker;
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 7040
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* the trust anchor used to validate the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    TrustAnchor trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* Flag indicating if current cert can vouch for the CRL for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * the next cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   112
    boolean crlSign = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns a boolean flag indicating if the state is initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * (just starting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return boolean flag indicating if the state is initial (just starting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   120
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public boolean isInitial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Display state for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   128
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public String toString() {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   130
        StringBuilder sb = new StringBuilder();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   131
        sb.append("State [");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   132
        sb.append("\n  subjectDN of last cert: ").append(subjectDN);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   133
        sb.append("\n  subjectKeyIdentifier: ").append
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   134
                 (String.valueOf(subjKeyId));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   135
        sb.append("\n  nameConstraints: ").append(String.valueOf(nc));
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   136
        sb.append("\n  certIndex: ").append(certIndex);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   137
        sb.append("\n  explicitPolicy: ").append(explicitPolicy);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   138
        sb.append("\n  policyMapping:  ").append(policyMapping);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   139
        sb.append("\n  inhibitAnyPolicy:  ").append(inhibitAnyPolicy);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   140
        sb.append("\n  rootNode: ").append(rootNode);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   141
        sb.append("\n  remainingCACerts: ").append(remainingCACerts);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   142
        sb.append("\n  crlSign: ").append(crlSign);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   143
        sb.append("\n  init: ").append(init);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   144
        sb.append("\n]\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Initialize the state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   151
     * @param buildParams builder parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   153
    public void initState(BuilderParams buildParams)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * Initialize number of remainingCACerts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * Note that -1 maxPathLen implies unlimited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * 0 implies only an EE cert is acceptable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   161
        int maxPathLen = buildParams.maxPathLength();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   162
        remainingCACerts = (maxPathLen == -1) ? Integer.MAX_VALUE
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   163
                                              : maxPathLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /* Initialize explicit policy state variable */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   166
        if (buildParams.explicitPolicyRequired()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            explicitPolicy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // unconstrained if maxPathLen is -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // otherwise, we want to initialize this to the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // longest possible path + 1 (i.e. maxpathlen + finalcert + 1)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   172
            explicitPolicy = (maxPathLen == -1) ? maxPathLen : maxPathLen + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /* Initialize policy mapping state variable */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   176
        if (buildParams.policyMappingInhibited()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            policyMapping = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   179
            policyMapping = (maxPathLen == -1) ? maxPathLen : maxPathLen + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /* Initialize inhibit any policy state variable */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   183
        if (buildParams.anyPolicyInhibited()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            inhibitAnyPolicy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   186
            inhibitAnyPolicy = (maxPathLen == -1) ? maxPathLen : maxPathLen + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /* Initialize certIndex */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        certIndex = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /* Initialize policy tree */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   193
        Set<String> initExpPolSet = new HashSet<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        initExpPolSet.add(PolicyChecker.ANY_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   196
        rootNode = new PolicyNodeImpl(null, PolicyChecker.ANY_POLICY, null,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   197
                                      false, initExpPolSet, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * Initialize each user-defined checker
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   201
         * Shallow copy the checkers
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   203
        userCheckers = new ArrayList<>(buildParams.certPathCheckers());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   204
        /* initialize each checker (just in case) */
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   205
        for (PKIXCertPathChecker checker : userCheckers) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   206
            checker.init(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /* Start by trusting the cert to sign CRLs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        crlSign = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        init = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Update the state with the specified trust anchor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param anchor the most-trusted CA
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   219
     * @param buildParams builder parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   221
    public void updateState(TrustAnchor anchor, BuilderParams buildParams)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        throws CertificateException, IOException, CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        trustAnchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            updateState(trustedCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            X500Principal caName = anchor.getCA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            updateState(anchor.getCAPublicKey(), caName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   233
        // The user specified AlgorithmChecker and RevocationChecker may not be
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   234
        // able to set the trust anchor until now.
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   235
        boolean revCheckerAdded = false;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   236
        for (PKIXCertPathChecker checker : userCheckers) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   237
            if (checker instanceof AlgorithmChecker) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   238
                ((AlgorithmChecker)checker).trySetTrustAnchor(anchor);
19045
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   239
            } else if (checker instanceof PKIXRevocationChecker) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   240
                if (revCheckerAdded) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   241
                    throw new CertPathValidatorException(
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   242
                        "Only one PKIXRevocationChecker can be specified");
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   243
                }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   244
                // if it's our own, initialize it
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   245
                if (checker instanceof RevocationChecker) {
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   246
                    ((RevocationChecker)checker).init(anchor, buildParams);
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   247
                }
bc9a25fff6c5 8010748: Add PKIXRevocationChecker NO_FALLBACK option and improve SOFT_FAIL option
mullan
parents: 13806
diff changeset
   248
                ((PKIXRevocationChecker)checker).init(false);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   249
                revCheckerAdded = true;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   250
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   251
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   252
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   253
        // only create a RevocationChecker if revocation is enabled and
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   254
        // a PKIXRevocationChecker has not already been added
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   255
        if (buildParams.revocationEnabled() && !revCheckerAdded) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   256
            revChecker = new RevocationChecker(anchor, buildParams);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   257
            revChecker.init(false);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   258
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   259
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        init = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Update the state. This method is used when the most-trusted CA is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * a trusted public-key and caName, instead of a trusted cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param pubKey the public key of the trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param subjectDN the subject distinguished name of the trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private void updateState(PublicKey pubKey, X500Principal subjectDN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        /* update subject DN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.subjectDN = subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        /* update subject public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        this.pubKey = pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Update the state with the next certificate added to the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param cert the certificate which is used to update the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public void updateState(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        throws CertificateException, IOException, CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /* update subject DN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        subjectDN = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        /* check for key needing to inherit alg parameters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        X509CertImpl icert = X509CertImpl.toImpl(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        PublicKey newKey = cert.getPublicKey();
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 12861
diff changeset
   297
        if (PKIX.isDSAPublicKeyWithoutParams(newKey)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            newKey = BasicChecker.makeInheritedParamsKey(newKey, pubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /* update subject public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        pubKey = newKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * if this is a trusted cert (init == true), then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * don't update any of the remaining fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (init) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            init = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /* update subject key identifier */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        subjKeyId = icert.getSubjectKeyIdentifierExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        /* update crlSign */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   317
        crlSign = RevocationChecker.certCanSignCrl(cert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        /* update current name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (nc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            nc.merge(icert.getNameConstraintsExtension());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            nc = icert.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (nc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // Make sure we do a clone here, because we're probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // going to modify this object later and we don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // be sharing it with a Certificate object!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                nc = (NameConstraintsExtension) nc.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /* update policy state variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        explicitPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            PolicyChecker.mergeExplicitPolicy(explicitPolicy, icert, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        policyMapping =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            PolicyChecker.mergePolicyMapping(policyMapping, icert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        inhibitAnyPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            PolicyChecker.mergeInhibitAnyPolicy(inhibitAnyPolicy, icert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        certIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * Update remaining CA certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        remainingCACerts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            ConstraintsChecker.mergeBasicConstraints(cert, remainingCACerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        init = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns a boolean flag indicating if a key lacking necessary key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * algorithm parameters has been encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return boolean flag indicating if key lacking parameters encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   356
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public boolean keyParamsNeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        /* when building in reverse, we immediately get parameters needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * or else throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Clone current state. The state is cloned as each cert is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * added to the path. This is necessary if backtracking occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * and a prior state needs to be restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Note that this is a SMART clone. Not all fields are fully copied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * because some of them (e.g., subjKeyId) will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * not have their contents modified by subsequent calls to updateState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10419
diff changeset
   373
    @Override
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7040
diff changeset
   374
    @SuppressWarnings("unchecked") // Safe casts assuming clone() works correctly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            ReverseState clonedState = (ReverseState) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            /* clone checkers, if cloneable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            clonedState.userCheckers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        (ArrayList<PKIXCertPathChecker>)userCheckers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            ListIterator<PKIXCertPathChecker> li =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        clonedState.userCheckers.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            while (li.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                PKIXCertPathChecker checker = li.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (checker instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    li.set((PKIXCertPathChecker)checker.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            /* make copy of name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (nc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                clonedState.nc = (NameConstraintsExtension) nc.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            /* make copy of policy tree */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (rootNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                clonedState.rootNode = rootNode.copyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return clonedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10336
diff changeset
   403
            throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}