• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, 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 package java.security.cert;
26 
27 import java.net.URI;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 
37 /**
38  * A {@code PKIXCertPathChecker} for checking the revocation status of
39  * certificates with the PKIX algorithm.
40  *
41  * <p>A {@code PKIXRevocationChecker} checks the revocation status of
42  * certificates with the Online Certificate Status Protocol (OCSP) or
43  * Certificate Revocation Lists (CRLs). OCSP is described in RFC 2560 and
44  * is a network protocol for determining the status of a certificate. A CRL
45  * is a time-stamped list identifying revoked certificates, and RFC 5280
46  * describes an algorithm for determining the revocation status of certificates
47  * using CRLs.
48  *
49  * <p>Each {@code PKIXRevocationChecker} must be able to check the revocation
50  * status of certificates with OCSP and CRLs. By default, OCSP is the
51  * preferred mechanism for checking revocation status, with CRLs as the
52  * fallback mechanism. However, this preference can be switched to CRLs with
53  * the {@link Option#PREFER_CRLS PREFER_CRLS} option. In addition, the fallback
54  * mechanism can be disabled with the {@link Option#NO_FALLBACK NO_FALLBACK}
55  * option.
56  *
57  * <p>A {@code PKIXRevocationChecker} is obtained by calling the
58  * {@link CertPathValidator#getRevocationChecker getRevocationChecker} method
59  * of a PKIX {@code CertPathValidator}. Additional parameters and options
60  * specific to revocation can be set (by calling the
61  * {@link #setOcspResponder setOcspResponder} method for instance). The
62  * {@code PKIXRevocationChecker} is added to a {@code PKIXParameters} object
63  * using the {@link PKIXParameters#addCertPathChecker addCertPathChecker}
64  * or {@link PKIXParameters#setCertPathCheckers setCertPathCheckers} method,
65  * and then the {@code PKIXParameters} is passed along with the {@code CertPath}
66  * to be validated to the {@link CertPathValidator#validate validate} method
67  * of a PKIX {@code CertPathValidator}. When supplying a revocation checker in
68  * this manner, it will be used to check revocation irrespective of the setting
69  * of the {@link PKIXParameters#isRevocationEnabled RevocationEnabled} flag.
70  * Similarly, a {@code PKIXRevocationChecker} may be added to a
71  * {@code PKIXBuilderParameters} object for use with a PKIX
72  * {@code CertPathBuilder}.
73  *
74  * <p>Note that when a {@code PKIXRevocationChecker} is added to
75  * {@code PKIXParameters}, it clones the {@code PKIXRevocationChecker};
76  * thus any subsequent modifications to the {@code PKIXRevocationChecker}
77  * have no effect.
78  *
79  * <p>Any parameter that is not set (or is set to {@code null}) will be set to
80  * the default value for that parameter.
81  *
82  * <p><b>Concurrent Access</b>
83  *
84  * <p>Unless otherwise specified, the methods defined in this class are not
85  * thread-safe. Multiple threads that need to access a single object
86  * concurrently should synchronize amongst themselves and provide the
87  * necessary locking. Multiple threads each manipulating separate objects
88  * need not synchronize.
89  *
90  * @since 1.8
91  */
92 public abstract class PKIXRevocationChecker extends PKIXCertPathChecker {
93     private URI ocspResponder;
94     private X509Certificate ocspResponderCert;
95     private List<Extension> ocspExtensions = Collections.<Extension>emptyList();
96     private Map<X509Certificate, byte[]> ocspResponses = Collections.emptyMap();
97     private Set<Option> options = Collections.emptySet();
98 
99     /**
100      * Default constructor.
101      */
PKIXRevocationChecker()102     protected PKIXRevocationChecker() {}
103 
104     /**
105      * Sets the URI that identifies the location of the OCSP responder. This
106      * overrides the {@code ocsp.responderURL} security property and any
107      * responder specified in a certificate's Authority Information Access
108      * Extension, as defined in RFC 5280.
109      *
110      * @param uri the responder URI
111      */
setOcspResponder(URI uri)112     public void setOcspResponder(URI uri) {
113         this.ocspResponder = uri;
114     }
115 
116     /**
117      * Gets the URI that identifies the location of the OCSP responder. This
118      * overrides the {@code ocsp.responderURL} security property. If this
119      * parameter or the {@code ocsp.responderURL} property is not set, the
120      * location is determined from the certificate's Authority Information
121      * Access Extension, as defined in RFC 5280.
122      *
123      * @return the responder URI, or {@code null} if not set
124      */
getOcspResponder()125     public URI getOcspResponder() {
126         return ocspResponder;
127     }
128 
129     /**
130      * Sets the OCSP responder's certificate. This overrides the
131      * {@code ocsp.responderCertSubjectName},
132      * {@code ocsp.responderCertIssuerName},
133      * and {@code ocsp.responderCertSerialNumber} security properties.
134      *
135      * @param cert the responder's certificate
136      */
setOcspResponderCert(X509Certificate cert)137     public void setOcspResponderCert(X509Certificate cert) {
138         this.ocspResponderCert = cert;
139     }
140 
141     /**
142      * Gets the OCSP responder's certificate. This overrides the
143      * {@code ocsp.responderCertSubjectName},
144      * {@code ocsp.responderCertIssuerName},
145      * and {@code ocsp.responderCertSerialNumber} security properties. If this
146      * parameter or the aforementioned properties are not set, then the
147      * responder's certificate is determined as specified in RFC 2560.
148      *
149      * @return the responder's certificate, or {@code null} if not set
150      */
getOcspResponderCert()151     public X509Certificate getOcspResponderCert() {
152         return ocspResponderCert;
153     }
154 
155     // request extensions; single extensions not supported
156     /**
157      * Sets the optional OCSP request extensions.
158      *
159      * @param extensions a list of extensions. The list is copied to protect
160      *        against subsequent modification.
161      */
setOcspExtensions(List<Extension> extensions)162     public void setOcspExtensions(List<Extension> extensions)
163     {
164         this.ocspExtensions = (extensions == null)
165                               ? Collections.<Extension>emptyList()
166                               : new ArrayList<Extension>(extensions);
167     }
168 
169     /**
170      * Gets the optional OCSP request extensions.
171      *
172      * @return an unmodifiable list of extensions. The list is empty if no
173      *         extensions have been specified.
174      */
getOcspExtensions()175     public List<Extension> getOcspExtensions() {
176         return Collections.unmodifiableList(ocspExtensions);
177     }
178 
179     /**
180      * Sets the OCSP responses. These responses are used to determine
181      * the revocation status of the specified certificates when OCSP is used.
182      *
183      * @param responses a map of OCSP responses. Each key is an
184      *        {@code X509Certificate} that maps to the corresponding
185      *        DER-encoded OCSP response for that certificate. A deep copy of
186      *        the map is performed to protect against subsequent modification.
187      */
setOcspResponses(Map<X509Certificate, byte[]> responses)188     public void setOcspResponses(Map<X509Certificate, byte[]> responses)
189     {
190         if (responses == null) {
191             this.ocspResponses = Collections.<X509Certificate, byte[]>emptyMap();
192         } else {
193             Map<X509Certificate, byte[]> copy = new HashMap<>(responses.size());
194             for (Map.Entry<X509Certificate, byte[]> e : responses.entrySet()) {
195                 copy.put(e.getKey(), e.getValue().clone());
196             }
197             this.ocspResponses = copy;
198         }
199     }
200 
201     /**
202      * Gets the OCSP responses. These responses are used to determine
203      * the revocation status of the specified certificates when OCSP is used.
204      *
205      * @return a map of OCSP responses. Each key is an
206      *        {@code X509Certificate} that maps to the corresponding
207      *        DER-encoded OCSP response for that certificate. A deep copy of
208      *        the map is returned to protect against subsequent modification.
209      *        Returns an empty map if no responses have been specified.
210      */
getOcspResponses()211     public Map<X509Certificate, byte[]> getOcspResponses() {
212         Map<X509Certificate, byte[]> copy = new HashMap<>(ocspResponses.size());
213         for (Map.Entry<X509Certificate, byte[]> e : ocspResponses.entrySet()) {
214             copy.put(e.getKey(), e.getValue().clone());
215         }
216         return copy;
217     }
218 
219     /**
220      * Sets the revocation options.
221      *
222      * @param options a set of revocation options. The set is copied to protect
223      *        against subsequent modification.
224      */
setOptions(Set<Option> options)225     public void setOptions(Set<Option> options) {
226         this.options = (options == null)
227                        ? Collections.<Option>emptySet()
228                        : new HashSet<Option>(options);
229     }
230 
231     /**
232      * Gets the revocation options.
233      *
234      * @return an unmodifiable set of revocation options. The set is empty if
235      *         no options have been specified.
236      */
getOptions()237     public Set<Option> getOptions() {
238         return Collections.unmodifiableSet(options);
239     }
240 
241     /**
242      * Returns a list containing the exceptions that are ignored by the
243      * revocation checker when the {@link Option#SOFT_FAIL SOFT_FAIL} option
244      * is set. The list is cleared each time {@link #init init} is called.
245      * The list is ordered in ascending order according to the certificate
246      * index returned by {@link CertPathValidatorException#getIndex getIndex}
247      * method of each entry.
248      * <p>
249      * An implementation of {@code PKIXRevocationChecker} is responsible for
250      * adding the ignored exceptions to the list.
251      *
252      * @return an unmodifiable list containing the ignored exceptions. The list
253      *         is empty if no exceptions have been ignored.
254      */
getSoftFailExceptions()255     public abstract List<CertPathValidatorException> getSoftFailExceptions();
256 
257     @Override
clone()258     public PKIXRevocationChecker clone() {
259         PKIXRevocationChecker copy = (PKIXRevocationChecker)super.clone();
260         copy.ocspExtensions = new ArrayList<>(ocspExtensions);
261         copy.ocspResponses = new HashMap<>(ocspResponses);
262         // deep-copy the encoded responses, since they are mutable
263         for (Map.Entry<X509Certificate, byte[]> entry :
264                  copy.ocspResponses.entrySet())
265         {
266             byte[] encoded = entry.getValue();
267             entry.setValue(encoded.clone());
268         }
269         copy.options = new HashSet<>(options);
270         return copy;
271     }
272 
273     /**
274      * Various revocation options that can be specified for the revocation
275      * checking mechanism.
276      */
277     public enum Option {
278         /**
279          * Only check the revocation status of end-entity certificates.
280          */
281         ONLY_END_ENTITY,
282         /**
283          * Prefer CRLs to OSCP. The default behavior is to prefer OCSP. Each
284          * PKIX implementation should document further details of their
285          * specific preference rules and fallback policies.
286          */
287         PREFER_CRLS,
288         /**
289          * Disable the fallback mechanism.
290          */
291         NO_FALLBACK,
292         /**
293          * Allow revocation check to succeed if the revocation status cannot be
294          * determined for one of the following reasons:
295          * <ul>
296          *  <li>The CRL or OCSP response cannot be obtained because of a
297          *      network error.
298          *  <li>The OCSP responder returns one of the following errors
299          *      specified in section 2.3 of RFC 2560: internalError or tryLater.
300          * </ul><br>
301          * Note that these conditions apply to both OCSP and CRLs, and unless
302          * the {@code NO_FALLBACK} option is set, the revocation check is
303          * allowed to succeed only if both mechanisms fail under one of the
304          * conditions as stated above.
305          * Exceptions that cause the network errors are ignored but can be
306          * later retrieved by calling the
307          * {@link #getSoftFailExceptions getSoftFailExceptions} method.
308          */
309         SOFT_FAIL
310     }
311 }
312