History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/provider/certpath/KeyChecker.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/provider/certpath/KeyChecker.java+170−0
@@ -0,0 +1,170 @@
1/*
2 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.provider.certpath;
27
28import java.util.*;
29import java.security.cert.*;
30import java.security.cert.PKIXReason;
31
32import sun.security.util.Debug;
33import sun.security.x509.PKIXExtensions;
34
35/**
36 * KeyChecker is a <code>PKIXCertPathChecker</code> that checks that the
37 * keyCertSign bit is set in the keyUsage extension in an intermediate CA
38 * certificate. It also checks whether the final certificate in a
39 * certification path meets the specified target constraints specified as
40 * a CertSelector in the PKIXParameters passed to the CertPathValidator.
41 *
42 * @since 1.4
43 * @author Yassir Elley
44 */
45class KeyChecker extends PKIXCertPathChecker {
46
47 private static final Debug debug = Debug.getInstance("certpath");
48 // the index of keyCertSign in the boolean KeyUsage array
49 private static final int keyCertSign = 5;
50 private final int certPathLen;
51 private CertSelector targetConstraints;
52 private int remainingCerts;
53
54 private Set<String> supportedExts;
55
56 /**
57 * Default Constructor
58 *
59 * @param certPathLen allowable cert path length
60 * @param targetCertSel a CertSelector object specifying the constraints
61 * on the target certificate
62 */
63 KeyChecker(int certPathLen, CertSelector targetCertSel)
64 throws CertPathValidatorException
65 {
66 this.certPathLen = certPathLen;
67 this.targetConstraints = targetCertSel;
68 init(false);
69 }
70
71 /**
72 * Initializes the internal state of the checker from parameters
73 * specified in the constructor
74 */
75 public void init(boolean forward) throws CertPathValidatorException {
76 if (!forward) {
77 remainingCerts = certPathLen;
78 } else {
79 throw new CertPathValidatorException
80 ("forward checking not supported");
81 }
82 }
83
84 public final boolean isForwardCheckingSupported() {
85 return false;
86 }
87
88 public Set<String> getSupportedExtensions() {
89 if (supportedExts == null) {
90 supportedExts = new HashSet<String>();
91 supportedExts.add(PKIXExtensions.KeyUsage_Id.toString());
92 supportedExts.add(PKIXExtensions.ExtendedKeyUsage_Id.toString());
93 supportedExts.add(PKIXExtensions.SubjectAlternativeName_Id.toString());
94 supportedExts = Collections.unmodifiableSet(supportedExts);
95 }
96 return supportedExts;
97 }
98
99 /**
100 * Checks that keyUsage and target constraints are satisfied by
101 * the specified certificate.
102 *
103 * @param cert the Certificate
104 * @param unresolvedCritExts the unresolved critical extensions
105 * @exception CertPathValidatorException Exception thrown if certificate
106 * does not verify
107 */
108 public void check(Certificate cert, Collection<String> unresCritExts)
109 throws CertPathValidatorException
110 {
111 X509Certificate currCert = (X509Certificate) cert;
112
113 remainingCerts--;
114
115 // if final certificate, check that target constraints are satisfied
116 if (remainingCerts == 0) {
117 if ((targetConstraints != null) &&
118 (targetConstraints.match(currCert) == false)) {
119 throw new CertPathValidatorException("target certificate " +
120 "constraints check failed");
121 }
122 } else {
123 // otherwise, verify that keyCertSign bit is set in CA certificate
124 verifyCAKeyUsage(currCert);
125 }
126
127 // remove the extensions that we have checked
128 if (unresCritExts != null && !unresCritExts.isEmpty()) {
129 unresCritExts.remove(PKIXExtensions.KeyUsage_Id.toString());
130 unresCritExts.remove(PKIXExtensions.ExtendedKeyUsage_Id.toString());
131 unresCritExts.remove(
132 PKIXExtensions.SubjectAlternativeName_Id.toString());
133 }
134 }
135
136 /**
137 * Static method to verify that the key usage and extended key usage
138 * extension in a CA cert. The key usage extension, if present, must
139 * assert the keyCertSign bit. The extended key usage extension, if
140 * present, must include anyExtendedKeyUsage.
141 */
142 static void verifyCAKeyUsage(X509Certificate cert)
143 throws CertPathValidatorException {
144 String msg = "CA key usage";
145 if (debug != null) {
146 debug.println("KeyChecker.verifyCAKeyUsage() ---checking " + msg
147 + "...");
148 }
149
150 boolean[] keyUsageBits = cert.getKeyUsage();
151
152 // getKeyUsage returns null if the KeyUsage extension is not present
153 // in the certificate - in which case there is nothing to check
154 if (keyUsageBits == null) {
155 return;
156 }
157
158 // throw an exception if the keyCertSign bit is not set
159 if (!keyUsageBits[keyCertSign]) {
160 throw new CertPathValidatorException
161 (msg + " check failed: keyCertSign bit is not set", null,
162 null, -1, PKIXReason.INVALID_KEY_USAGE);
163 }
164
165 if (debug != null) {
166 debug.println("KeyChecker.verifyCAKeyUsage() " + msg
167 + " verified.");
168 }
169 }
170}