blob: d35779536f7a5dcf4e15d3de0ec954a322d9b493 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include <ndn-cpp-dev/security/identity-certificate.hpp>
2#include <ndn-cpp-dev/encoding/block.hpp>
akmhoqueeb764c52014-03-11 16:01:09 -05003#include <ndn-cpp-dev/util/io.hpp>
4#include <stdexcept>
akmhoqueba094742014-02-28 11:47:21 -06005#include "nlsr_sm.hpp"
6#include "nlsr_km.hpp"
akmhoquefa8ee9b2014-03-14 09:06:24 -05007#include "nlsr.hpp"
akmhoqueba094742014-02-28 11:47:21 -06008
akmhoque05d5fcf2014-04-15 14:58:45 -05009#define THIS_FILE "nlsr_km.cpp"
10
akmhoqueba094742014-02-28 11:47:21 -060011namespace nlsr
12{
akmhoque5a44dd42014-03-12 18:11:32 -050013 bool
akmhoque05d5fcf2014-04-15 14:58:45 -050014 KeyManager::initialize(ConfParameter &cp)
akmhoque5a44dd42014-03-12 18:11:32 -050015 {
16 initCertSeqFromFile(cp.getSeqFileDir());
17 if( !loadAllCertificates(cp.getCertDir()) )
akmhoqueba094742014-02-28 11:47:21 -060018 {
akmhoque5a44dd42014-03-12 18:11:32 -050019 return false;
akmhoqueeb764c52014-03-11 16:01:09 -050020 }
akmhoque05d5fcf2014-04-15 14:58:45 -050021 m_nlsrRootKeyPrefix=cp.getRootKeyPrefix();
akmhoque5a44dd42014-03-12 18:11:32 -050022 string processIdentityName(cp.getRootKeyPrefix());
23 processIdentityName += "/";
24 processIdentityName += cp.getSiteName();
25 processIdentityName += "/";
26 processIdentityName += "R.Start";
27 processIdentityName += "/";
28 processIdentityName += cp.getRouterName();
29 ndn::Name ri(processIdentityName);
30 std::cout<<"Router Identity: "<<ri.toUri()<<std::endl;
akmhoque05d5fcf2014-04-15 14:58:45 -050031 m_routerIdentity=ri;
akmhoque5a44dd42014-03-12 18:11:32 -050032 processIdentityName += "/";
33 processIdentityName += "nlsr";
34 cout<<"Proces Identity Name: "<<processIdentityName<<endl;
35 ndn::Name identityName(processIdentityName);
akmhoque05d5fcf2014-04-15 14:58:45 -050036 m_processIdentity=identityName;
37 ndn::KeyChain::deleteIdentity(m_processIdentity);
38 m_processCertName = ndn::KeyChain::createIdentity(m_processIdentity);
39 cout<<"Certificate Name: "<<m_processCertName.toUri()<<endl;
40 m_processKeyName=m_processCertName.getPrefix(-2);
41 cout<<"Key Name: "<<m_processKeyName.toUri()<<endl;
akmhoquefa8ee9b2014-03-14 09:06:24 -050042 ndn::shared_ptr<ndn::IdentityCertificate> cert =
akmhoque05d5fcf2014-04-15 14:58:45 -050043 getCertificate(m_processCertName);
44 signByIdentity(*(cert),m_routerIdentity);
45 m_certStore.addCertificate(cert, m_certSeqNo, true);
46 m_certStore.print();
akmhoque5a44dd42014-03-12 18:11:32 -050047 return true;
48 }
akmhoqueba094742014-02-28 11:47:21 -060049
akmhoque5a44dd42014-03-12 18:11:32 -050050 bool
51 KeyManager::loadAllCertificates(std::string certDirPath)
52 {
53 std::string filePath=certDirPath;
54 if(filePath.empty())
akmhoqueeb764c52014-03-11 16:01:09 -050055 {
akmhoque5a44dd42014-03-12 18:11:32 -050056 SequencingManager sm;
57 filePath=sm.getUserHomeDirectory();
58 filePath+="/nlsrCertDir";
akmhoqueeb764c52014-03-11 16:01:09 -050059 }
akmhoque5a44dd42014-03-12 18:11:32 -050060 return loadCertificate(filePath+"/root.cert", KEY_TYPE_ROOT)
61 && loadCertificate(filePath+"/site.cert", KEY_TYPE_SITE)
62 && loadCertificate(filePath+"/operator.cert", KEY_TYPE_OPERATOR)
63 && loadCertificate(filePath+"/router.cert", KEY_TYPE_ROUTER) ;
64 }
akmhoqueba094742014-02-28 11:47:21 -060065
akmhoque5a44dd42014-03-12 18:11:32 -050066 bool
67 KeyManager::loadCertificate(std::string inputFile, nlsrKeyType keyType)
68 {
69 try
akmhoqueba094742014-02-28 11:47:21 -060070 {
akmhoque5a44dd42014-03-12 18:11:32 -050071 ndn::shared_ptr<ndn::IdentityCertificate> cert =
72 ndn::io::load<ndn::IdentityCertificate>(inputFile, ndn::io::BASE_64);
73 ndn::Name certName=cert->getName();
74 switch(keyType)
akmhoque05d5fcf2014-04-15 14:58:45 -050075 {
akmhoque5a44dd42014-03-12 18:11:32 -050076 case KEY_TYPE_ROOT:
akmhoque05d5fcf2014-04-15 14:58:45 -050077 m_certStore.addCertificate(cert, 10, true);
78 m_rootCertName=certName;
79 std::cout<<"Root Cert: "<<m_rootCertName<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -050080 break;
81 case KEY_TYPE_SITE:
akmhoque05d5fcf2014-04-15 14:58:45 -050082 m_certStore.addCertificate(cert, 10, true);
83 m_siteCertName=certName;
84 std::cout<<"Site Cert: "<<m_siteCertName<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -050085 break;
86 case KEY_TYPE_OPERATOR:
akmhoque05d5fcf2014-04-15 14:58:45 -050087 m_certStore.addCertificate(cert, 10, true);
88 m_opCertName=certName;
89 std::cout<<"Operator Cert: "<<m_opCertName<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -050090 break;
91 case KEY_TYPE_ROUTER:
akmhoque05d5fcf2014-04-15 14:58:45 -050092 m_certStore.addCertificate(cert, m_certSeqNo, true);
93 m_routerCertName=certName;
94 std::cout<<"Router Cert: "<<m_routerCertName<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -050095 break;
96 case KEY_TYPE_PROCESS:
akmhoque05d5fcf2014-04-15 14:58:45 -050097 m_certStore.addCertificate(cert, m_certSeqNo, true);
98 m_processCertName=certName;
99 std::cout<<"Process Cert: "<<m_processCertName<<std::endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500100 break;
101 default:
102 break;
akmhoque05d5fcf2014-04-15 14:58:45 -0500103 }
akmhoque5a44dd42014-03-12 18:11:32 -0500104 return true;
akmhoqueba094742014-02-28 11:47:21 -0600105 }
akmhoque5a44dd42014-03-12 18:11:32 -0500106 catch(std::exception& e)
107 {
108 return false;
109 }
110 return false;
111 }
akmhoqueba094742014-02-28 11:47:21 -0600112
akmhoque5a44dd42014-03-12 18:11:32 -0500113 ndn::Name
114 KeyManager::getProcessCertName()
115 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500116 return m_processCertName;
akmhoque5a44dd42014-03-12 18:11:32 -0500117 }
akmhoqueba094742014-02-28 11:47:21 -0600118
akmhoque5a44dd42014-03-12 18:11:32 -0500119 ndn::Name
120 KeyManager::getRouterCertName()
121 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500122 return m_routerCertName;
akmhoque5a44dd42014-03-12 18:11:32 -0500123 }
akmhoqueba094742014-02-28 11:47:21 -0600124
akmhoque5a44dd42014-03-12 18:11:32 -0500125 ndn::Name
126 KeyManager::getOperatorCertName()
127 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500128 return m_opCertName;
akmhoque5a44dd42014-03-12 18:11:32 -0500129 }
130
131 ndn::Name
132 KeyManager::getSiteCertName()
133 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500134 return m_siteCertName;
akmhoque5a44dd42014-03-12 18:11:32 -0500135 }
136
137 ndn::Name
138 KeyManager::getRootCertName()
139 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500140 return m_rootCertName;
akmhoque5a44dd42014-03-12 18:11:32 -0500141 }
142
143 uint32_t
144 KeyManager::getCertSeqNo()
145 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500146 return m_certSeqNo;
akmhoque5a44dd42014-03-12 18:11:32 -0500147 }
148
149 void
150 KeyManager::setCerSeqNo(uint32_t csn)
151 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500152 m_certSeqNo=csn;
akmhoque5a44dd42014-03-12 18:11:32 -0500153 }
154
155 void
156 KeyManager::initCertSeqFromFile(string certSeqFileDir)
157 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500158 m_certSeqFileNameWithPath=certSeqFileDir;
159 if( m_certSeqFileNameWithPath.empty() )
akmhoqueba094742014-02-28 11:47:21 -0600160 {
akmhoque5a44dd42014-03-12 18:11:32 -0500161 SequencingManager sm;
akmhoque05d5fcf2014-04-15 14:58:45 -0500162 m_certSeqFileNameWithPath=sm.getUserHomeDirectory();
akmhoqueba094742014-02-28 11:47:21 -0600163 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500164 m_certSeqFileNameWithPath += "/nlsrCertSeqNo.txt";
165 cout<<"Key Seq File Name: "<< m_certSeqFileNameWithPath<<endl;
166 std::ifstream inputFile(m_certSeqFileNameWithPath.c_str(),ios::binary);
akmhoque5a44dd42014-03-12 18:11:32 -0500167 if ( inputFile.good() )
akmhoqueeb764c52014-03-11 16:01:09 -0500168 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500169 inputFile>>m_certSeqNo;
170 m_certSeqNo++;
akmhoqueeb764c52014-03-11 16:01:09 -0500171 }
akmhoque5a44dd42014-03-12 18:11:32 -0500172 else
akmhoqueeb764c52014-03-11 16:01:09 -0500173 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500174 m_certSeqNo=1;
akmhoqueeb764c52014-03-11 16:01:09 -0500175 }
akmhoque5a44dd42014-03-12 18:11:32 -0500176 writeCertSeqToFile();
177 }
178
179 void
180 KeyManager::writeCertSeqToFile()
181 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500182 std::ofstream outputFile(m_certSeqFileNameWithPath.c_str(),ios::binary);
183 outputFile<<m_certSeqNo;
akmhoque5a44dd42014-03-12 18:11:32 -0500184 outputFile.close();
185 }
186
187 bool
188 KeyManager::isNewCertificate(std::string certName, int checkSeqNum)
189 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500190 return m_certStore.isCertificateNewInStore(certName,checkSeqNum);
akmhoque5a44dd42014-03-12 18:11:32 -0500191 }
192
193 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
194 KeyManager::getCertificateFromStore(const std::string certName, int checkSeqNum)
195 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500196 return m_certStore.getCertificateFromStore(certName, checkSeqNum);
akmhoque5a44dd42014-03-12 18:11:32 -0500197 }
198
akmhoquefa8ee9b2014-03-14 09:06:24 -0500199 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
200 KeyManager::getCertificateFromStore(const std::string certName)
201 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500202 return m_certStore.getCertificateFromStore(certName);
akmhoquefa8ee9b2014-03-14 09:06:24 -0500203 }
204
akmhoque5a44dd42014-03-12 18:11:32 -0500205 bool
206 KeyManager::addCertificate(ndn::shared_ptr<ndn::IdentityCertificate> pcert
207 , uint32_t csn, bool isv)
208 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500209 return m_certStore.addCertificate(pcert, csn, isv);
akmhoque5a44dd42014-03-12 18:11:32 -0500210 }
akmhoquefa8ee9b2014-03-14 09:06:24 -0500211
212 std::pair<uint32_t, bool>
213 KeyManager::getCertificateSeqNum(std::string certName)
214 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500215 return m_certStore.getCertificateSeqNum(certName);
akmhoquefa8ee9b2014-03-14 09:06:24 -0500216 }
akmhoque5a44dd42014-03-12 18:11:32 -0500217
218 nlsrKeyType
219 KeyManager::getKeyTypeFromName(const std::string keyName)
220 {
221 nlsrTokenizer nt(keyName,"/");
222 std::string KEY("KEY");
223 std::string opHandle("O.Start");
224 std::string routerHandle("R.Start");
225 std::string processHandle("nlsr");
akmhoque05d5fcf2014-04-15 14:58:45 -0500226 if ( nt.getTokenString(0,nt.getTokenPosition(KEY)-1) == m_nlsrRootKeyPrefix)
akmhoqueeb764c52014-03-11 16:01:09 -0500227 {
akmhoque5a44dd42014-03-12 18:11:32 -0500228 return KEY_TYPE_ROOT;
akmhoqueeb764c52014-03-11 16:01:09 -0500229 }
akmhoque5a44dd42014-03-12 18:11:32 -0500230 else if ( nt.doesTokenExist(opHandle) )
akmhoqueeb764c52014-03-11 16:01:09 -0500231 {
akmhoque5a44dd42014-03-12 18:11:32 -0500232 return KEY_TYPE_OPERATOR;
akmhoqueeb764c52014-03-11 16:01:09 -0500233 }
akmhoque5a44dd42014-03-12 18:11:32 -0500234 else if ( nt.doesTokenExist(routerHandle) &&
235 nt.doesTokenExist(processHandle))
akmhoqueeb764c52014-03-11 16:01:09 -0500236 {
akmhoque5a44dd42014-03-12 18:11:32 -0500237 return KEY_TYPE_PROCESS;
akmhoqueeb764c52014-03-11 16:01:09 -0500238 }
akmhoque5a44dd42014-03-12 18:11:32 -0500239 else if ( nt.doesTokenExist(routerHandle) &&
240 !nt.doesTokenExist(processHandle))
akmhoqueeb764c52014-03-11 16:01:09 -0500241 {
akmhoque5a44dd42014-03-12 18:11:32 -0500242 return KEY_TYPE_ROUTER;
243 }
244 else
245 {
246 return KEY_TYPE_SITE;
247 }
248 }
249
250 std::string
251 KeyManager::getRouterName(const std::string name)
252 {
253 std::string routerName;
akmhoque05d5fcf2014-04-15 14:58:45 -0500254 std::string rkp(m_nlsrRootKeyPrefix);
akmhoque5a44dd42014-03-12 18:11:32 -0500255 nlsrTokenizer ntRkp(rkp,"/");
256 nlsrTokenizer nt(name,"/");
257 std::string KEY("KEY");
258 std::string opHandle("O.Start");
259 std::string routerHandle("R.Start");
260 std::string processHandle("nlsr");
261 std::string infoHandle("info");
262 std::string lsaHandle("LSA");
263 if ( nt.doesTokenExist(processHandle) && nt.doesTokenExist(routerHandle))
264 {
265 routerName="/ndn" +
266 nt.getTokenString(ntRkp.getTokenNumber(),
267 nt.getTokenPosition(routerHandle)-1) +
268 nt.getTokenString(nt.getTokenPosition(routerHandle)+1,
269 nt.getTokenPosition(processHandle)-1);
270 }
271 else if(nt.doesTokenExist(routerHandle) && !nt.doesTokenExist(infoHandle)
272 && !nt.doesTokenExist(lsaHandle))
273 {
274 routerName="/ndn" +
275 nt.getTokenString(ntRkp.getTokenNumber(),
276 nt.getTokenPosition(routerHandle)-1) +
277 nt.getTokenString(nt.getTokenPosition(routerHandle)+1,
278 nt.getTokenPosition(KEY)-1);
279 }
280 else
281 {
282 if (nt.doesTokenExist(infoHandle) )
283 {
284 routerName=nt.getTokenString(0,nt.getTokenPosition(infoHandle)-1);
285 }
286 else
287 {
288 routerName=nt.getTokenString(nt.getTokenPosition(lsaHandle)+1,
289 nt.getTokenNumber()-4);
290 }
291 }
292 return routerName;
293 }
294
295 std::string
296 KeyManager::getSiteName(const std::string name)
297 {
298 std::string siteName;
299 std::string routerName;
akmhoque05d5fcf2014-04-15 14:58:45 -0500300 std::string rkp(m_nlsrRootKeyPrefix);
akmhoque5a44dd42014-03-12 18:11:32 -0500301 nlsrTokenizer ntRkp(rkp,"/");
302 nlsrTokenizer nt(name,"/");
303 std::string KEY("KEY");
304 std::string opHandle("O.Start");
305 std::string routerHandle("R.Start");
306 if ( nt.doesTokenExist(routerHandle) )
307 {
308 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
309 nt.getTokenPosition(routerHandle) -1);
310 }
311 else if ( nt.doesTokenExist(opHandle) )
312 {
313 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
akmhoqueeb764c52014-03-11 16:01:09 -0500314 nt.getTokenPosition(opHandle) -1);
akmhoqueeb764c52014-03-11 16:01:09 -0500315 }
akmhoque5a44dd42014-03-12 18:11:32 -0500316 else
317 {
318 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
319 nt.getTokenPosition(KEY) -1);
320 }
321 return siteName;
322 }
akmhoquefa8ee9b2014-03-14 09:06:24 -0500323
324 std::string
325 KeyManager::getRootName(const std::string name)
326 {
327 std::string rName;
328 nlsrTokenizer nt(name,"/");
akmhoque05d5fcf2014-04-15 14:58:45 -0500329 std::string rkp(m_nlsrRootKeyPrefix);
akmhoquefa8ee9b2014-03-14 09:06:24 -0500330 nlsrTokenizer ntRkp(rkp,"/");
331 rName=nt.getTokenString(0,ntRkp.getTokenNumber()-1);
332 return rName;
333 }
334
335
336 bool
337 KeyManager::verifyCertPacket(Nlsr& pnlsr, ndn::IdentityCertificate& packet)
338 {
339 std::cout<<"KeyManager::verifyCertPacket Called"<<std::endl;
340 ndn::SignatureSha256WithRsa signature(packet.getSignature());
341 std::string signingCertName=signature.getKeyLocator().getName().toUri();
342 std::string packetName=packet.getName().toUri();
343
344 std::cout<<"Packet Name: "<<packetName<<std::endl;
345 std::cout<<"Signee Name: "<<signingCertName<<std::endl;
346
347 int paketCertType=getKeyTypeFromName(packetName);
348 int signingCertType=getKeyTypeFromName(signingCertName);
349
350 if( signingCertType > paketCertType ) //lower level Cert can not sign
351 { //upper level Cert
352 return false;
353 }
354
355 if((signingCertType == paketCertType) && (paketCertType != KEY_TYPE_ROOT))
356 {
357 return false;
358 }
359
360 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> signee=
akmhoque05d5fcf2014-04-15 14:58:45 -0500361 m_certStore.getCertificateFromStore(signingCertName);
akmhoquefa8ee9b2014-03-14 09:06:24 -0500362
363 if( signee.second )
364 {
365 switch(paketCertType)
akmhoque05d5fcf2014-04-15 14:58:45 -0500366 {
akmhoquefa8ee9b2014-03-14 09:06:24 -0500367 case KEY_TYPE_ROOT:
akmhoque05d5fcf2014-04-15 14:58:45 -0500368 return ((getRootName(packetName) == m_nlsrRootKeyPrefix) &&
akmhoquefa8ee9b2014-03-14 09:06:24 -0500369 verifySignature(packet,signee.first->getPublicKeyInfo()));
370 break;
371 case KEY_TYPE_SITE:
372 return ((getRootName(packetName) == getRootName(signingCertName)) &&
373 verifySignature(packet,signee.first->getPublicKeyInfo()) &&
akmhoque05d5fcf2014-04-15 14:58:45 -0500374 m_certStore.getCertificateIsVerified(signingCertName));
akmhoquefa8ee9b2014-03-14 09:06:24 -0500375 break;
376 case KEY_TYPE_OPERATOR:
377 return ((getSiteName(packetName) == getSiteName(signingCertName)) &&
378 verifySignature(packet,signee.first->getPublicKeyInfo()) &&
akmhoque05d5fcf2014-04-15 14:58:45 -0500379 m_certStore.getCertificateIsVerified(signingCertName));
akmhoquefa8ee9b2014-03-14 09:06:24 -0500380 break;
381 case KEY_TYPE_ROUTER:
382 return ((getSiteName(packetName) == getSiteName(signingCertName)) &&
383 verifySignature(packet,signee.first->getPublicKeyInfo()) &&
akmhoque05d5fcf2014-04-15 14:58:45 -0500384 m_certStore.getCertificateIsVerified(signingCertName));
akmhoquefa8ee9b2014-03-14 09:06:24 -0500385 break;
386 case KEY_TYPE_PROCESS:
387 return ((getRouterName(packetName) == getRouterName(signingCertName)) &&
388 verifySignature(packet,signee.first->getPublicKeyInfo()) &&
akmhoque05d5fcf2014-04-15 14:58:45 -0500389 m_certStore.getCertificateIsVerified(signingCertName));
akmhoquefa8ee9b2014-03-14 09:06:24 -0500390 break;
akmhoque05d5fcf2014-04-15 14:58:45 -0500391 }
akmhoquefa8ee9b2014-03-14 09:06:24 -0500392 }
393 else
394 {
395 std::cout<<"Certificate Not Found in store. Sending Interest"<<std::endl;
396 pnlsr.getIm().expressInterest(pnlsr, signingCertName, 3,
397 pnlsr.getConfParameter().getInterestResendTime());
398 return false;
399 }
400 return false;
401 }
akmhoqueba094742014-02-28 11:47:21 -0600402}
403
404
405