blob: 897d44aa2a0458f2e2b79ea469e1e8cd046464e4 [file] [log] [blame]
akmhoquefa8ee9b2014-03-14 09:06:24 -05001#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
2#include <ndn-cpp-dev/security/key-chain.hpp>
akmhoque099495b2014-03-11 16:01:19 -05003#include "nlsr_cert_store.hpp"
akmhoquefa8ee9b2014-03-14 09:06:24 -05004#include "nlsr_wle.hpp"
5#include "nlsr_km.hpp"
akmhoque099495b2014-03-11 16:01:19 -05006
7namespace nlsr
8{
akmhoque5a44dd42014-03-12 18:11:32 -05009 static bool
10 nlsrCertificateStoreEntryCompare(NlsrCertificateStoreEntry& ncse1,
11 NlsrCertificateStoreEntry& ncse2)
12
akmhoquefa8ee9b2014-03-14 09:06:24 -050013 {
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
akmhoque5a44dd42014-03-12 18:11:32 -050020 }
21
22 static bool
23 nlsrCertificateStoreEntryCompareByName(NlsrCertificateStoreEntry& ncse1,
24 std::string compCertName)
25
26 {
27 ndn::Name ccn(compCertName);
akmhoquefa8ee9b2014-03-14 09:06:24 -050028 int sizeDiff= ncse1.getCert()->getName().size() -ccn.size();
29 return ( ccn.isPrefixOf(ncse1.getCert()->getName()) &&
30 (sizeDiff <= 1 && sizeDiff>= 0));
31 }
32
33 void
34 NlsrCertificateStore::updateWaitingList(std::string respCertName)
35 {
36 ndn::Name tmpName(respCertName);
37 respCertName=tmpName.getPrefix(-1).toUri();
38 std::pair<WaitingListEntry, bool> chkWle=
39 waitingList.getWaitingListEntry(respCertName);
40 if( chkWle.second )
41 {
42 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> sc=
43 getCertificateFromStore(respCertName);
44 std::list<std::string> waitees=(chkWle.first).getWaitingCerts();
45 for(std::list<std::string>::iterator it = waitees.begin();
46 it != waitees.end();++it)
47 {
48 KeyManager km;
49 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> wc=
50 getCertificateFromStore(*(it));
51 if( wc.second && sc.second )
52 {
53 if(km.verifySignature(*(wc.first),sc.first->getPublicKeyInfo()))
54 {
55 //1. Update Certificate Store
56 setCertificateIsVerified(*(it),true);
57 //2. Call updateWaitingList for waitee ( *(it) )
58 updateWaitingList(*(it));
59 }
60 }
61 }
62 }
63
64 //remove that entry from waiting list
65 waitingList.removeFromWaitingList(respCertName);
66 }
67
68 void
69 NlsrCertificateStore::updateWaitingList(NlsrCertificateStoreEntry& ncse)
70 {
71 if( ncse.getIsSignerVerified())
72 {
73 updateWaitingList(ncse.getCert()->getName().toUri());
74 }
75 else
76 {
77 ndn::SignatureSha256WithRsa signature(ncse.getCert()->getSignature());
78 waitingList.addtoWaitingList(signature.getKeyLocator().getName().toUri(),
79 ncse.getCert()->getName().toUri());
80 }
akmhoque5a44dd42014-03-12 18:11:32 -050081 }
82
83 bool
84 NlsrCertificateStore::addCertificate(NlsrCertificateStoreEntry & ncse)
85 {
86 std::list<NlsrCertificateStoreEntry>::iterator it =
87 std::find_if( certTable.begin(), certTable.end(),
88 bind(&nlsrCertificateStoreEntryCompare, _1, ncse));
89 if(it == certTable.end())
akmhoque099495b2014-03-11 16:01:19 -050090 {
akmhoque5a44dd42014-03-12 18:11:32 -050091 certTable.push_back(ncse);
akmhoquefa8ee9b2014-03-14 09:06:24 -050092 updateWaitingList(ncse);
akmhoque5a44dd42014-03-12 18:11:32 -050093 return true;
akmhoque099495b2014-03-11 16:01:19 -050094 }
akmhoquefa8ee9b2014-03-14 09:06:24 -050095 else if( it != certTable.end() )
akmhoque099495b2014-03-11 16:01:19 -050096 {
akmhoque5a44dd42014-03-12 18:11:32 -050097 if ( (*it).getCertSeqNum() < ncse.getCertSeqNum() )
98 {
99 certTable.erase(it);
100 certTable.push_back(ncse);
akmhoquefa8ee9b2014-03-14 09:06:24 -0500101 updateWaitingList(ncse);
akmhoque099495b2014-03-11 16:01:19 -0500102 return true;
akmhoque5a44dd42014-03-12 18:11:32 -0500103 }
akmhoque099495b2014-03-11 16:01:19 -0500104 }
akmhoque5a44dd42014-03-12 18:11:32 -0500105 return false;
106 }
107
108 bool
109 NlsrCertificateStore::addCertificate(
110 ndn::shared_ptr<ndn::IdentityCertificate> pcert, uint32_t csn, bool isv)
111 {
112 NlsrCertificateStoreEntry ncse(pcert, csn, isv);
113 return addCertificate(ncse);
114 }
115
akmhoquefa8ee9b2014-03-14 09:06:24 -0500116 std::pair<uint32_t, bool>
117 NlsrCertificateStore::getCertificateSeqNum(std::string certName)
118 {
119 std::list<NlsrCertificateStoreEntry>::iterator it =
120 std::find_if( certTable.begin(), certTable.end(),
121 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
122 if(it == certTable.end())
123 {
124 return std::make_pair(0,false);
125 }
126 return std::make_pair((*it).getCertSeqNum(),true);
127 }
128
129
130
131 void
132 NlsrCertificateStore::setCertificateIsVerified(std::string certName,
133 bool isVerified)
134 {
135 std::list<NlsrCertificateStoreEntry>::iterator it =
136 std::find_if( certTable.begin(), certTable.end(),
137 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
138 if(it != certTable.end())
139 {
140 it->setIsSignerVerified(true);
141 }
142 }
143
144 bool
145 NlsrCertificateStore::getCertificateIsVerified( std::string certName )
146 {
147 std::list<NlsrCertificateStoreEntry>::iterator it =
148 std::find_if( certTable.begin(), certTable.end(),
149 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
150 if(it != certTable.end())
151 {
152 return it->getIsSignerVerified();
153 }
154
155 return false;
156 }
157
akmhoque5a44dd42014-03-12 18:11:32 -0500158 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
159 NlsrCertificateStore::getCertificateFromStore(const std::string certName)
160 {
161 std::list<NlsrCertificateStoreEntry>::iterator it =
162 std::find_if( certTable.begin(), certTable.end(),
163 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
164 if(it == certTable.end())
akmhoque099495b2014-03-11 16:01:19 -0500165 {
akmhoque5a44dd42014-03-12 18:11:32 -0500166 ndn::shared_ptr<ndn::IdentityCertificate> cert=
akmhoquefa8ee9b2014-03-14 09:06:24 -0500167 ndn::make_shared<ndn::IdentityCertificate>();
akmhoque5a44dd42014-03-12 18:11:32 -0500168 return std::make_pair(cert,false);
akmhoque099495b2014-03-11 16:01:19 -0500169 }
akmhoque5a44dd42014-03-12 18:11:32 -0500170 return std::make_pair((*it).getCert(),true);
171 }
172
173 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
174 NlsrCertificateStore::getCertificateFromStore(
175 const std::string certName, int checkSeqNum)
176 {
177 std::list<NlsrCertificateStoreEntry>::iterator it =
178 std::find_if( certTable.begin(), certTable.end(),
179 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
180 if(it == certTable.end())
akmhoque099495b2014-03-11 16:01:19 -0500181 {
akmhoque5a44dd42014-03-12 18:11:32 -0500182 ndn::shared_ptr<ndn::IdentityCertificate> cert=
183 ndn::make_shared<ndn::IdentityCertificate>();
184 return std::make_pair(cert,false);
akmhoque099495b2014-03-11 16:01:19 -0500185 }
akmhoque5a44dd42014-03-12 18:11:32 -0500186 else
187 {
188 if( (*it).getCertSeqNum() == checkSeqNum )
189 {
190 return std::make_pair((*it).getCert(),true);
191 }
192 }
193 return std::make_pair((*it).getCert(),false);
194 }
195
196 bool
197 NlsrCertificateStore::isCertificateNewInStore(const std::string certName,
198 int checkSeqNo)
199 {
200 std::list<NlsrCertificateStoreEntry>::iterator it =
201 std::find_if( certTable.begin(), certTable.end(),
202 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
203 if(it != certTable.end())
204 {
205 return (*it).getCertSeqNum() < checkSeqNo ;
206 }
207 return true;
208 }
209
210 bool
211 NlsrCertificateStore::removeCertificateFromStroe(const std::string certName)
212 {
213 std::list<NlsrCertificateStoreEntry>::iterator it =
214 std::find_if( certTable.begin(), certTable.end(),
215 bind(&nlsrCertificateStoreEntryCompareByName, _1, certName));
216 if(it != certTable.end())
217 {
218 certTable.erase(it);
219 return true;
220 }
221 return false;
222 }
223
224 void
225 NlsrCertificateStore::printCertStore()
226 {
227 std::list<NlsrCertificateStoreEntry>::iterator it;
228 for(it=certTable.begin(); it!=certTable.end(); ++it)
229 {
230 std::cout<<(*it)<<std::endl;
231 }
akmhoquefa8ee9b2014-03-14 09:06:24 -0500232 std::cout<<waitingList<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500233 }
akmhoque099495b2014-03-11 16:01:19 -0500234}