blob: 8838878e648fa83503af2d34bd11b18831eea537 [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"
7
8namespace nlsr
9{
akmhoque5a44dd42014-03-12 18:11:32 -050010 bool
11 KeyManager::initKeyManager(ConfParameter &cp)
12 {
13 initCertSeqFromFile(cp.getSeqFileDir());
14 if( !loadAllCertificates(cp.getCertDir()) )
akmhoqueba094742014-02-28 11:47:21 -060015 {
akmhoque5a44dd42014-03-12 18:11:32 -050016 return false;
akmhoqueeb764c52014-03-11 16:01:09 -050017 }
akmhoque5a44dd42014-03-12 18:11:32 -050018 nlsrRootKeyPrefix=cp.getRootKeyPrefix();
19 string processIdentityName(cp.getRootKeyPrefix());
20 processIdentityName += "/";
21 processIdentityName += cp.getSiteName();
22 processIdentityName += "/";
23 processIdentityName += "R.Start";
24 processIdentityName += "/";
25 processIdentityName += cp.getRouterName();
26 ndn::Name ri(processIdentityName);
27 std::cout<<"Router Identity: "<<ri.toUri()<<std::endl;
28 routerIdentity=ri;
29 processIdentityName += "/";
30 processIdentityName += "nlsr";
31 cout<<"Proces Identity Name: "<<processIdentityName<<endl;
32 ndn::Name identityName(processIdentityName);
33 processIdentity=identityName;
34 ndn::KeyChain::deleteIdentity(processIdentity);
35 processCertName = ndn::KeyChain::createIdentity(processIdentity);
36 cout<<"Certificate Name: "<<processCertName.toUri()<<endl;
37 processKeyName=
38 ndn::IdentityCertificate::certificateNameToPublicKeyName(processCertName);
39 cout<<"Key Name: "<<processKeyName.toUri()<<endl;
40 ndn::shared_ptr<ndn::IdentityCertificate> cert=getCertificate(processCertName);
41 signByIdentity(*(cert),routerIdentity);
42 certStore.addCertificate(cert, certSeqNo, true);
43 //certStore.printCertStore();
44 return true;
45 }
akmhoqueba094742014-02-28 11:47:21 -060046
akmhoque5a44dd42014-03-12 18:11:32 -050047 bool
48 KeyManager::loadAllCertificates(std::string certDirPath)
49 {
50 std::string filePath=certDirPath;
51 if(filePath.empty())
akmhoqueeb764c52014-03-11 16:01:09 -050052 {
akmhoque5a44dd42014-03-12 18:11:32 -050053 SequencingManager sm;
54 filePath=sm.getUserHomeDirectory();
55 filePath+="/nlsrCertDir";
akmhoqueeb764c52014-03-11 16:01:09 -050056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 return loadCertificate(filePath+"/root.cert", KEY_TYPE_ROOT)
58 && loadCertificate(filePath+"/site.cert", KEY_TYPE_SITE)
59 && loadCertificate(filePath+"/operator.cert", KEY_TYPE_OPERATOR)
60 && loadCertificate(filePath+"/router.cert", KEY_TYPE_ROUTER) ;
61 }
akmhoqueba094742014-02-28 11:47:21 -060062
akmhoque5a44dd42014-03-12 18:11:32 -050063 bool
64 KeyManager::loadCertificate(std::string inputFile, nlsrKeyType keyType)
65 {
66 try
akmhoqueba094742014-02-28 11:47:21 -060067 {
akmhoque5a44dd42014-03-12 18:11:32 -050068 ndn::shared_ptr<ndn::IdentityCertificate> cert =
69 ndn::io::load<ndn::IdentityCertificate>(inputFile, ndn::io::BASE_64);
70 ndn::Name certName=cert->getName();
71 switch(keyType)
72 {
73 case KEY_TYPE_ROOT:
74 certStore.addCertificate(cert, 10, true);
75 rootCertName=certName;
76 break;
77 case KEY_TYPE_SITE:
78 certStore.addCertificate(cert, 10, true);
79 siteCertName=certName;
80 break;
81 case KEY_TYPE_OPERATOR:
82 certStore.addCertificate(cert, 10, true);
83 opCertName=certName;
84 break;
85 case KEY_TYPE_ROUTER:
86 certStore.addCertificate(cert, certSeqNo, true);
87 routerCertName=certName;
88 break;
89 case KEY_TYPE_PROCESS:
90 certStore.addCertificate(cert, certSeqNo, true);
91 processCertName=certName;
92 break;
93 default:
94 break;
95 }
96 return true;
akmhoqueba094742014-02-28 11:47:21 -060097 }
akmhoque5a44dd42014-03-12 18:11:32 -050098 catch(std::exception& e)
99 {
100 return false;
101 }
102 return false;
103 }
akmhoqueba094742014-02-28 11:47:21 -0600104
akmhoque5a44dd42014-03-12 18:11:32 -0500105 ndn::Name
106 KeyManager::getProcessCertName()
107 {
108 return processCertName;
109 }
akmhoqueba094742014-02-28 11:47:21 -0600110
akmhoque5a44dd42014-03-12 18:11:32 -0500111 ndn::Name
112 KeyManager::getRouterCertName()
113 {
114 return routerCertName;
115 }
akmhoqueba094742014-02-28 11:47:21 -0600116
akmhoque5a44dd42014-03-12 18:11:32 -0500117 ndn::Name
118 KeyManager::getOperatorCertName()
119 {
120 return opCertName;
121 }
122
123 ndn::Name
124 KeyManager::getSiteCertName()
125 {
126 return siteCertName;
127 }
128
129 ndn::Name
130 KeyManager::getRootCertName()
131 {
132 return rootCertName;
133 }
134
135 uint32_t
136 KeyManager::getCertSeqNo()
137 {
138 return certSeqNo;
139 }
140
141 void
142 KeyManager::setCerSeqNo(uint32_t csn)
143 {
144 certSeqNo=csn;
145 }
146
147 void
148 KeyManager::initCertSeqFromFile(string certSeqFileDir)
149 {
150 certSeqFileNameWithPath=certSeqFileDir;
151 if( certSeqFileNameWithPath.empty() )
akmhoqueba094742014-02-28 11:47:21 -0600152 {
akmhoque5a44dd42014-03-12 18:11:32 -0500153 SequencingManager sm;
154 certSeqFileNameWithPath=sm.getUserHomeDirectory();
akmhoqueba094742014-02-28 11:47:21 -0600155 }
akmhoque5a44dd42014-03-12 18:11:32 -0500156 certSeqFileNameWithPath += "/nlsrCertSeqNo.txt";
157 cout<<"Key Seq File Name: "<< certSeqFileNameWithPath<<endl;
158 std::ifstream inputFile(certSeqFileNameWithPath.c_str(),ios::binary);
159 if ( inputFile.good() )
akmhoqueeb764c52014-03-11 16:01:09 -0500160 {
akmhoque5a44dd42014-03-12 18:11:32 -0500161 inputFile>>certSeqNo;
162 certSeqNo++;
akmhoqueeb764c52014-03-11 16:01:09 -0500163 }
akmhoque5a44dd42014-03-12 18:11:32 -0500164 else
akmhoqueeb764c52014-03-11 16:01:09 -0500165 {
akmhoque5a44dd42014-03-12 18:11:32 -0500166 certSeqNo=1;
akmhoqueeb764c52014-03-11 16:01:09 -0500167 }
akmhoque5a44dd42014-03-12 18:11:32 -0500168 writeCertSeqToFile();
169 }
170
171 void
172 KeyManager::writeCertSeqToFile()
173 {
174 std::ofstream outputFile(certSeqFileNameWithPath.c_str(),ios::binary);
175 outputFile<<certSeqNo;
176 outputFile.close();
177 }
178
179 bool
180 KeyManager::isNewCertificate(std::string certName, int checkSeqNum)
181 {
182 return certStore.isCertificateNewInStore(certName,checkSeqNum);
183 }
184
185 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
186 KeyManager::getCertificateFromStore(const std::string certName, int checkSeqNum)
187 {
188 return certStore.getCertificateFromStore(certName, checkSeqNum);
189 }
190
191 bool
192 KeyManager::addCertificate(ndn::shared_ptr<ndn::IdentityCertificate> pcert
193 , uint32_t csn, bool isv)
194 {
195 return certStore.addCertificate(pcert, csn, isv);
196 }
197
198 nlsrKeyType
199 KeyManager::getKeyTypeFromName(const std::string keyName)
200 {
201 nlsrTokenizer nt(keyName,"/");
202 std::string KEY("KEY");
203 std::string opHandle("O.Start");
204 std::string routerHandle("R.Start");
205 std::string processHandle("nlsr");
206 if ( nt.getTokenString(0,nt.getTokenPosition(KEY)-1) == nlsrRootKeyPrefix)
akmhoqueeb764c52014-03-11 16:01:09 -0500207 {
akmhoque5a44dd42014-03-12 18:11:32 -0500208 return KEY_TYPE_ROOT;
akmhoqueeb764c52014-03-11 16:01:09 -0500209 }
akmhoque5a44dd42014-03-12 18:11:32 -0500210 else if ( nt.doesTokenExist(opHandle) )
akmhoqueeb764c52014-03-11 16:01:09 -0500211 {
akmhoque5a44dd42014-03-12 18:11:32 -0500212 return KEY_TYPE_OPERATOR;
akmhoqueeb764c52014-03-11 16:01:09 -0500213 }
akmhoque5a44dd42014-03-12 18:11:32 -0500214 else if ( nt.doesTokenExist(routerHandle) &&
215 nt.doesTokenExist(processHandle))
akmhoqueeb764c52014-03-11 16:01:09 -0500216 {
akmhoque5a44dd42014-03-12 18:11:32 -0500217 return KEY_TYPE_PROCESS;
akmhoqueeb764c52014-03-11 16:01:09 -0500218 }
akmhoque5a44dd42014-03-12 18:11:32 -0500219 else if ( nt.doesTokenExist(routerHandle) &&
220 !nt.doesTokenExist(processHandle))
akmhoqueeb764c52014-03-11 16:01:09 -0500221 {
akmhoque5a44dd42014-03-12 18:11:32 -0500222 return KEY_TYPE_ROUTER;
223 }
224 else
225 {
226 return KEY_TYPE_SITE;
227 }
228 }
229
230 std::string
231 KeyManager::getRouterName(const std::string name)
232 {
233 std::string routerName;
234 std::string rkp(nlsrRootKeyPrefix);
235 nlsrTokenizer ntRkp(rkp,"/");
236 nlsrTokenizer nt(name,"/");
237 std::string KEY("KEY");
238 std::string opHandle("O.Start");
239 std::string routerHandle("R.Start");
240 std::string processHandle("nlsr");
241 std::string infoHandle("info");
242 std::string lsaHandle("LSA");
243 if ( nt.doesTokenExist(processHandle) && nt.doesTokenExist(routerHandle))
244 {
245 routerName="/ndn" +
246 nt.getTokenString(ntRkp.getTokenNumber(),
247 nt.getTokenPosition(routerHandle)-1) +
248 nt.getTokenString(nt.getTokenPosition(routerHandle)+1,
249 nt.getTokenPosition(processHandle)-1);
250 }
251 else if(nt.doesTokenExist(routerHandle) && !nt.doesTokenExist(infoHandle)
252 && !nt.doesTokenExist(lsaHandle))
253 {
254 routerName="/ndn" +
255 nt.getTokenString(ntRkp.getTokenNumber(),
256 nt.getTokenPosition(routerHandle)-1) +
257 nt.getTokenString(nt.getTokenPosition(routerHandle)+1,
258 nt.getTokenPosition(KEY)-1);
259 }
260 else
261 {
262 if (nt.doesTokenExist(infoHandle) )
263 {
264 routerName=nt.getTokenString(0,nt.getTokenPosition(infoHandle)-1);
265 }
266 else
267 {
268 routerName=nt.getTokenString(nt.getTokenPosition(lsaHandle)+1,
269 nt.getTokenNumber()-4);
270 }
271 }
272 return routerName;
273 }
274
275 std::string
276 KeyManager::getSiteName(const std::string name)
277 {
278 std::string siteName;
279 std::string routerName;
280 std::string rkp(nlsrRootKeyPrefix);
281 nlsrTokenizer ntRkp(rkp,"/");
282 nlsrTokenizer nt(name,"/");
283 std::string KEY("KEY");
284 std::string opHandle("O.Start");
285 std::string routerHandle("R.Start");
286 if ( nt.doesTokenExist(routerHandle) )
287 {
288 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
289 nt.getTokenPosition(routerHandle) -1);
290 }
291 else if ( nt.doesTokenExist(opHandle) )
292 {
293 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
akmhoqueeb764c52014-03-11 16:01:09 -0500294 nt.getTokenPosition(opHandle) -1);
akmhoqueeb764c52014-03-11 16:01:09 -0500295 }
akmhoque5a44dd42014-03-12 18:11:32 -0500296 else
297 {
298 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
299 nt.getTokenPosition(KEY) -1);
300 }
301 return siteName;
302 }
akmhoqueba094742014-02-28 11:47:21 -0600303}
304
305
306