monotone

monotone Mtn Source Tree

Root/botan/x509stor.cpp

1/*************************************************
2* X.509 Certificate Store Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/x509stor.h>
7#include <botan/parsing.h>
8#include <botan/pubkey.h>
9#include <botan/look_pk.h>
10#include <botan/oids.h>
11#include <botan/conf.h>
12#include <botan/util.h>
13#include <algorithm>
14#include <memory>
15
16namespace Botan {
17
18namespace {
19
20/*************************************************
21* Compare the value of unique ID fields *
22*************************************************/
23bool compare_ids(const MemoryVector<byte>& id1,
24 const MemoryVector<byte>& id2)
25 {
26 if(!id1.size() || !id2.size())
27 return true;
28 return (id1 == id2);
29 }
30
31/*************************************************
32* Check a particular usage restriction *
33*************************************************/
34bool check_usage(const X509_Certificate& cert, X509_Store::Cert_Usage usage,
35 X509_Store::Cert_Usage check_for, Key_Constraints constraints)
36 {
37 if((usage & check_for) == 0)
38 return true;
39 if(cert.constraints() == NO_CONSTRAINTS)
40 return true;
41 if(cert.constraints() & constraints)
42 return true;
43 return false;
44 }
45
46/*************************************************
47* Check a particular usage restriction *
48*************************************************/
49bool check_usage(const X509_Certificate& cert, X509_Store::Cert_Usage usage,
50 X509_Store::Cert_Usage check_for,
51 const std::string& usage_oid)
52 {
53 if((usage & check_for) == 0)
54 return true;
55
56 const std::vector<OID> constraints = cert.ex_constraints();
57
58 if(constraints.size() == 0)
59 return true;
60
61 return std::binary_search(constraints.begin(), constraints.end(),
62 OIDS::lookup(usage_oid));
63 }
64
65/*************************************************
66* Check the usage restrictions *
67*************************************************/
68X509_Code usage_check(const X509_Certificate& cert,
69 X509_Store::Cert_Usage usage)
70 {
71 if(usage == X509_Store::ANY)
72 return VERIFIED;
73
74 if(!check_usage(cert, usage, X509_Store::CRL_SIGNING, CRL_SIGN))
75 return CA_CERT_NOT_FOR_CRL_ISSUER;
76
77 if(!check_usage(cert, usage, X509_Store::TLS_SERVER, "PKIX.ServerAuth"))
78 return INVALID_USAGE;
79 if(!check_usage(cert, usage, X509_Store::TLS_CLIENT, "PKIX.ClientAuth"))
80 return INVALID_USAGE;
81 if(!check_usage(cert, usage, X509_Store::CODE_SIGNING, "PKIX.CodeSigning"))
82 return INVALID_USAGE;
83 if(!check_usage(cert, usage, X509_Store::EMAIL_PROTECTION,
84 "PKIX.EmailProtection"))
85 return INVALID_USAGE;
86 if(!check_usage(cert, usage, X509_Store::TIME_STAMPING,
87 "PKIX.TimeStamping"))
88 return INVALID_USAGE;
89
90 return VERIFIED;
91 }
92
93}
94
95/*************************************************
96* Define equality for revocation data *
97*************************************************/
98bool X509_Store::CRL_Data::operator==(const CRL_Data& other) const
99 {
100 if(issuer != other.issuer)
101 return false;
102 if(serial != other.serial)
103 return false;
104 return compare_ids(auth_key_id, other.auth_key_id);
105 }
106
107/*************************************************
108* Define inequality for revocation data *
109*************************************************/
110bool X509_Store::CRL_Data::operator!=(const CRL_Data& other) const
111 {
112 return !((*this) == other);
113 }
114
115/*************************************************
116* Define an ordering for revocation data *
117*************************************************/
118bool X509_Store::CRL_Data::operator<(const X509_Store::CRL_Data& other) const
119 {
120 if(*this == other)
121 return false;
122
123 const MemoryVector<byte>& serial1 = serial;
124 const MemoryVector<byte>& key_id1 = auth_key_id;
125 const MemoryVector<byte>& serial2 = other.serial;
126 const MemoryVector<byte>& key_id2 = other.auth_key_id;
127
128 if(compare_ids(key_id1, key_id2) == false)
129 {
130 if(std::lexicographical_compare(key_id1.begin(), key_id1.end(),
131 key_id2.begin(), key_id2.end()))
132 return true;
133
134 if(std::lexicographical_compare(key_id2.begin(), key_id2.end(),
135 key_id1.begin(), key_id1.end()))
136 return false;
137 }
138
139 if(compare_ids(serial1, serial2) == false)
140 {
141 if(std::lexicographical_compare(serial1.begin(), serial1.end(),
142 serial2.begin(), serial2.end()))
143 return true;
144
145 if(std::lexicographical_compare(serial2.begin(), serial2.end(),
146 serial1.begin(), serial1.end()))
147 return false;
148 }
149
150 return (issuer < other.issuer);
151 }
152
153/*************************************************
154* X509_Store Constructor *
155*************************************************/
156X509_Store::X509_Store()
157 {
158 revoked_info_valid = true;
159 }
160
161/*************************************************
162* X509_Store Copy Constructor *
163*************************************************/
164X509_Store::X509_Store(const X509_Store& store)
165 {
166 certs = store.certs;
167 revoked = store.revoked;
168 revoked_info_valid = store.revoked_info_valid;
169 for(u32bit j = 0; j != store.stores.size(); j++)
170 stores[j] = store.stores[j]->clone();
171 }
172
173/*************************************************
174* X509_Store Destructor *
175*************************************************/
176X509_Store::~X509_Store()
177 {
178 for(u32bit j = 0; j != stores.size(); j++)
179 delete stores[j];
180 }
181
182/*************************************************
183* Verify a certificate's authenticity *
184*************************************************/
185X509_Code X509_Store::validate_cert(const X509_Certificate& cert,
186 Cert_Usage cert_usage)
187 {
188 recompute_revoked_info();
189
190 std::vector<u32bit> indexes;
191 X509_Code chaining_result = construct_cert_chain(cert, indexes);
192 if(chaining_result != VERIFIED)
193 return chaining_result;
194
195 const u64bit current_time = system_time();
196
197 s32bit time_check = validity_check(cert.start_time(), cert.end_time(),
198 current_time);
199 if(time_check < 0) return CERT_NOT_YET_VALID;
200 else if(time_check > 0) return CERT_HAS_EXPIRED;
201
202 X509_Code sig_check_result = check_sig(cert, certs[indexes[0]]);
203 if(sig_check_result != VERIFIED)
204 return sig_check_result;
205
206 if(is_revoked(cert))
207 return CERT_IS_REVOKED;
208
209 for(u32bit j = 0; j != indexes.size() - 1; j++)
210 {
211 const X509_Certificate& current_cert = certs[indexes[j]].cert;
212 time_check = validity_check(current_cert.start_time(),
213 current_cert.end_time(), current_time);
214 if(time_check < 0) return CERT_NOT_YET_VALID;
215 else if(time_check > 0) return CERT_HAS_EXPIRED;
216
217 sig_check_result = check_sig(certs[indexes[j]], certs[indexes[j+1]]);
218 if(sig_check_result != VERIFIED)
219 return sig_check_result;
220 }
221
222 return usage_check(cert, cert_usage);
223 }
224
225/*************************************************
226* Find this certificate *
227*************************************************/
228u32bit X509_Store::find_cert(const X509_DN& subject_dn,
229 const MemoryRegion<byte>& subject_key_id) const
230 {
231 for(u32bit j = 0; j != certs.size(); j++)
232 {
233 const X509_Certificate& this_cert = certs[j].cert;
234 if(compare_ids(this_cert.subject_key_id(), subject_key_id) &&
235 this_cert.subject_dn() == subject_dn)
236 return j;
237 }
238 return NO_CERT_FOUND;
239 }
240
241/*************************************************
242* Find the parent of this certificate *
243*************************************************/
244u32bit X509_Store::find_parent_of(const X509_Certificate& cert)
245 {
246 const X509_DN issuer_dn = cert.issuer_dn();
247 const MemoryVector<byte> auth_key_id = cert.authority_key_id();
248
249 u32bit index = find_cert(issuer_dn, auth_key_id);
250
251 if(index != NO_CERT_FOUND)
252 return index;
253
254 if(auth_key_id.size())
255 {
256 for(u32bit j = 0; j != stores.size(); j++)
257 {
258 std::vector<X509_Certificate> got = stores[j]->by_SKID(auth_key_id);
259
260 if(got.size() == 0) continue;
261
262 for(u32bit k = 0; k != got.size(); k++)
263 add_cert(got[k]);
264 return find_cert(issuer_dn, auth_key_id);
265 }
266 }
267
268 return NO_CERT_FOUND;
269 }
270
271/*************************************************
272* Construct a chain of certificate relationships *
273*************************************************/
274X509_Code X509_Store::construct_cert_chain(const X509_Certificate& end_cert,
275 std::vector<u32bit>& indexes,
276 bool need_full_chain)
277 {
278 u32bit parent = find_parent_of(end_cert);
279
280 while(true)
281 {
282 if(parent == NO_CERT_FOUND)
283 return CERT_ISSUER_NOT_FOUND;
284 indexes.push_back(parent);
285
286 if(certs[parent].is_verified())
287 if(certs[parent].verify_result() != VERIFIED)
288 return certs[parent].verify_result();
289
290 const X509_Certificate& parent_cert = certs[parent].cert;
291 if(!parent_cert.is_CA_cert())
292 return CA_CERT_NOT_FOR_CERT_ISSUER;
293
294 if(certs[parent].is_trusted())
295 break;
296 if(parent_cert.self_signed())
297 return CANNOT_ESTABLISH_TRUST;
298
299 if(parent_cert.path_limit() < indexes.size() - 1)
300 return CERT_CHAIN_TOO_LONG;
301
302 parent = find_parent_of(parent_cert);
303 }
304
305 if(need_full_chain)
306 return VERIFIED;
307
308 while(true)
309 {
310 if(indexes.size() < 2)
311 break;
312
313 const u32bit cert = indexes.back();
314
315 if(certs[cert].is_verified())
316 {
317 if(certs[cert].verify_result() != VERIFIED)
318 throw Internal_Error("X509_Store::construct_cert_chain");
319 indexes.pop_back();
320 }
321 else
322 break;
323 }
324
325 const u32bit last_cert = indexes.back();
326 const u32bit parent_of_last_cert = find_parent_of(certs[last_cert].cert);
327 if(parent_of_last_cert == NO_CERT_FOUND)
328 return CERT_ISSUER_NOT_FOUND;
329 indexes.push_back(parent_of_last_cert);
330
331 return VERIFIED;
332 }
333
334/*************************************************
335* Check the CAs signature on a certificate *
336*************************************************/
337X509_Code X509_Store::check_sig(const Cert_Info& cert_info,
338 const Cert_Info& ca_cert_info) const
339 {
340 if(cert_info.is_verified())
341 return cert_info.verify_result();
342
343 const X509_Certificate& cert = cert_info.cert;
344 const X509_Certificate& ca_cert = ca_cert_info.cert;
345
346 X509_Code verify_code = check_sig(cert, ca_cert.subject_public_key());
347
348 cert_info.set_result(verify_code);
349
350 return verify_code;
351 }
352
353/*************************************************
354* Check a CA's signature *
355*************************************************/
356X509_Code X509_Store::check_sig(const X509_Object& object, X509_PublicKey* key)
357 {
358 std::auto_ptr<X509_PublicKey> pub_key(key);
359 std::auto_ptr<PK_Verifier> verifier;
360
361 try {
362 std::vector<std::string> sig_info =
363 split_on(OIDS::lookup(object.signature_algorithm().oid), '/');
364
365 if(sig_info.size() != 2 || sig_info[0] != pub_key->algo_name())
366 return SIGNATURE_ERROR;
367
368 std::string padding = sig_info[1];
369 Signature_Format format;
370 if(key->message_parts() >= 2) format = DER_SEQUENCE;
371 else format = IEEE_1363;
372
373 if(dynamic_cast<PK_Verifying_with_MR_Key*>(pub_key.get()))
374 {
375 PK_Verifying_with_MR_Key* sig_key =
376 dynamic_cast<PK_Verifying_with_MR_Key*>(pub_key.get());
377 verifier.reset(get_pk_verifier(*sig_key, padding, format));
378 }
379 else if(dynamic_cast<PK_Verifying_wo_MR_Key*>(pub_key.get()))
380 {
381 PK_Verifying_wo_MR_Key* sig_key =
382 dynamic_cast<PK_Verifying_wo_MR_Key*>(pub_key.get());
383 verifier.reset(get_pk_verifier(*sig_key, padding, format));
384 }
385 else
386 return CA_CERT_CANNOT_SIGN;
387
388 bool valid = verifier->verify_message(object.tbs_data(),
389 object.signature());
390
391 if(valid)
392 return VERIFIED;
393 else
394 return SIGNATURE_ERROR;
395 }
396 catch(Decoding_Error) { return CERT_FORMAT_ERROR; }
397 catch(Exception) {}
398
399 return UNKNOWN_X509_ERROR;
400 }
401
402/*************************************************
403* Recompute the revocation status of the certs *
404*************************************************/
405void X509_Store::recompute_revoked_info() const
406 {
407 if(revoked_info_valid)
408 return;
409
410 for(u32bit j = 0; j != certs.size(); j++)
411 {
412 if((certs[j].is_verified()) && (certs[j].verify_result() != VERIFIED))
413 continue;
414
415 if(is_revoked(certs[j].cert))
416 certs[j].set_result(CERT_IS_REVOKED);
417 }
418
419 revoked_info_valid = true;
420 }
421
422/*************************************************
423* Check if a certificate is revoked *
424*************************************************/
425bool X509_Store::is_revoked(const X509_Certificate& cert) const
426 {
427 CRL_Data revoked_info;
428 revoked_info.issuer = cert.issuer_dn();
429 revoked_info.serial = cert.serial_number();
430 revoked_info.auth_key_id = cert.authority_key_id();
431
432 if(std::binary_search(revoked.begin(), revoked.end(), revoked_info))
433 return true;
434 return false;
435 }
436
437/*************************************************
438* Retrieve all the certificates in the store *
439*************************************************/
440std::vector<X509_Certificate>
441X509_Store::get_certs(const Search_Func& search) const
442 {
443 std::vector<X509_Certificate> found_certs;
444 for(u32bit j = 0; j != certs.size(); j++)
445 {
446 if(search.match(certs[j].cert))
447 found_certs.push_back(certs[j].cert);
448 }
449 return found_certs;
450 }
451
452/*************************************************
453* Construct a path back to a root for this cert *
454*************************************************/
455std::vector<X509_Certificate>
456X509_Store::get_cert_chain(const X509_Certificate& cert)
457 {
458 std::vector<X509_Certificate> result;
459 std::vector<u32bit> indexes;
460 X509_Code chaining_result = construct_cert_chain(cert, indexes, true);
461
462 if(chaining_result != VERIFIED)
463 throw Invalid_State("X509_Store::get_cert_chain: Can't construct chain");
464
465 for(u32bit j = 0; j != indexes.size(); j++)
466 result.push_back(certs[indexes[j]].cert);
467 return result;
468 }
469
470/*************************************************
471* Add a certificate store to the list of stores *
472*************************************************/
473void X509_Store::add_new_certstore(Certificate_Store* certstore)
474 {
475 stores.push_back(certstore);
476 }
477
478/*************************************************
479* Add a certificate to the store *
480*************************************************/
481void X509_Store::add_cert(const X509_Certificate& cert, bool trusted)
482 {
483 if(trusted && !cert.self_signed())
484 throw Invalid_Argument("X509_Store: Trusted certs must be self-signed");
485
486 if(find_cert(cert.subject_dn(), cert.subject_key_id()) == NO_CERT_FOUND)
487 {
488 revoked_info_valid = false;
489 Cert_Info info(cert, trusted);
490 certs.push_back(info);
491 }
492 else if(trusted)
493 {
494 for(u32bit j = 0; j != certs.size(); j++)
495 {
496 const X509_Certificate& this_cert = certs[j].cert;
497 if(this_cert == cert)
498 certs[j].trusted = trusted;
499 }
500 }
501 }
502
503/*************************************************
504* Add one or more certificates to the store *
505*************************************************/
506void X509_Store::do_add_certs(DataSource& source, bool trusted)
507 {
508 while(!source.end_of_data())
509 {
510 try {
511 X509_Certificate cert(source);
512 add_cert(cert, trusted);
513 }
514 catch(Decoding_Error) {}
515 catch(Invalid_Argument) {}
516 }
517 }
518
519/*************************************************
520* Add one or more certificates to the store *
521*************************************************/
522void X509_Store::add_certs(DataSource& source)
523 {
524 do_add_certs(source, false);
525 }
526
527/*************************************************
528* Add one or more certificates to the store *
529*************************************************/
530void X509_Store::add_trusted_certs(DataSource& source)
531 {
532 do_add_certs(source, true);
533 }
534
535/*************************************************
536* Add one or more certificates to the store *
537*************************************************/
538X509_Code X509_Store::add_crl(const X509_CRL& crl)
539 {
540 s32bit time_check = validity_check(crl.this_update(), crl.next_update(),
541 system_time());
542 if(time_check < 0) return CRL_NOT_YET_VALID;
543 else if(time_check > 0) return CRL_HAS_EXPIRED;
544
545 u32bit cert_index = NO_CERT_FOUND;
546
547 for(u32bit j = 0; j != certs.size(); j++)
548 {
549 const X509_Certificate& this_cert = certs[j].cert;
550 if(compare_ids(this_cert.subject_key_id(), crl.authority_key_id()))
551 {
552 if(this_cert.subject_dn() == crl.issuer_dn())
553 cert_index = j;
554 }
555 }
556
557 if(cert_index == NO_CERT_FOUND)
558 return CRL_ISSUER_NOT_FOUND;
559
560 const X509_Certificate& ca_cert = certs[cert_index].cert;
561
562 X509_Code verify_result = validate_cert(ca_cert, CRL_SIGNING);
563 if(verify_result != VERIFIED)
564 return verify_result;
565
566 verify_result = check_sig(crl, ca_cert.subject_public_key());
567 if(verify_result != VERIFIED)
568 return verify_result;
569
570 std::vector<CRL_Entry> revoked_certs = crl.get_revoked();
571
572 for(u32bit j = 0; j != revoked_certs.size(); j++)
573 {
574 CRL_Data revoked_info;
575 revoked_info.issuer = crl.issuer_dn();
576 revoked_info.serial = revoked_certs[j].serial;
577 revoked_info.auth_key_id = crl.authority_key_id();
578
579 std::vector<CRL_Data>::iterator p =
580 std::find(revoked.begin(), revoked.end(), revoked_info);
581
582 if(revoked_certs[j].reason == REMOVE_FROM_CRL)
583 {
584 if(p == revoked.end()) continue;
585 revoked.erase(p);
586 }
587 else
588 {
589 if(p != revoked.end()) continue;
590 revoked.push_back(revoked_info);
591 }
592 }
593
594 std::sort(revoked.begin(), revoked.end());
595 revoked_info_valid = false;
596
597 return VERIFIED;
598 }
599
600/*************************************************
601* PEM encode the set of certificates *
602*************************************************/
603std::string X509_Store::PEM_encode() const
604 {
605 std::string cert_store;
606 for(u32bit j = 0; j != certs.size(); j++)
607 cert_store += certs[j].cert.PEM_encode();
608 return cert_store;
609 }
610
611/*************************************************
612* Create a Cert_Info structure *
613*************************************************/
614X509_Store::Cert_Info::Cert_Info(const X509_Certificate& c,
615 bool t) : cert(c), trusted(t)
616 {
617 checked = false;
618 result = UNKNOWN_X509_ERROR;
619 last_checked = 0;
620 }
621
622/*************************************************
623* Return the verification results *
624*************************************************/
625X509_Code X509_Store::Cert_Info::verify_result() const
626 {
627 if(!checked)
628 throw Invalid_State("Cert_Info::verify_result() called; not checked");
629 return result;
630 }
631
632/*************************************************
633* Set the verification results *
634*************************************************/
635void X509_Store::Cert_Info::set_result(X509_Code code) const
636 {
637 result = code;
638 last_checked = system_time();
639 checked = true;
640 }
641
642/*************************************************
643* Check if this certificate can be trusted *
644*************************************************/
645bool X509_Store::Cert_Info::is_trusted() const
646 {
647 return trusted;
648 }
649
650/*************************************************
651* Check if this certificate has been verified *
652*************************************************/
653bool X509_Store::Cert_Info::is_verified() const
654 {
655 if(!checked)
656 return false;
657 if(result != VERIFIED && result != CERT_NOT_YET_VALID)
658 return true;
659
660 const u32bit CACHE_TIME = Config::get_time("x509/cache_verify_results");
661 const u64bit current_time = system_time();
662
663 if(current_time > last_checked + CACHE_TIME)
664 checked = false;
665
666 return checked;
667 }
668
669}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status