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