jdk/src/java.base/share/classes/sun/security/provider/certpath/PKIXMasterCertPathValidator.java
author juh
Tue, 03 Mar 2015 14:16:49 -0800
changeset 29264 5172066a2da6
parent 25859 3317bb8137f4
child 37726 bbecfff95ec3
permissions -rw-r--r--
8054037: Improve tracing for java.security.debug=certpath 8055207: keystore and truststore debug output could be much better Reviewed-by: mullan, coffeys, jnimeh
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: 2283
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: 2283
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: 2283
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2283
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2283
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 sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    33
import java.util.StringJoiner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.PKIXCertPathChecker;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    37
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class is initialized with a list of <code>PKIXCertPathChecker</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * and is used to verify the certificates in a <code>CertPath</code> by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * feeding each certificate to each <code>PKIXCertPathChecker</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class PKIXMasterCertPathValidator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Validates a certification path consisting exclusively of
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    54
     * <code>X509Certificate</code>s using the specified
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    55
     * <code>PKIXCertPathChecker</code>s. It is assumed that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * <code>PKIXCertPathChecker</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * have been initialized with any input parameters they may need.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param cpOriginal the original X509 CertPath passed in by the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param reversedCertList the reversed X509 CertPath (as a List)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    61
     * @param certPathCheckers the PKIXCertPathCheckers
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    62
     * @throws CertPathValidatorException if cert path does not validate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    64
    static void validate(CertPath cpOriginal,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    65
                         List<X509Certificate> reversedCertList,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    66
                         List<PKIXCertPathChecker> certPathCheckers)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // we actually process reversedCertList, but we keep cpOriginal because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // we need to return the original certPath when we throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // we will also need to modify the index appropriately when we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int cpSize = reversedCertList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            debug.println("--------------------------------------------------"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                  + "------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            debug.println("Executing PKIX certification path validation "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                  + "algorithm.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i = 0; i < cpSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            /* The basic loop algorithm is that we get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
             * current certificate, we verify the current certificate using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             * information from the previous certificate and from the state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             * and we modify the state for the next loop by setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * current certificate of this loop to be the previous certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * of the next loop. The state is initialized during first loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             */
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    92
            X509Certificate currCert = reversedCertList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    94
            if (debug != null) {
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    95
                debug.println("Checking cert" + (i+1) + " - Subject: " +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    96
                    currCert.getSubjectX500Principal());
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    97
            }
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
    98
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    99
            Set<String> unresCritExts = currCert.getCriticalExtensionOIDs();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   100
            if (unresCritExts == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   101
                unresCritExts = Collections.<String>emptySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   104
            if (debug != null && !unresCritExts.isEmpty()) {
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   105
                StringJoiner joiner = new StringJoiner(", ", "{", "}");
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   106
                for (String oid : unresCritExts) {
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   107
                  joiner.add(oid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   109
                debug.println("Set of critical extensions: " +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   110
                        joiner.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            for (int j = 0; j < certPathCheckers.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                PKIXCertPathChecker currChecker = certPathCheckers.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    debug.println("-Using checker" + (j + 1) + " ... [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        currChecker.getClass().getName() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (i == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    currChecker.init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   125
                    currChecker.check(currCert, unresCritExts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   127
                    if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   128
                        debug.println("-checker" + (j + 1) +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   129
                            " validation succeeded");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } catch (CertPathValidatorException cpve) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   133
                    throw new CertPathValidatorException(cpve.getMessage(),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   134
                        cpve.getCause(), cpOriginal, cpSize - (i + 1),
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   135
                        cpve.getReason());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   139
            if (!unresCritExts.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new CertPathValidatorException("unrecognized " +
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   141
                    "critical extension(s)", null, cpOriginal, cpSize-(i+1),
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   142
                    PKIXReason.UNRECOGNIZED_CRIT_EXT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                debug.println("\ncert" + (i+1) + " validation succeeded.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            debug.println("Cert path validation succeeded. (PKIX validation "
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   151
                          + "algorithm)");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            debug.println("-------------------------------------------------"
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   153
                          + "-------------");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}