blob: e1a98ea616347b181bdee1be27b6104414dc7472 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.provider.certpath;
27
28import java.util.*;
29import java.security.cert.*;
30
31import sun.security.util.Debug;
32import sun.security.x509.PKIXExtensions;
33
34/**
35 * KeyChecker is a <code>PKIXCertPathChecker</code> that checks that the
36 * keyCertSign bit is set in the keyUsage extension in an intermediate CA
37 * certificate. It also checks whether the final certificate in a
38 * certification path meets the specified target constraints specified as
39 * a CertSelector in the PKIXParameters passed to the CertPathValidator.
40 *
41 * @since 1.4
42 * @author Yassir Elley
43 */
44class KeyChecker extends PKIXCertPathChecker {
45
46 private static final Debug debug = Debug.getInstance("certpath");
47 // the index of keyCertSign in the boolean KeyUsage array
48 private static final int keyCertSign = 5;
49 private final int certPathLen;
50 private CertSelector targetConstraints;
51 private int remainingCerts;
52
53 private static Set<String> supportedExts;
54
55 /**
56 * Default Constructor
57 *
58 * @param certPathLen allowable cert path length
59 * @param targetCertSel a CertSelector object specifying the constraints
60 * on the target certificate
61 */
62 KeyChecker(int certPathLen, CertSelector targetCertSel)
63 throws CertPathValidatorException
64 {
65 this.certPathLen = certPathLen;
66 this.targetConstraints = targetCertSel;
67 init(false);
68 }
69
70 /**
71 * Initializes the internal state of the checker from parameters
72 * specified in the constructor
73 */
74 public void init(boolean forward) throws CertPathValidatorException {
75 if (!forward) {
76 remainingCerts = certPathLen;
77 } else {
78 throw new CertPathValidatorException("forward checking not supported");
79 }
80 }
81
82 public boolean isForwardCheckingSupported() {
83 return false;
84 }
85
86 public Set<String> getSupportedExtensions() {
87 if (supportedExts == null) {
88 supportedExts = new HashSet<String>();
89 supportedExts.add(PKIXExtensions.KeyUsage_Id.toString());
90 supportedExts.add(PKIXExtensions.ExtendedKeyUsage_Id.toString());
91 supportedExts.add(PKIXExtensions.SubjectAlternativeName_Id.toString());
92 supportedExts = Collections.unmodifiableSet(supportedExts);
93 }
94 return supportedExts;
95 }
96
97 /**
98 * Checks that keyUsage and target constraints are satisfied by
99 * the specified certificate.
100 *
101 * @param cert the Certificate
102 * @param unresolvedCritExts the unresolved critical extensions
103 * @exception CertPathValidatorException Exception thrown if certificate
104 * does not verify
105 */
106 public void check(Certificate cert, Collection<String> unresCritExts)
107 throws CertPathValidatorException
108 {
109 X509Certificate currCert = (X509Certificate) cert;
110
111 remainingCerts--;
112
113 // if final certificate, check that target constraints are satisfied
114 if (remainingCerts == 0) {
115 if ((targetConstraints != null) &&
116 (targetConstraints.match(currCert) == false)) {
117 throw new CertPathValidatorException("target certificate " +
118 "constraints check failed");
119 }
120 } else {
121 // otherwise, verify that keyCertSign bit is set in CA certificate
122 verifyCAKeyUsage(currCert);
123 }
124
125 // remove the extensions that we have checked
126 if (unresCritExts != null && !unresCritExts.isEmpty()) {
127 unresCritExts.remove(PKIXExtensions.KeyUsage_Id.toString());
128 unresCritExts.remove(PKIXExtensions.ExtendedKeyUsage_Id.toString());
129 unresCritExts.remove(
130 PKIXExtensions.SubjectAlternativeName_Id.toString());
131 }
132 }
133
134 /**
135 * Static method to verify that the key usage and extended key usage
136 * extension in a CA cert. The key usage extension, if present, must
137 * assert the keyCertSign bit. The extended key usage extension, if
138 * present, must include anyExtendedKeyUsage.
139 */
140 static void verifyCAKeyUsage(X509Certificate cert)
141 throws CertPathValidatorException {
142 String msg = "CA key usage";
143 if (debug != null) {
144 debug.println("KeyChecker.verifyCAKeyUsage() ---checking " + msg
145 + "...");
146 }
147
148 boolean[] keyUsageBits = cert.getKeyUsage();
149
150 // getKeyUsage returns null if the KeyUsage extension is not present
151 // in the certificate - in which case there is nothing to check
152 if (keyUsageBits == null) {
153 return;
154 }
155
156 // throw an exception if the keyCertSign bit is not set
157 if (!keyUsageBits[keyCertSign]) {
158 throw new CertPathValidatorException(msg + " check failed: "
159 + "keyCertSign bit is not set");
160 }
161
162 if (debug != null) {
163 debug.println("KeyChecker.verifyCAKeyUsage() " + msg
164 + " verified.");
165 }
166 }
167}