jdk/src/share/classes/sun/security/provider/certpath/ConstraintsChecker.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2941 af92a83128fe
child 12860 9ffbd4e43413
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2941
diff changeset
     2
 * Copyright (c) 2000, 2008, 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: 2941
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: 2941
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: 2941
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2941
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2941
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.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertificateException;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
    35
import java.security.cert.CertPathValidatorException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.PKIXCertPathChecker;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
    38
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.PKIXExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.x509.NameConstraintsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * ConstraintsChecker is a <code>PKIXCertPathChecker</code> that checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * constraints information on a PKIX certificate, namely basic constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * and name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class ConstraintsChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* length of cert path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final int certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* current maximum path length (as defined in PKIX) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int maxPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* current index of cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private NameConstraintsExtension prevNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
790
b91742db13e2 6673277: Thread unsafe lazy initialization code in sun.security.provider.certpath.*Checker classes
mullan
parents: 2
diff changeset
    63
    private Set<String> supportedExts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a ConstraintsChecker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param certPathLength the length of the certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws CertPathValidatorException if the checker cannot be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    ConstraintsChecker(int certPathLength) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.certPathLength = certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void init(boolean forward) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (!forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            maxPathLength = certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            prevNC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                ("forward checking not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public boolean isForwardCheckingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (supportedExts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            supportedExts = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            supportedExts.add(PKIXExtensions.BasicConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            supportedExts.add(PKIXExtensions.NameConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            supportedExts = Collections.unmodifiableSet(supportedExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return supportedExts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Performs the basic constraints and name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * checks on the certificate using its internal state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param cert the <code>Certificate</code> to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param unresCritExts a <code>Collection</code> of OID strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * representing the current set of unresolved critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws CertPathValidatorException if the specified certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * does not pass the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void check(Certificate cert, Collection<String> unresCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        X509Certificate currCert = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // MUST run NC check second, since it depends on BC check to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // update remainingCerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        checkBasicConstraints(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        verifyNameConstraints(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (unresCritExts != null && !unresCritExts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            unresCritExts.remove(PKIXExtensions.BasicConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            unresCritExts.remove(PKIXExtensions.NameConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Internal method to check the name constraints against a cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private void verifyNameConstraints(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String msg = "name constraints";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // check name constraints only if there is a previous name constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // and either the currCert is the final cert or the currCert is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // self-issued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (prevNC != null && ((i == certPathLength) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                !X509CertImpl.isSelfIssued(currCert))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                debug.println("prevNC = " + prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                debug.println("currDN = " + currCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if (!prevNC.verify(currCert)) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   151
                    throw new CertPathValidatorException(msg + " check failed",
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   152
                        null, null, -1, PKIXReason.INVALID_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new CertPathValidatorException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // merge name constraints regardless of whether cert is self-issued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        prevNC = mergeNameConstraints(currCert, prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Helper to fold sets of name constraints together
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    static NameConstraintsExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        mergeNameConstraints(X509Certificate currCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            NameConstraintsExtension prevNC) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        X509CertImpl currCertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            currCertImpl = X509CertImpl.toImpl(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new CertPathValidatorException(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        NameConstraintsExtension newConstraints =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            currCertImpl.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            debug.println("prevNC = " + prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            debug.println("newNC = " + String.valueOf(newConstraints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // if there are no previous name constraints, we just return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // new name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (prevNC == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                debug.println("mergedNC = " + String.valueOf(newConstraints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (newConstraints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return newConstraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // Make sure we do a clone here, because we're probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // going to modify this object later and we don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // be sharing it with a Certificate object!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                return (NameConstraintsExtension) newConstraints.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // after merge, prevNC should contain the merged constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                prevNC.merge(newConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                throw new CertPathValidatorException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                debug.println("mergedNC = " + prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return prevNC;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Internal method to check that a given cert meets basic constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void checkBasicConstraints(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        String msg = "basic constraints";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            debug.println("i = " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            debug.println("maxPathLength = " + maxPathLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        /* check if intermediate cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (i < certPathLength) {
2941
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   231
            // RFC5280: If certificate i is a version 3 certificate, verify
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   232
            // that the basicConstraints extension is present and that cA is
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   233
            // set to TRUE.  (If certificate i is a version 1 or version 2
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   234
            // certificate, then the application MUST either verify that
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   235
            // certificate i is a CA certificate through out-of-band means
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   236
            // or reject the certificate.  Conforming implementations may
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   237
            // choose to reject all version 1 and version 2 intermediate
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   238
            // certificates.)
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   239
            //
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   240
            // We choose to reject all version 1 and version 2 intermediate
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   241
            // certificates except that it is self issued by the trust
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   242
            // anchor in order to support key rollover or changes in
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   243
            // certificate policies.
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   244
            int pathLenConstraint = -1;
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   245
            if (currCert.getVersion() < 3) {    // version 1 or version 2
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   246
                if (i == 1) {                   // issued by a trust anchor
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   247
                    if (X509CertImpl.isSelfIssued(currCert)) {
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   248
                        pathLenConstraint = Integer.MAX_VALUE;
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   249
                    }
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   250
                }
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   251
            } else {
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   252
                pathLenConstraint = currCert.getBasicConstraints();
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   253
            }
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   254
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (pathLenConstraint == -1) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   256
                throw new CertPathValidatorException
2941
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   257
                    (msg + " check failed: this is not a CA certificate",
af92a83128fe 6847459: Allow trust anchor self-issued intermediate version 1 and version 2 certificate
xuelei
parents: 1238
diff changeset
   258
                     null, null, -1, PKIXReason.NOT_CA_CERT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (!X509CertImpl.isSelfIssued(currCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (maxPathLength <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                   throw new CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        (msg + " check failed: pathLenConstraint violated - "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                         + "this cert must be the last cert in the "
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   266
                         + "certification path", null, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   267
                         PKIXReason.PATH_TOO_LONG);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                maxPathLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (pathLenConstraint < maxPathLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                maxPathLength = pathLenConstraint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            debug.println("after processing, maxPathLength = " + maxPathLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Merges the specified maxPathLength with the pathLenConstraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * obtained from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param cert the <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param maxPathLength the previous maximum path length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return the new maximum path length constraint (-1 means no more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * certificates can follow, Integer.MAX_VALUE means path length is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * unconstrained)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static int mergeBasicConstraints(X509Certificate cert, int maxPathLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int pathLenConstraint = cert.getBasicConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!X509CertImpl.isSelfIssued(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            maxPathLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (pathLenConstraint < maxPathLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            maxPathLength = pathLenConstraint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return maxPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}