aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/cert/x509/certstor.cpp
blob: 28b46ef1aecec6f38dc181f06b9934c32678b356 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Certificate Store
* (C) 1999-2010,2013 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/certstor.h>
#include <botan/internal/filesystem.h>

namespace Botan {

std::shared_ptr<const X509_CRL> Certificate_Store::find_crl_for(const X509_Certificate&) const
   {
   return std::shared_ptr<const X509_CRL>();
   }

void Certificate_Store_In_Memory::add_certificate(const X509_Certificate& cert)
   {
   for(size_t i = 0; i != m_certs.size(); ++i)
      {
      if(*m_certs[i] == cert)
         return;
      }

   m_certs.push_back(std::make_shared<X509_Certificate>(cert));
   }

std::vector<X509_DN> Certificate_Store_In_Memory::all_subjects() const
   {
   std::vector<X509_DN> subjects;
   for(size_t i = 0; i != m_certs.size(); ++i)
      subjects.push_back(m_certs[i]->subject_dn());
   return subjects;
   }

namespace {

template<typename T>
std::shared_ptr<const X509_Certificate>
cert_search(const X509_DN& subject_dn, const std::vector<byte>& key_id,
            const std::vector<std::shared_ptr<T>>& certs)
   {
   for(size_t i = 0; i != certs.size(); ++i)
      {
      // Only compare key ids if set in both call and in the cert
      if(key_id.size())
         {
         std::vector<byte> skid = certs[i]->subject_key_id();

         if(skid.size() && skid != key_id) // no match
            continue;
         }

      if(certs[i]->subject_dn() == subject_dn)
         return certs[i];
      }

   return std::shared_ptr<const X509_Certificate>();
   }

}

std::shared_ptr<const X509_Certificate>
Certificate_Store_In_Memory::find_cert(const X509_DN& subject_dn,
                                       const std::vector<byte>& key_id) const
   {
   return cert_search(subject_dn, key_id, m_certs);
   }

void Certificate_Store_In_Memory::add_crl(const X509_CRL& crl)
   {
   X509_DN crl_issuer = crl.issuer_dn();

   for(size_t i = 0; i != m_crls.size(); ++i)
      {
      // Found an update of a previously existing one; replace it
      if(m_crls[i]->issuer_dn() == crl_issuer)
         {
         if(m_crls[i]->this_update() <= crl.this_update())
            m_crls[i] = std::make_shared<X509_CRL>(crl);
         return;
         }
      }

   // Totally new CRL, add to the list
   m_crls.push_back(std::make_shared<X509_CRL>(crl));
   }

std::shared_ptr<const X509_CRL> Certificate_Store_In_Memory::find_crl_for(const X509_Certificate& subject) const
   {
   const std::vector<byte>& key_id = subject.authority_key_id();

   for(size_t i = 0; i != m_crls.size(); ++i)
      {
      // Only compare key ids if set in both call and in the CRL
      if(key_id.size())
         {
         std::vector<byte> akid = m_crls[i]->authority_key_id();

         if(akid.size() && akid != key_id) // no match
            continue;
         }

      if(m_crls[i]->issuer_dn() == subject.issuer_dn())
         return m_crls[i];
      }

   return std::shared_ptr<const X509_CRL>();
   }

Certificate_Store_In_Memory::Certificate_Store_In_Memory(const X509_Certificate& cert)
   {
   add_certificate(cert);
   }

Certificate_Store_In_Memory::Certificate_Store_In_Memory(const std::string& dir)
   {
   if(dir.empty())
      return;

   std::vector<std::string> maybe_certs = get_files_recursive(dir);
   for(auto&& cert_file : maybe_certs)
      {
      try
         {
         m_certs.push_back(std::make_shared<X509_Certificate>(cert_file));
         }
      catch(std::exception&)
         {
         }
      }
   }

std::shared_ptr<const X509_Certificate>
Certificate_Store_Overlay::find_cert(const X509_DN& subject_dn,
                                     const std::vector<byte>& key_id) const
   {
   return cert_search(subject_dn, key_id, m_certs);
   }

std::vector<X509_DN> Certificate_Store_Overlay::all_subjects() const
   {
   std::vector<X509_DN> subjects;
   for(size_t i = 0; i != m_certs.size(); ++i)
      subjects.push_back(m_certs[i]->subject_dn());
   return subjects;
   }

}