summaryrefslogtreecommitdiffstats
path: root/old/botan/src/cert/x509/x509find.cpp
blob: 257367da9f58da9d2eab540c52d7ffe44b465933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* X.509 Certificate Store Searching
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/x509find.h>
#include <botan/charset.h>
#include <algorithm>

namespace Botan {

namespace {

/*
* Compare based on case-insensive substrings
*/
bool substring_match(const std::string& searching_for,
                     const std::string& found)
   {
   if(std::search(found.begin(), found.end(), searching_for.begin(),
                  searching_for.end(), Charset::caseless_cmp) != found.end())
      return true;
   return false;
   }

/*
* Compare based on case-insensive match
*/
bool ignore_case(const std::string& searching_for, const std::string& found)
   {
   if(searching_for.size() != found.size())
      return false;

   return std::equal(found.begin(), found.end(),
                     searching_for.begin(), Charset::caseless_cmp);
   }

}

/*
* Search based on the contents of a DN entry
*/
bool DN_Check::match(const X509_Certificate& cert) const
   {
   std::vector<std::string> info = cert.subject_info(dn_entry);

   for(u32bit j = 0; j != info.size(); ++j)
      if(compare(info[j], looking_for))
         return true;
   return false;
   }

/*
* DN_Check Constructor
*/
DN_Check::DN_Check(const std::string& dn_entry, const std::string& looking_for,
                   compare_fn func)
   {
   this->dn_entry = dn_entry;
   this->looking_for = looking_for;
   compare = func;
   }

/*
* DN_Check Constructor
*/
DN_Check::DN_Check(const std::string& dn_entry, const std::string& looking_for,
                   Search_Type method)
   {
   this->dn_entry = dn_entry;
   this->looking_for = looking_for;

   if(method == SUBSTRING_MATCHING)
      compare = &substring_match;
   else if(method == IGNORE_CASE)
      compare = &ignore_case;
   else
      throw Invalid_Argument("Unknown method argument to DN_Check()");
   }

/*
* Match by issuer and serial number
*/
bool IandS_Match::match(const X509_Certificate& cert) const
   {
   if(cert.serial_number() != serial)
      return false;
   return (cert.issuer_dn() == issuer);
   }

/*
* IandS_Match Constructor
*/
IandS_Match::IandS_Match(const X509_DN& issuer,
                         const MemoryRegion<byte>& serial)
   {
   this->issuer = issuer;
   this->serial = serial;
   }

/*
* Match by subject key identifier
*/
bool SKID_Match::match(const X509_Certificate& cert) const
   {
   return (cert.subject_key_id() == skid);
   }

}