blob: 6990b075d2edeac44ed37ecb78b4871dd34913b0 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
2#include <ndn-cpp-dev/security/key-chain.hpp>
3#include "certificate-store.hpp"
4#include "waiting-list-entry.hpp"
5#include "key-manager.hpp"
6
7
8namespace nlsr {
9static bool
10nlsrCertificateStoreEntryCompare(CertificateStoreEntry& ncse1,
11 CertificateStoreEntry& ncse2)
12
13{
14 int sizeDiff = ncse1.getCert()->getName().size() -
15 ncse2.getCert()->getName().size();
16 return (ncse2.getCert()->getName().isPrefixOf(ncse1.getCert()->getName()) &&
17 (sizeDiff <= 1 && sizeDiff >= 0));
18}
19
20static bool
21nlsrCertificateStoreEntryCompareByName(CertificateStoreEntry& ncse1,
22 std::string compCertName)
23
24{
25 ndn::Name ccn(compCertName);
26 int sizeDiff = ncse1.getCert()->getName().size() - ccn.size();
27 return (ccn.isPrefixOf(ncse1.getCert()->getName()) &&
28 (sizeDiff <= 1 && sizeDiff >= 0));
29}
30
31void
32CertificateStore::updateWaitingList(std::string respCertName)
33{
34 ndn::Name tmpName(respCertName);
35 respCertName = tmpName.getPrefix(-1).toUri();
36 std::pair<WaitingListEntry, bool> chkWle =
37 m_waitingList.getWaitingListEntry(respCertName);
38 if (chkWle.second)
39 {
40 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> sc =
41 getCertificateFromStore(respCertName);
42 std::list<std::string> waitees = (chkWle.first).getWaitingCerts();
43 for (std::list<std::string>::iterator it = waitees.begin();
44 it != waitees.end(); ++it)
45 {
46 KeyManager km;
47 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> wc =
48 getCertificateFromStore(*(it));
49 if (wc.second && sc.second)
50 {
51 if (km.verifySignature(*(wc.first), sc.first->getPublicKeyInfo()))
52 {
53 //1. Update Certificate Store
54 setCertificateIsVerified(*(it), true);
55 //2. Call updateWaitingList for waitee ( *(it) )
56 updateWaitingList(*(it));
57 }
58 }
59 }
60 }
akmhoque53353462014-04-22 08:43:45 -050061 //remove that entry from waiting list
62 m_waitingList.remove(respCertName);
63}
64
65void
66CertificateStore::updateWaitingList(CertificateStoreEntry& ncse)
67{
68 if (ncse.getIsSignerVerified())
69 {
70 updateWaitingList(ncse.getCert()->getName().toUri());
71 }
72 else
73 {
74 ndn::SignatureSha256WithRsa signature(ncse.getCert()->getSignature());
75 m_waitingList.add(signature.getKeyLocator().getName().toUri(),
76 ncse.getCert()->getName().toUri());
77 }
78}
79
80bool
81CertificateStore::addCertificate(CertificateStoreEntry& ncse)
82{
83 std::list<CertificateStoreEntry>::iterator it =
84 std::find_if(m_certTable.begin(), m_certTable.end(),
85 bind(&nlsrCertificateStoreEntryCompare, _1, ncse));
86 if (it == m_certTable.end())
87 {
88 m_certTable.push_back(ncse);
89 updateWaitingList(ncse);
90 return true;
91 }
92 else if (it != m_certTable.end())
93 {
94 if ((*it).getCertSeqNum() < ncse.getCertSeqNum())
95 {
96 m_certTable.erase(it);
97 m_certTable.push_back(ncse);
98 updateWaitingList(ncse);
99 return true;
100 }
101 }
102 return false;
103}
104
105bool
106CertificateStore::addCertificate(
107 ndn::shared_ptr<ndn::IdentityCertificate> pcert, uint32_t csn, bool isv)
108{
109 CertificateStoreEntry ncse(pcert, csn, isv);
110 return addCertificate(ncse);
111}
112
113std::pair<uint32_t, bool>
114CertificateStore::getCertificateSeqNum(std::string certName)
115{
116 std::list<CertificateStoreEntry>::iterator it =
117 std::find_if(m_certTable.begin(), m_certTable.end(),
118 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
119 if (it == m_certTable.end())
120 {
121 return std::make_pair(0, false);
122 }
123 return std::make_pair((*it).getCertSeqNum(), true);
124}
125
126
127
128void
129CertificateStore::setCertificateIsVerified(std::string certName,
130 bool isVerified)
131{
132 std::list<CertificateStoreEntry>::iterator it =
133 std::find_if(m_certTable.begin(), m_certTable.end(),
134 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
135 if (it != m_certTable.end())
136 {
137 it->setIsSignerVerified(true);
138 }
139}
140
141bool
142CertificateStore::getCertificateIsVerified(std::string certName)
143{
144 std::list<CertificateStoreEntry>::iterator it =
145 std::find_if(m_certTable.begin(), m_certTable.end(),
146 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
147 if (it != m_certTable.end())
148 {
149 return it->getIsSignerVerified();
150 }
akmhoque53353462014-04-22 08:43:45 -0500151 return false;
152}
153
154std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
155CertificateStore::getCertificateFromStore(const std::string certName)
156{
157 std::list<CertificateStoreEntry>::iterator it =
158 std::find_if(m_certTable.begin(), m_certTable.end(),
159 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
160 if (it == m_certTable.end())
161 {
162 ndn::shared_ptr<ndn::IdentityCertificate> cert =
163 ndn::make_shared<ndn::IdentityCertificate>();
164 return std::make_pair(cert, false);
165 }
166 return std::make_pair((*it).getCert(), true);
167}
168
169std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
170CertificateStore::getCertificateFromStore(
171 const std::string certName, uint64_t checkSeqNum)
172{
173 std::list<CertificateStoreEntry>::iterator it =
174 std::find_if(m_certTable.begin(), m_certTable.end(),
175 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
176 if (it == m_certTable.end())
177 {
178 ndn::shared_ptr<ndn::IdentityCertificate> cert =
179 ndn::make_shared<ndn::IdentityCertificate>();
180 return std::make_pair(cert, false);
181 }
182 else
183 {
184 if ((*it).getCertSeqNum() == checkSeqNum)
185 {
186 return std::make_pair((*it).getCert(), true);
187 }
188 }
189 return std::make_pair((*it).getCert(), false);
190}
191
192bool
193CertificateStore::isCertificateNewInStore(const std::string certName,
194 int checkSeqNo)
195{
196 std::list<CertificateStoreEntry>::iterator it =
197 std::find_if(m_certTable.begin(), m_certTable.end(),
198 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
199 if (it != m_certTable.end())
200 {
201 return (*it).getCertSeqNum() < checkSeqNo ;
202 }
203 return true;
204}
205
206bool
207CertificateStore::removeCertificateFromStroe(const std::string certName)
208{
209 std::list<CertificateStoreEntry>::iterator it =
210 std::find_if(m_certTable.begin(), m_certTable.end(),
211 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
212 if (it != m_certTable.end())
213 {
214 m_certTable.erase(it);
215 return true;
216 }
217 return false;
218}
219
220void
221CertificateStore::print()
222{
223 std::list<CertificateStoreEntry>::iterator it;
224 for (it = m_certTable.begin(); it != m_certTable.end(); ++it)
225 {
226 std::cout << (*it) << std::endl;
227 }
228 std::cout << m_waitingList << std::endl;
229}
230
231} //namespace nlsr