jdk/src/share/classes/sun/security/provider/certpath/ConstraintsChecker.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 790 b91742db13e2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.PKIXCertPathChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.x509.PKIXExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.NameConstraintsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ConstraintsChecker is a <code>PKIXCertPathChecker</code> that checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * constraints information on a PKIX certificate, namely basic constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * and name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class ConstraintsChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /* length of cert path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final int certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* current maximum path length (as defined in PKIX) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int maxPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* current index of cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private NameConstraintsExtension prevNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static Set<String> supportedExts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates a ConstraintsChecker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param certPathLength the length of the certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @throws CertPathValidatorException if the checker cannot be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    ConstraintsChecker(int certPathLength) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.certPathLength = certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void init(boolean forward) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            maxPathLength = certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            prevNC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                ("forward checking not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public boolean isForwardCheckingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (supportedExts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            supportedExts = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            supportedExts.add(PKIXExtensions.BasicConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            supportedExts.add(PKIXExtensions.NameConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            supportedExts = Collections.unmodifiableSet(supportedExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return supportedExts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Performs the basic constraints and name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * checks on the certificate using its internal state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param cert the <code>Certificate</code> to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param unresCritExts a <code>Collection</code> of OID strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * representing the current set of unresolved critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws CertPathValidatorException if the specified certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * does not pass the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void check(Certificate cert, Collection<String> unresCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        X509Certificate currCert = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // MUST run NC check second, since it depends on BC check to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // update remainingCerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        checkBasicConstraints(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        verifyNameConstraints(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (unresCritExts != null && !unresCritExts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            unresCritExts.remove(PKIXExtensions.BasicConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            unresCritExts.remove(PKIXExtensions.NameConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
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
     * Internal method to check the name constraints against a cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private void verifyNameConstraints(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String msg = "name constraints";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // check name constraints only if there is a previous name constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // and either the currCert is the final cert or the currCert is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // self-issued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (prevNC != null && ((i == certPathLength) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                !X509CertImpl.isSelfIssued(currCert))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                debug.println("prevNC = " + prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                debug.println("currDN = " + currCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (!prevNC.verify(currCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    throw new CertPathValidatorException(msg + " check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new CertPathValidatorException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // merge name constraints regardless of whether cert is self-issued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        prevNC = mergeNameConstraints(currCert, prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Helper to fold sets of name constraints together
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static NameConstraintsExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        mergeNameConstraints(X509Certificate currCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            NameConstraintsExtension prevNC) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        X509CertImpl currCertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            currCertImpl = X509CertImpl.toImpl(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new CertPathValidatorException(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        NameConstraintsExtension newConstraints =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            currCertImpl.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            debug.println("prevNC = " + prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            debug.println("newNC = " + String.valueOf(newConstraints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // if there are no previous name constraints, we just return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // new name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (prevNC == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                debug.println("mergedNC = " + String.valueOf(newConstraints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (newConstraints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                return newConstraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // Make sure we do a clone here, because we're probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // going to modify this object later and we don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // be sharing it with a Certificate object!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                return (NameConstraintsExtension) newConstraints.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                // after merge, prevNC should contain the merged constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                prevNC.merge(newConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                throw new CertPathValidatorException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                debug.println("mergedNC = " + prevNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return prevNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Internal method to check that a given cert meets basic constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private void checkBasicConstraints(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        String msg = "basic constraints";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            debug.println("i = " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            debug.println("maxPathLength = " + maxPathLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        /* check if intermediate cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (i < certPathLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            int pathLenConstraint = currCert.getBasicConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (pathLenConstraint == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                throw new CertPathValidatorException(msg + " check failed: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    + "this is not a CA certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (!X509CertImpl.isSelfIssued(currCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (maxPathLength <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                   throw new CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        (msg + " check failed: pathLenConstraint violated - "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                         + "this cert must be the last cert in the "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                         + "certification path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                maxPathLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (pathLenConstraint < maxPathLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                maxPathLength = pathLenConstraint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            debug.println("after processing, maxPathLength = " + maxPathLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Merges the specified maxPathLength with the pathLenConstraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * obtained from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param cert the <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param maxPathLength the previous maximum path length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return the new maximum path length constraint (-1 means no more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * certificates can follow, Integer.MAX_VALUE means path length is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * unconstrained)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static int mergeBasicConstraints(X509Certificate cert, int maxPathLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int pathLenConstraint = cert.getBasicConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (!X509CertImpl.isSelfIssued(cert)) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (pathLenConstraint < maxPathLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            maxPathLength = pathLenConstraint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return maxPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
}