blob: d1e821adc697b4759c484eae6f1ed14c30077b71 [file] [log] [blame]
akmhoque099495b2014-03-11 16:01:19 -05001#include "nlsr_cert_store.hpp"
2
3namespace nlsr
4{
akmhoque5a44dd42014-03-12 18:11:32 -05005 static bool
6 nlsrCertificateStoreEntryCompare(NlsrCertificateStoreEntry& ncse1,
7 NlsrCertificateStoreEntry& ncse2)
8
9 {
10 return ncse1.getCert()->getName().toUri() ==
11 ncse2.getCert()->getName().toUri() ;
12 }
13
14 static bool
15 nlsrCertificateStoreEntryCompareByName(NlsrCertificateStoreEntry& ncse1,
16 std::string compCertName)
17
18 {
19 ndn::Name ccn(compCertName);
20 return ( ncse1.getCert()->getName().toUri() == compCertName ||
21 ccn.isPrefixOf(ncse1.getCert()->getName()) );
22 }
23
24 bool
25 NlsrCertificateStore::addCertificate(NlsrCertificateStoreEntry & ncse)
26 {
27 std::list<NlsrCertificateStoreEntry>::iterator it =
28 std::find_if( certTable.begin(), certTable.end(),
29 bind(&nlsrCertificateStoreEntryCompare, _1, ncse));
30 if(it == certTable.end())
akmhoque099495b2014-03-11 16:01:19 -050031 {
akmhoque5a44dd42014-03-12 18:11:32 -050032 certTable.push_back(ncse);
33 return true;
akmhoque099495b2014-03-11 16:01:19 -050034 }
akmhoque5a44dd42014-03-12 18:11:32 -050035 if( it != certTable.end() )
akmhoque099495b2014-03-11 16:01:19 -050036 {
akmhoque5a44dd42014-03-12 18:11:32 -050037 if ( (*it).getCertSeqNum() < ncse.getCertSeqNum() )
38 {
39 certTable.erase(it);
40 certTable.push_back(ncse);
akmhoque099495b2014-03-11 16:01:19 -050041 return true;
akmhoque5a44dd42014-03-12 18:11:32 -050042 }
akmhoque099495b2014-03-11 16:01:19 -050043 }
akmhoque5a44dd42014-03-12 18:11:32 -050044 return false;
45 }
46
47 bool
48 NlsrCertificateStore::addCertificate(
49 ndn::shared_ptr<ndn::IdentityCertificate> pcert, uint32_t csn, bool isv)
50 {
51 NlsrCertificateStoreEntry ncse(pcert, csn, isv);
52 return addCertificate(ncse);
53 }
54
55 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
56 NlsrCertificateStore::getCertificateFromStore(const std::string certName)
57 {
58 std::list<NlsrCertificateStoreEntry>::iterator it =
59 std::find_if( certTable.begin(), certTable.end(),
60 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
61 if(it == certTable.end())
akmhoque099495b2014-03-11 16:01:19 -050062 {
akmhoque5a44dd42014-03-12 18:11:32 -050063 ndn::shared_ptr<ndn::IdentityCertificate> cert=
64 ndn::make_shared<ndn::IdentityCertificate>();
65 return std::make_pair(cert,false);
akmhoque099495b2014-03-11 16:01:19 -050066 }
akmhoque5a44dd42014-03-12 18:11:32 -050067 return std::make_pair((*it).getCert(),true);
68 }
69
70 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
71 NlsrCertificateStore::getCertificateFromStore(
72 const std::string certName, int checkSeqNum)
73 {
74 std::list<NlsrCertificateStoreEntry>::iterator it =
75 std::find_if( certTable.begin(), certTable.end(),
76 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
77 if(it == certTable.end())
akmhoque099495b2014-03-11 16:01:19 -050078 {
akmhoque5a44dd42014-03-12 18:11:32 -050079 ndn::shared_ptr<ndn::IdentityCertificate> cert=
80 ndn::make_shared<ndn::IdentityCertificate>();
81 return std::make_pair(cert,false);
akmhoque099495b2014-03-11 16:01:19 -050082 }
akmhoque5a44dd42014-03-12 18:11:32 -050083 else
84 {
85 if( (*it).getCertSeqNum() == checkSeqNum )
86 {
87 return std::make_pair((*it).getCert(),true);
88 }
89 }
90 return std::make_pair((*it).getCert(),false);
91 }
92
93 bool
94 NlsrCertificateStore::isCertificateNewInStore(const std::string certName,
95 int checkSeqNo)
96 {
97 std::list<NlsrCertificateStoreEntry>::iterator it =
98 std::find_if( certTable.begin(), certTable.end(),
99 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
100 if(it != certTable.end())
101 {
102 return (*it).getCertSeqNum() < checkSeqNo ;
103 }
104 return true;
105 }
106
107 bool
108 NlsrCertificateStore::removeCertificateFromStroe(const std::string certName)
109 {
110 std::list<NlsrCertificateStoreEntry>::iterator it =
111 std::find_if( certTable.begin(), certTable.end(),
112 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
113 if(it != certTable.end())
114 {
115 certTable.erase(it);
116 return true;
117 }
118 return false;
119 }
120
121 void
122 NlsrCertificateStore::printCertStore()
123 {
124 std::list<NlsrCertificateStoreEntry>::iterator it;
125 for(it=certTable.begin(); it!=certTable.end(); ++it)
126 {
127 std::cout<<(*it)<<std::endl;
128 }
129 }
akmhoque099495b2014-03-11 16:01:19 -0500130}