jdk/src/share/classes/sun/security/provider/certpath/KeyChecker.java
author mullan
Thu, 11 Sep 2008 14:05:16 -0400
changeset 1238 6d1f4b722acd
parent 790 b91742db13e2
child 5506 202f599c92aa
permissions -rw-r--r--
6465942: Add problem identification facility to the CertPathValidator framework Summary: Add support to the java.security.cert APIs for determining the reason that a certification path is invalid. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.*;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
    30
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.x509.PKIXExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * KeyChecker is a <code>PKIXCertPathChecker</code> that checks that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * keyCertSign bit is set in the keyUsage extension in an intermediate CA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * certificate. It also checks whether the final certificate in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * certification path meets the specified target constraints specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a CertSelector in the PKIXParameters passed to the CertPathValidator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class KeyChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // the index of keyCertSign in the boolean KeyUsage array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int keyCertSign = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final int certPathLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private CertSelector targetConstraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int remainingCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
790
b91742db13e2 6673277: Thread unsafe lazy initialization code in sun.security.provider.certpath.*Checker classes
mullan
parents: 2
diff changeset
    54
    private Set<String> supportedExts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Default Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param certPathLen allowable cert path length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param targetCertSel a CertSelector object specifying the constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * on the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    KeyChecker(int certPathLen, CertSelector targetCertSel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.certPathLen = certPathLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.targetConstraints = targetCertSel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Initializes the internal state of the checker from parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * specified in the constructor
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
            remainingCerts = certPathLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } else {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
    79
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
    80
                ("forward checking not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
    84
    public final boolean isForwardCheckingSupported() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (supportedExts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            supportedExts = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            supportedExts.add(PKIXExtensions.KeyUsage_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            supportedExts.add(PKIXExtensions.ExtendedKeyUsage_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            supportedExts.add(PKIXExtensions.SubjectAlternativeName_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            supportedExts = Collections.unmodifiableSet(supportedExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return supportedExts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Checks that keyUsage and target constraints are satisfied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the specified certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param unresolvedCritExts the unresolved critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception CertPathValidatorException Exception thrown if certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * does not verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void check(Certificate cert, Collection<String> unresCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        X509Certificate currCert = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        remainingCerts--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // if final certificate, check that target constraints are satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (remainingCerts == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if ((targetConstraints != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                (targetConstraints.match(currCert) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new CertPathValidatorException("target certificate " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    "constraints check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // otherwise, verify that keyCertSign bit is set in CA certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            verifyCAKeyUsage(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // remove the extensions that we have checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (unresCritExts != null && !unresCritExts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            unresCritExts.remove(PKIXExtensions.KeyUsage_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            unresCritExts.remove(PKIXExtensions.ExtendedKeyUsage_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                PKIXExtensions.SubjectAlternativeName_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Static method to verify that the key usage and extended key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * extension in a CA cert. The key usage extension, if present, must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * assert the keyCertSign bit. The extended key usage extension, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * present, must include anyExtendedKeyUsage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static void verifyCAKeyUsage(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String msg = "CA key usage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            debug.println("KeyChecker.verifyCAKeyUsage() ---checking " + msg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        boolean[] keyUsageBits = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // getKeyUsage returns null if the KeyUsage extension is not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // in the certificate - in which case there is nothing to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (keyUsageBits == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // throw an exception if the keyCertSign bit is not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (!keyUsageBits[keyCertSign]) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   160
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   161
                (msg + " check failed: keyCertSign bit is not set", null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 790
diff changeset
   162
                 null, -1, PKIXReason.INVALID_KEY_USAGE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
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("KeyChecker.verifyCAKeyUsage() " + msg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}