1 /*
2 * Certificate Store
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/certstor.h>
9 
10 namespace Botan {
11 
clone() const12 Certificate_Store* Certificate_Store_Memory::clone() const
13    {
14    return new Certificate_Store_Memory(*this);
15    }
16 
add_certificate(const X509_Certificate & cert)17 void Certificate_Store_Memory::add_certificate(const X509_Certificate& cert)
18    {
19    for(size_t i = 0; i != certs.size(); ++i)
20       {
21       if(certs[i] == cert)
22          return;
23       }
24 
25    certs.push_back(cert);
26    }
27 
28 std::vector<X509_Certificate>
find_cert_by_subject_and_key_id(const X509_DN & subject_dn,const MemoryRegion<byte> & key_id) const29 Certificate_Store_Memory::find_cert_by_subject_and_key_id(
30    const X509_DN& subject_dn,
31    const MemoryRegion<byte>& key_id) const
32    {
33    std::vector<X509_Certificate> result;
34 
35    for(size_t i = 0; i != certs.size(); ++i)
36       {
37       // Only compare key ids if set in both call and in the cert
38       if(key_id.size())
39          {
40          MemoryVector<byte> skid = certs[i].subject_key_id();
41 
42          if(skid.size() && skid != key_id) // no match
43             continue;
44          }
45 
46       if(certs[i].subject_dn() == subject_dn)
47          result.push_back(certs[i]);
48       }
49 
50    return result;
51    }
52 
add_crl(const X509_CRL & crl)53 void Certificate_Store_Memory::add_crl(const X509_CRL& crl)
54    {
55    X509_DN crl_issuer = crl.issuer_dn();
56 
57    for(size_t i = 0; i != crls.size(); ++i)
58       {
59       // Found an update of a previously existing one; replace it
60       if(crls[i].issuer_dn() == crl_issuer)
61          {
62          if(crls[i].this_update() < crl.this_update())
63             {
64             crls[i] = crl;
65             return;
66             }
67          }
68       }
69 
70    // Totally new CRL, add to the list
71    crls.push_back(crl);
72    }
73 
74 std::vector<X509_CRL>
find_crl_by_subject_and_key_id(const X509_DN & issuer_dn,const MemoryRegion<byte> & key_id) const75 Certificate_Store_Memory::find_crl_by_subject_and_key_id(
76    const X509_DN& issuer_dn,
77    const MemoryRegion<byte>& key_id) const
78    {
79    std::vector<X509_CRL> result;
80 
81    for(size_t i = 0; i != crls.size(); ++i)
82       {
83       // Only compare key ids if set in both call and in the CRL
84       if(key_id.size())
85          {
86          MemoryVector<byte> akid = crls[i].authority_key_id();
87 
88          if(akid.size() && akid != key_id) // no match
89             continue;
90          }
91 
92       if(crls[i].issuer_dn() == issuer_dn)
93          result.push_back(crls[i]);
94       }
95 
96    return result;
97    }
98 
99 }
100