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