jdk/src/share/classes/sun/security/provider/certpath/ReverseState.java
author xuelei
Mon, 01 Nov 2010 07:57:46 -0700
changeset 7040 659824c2a550
parent 5506 202f599c92aa
child 10336 0bb1999251f8
child 11900 9b1d5bef8038
permissions -rw-r--r--
6792180: Enhance to reject weak algorithms or conform to crypto recommendations Reviewed-by: mullan, weijun, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2010, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.interfaces.DSAPublicKey;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.x509.NameConstraintsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.x509.SubjectKeyIdentifierExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * A specification of a reverse PKIX validation state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * which is initialized by each build and updated each time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * certificate is added to the current path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class ReverseState implements State {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* The subject DN of the last cert in the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    X500Principal subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* The subject public key of the last cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    PublicKey pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* The subject key identifier extension (if any) of the last cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    SubjectKeyIdentifierExtension subjKeyId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* The PKIX constrained/excluded subtrees state variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    NameConstraintsExtension nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* The PKIX explicit policy, policy mapping, and inhibit_any-policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
       state variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int explicitPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    int policyMapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int inhibitAnyPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    int certIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    PolicyNodeImpl rootNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* The number of remaining CA certs which may follow in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * -1: previous cert was an EE cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * 0: only EE certs may follow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * >0 and <Integer.MAX_VALUE:no more than this number of CA certs may follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Integer.MAX_VALUE: unlimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int remainingCACerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* The list of user-defined checkers retrieved from the PKIXParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * instance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    ArrayList<PKIXCertPathChecker> userCheckers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* Flag indicating if state is initial (path is just starting) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private boolean init = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /* the checker used for revocation status */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public CrlRevocationChecker crlChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    99
    /* the algorithm checker */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   100
    AlgorithmChecker algorithmChecker;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /* the trust anchor used to validate the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    TrustAnchor trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* Flag indicating if current cert can vouch for the CRL for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the next cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public boolean crlSign = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns a boolean flag indicating if the state is initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * (just starting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return boolean flag indicating if the state is initial (just starting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public boolean isInitial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Display state for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            sb.append("State [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            sb.append("\n  subjectDN of last cert: " + subjectDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            sb.append("\n  subjectKeyIdentifier: " + String.valueOf(subjKeyId));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            sb.append("\n  nameConstraints: " + String.valueOf(nc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            sb.append("\n  certIndex: " + certIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            sb.append("\n  explicitPolicy: " + explicitPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            sb.append("\n  policyMapping:  " + policyMapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            sb.append("\n  inhibitAnyPolicy:  " + inhibitAnyPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            sb.append("\n  rootNode: " + rootNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            sb.append("\n  remainingCACerts: " + remainingCACerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            sb.append("\n  crlSign: " + crlSign);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            sb.append("\n  init: " + init);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            sb.append("\n]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                debug.println("ReverseState.toString() unexpected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param maxPathLen The maximum number of CA certs in a path, where -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * means unlimited and 0 means only a single EE cert is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param explicitPolicyRequired True, if explicit policy is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param policyMappingInhibited True, if policy mapping is inhibited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param anyPolicyInhibited True, if any policy is inhibited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param certPathCheckers the list of user-defined PKIXCertPathCheckers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void initState(int maxPathLen, boolean explicitPolicyRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        boolean policyMappingInhibited, boolean anyPolicyInhibited,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        List<PKIXCertPathChecker> certPathCheckers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * Initialize number of remainingCACerts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * Note that -1 maxPathLen implies unlimited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * 0 implies only an EE cert is acceptable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        remainingCACerts = (maxPathLen == -1 ? Integer.MAX_VALUE : maxPathLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /* Initialize explicit policy state variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (explicitPolicyRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            explicitPolicy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // unconstrained if maxPathLen is -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // otherwise, we want to initialize this to the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // longest possible path + 1 (i.e. maxpathlen + finalcert + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            explicitPolicy = (maxPathLen == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                ? maxPathLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                : maxPathLen + 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 policy mapping state variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (policyMappingInhibited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            policyMapping = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            policyMapping = (maxPathLen == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ? maxPathLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                : maxPathLen + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /* Initialize inhibit any policy state variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (anyPolicyInhibited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            inhibitAnyPolicy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            inhibitAnyPolicy = (maxPathLen == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                ? maxPathLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                : maxPathLen + 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 certIndex */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        certIndex = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /* Initialize policy tree */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Set<String> initExpPolSet = new HashSet<String>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        initExpPolSet.add(PolicyChecker.ANY_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        rootNode = new PolicyNodeImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            (null, PolicyChecker.ANY_POLICY, null, false, initExpPolSet, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * Initialize each user-defined checker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (certPathCheckers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            /* Shallow copy the checkers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            userCheckers = new ArrayList<PKIXCertPathChecker>(certPathCheckers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            /* initialize each checker (just in case) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            for (PKIXCertPathChecker checker : certPathCheckers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                checker.init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            userCheckers = new ArrayList<PKIXCertPathChecker>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        /* Start by trusting the cert to sign CRLs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        crlSign = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        init = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Update the state with the specified trust anchor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param anchor the most-trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void updateState(TrustAnchor anchor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        throws CertificateException, IOException, CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        trustAnchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            updateState(trustedCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            X500Principal caName = anchor.getCA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            updateState(anchor.getCAPublicKey(), caName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   247
        // The user specified AlgorithmChecker may not be
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   248
        // able to set the trust anchor until now.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   249
        for (PKIXCertPathChecker checker : userCheckers) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   250
            if (checker instanceof AlgorithmChecker) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   251
                ((AlgorithmChecker)checker).trySetTrustAnchor(anchor);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   252
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   253
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   254
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        init = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Update the state. This method is used when the most-trusted CA is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * a trusted public-key and caName, instead of a trusted cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param pubKey the public key of the trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param subjectDN the subject distinguished name of the trusted CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private void updateState(PublicKey pubKey, X500Principal subjectDN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /* update subject DN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        this.subjectDN = subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /* update subject public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this.pubKey = pubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Update the state with the next certificate added to the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param cert the certificate which is used to update the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public void updateState(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        throws CertificateException, IOException, CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /* update subject DN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        subjectDN = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /* check for key needing to inherit alg parameters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        X509CertImpl icert = X509CertImpl.toImpl(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        PublicKey newKey = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (newKey instanceof DSAPublicKey &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            (((DSAPublicKey)newKey).getParams() == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            newKey = BasicChecker.makeInheritedParamsKey(newKey, pubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        /* update subject public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        pubKey = newKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * if this is a trusted cert (init == true), then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * don't update any of the remaining fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (init) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            init = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /* update subject key identifier */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        subjKeyId = icert.getSubjectKeyIdentifierExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        /* update crlSign */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        crlSign = CrlRevocationChecker.certCanSignCrl(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        /* update current name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (nc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            nc.merge(icert.getNameConstraintsExtension());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            nc = icert.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (nc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // Make sure we do a clone here, because we're probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // going to modify this object later and we don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // be sharing it with a Certificate object!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                nc = (NameConstraintsExtension) nc.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /* update policy state variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        explicitPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            PolicyChecker.mergeExplicitPolicy(explicitPolicy, icert, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        policyMapping =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            PolicyChecker.mergePolicyMapping(policyMapping, icert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        inhibitAnyPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            PolicyChecker.mergeInhibitAnyPolicy(inhibitAnyPolicy, icert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        certIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * Update remaining CA certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        remainingCACerts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            ConstraintsChecker.mergeBasicConstraints(cert, remainingCACerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        init = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Returns a boolean flag indicating if a key lacking necessary key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * algorithm parameters has been encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return boolean flag indicating if key lacking parameters encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean keyParamsNeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        /* when building in reverse, we immediately get parameters needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         * or else throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Clone current state. The state is cloned as each cert is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * added to the path. This is necessary if backtracking occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * and a prior state needs to be restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Note that this is a SMART clone. Not all fields are fully copied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * because some of them (e.g., subjKeyId) will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * not have their contents modified by subsequent calls to updateState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            ReverseState clonedState = (ReverseState) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            /* clone checkers, if cloneable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            clonedState.userCheckers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        (ArrayList<PKIXCertPathChecker>)userCheckers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            ListIterator<PKIXCertPathChecker> li =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        clonedState.userCheckers.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            while (li.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                PKIXCertPathChecker checker = li.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                if (checker instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    li.set((PKIXCertPathChecker)checker.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            /* make copy of name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (nc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                clonedState.nc = (NameConstraintsExtension) nc.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            /* make copy of policy tree */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (rootNode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                clonedState.rootNode = rootNode.copyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return clonedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new InternalError(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}