monotone

monotone Mtn Source Tree

Root/botan/x509find.cpp

1/*************************************************
2* X.509 Certificate Store Searching Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/x509stor.h>
7#include <botan/charset.h>
8#include <algorithm>
9#include <memory>
10
11namespace Botan {
12
13namespace X509_Store_Search {
14
15namespace {
16
17/*************************************************
18* Comparison Function Pointer *
19*************************************************/
20typedef bool (*compare_fn)(const std::string&, const std::string&);
21
22/*************************************************
23* Predicate for caseless searching *
24*************************************************/
25bool caseless_cmp(char a, char b)
26 {
27 return (to_lower(a) == to_lower(b));
28 }
29
30/*************************************************
31* Compare based on case-insensive substrings *
32*************************************************/
33bool substring_match(const std::string& searching_for,
34 const std::string& found)
35 {
36 if(std::search(found.begin(), found.end(), searching_for.begin(),
37 searching_for.end(), caseless_cmp) != found.end())
38 return true;
39 return false;
40 }
41
42/*************************************************
43* Compare based on case-insensive match *
44*************************************************/
45bool ignore_case(const std::string& searching_for, const std::string& found)
46 {
47 if(searching_for.size() != found.size())
48 return false;
49
50 return std::equal(found.begin(), found.end(),
51 searching_for.begin(), caseless_cmp);
52 }
53
54/*************************************************
55* Search based on the contents of a DN entry *
56*************************************************/
57class DN_Check : public X509_Store::Search_Func
58 {
59 public:
60 bool match(const X509_Certificate& cert) const
61 {
62 return compare(looking_for, cert.subject_info(dn_entry));
63 }
64
65 DN_Check(const std::string& entry, const std::string& target,
66 compare_fn func) :
67 compare(func), dn_entry(entry), looking_for(target) {}
68 private:
69 compare_fn compare;
70 const std::string dn_entry;
71 const std::string looking_for;
72 };
73
74/*************************************************
75* Search based on the key id *
76*************************************************/
77class KeyID_Match : public X509_Store::Search_Func
78 {
79 public:
80 bool match(const X509_Certificate& cert) const
81 {
82 std::auto_ptr<X509_PublicKey> key(cert.subject_public_key());
83 return (key->key_id() == key_id);
84 }
85 KeyID_Match(u64bit id) : key_id(id) {}
86 private:
87 u64bit key_id;
88 };
89
90/*************************************************
91* Search based on the issuer and serial number *
92*************************************************/
93class IandS_Match : public X509_Store::Search_Func
94 {
95 public:
96 bool match(const X509_Certificate& cert) const
97 {
98 if(cert.serial_number() != serial)
99 return false;
100 return (cert.issuer_dn() == issuer);
101 }
102 IandS_Match(const X509_DN& i, const MemoryRegion<byte>& s) :
103 issuer(i), serial(s) {}
104 private:
105 X509_DN issuer;
106 MemoryVector<byte> serial;
107 };
108
109/*************************************************
110* Search based on the subject key id *
111*************************************************/
112class SKID_Match : public X509_Store::Search_Func
113 {
114 public:
115 bool match(const X509_Certificate& cert) const
116 {
117 return (cert.subject_key_id() == skid);
118 }
119 SKID_Match(const MemoryRegion<byte>& s) : skid(s) {}
120 private:
121 MemoryVector<byte> skid;
122 };
123
124}
125
126/*************************************************
127* Search for a certificate by email address *
128*************************************************/
129std::vector<X509_Certificate> by_email(const X509_Store& store,
130 const std::string& email)
131 {
132 DN_Check search_params("RFC822", email, ignore_case);
133 return store.get_certs(search_params);
134 }
135
136/*************************************************
137* Search for a certificate by CommonName *
138*************************************************/
139std::vector<X509_Certificate> by_name(const X509_Store& store,
140 const std::string& name)
141 {
142 DN_Check search_params("CommonName", name, substring_match);
143 return store.get_certs(search_params);
144 }
145
146/*************************************************
147* Search for a certificate by DNS name *
148*************************************************/
149std::vector<X509_Certificate> by_dns(const X509_Store& store,
150 const std::string& dns)
151 {
152 DN_Check search_params("DNS", dns, ignore_case);
153 return store.get_certs(search_params);
154 }
155
156/*************************************************
157* Search for a certificate by key id *
158*************************************************/
159std::vector<X509_Certificate> by_keyid(const X509_Store& store, u64bit key_id)
160 {
161 KeyID_Match search_params(key_id);
162 return store.get_certs(search_params);
163 }
164
165/*************************************************
166* Search for a certificate by issuer/serial *
167*************************************************/
168std::vector<X509_Certificate> by_iands(const X509_Store& store,
169 const X509_DN& issuer,
170 const MemoryRegion<byte>& serial)
171 {
172 IandS_Match search_params(issuer, serial);
173 return store.get_certs(search_params);
174 }
175
176/*************************************************
177* Search for a certificate by subject keyid *
178*************************************************/
179std::vector<X509_Certificate> by_SKID(const X509_Store& store,
180 const MemoryRegion<byte>& skid)
181 {
182 SKID_Match search_params(skid);
183 return store.get_certs(search_params);
184 }
185
186}
187
188}

Archive Download this file

Branches

Tags

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