Botan  2.4.0
Crypto and TLS for C++11
x509path.h
Go to the documentation of this file.
1 /*
2 * X.509 Cert Path Validation
3 * (C) 2010-2011 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_X509_CERT_PATH_VALIDATION_H_
9 #define BOTAN_X509_CERT_PATH_VALIDATION_H_
10 
11 #include <botan/cert_status.h>
12 #include <botan/x509cert.h>
13 #include <botan/certstor.h>
14 #include <botan/ocsp.h>
15 #include <functional>
16 #include <set>
17 #include <chrono>
18 
19 #if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
20  #define BOTAN_HAS_ONLINE_REVOCATION_CHECKS
21 #endif
22 
23 namespace Botan {
24 
25 /**
26 * This type represents the validation status of an entire certificate path.
27 * There is one set of status codes for each certificate in the path.
28 */
29 typedef std::vector<std::set<Certificate_Status_Code>> CertificatePathStatusCodes;
30 
31 /**
32 * Specifies restrictions on the PKIX path validation
33 */
35  {
36  public:
37  /**
38  * @param require_rev if true, revocation information is required
39 
40  * @param minimum_key_strength is the minimum strength (in terms of
41  * operations, eg 80 means 2^80) of a signature. Signatures weaker than
42  * this are rejected. If more than 80, SHA-1 signatures are also
43  * rejected. If possible use at least setting 110.
44  *
45  * 80 bit strength requires 1024 bit RSA
46  * 110 bit strength requires 2k bit RSA
47  * 128 bit strength requires ~3k bit RSA or P-256
48  * @param ocsp_all_intermediates Make OCSP requests for all CAs as
49  * well as end entity (if OCSP enabled in path validation request)
50  */
51  Path_Validation_Restrictions(bool require_rev = false,
52  size_t minimum_key_strength = 110,
53  bool ocsp_all_intermediates = false);
54 
55  /**
56  * @param require_rev if true, revocation information is required
57  * @param minimum_key_strength is the minimum strength (in terms of
58  * operations, eg 80 means 2^80) of a signature. Signatures
59  * weaker than this are rejected.
60  * @param ocsp_all_intermediates Make OCSP requests for all CAs as
61  * well as end entity (if OCSP enabled in path validation request)
62  * @param trusted_hashes a set of trusted hashes. Any signatures
63  * created using a hash other than one of these will be
64  * rejected.
65  */
66  Path_Validation_Restrictions(bool require_rev,
67  size_t minimum_key_strength,
68  bool ocsp_all_intermediates,
69  const std::set<std::string>& trusted_hashes) :
70  m_require_revocation_information(require_rev),
71  m_ocsp_all_intermediates(ocsp_all_intermediates),
72  m_trusted_hashes(trusted_hashes),
73  m_minimum_key_strength(minimum_key_strength) {}
74 
75  /**
76  * @return whether revocation information is required
77  */
79  { return m_require_revocation_information; }
80 
81  /**
82  * @return whether all intermediate CAs should also be OCSPed. If false
83  * then only end entity OCSP is required/requested.
84  */
86  { return m_ocsp_all_intermediates; }
87 
88  /**
89  * @return trusted signature hash functions
90  */
91  const std::set<std::string>& trusted_hashes() const
92  { return m_trusted_hashes; }
93 
94  /**
95  * @return minimum required key strength
96  */
97  size_t minimum_key_strength() const
98  { return m_minimum_key_strength; }
99 
100  private:
101  bool m_require_revocation_information;
102  bool m_ocsp_all_intermediates;
103  std::set<std::string> m_trusted_hashes;
104  size_t m_minimum_key_strength;
105  };
106 
107 /**
108 * Represents the result of a PKIX path validation
109 */
111  {
112  public:
114 
115  /**
116  * @return the set of hash functions you are implicitly
117  * trusting by trusting this result.
118  */
119  std::set<std::string> trusted_hashes() const;
120 
121  /**
122  * @return the trust root of the validation if successful
123  * throws an exception if the validation failed
124  */
125  const X509_Certificate& trust_root() const;
126 
127  /**
128  * @return the full path from subject to trust root
129  * This path may be empty
130  */
131  const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path() const { return m_cert_path; }
132 
133  /**
134  * @return true iff the validation was successful
135  */
136  bool successful_validation() const;
137 
138  /**
139  * @return true iff no warnings occured during validation
140  */
141  bool no_warnings() const;
142 
143  /**
144  * @return overall validation result code
145  */
146  Certificate_Status_Code result() const { return m_overall; }
147 
148  /**
149  * @return a set of status codes for each certificate in the chain
150  */
151  const CertificatePathStatusCodes& all_statuses() const
152  { return m_all_status; }
153 
154  /**
155  * @return the subset of status codes that are warnings
156  */
157  CertificatePathStatusCodes warnings() const;
158 
159  /**
160  * @return string representation of the validation result
161  */
162  std::string result_string() const;
163 
164  /**
165  * @param code validation status code
166  * @return corresponding validation status message
167  */
168  static const char* status_string(Certificate_Status_Code code);
169 
170  /**
171  * Create a Path_Validation_Result
172  * @param status list of validation status codes
173  * @param cert_chain the certificate chain that was validated
174  */
175  Path_Validation_Result(CertificatePathStatusCodes status,
176  std::vector<std::shared_ptr<const X509_Certificate>>&& cert_chain);
177 
178  /**
179  * Create a Path_Validation_Result
180  * @param status validation status code
181  */
182  explicit Path_Validation_Result(Certificate_Status_Code status) : m_overall(status) {}
183 
184  private:
185  CertificatePathStatusCodes m_all_status;
186  CertificatePathStatusCodes m_warnings;
187  std::vector<std::shared_ptr<const X509_Certificate>> m_cert_path;
188  Certificate_Status_Code m_overall;
189  };
190 
191 /**
192 * PKIX Path Validation
193 * @param end_certs certificate chain to validate (with end entity certificate in end_certs[0])
194 * @param restrictions path validation restrictions
195 * @param trusted_roots list of certificate stores that contain trusted certificates
196 * @param hostname if not empty, compared against the DNS name in end_certs[0]
197 * @param usage if not set to UNSPECIFIED, compared against the key usage in end_certs[0]
198 * @param validation_time what reference time to use for validation
199 * @param ocsp_timeout timeout for OCSP operations, 0 disables OCSP check
200 * @param ocsp_resp additional OCSP responses to consider (eg from peer)
201 * @return result of the path validation
202 */
204  const std::vector<X509_Certificate>& end_certs,
205  const Path_Validation_Restrictions& restrictions,
206  const std::vector<Certificate_Store*>& trusted_roots,
207  const std::string& hostname = "",
209  std::chrono::system_clock::time_point validation_time = std::chrono::system_clock::now(),
210  std::chrono::milliseconds ocsp_timeout = std::chrono::milliseconds(0),
211  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp = {});
212 
213 /**
214 * PKIX Path Validation
215 * @param end_cert certificate to validate
216 * @param restrictions path validation restrictions
217 * @param trusted_roots list of stores that contain trusted certificates
218 * @param hostname if not empty, compared against the DNS name in end_cert
219 * @param usage if not set to UNSPECIFIED, compared against the key usage in end_cert
220 * @param validation_time what reference time to use for validation
221 * @param ocsp_timeout timeout for OCSP operations, 0 disables OCSP check
222 * @param ocsp_resp additional OCSP responses to consider (eg from peer)
223 * @return result of the path validation
224 */
226  const X509_Certificate& end_cert,
227  const Path_Validation_Restrictions& restrictions,
228  const std::vector<Certificate_Store*>& trusted_roots,
229  const std::string& hostname = "",
231  std::chrono::system_clock::time_point validation_time = std::chrono::system_clock::now(),
232  std::chrono::milliseconds ocsp_timeout = std::chrono::milliseconds(0),
233  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp = {});
234 
235 /**
236 * PKIX Path Validation
237 * @param end_cert certificate to validate
238 * @param restrictions path validation restrictions
239 * @param store store that contains trusted certificates
240 * @param hostname if not empty, compared against the DNS name in end_cert
241 * @param usage if not set to UNSPECIFIED, compared against the key usage in end_cert
242 * @param validation_time what reference time to use for validation
243 * @param ocsp_timeout timeout for OCSP operations, 0 disables OCSP check
244 * @param ocsp_resp additional OCSP responses to consider (eg from peer)
245 * @return result of the path validation
246 */
248  const X509_Certificate& end_cert,
249  const Path_Validation_Restrictions& restrictions,
250  const Certificate_Store& store,
251  const std::string& hostname = "",
253  std::chrono::system_clock::time_point validation_time = std::chrono::system_clock::now(),
254  std::chrono::milliseconds ocsp_timeout = std::chrono::milliseconds(0),
255  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp = {});
256 
257 /**
258 * PKIX Path Validation
259 * @param end_certs certificate chain to validate
260 * @param restrictions path validation restrictions
261 * @param store store that contains trusted certificates
262 * @param hostname if not empty, compared against the DNS name in end_certs[0]
263 * @param usage if not set to UNSPECIFIED, compared against the key usage in end_certs[0]
264 * @param validation_time what reference time to use for validation
265 * @param ocsp_timeout timeout for OCSP operations, 0 disables OCSP check
266 * @param ocsp_resp additional OCSP responses to consider (eg from peer)
267 * @return result of the path validation
268 */
270  const std::vector<X509_Certificate>& end_certs,
271  const Path_Validation_Restrictions& restrictions,
272  const Certificate_Store& store,
273  const std::string& hostname = "",
275  std::chrono::system_clock::time_point validation_time = std::chrono::system_clock::now(),
276  std::chrono::milliseconds ocsp_timeout = std::chrono::milliseconds(0),
277  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_resp = {});
278 
279 
280 /**
281 * namespace PKIX holds the building blocks that are called by x509_path_validate.
282 * This allows custom validation logic to be written by applications and makes
283 * for easier testing, but unless you're positive you know what you're doing you
284 * probably want to just call x509_path_validate instead.
285 */
286 namespace PKIX {
287 
289 build_all_certificate_paths(std::vector<std::vector<std::shared_ptr<const X509_Certificate>>>& cert_paths,
290  const std::vector<Certificate_Store*>& trusted_certstores,
291  const std::shared_ptr<const X509_Certificate>& end_entity,
292  const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra);
293 
294 
295 /**
296 * Build certificate path
297 * @param cert_path_out output parameter, cert_path will be appended to this vector
298 * @param trusted_certstores list of certificate stores that contain trusted certificates
299 * @param end_entity the cert to be validated
300 * @param end_entity_extra optional list of additional untrusted certs for path building
301 * @return result of the path building operation (OK or error)
302 */
304 BOTAN_PUBLIC_API(2,0) build_certificate_path(std::vector<std::shared_ptr<const X509_Certificate>>& cert_path_out,
305  const std::vector<Certificate_Store*>& trusted_certstores,
306  const std::shared_ptr<const X509_Certificate>& end_entity,
307  const std::vector<std::shared_ptr<const X509_Certificate>>& end_entity_extra);
308 
309 /**
310 * Check the certificate chain, but not any revocation data
311 *
312 * @param cert_path path built by build_certificate_path with OK result
313 * @param ref_time whatever time you want to perform the validation
314 * against (normally current system clock)
315 * @param hostname the hostname
316 * @param usage end entity usage checks
317 * @param min_signature_algo_strength 80 or 110 typically
318 * Note 80 allows 1024 bit RSA and SHA-1. 110 allows 2048 bit RSA and SHA-2.
319 * Using 128 requires ECC (P-256) or ~3000 bit RSA keys.
320 * @param trusted_hashes set of trusted hash functions, empty means accept any
321 * hash we have an OID for
322 * @return vector of results on per certificate in the path, each containing a set of
323 * results. If all codes in the set are < Certificate_Status_Code::FIRST_ERROR_STATUS,
324 * then the result for that certificate is successful. If all results are
325 */
326 CertificatePathStatusCodes
327 BOTAN_PUBLIC_API(2,0) check_chain(const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
328  std::chrono::system_clock::time_point ref_time,
329  const std::string& hostname,
330  Usage_Type usage,
331  size_t min_signature_algo_strength,
332  const std::set<std::string>& trusted_hashes);
333 
334 /**
335 * Check OCSP responses for revocation information
336 * @param cert_path path already validated by check_chain
337 * @param ocsp_responses the OCSP responses to consider
338 * @param certstores trusted roots
339 * @param ref_time whatever time you want to perform the validation against
340 * (normally current system clock)
341 * @return revocation status
342 */
343 CertificatePathStatusCodes
344 BOTAN_PUBLIC_API(2,0) check_ocsp(const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
345  const std::vector<std::shared_ptr<const OCSP::Response>>& ocsp_responses,
346  const std::vector<Certificate_Store*>& certstores,
347  std::chrono::system_clock::time_point ref_time);
348 
349 /**
350 * Check CRLs for revocation information
351 * @param cert_path path already validated by check_chain
352 * @param crls the list of CRLs to check, it is assumed that crls[i] (if not null)
353 * is the associated CRL for the subject in cert_path[i].
354 * @param ref_time whatever time you want to perform the validation against
355 * (normally current system clock)
356 * @return revocation status
357 */
358 CertificatePathStatusCodes
359 BOTAN_PUBLIC_API(2,0) check_crl(const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
360  const std::vector<std::shared_ptr<const X509_CRL>>& crls,
361  std::chrono::system_clock::time_point ref_time);
362 
363 /**
364 * Check CRLs for revocation information
365 * @param cert_path path already validated by check_chain
366 * @param certstores a list of certificate stores to query for the CRL
367 * @param ref_time whatever time you want to perform the validation against
368 * (normally current system clock)
369 * @return revocation status
370 */
371 CertificatePathStatusCodes
372 BOTAN_PUBLIC_API(2,0) check_crl(const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
373  const std::vector<Certificate_Store*>& certstores,
374  std::chrono::system_clock::time_point ref_time);
375 
376 #if defined(BOTAN_HAS_ONLINE_REVOCATION_CHECKS)
377 
378 /**
379 * Check OCSP using online (HTTP) access. Current version creates a thread and
380 * network connection per OCSP request made.
381 *
382 * @param cert_path path already validated by check_chain
383 * @param trusted_certstores a list of certstores with trusted certs
384 * @param ref_time whatever time you want to perform the validation against
385 * (normally current system clock)
386 * @param timeout for timing out the responses, though actually this function
387 * may block for up to timeout*cert_path.size()*C for some small C.
388 * @param ocsp_check_intermediate_CAs if true also performs OCSP on any intermediate
389 * CA certificates. If false, only does OCSP on the end entity cert.
390 * @return revocation status
391 */
392 CertificatePathStatusCodes
393 BOTAN_PUBLIC_API(2,0) check_ocsp_online(const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
394  const std::vector<Certificate_Store*>& trusted_certstores,
395  std::chrono::system_clock::time_point ref_time,
396  std::chrono::milliseconds timeout,
397  bool ocsp_check_intermediate_CAs);
398 
399 /**
400 * Check CRL using online (HTTP) access. Current version creates a thread and
401 * network connection per CRL access.
402 
403 * @param cert_path path already validated by check_chain
404 * @param trusted_certstores a list of certstores with trusted certs
405 * @param certstore_to_recv_crls optional (nullptr to disable), all CRLs
406 * retreived will be saved to this cert store.
407 * @param ref_time whatever time you want to perform the validation against
408 * (normally current system clock)
409 * @param timeout for timing out the responses, though actually this function
410 * may block for up to timeout*cert_path.size()*C for some small C.
411 * @return revocation status
412 */
413 CertificatePathStatusCodes
414 BOTAN_PUBLIC_API(2,0) check_crl_online(const std::vector<std::shared_ptr<const X509_Certificate>>& cert_path,
415  const std::vector<Certificate_Store*>& trusted_certstores,
416  Certificate_Store_In_Memory* certstore_to_recv_crls,
417  std::chrono::system_clock::time_point ref_time,
418  std::chrono::milliseconds timeout);
419 
420 #endif
421 
422 /**
423 * Find overall status (OK, error) of a validation
424 * @param cert_status result of merge_revocation_status or check_chain
425 */
426 Certificate_Status_Code BOTAN_PUBLIC_API(2,0) overall_status(const CertificatePathStatusCodes& cert_status);
427 
428 /**
429 * Merge the results from CRL and/or OCSP checks into chain_status
430 * @param chain_status the certificate status
431 * @param crl_status results from check_crl
432 * @param ocsp_status results from check_ocsp
433 * @param require_rev_on_end_entity require valid CRL or OCSP on end-entity cert
434 * @param require_rev_on_intermediates require valid CRL or OCSP on all intermediate certificates
435 */
436 void BOTAN_PUBLIC_API(2,0) merge_revocation_status(CertificatePathStatusCodes& chain_status,
437  const CertificatePathStatusCodes& crl_status,
438  const CertificatePathStatusCodes& ocsp_status,
439  bool require_rev_on_end_entity,
440  bool require_rev_on_intermediates);
441 
442 }
443 
444 }
445 
446 #endif
CertificatePathStatusCodes check_chain(const std::vector< std::shared_ptr< const X509_Certificate >> &cert_path, std::chrono::system_clock::time_point ref_time, const std::string &hostname, Usage_Type usage, size_t min_signature_algo_strength, const std::set< std::string > &trusted_hashes)
Definition: x509path.cpp:32
size_t minimum_key_strength() const
Definition: x509path.h:97
CertificatePathStatusCodes check_crl(const std::vector< std::shared_ptr< const X509_Certificate >> &cert_path, const std::vector< Certificate_Store *> &certstores, std::chrono::system_clock::time_point ref_time)
Definition: x509path.cpp:319
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Certificate_Status_Code overall_status(const CertificatePathStatusCodes &cert_status)
Definition: x509path.cpp:797
void merge_revocation_status(CertificatePathStatusCodes &chain_status, const CertificatePathStatusCodes &crl_status, const CertificatePathStatusCodes &ocsp_status, bool require_rev_on_end_entity, bool require_rev_on_intermediates)
Definition: x509path.cpp:748
bool require_revocation_information() const
Definition: x509path.h:78
Certificate_Status_Code Code
Definition: x509path.h:113
Certificate_Status_Code build_certificate_path(std::vector< std::shared_ptr< const X509_Certificate >> &cert_path_out, const std::vector< Certificate_Store *> &trusted_certstores, const std::shared_ptr< const X509_Certificate > &end_entity, const std::vector< std::shared_ptr< const X509_Certificate >> &end_entity_extra)
Definition: x509path.cpp:500
const std::set< std::string > & trusted_hashes() const
Definition: x509path.h:91
Definition: alg_id.cpp:13
Path_Validation_Result(Certificate_Status_Code status)
Definition: x509path.h:182
Path_Validation_Result x509_path_validate(const std::vector< X509_Certificate > &end_certs, const Path_Validation_Restrictions &restrictions, const std::vector< Certificate_Store *> &trusted_roots, const std::string &hostname, Usage_Type usage, std::chrono::system_clock::time_point ref_time, std::chrono::milliseconds ocsp_timeout, const std::vector< std::shared_ptr< const OCSP::Response >> &ocsp_resp)
Definition: x509path.cpp:820
const CertificatePathStatusCodes & all_statuses() const
Definition: x509path.h:151
Certificate_Status_Code build_all_certificate_paths(std::vector< std::vector< std::shared_ptr< const X509_Certificate >>> &cert_paths, const std::vector< Certificate_Store *> &trusted_certstores, const std::shared_ptr< const X509_Certificate > &end_entity, const std::vector< std::shared_ptr< const X509_Certificate >> &end_entity_extra)
Definition: x509path.cpp:605
const std::vector< std::shared_ptr< const X509_Certificate > > & cert_path() const
Definition: x509path.h:131
Path_Validation_Restrictions(bool require_rev, size_t minimum_key_strength, bool ocsp_all_intermediates, const std::set< std::string > &trusted_hashes)
Definition: x509path.h:66
std::vector< std::set< Certificate_Status_Code > > CertificatePathStatusCodes
Definition: x509path.h:29
CertificatePathStatusCodes check_ocsp(const std::vector< std::shared_ptr< const X509_Certificate >> &cert_path, const std::vector< std::shared_ptr< const OCSP::Response >> &ocsp_responses, const std::vector< Certificate_Store *> &certstores, std::chrono::system_clock::time_point ref_time)
Definition: x509path.cpp:203
Certificate_Status_Code
Definition: cert_status.h:18
Usage_Type
Definition: x509cert.h:25
Certificate_Status_Code result() const
Definition: x509path.h:146