blob: e97bbfe726baf15ff5118e143bb4203671fe168d [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{
akmhoqueeb764c52014-03-11 16:01:09 -050010 bool
akmhoqueba094742014-02-28 11:47:21 -060011 KeyManager::initKeyManager(ConfParameter &cp)
12 {
akmhoqueeb764c52014-03-11 16:01:09 -050013 initCertSeqFromFile(cp.getSeqFileDir());
14 if( !loadAllCertificates(cp.getCertDir()) )
15 {
16 return false;
17 }
18 nlsrRootKeyPrefix=cp.getRootKeyPrefix();
akmhoqueba094742014-02-28 11:47:21 -060019 string processIdentityName(cp.getRootKeyPrefix());
20 processIdentityName += "/";
21 processIdentityName += cp.getSiteName();
22 processIdentityName += "/";
23 processIdentityName += "R.Start";
24 processIdentityName += "/";
25 processIdentityName += cp.getRouterName();
akmhoqueeb764c52014-03-11 16:01:09 -050026 ndn::Name ri(processIdentityName);
27 std::cout<<"Router Identity: "<<ri.toUri()<<std::endl;
28 routerIdentity=ri;
akmhoqueba094742014-02-28 11:47:21 -060029 processIdentityName += "/";
30 processIdentityName += "nlsr";
31 cout<<"Proces Identity Name: "<<processIdentityName<<endl;
32 ndn::Name identityName(processIdentityName);
akmhoqueeb764c52014-03-11 16:01:09 -050033 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 //initCertSeqFromFile(cp.getSeqFileDir());
43 certStore.addCertificate(cert, certSeqNo, true);
44 certStore.printCertStore();
45
46 return true;
47 }
48
49 bool
50 KeyManager::loadAllCertificates(std::string certDirPath)
51 {
52 std::string filePath=certDirPath;
53 if(filePath.empty())
54 {
55 SequencingManager sm;
56 filePath=sm.getUserHomeDirectory();
57 filePath+="/nlsrCertDir";
58 }
59
60 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 }
65
66 bool
67 KeyManager::loadCertificate(std::string inputFile, nlsrKeyType keyType)
68 {
69 try
70 {
71 ndn::shared_ptr<ndn::IdentityCertificate> cert =
72 ndn::io::load<ndn::IdentityCertificate>(inputFile, ndn::io::BASE_64);
73 ndn::Name certName=cert->getName();
74 //certStore.addCertificate(cert, 10, true);
75 switch(keyType)
76 {
77 case KEY_TYPE_ROOT:
78 certStore.addCertificate(cert, 10, true);
79 rootCertName=certName;
80 break;
81 case KEY_TYPE_SITE:
82 certStore.addCertificate(cert, 10, true);
83 siteCertName=certName;
84 break;
85 case KEY_TYPE_OPERATOR:
86 certStore.addCertificate(cert, 10, true);
87 opCertName=certName;
88 break;
89 case KEY_TYPE_ROUTER:
90 certStore.addCertificate(cert, certSeqNo, true);
91 routerCertName=certName;
92 break;
93 case KEY_TYPE_PROCESS:
94 certStore.addCertificate(cert, certSeqNo, true);
95 processCertName=certName;
96 break;
97 default:
98 break;
99 }
100 return true;
101 }
102 catch(std::exception& e)
103 {
104 return false;
105 }
106
107 return false;
akmhoqueba094742014-02-28 11:47:21 -0600108 }
109
110 ndn::Name
akmhoqueeb764c52014-03-11 16:01:09 -0500111 KeyManager::getProcessCertName()
112 {
113 return processCertName;
114 }
115
116 ndn::Name
akmhoqueba094742014-02-28 11:47:21 -0600117 KeyManager::getRouterCertName()
118 {
119 return routerCertName;
120 }
akmhoqueeb764c52014-03-11 16:01:09 -0500121
122 ndn::Name
123 KeyManager::getOperatorCertName()
124 {
125 return opCertName;
126 }
127
128 ndn::Name
129 KeyManager::getSiteCertName()
130 {
131 return siteCertName;
132 }
133
134 ndn::Name
135 KeyManager::getRootCertName()
136 {
137 return rootCertName;
138 }
akmhoqueba094742014-02-28 11:47:21 -0600139
140 uint32_t
141 KeyManager::getCertSeqNo()
142 {
143 return certSeqNo;
144 }
145
146 void
147 KeyManager::setCerSeqNo(uint32_t csn)
148 {
149 certSeqNo=csn;
150 }
151
152 void
153 KeyManager::initCertSeqFromFile(string certSeqFileDir)
154 {
155 certSeqFileNameWithPath=certSeqFileDir;
156 if( certSeqFileNameWithPath.empty() )
157 {
158 SequencingManager sm;
159 certSeqFileNameWithPath=sm.getUserHomeDirectory();
160 }
161 certSeqFileNameWithPath += "/nlsrCertSeqNo.txt";
162 cout<<"Key Seq File Name: "<< certSeqFileNameWithPath<<endl;
163 std::ifstream inputFile(certSeqFileNameWithPath.c_str(),ios::binary);
164 if ( inputFile.good() )
165 {
166 inputFile>>certSeqNo;
167 certSeqNo++;
168 }
169 else
170 {
171 certSeqNo=1;
172 }
173 writeCertSeqToFile();
174 }
175
176 void
177 KeyManager::writeCertSeqToFile()
178 {
179 std::ofstream outputFile(certSeqFileNameWithPath.c_str(),ios::binary);
180 outputFile<<certSeqNo;
181 outputFile.close();
182 }
akmhoqueeb764c52014-03-11 16:01:09 -0500183
184 bool
185 KeyManager::isNewCertificate(std::string certName, int checkSeqNum)
186 {
187 return certStore.isCertificateNewInStore(certName,checkSeqNum);
188 }
189
190 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool>
191 KeyManager::getCertificateFromStore(const std::string certName, int checkSeqNum)
192 {
193 return certStore.getCertificateFromStore(certName, checkSeqNum);
194 }
195
196 bool
197 KeyManager::addCertificate(ndn::shared_ptr<ndn::IdentityCertificate> pcert
198 , uint32_t csn, bool isv)
199 {
200 return certStore.addCertificate(pcert, csn, isv);
201 }
202
203 nlsrKeyType
204 KeyManager::getKeyTypeFromName(const std::string keyName)
205 {
206 nlsrTokenizer nt(keyName,"/");
207 std::string KEY("KEY");
208 std::string opHandle("O.Start");
209 std::string routerHandle("R.Start");
210 std::string processHandle("nlsr");
211 if ( nt.getTokenString(0,nt.getTokenPosition(KEY)-1) == nlsrRootKeyPrefix)
212 {
213 return KEY_TYPE_ROOT;
214 }
215 else if ( nt.doesTokenExist(opHandle) )
216 {
217 return KEY_TYPE_OPERATOR;
218 }
219 else if ( nt.doesTokenExist(routerHandle) &&
220 nt.doesTokenExist(processHandle))
221 {
222 return KEY_TYPE_PROCESS;
223 }
224 else if ( nt.doesTokenExist(routerHandle) &&
225 !nt.doesTokenExist(processHandle))
226 {
227 return KEY_TYPE_ROUTER;
228 }
229 else
230 {
231 return KEY_TYPE_SITE;
232 }
233 }
234
235 std::string
236 KeyManager::getRouterName(const std::string name)
237 {
238 std::string routerName;
239 std::string rkp(nlsrRootKeyPrefix);
240 nlsrTokenizer ntRkp(rkp,"/");
241 nlsrTokenizer nt(name,"/");
242 std::string KEY("KEY");
243 std::string opHandle("O.Start");
244 std::string routerHandle("R.Start");
245 std::string processHandle("nlsr");
246 std::string infoHandle("info");
247 std::string lsaHandle("LSA");
248
249 if ( nt.doesTokenExist(processHandle) && nt.doesTokenExist(routerHandle))
250 {
251 routerName="/ndn" +
252 nt.getTokenString(ntRkp.getTokenNumber(),
253 nt.getTokenPosition(routerHandle)-1) +
254 nt.getTokenString(nt.getTokenPosition(routerHandle)+1,
255 nt.getTokenPosition(processHandle)-1);
256 }
257 else if(nt.doesTokenExist(routerHandle) && !nt.doesTokenExist(infoHandle)
258 && !nt.doesTokenExist(lsaHandle))
259 {
260 routerName="/ndn" +
261 nt.getTokenString(ntRkp.getTokenNumber(),
262 nt.getTokenPosition(routerHandle)-1) +
263 nt.getTokenString(nt.getTokenPosition(routerHandle)+1,
264 nt.getTokenPosition(KEY)-1);
265 }
266 else
267 {
268 if (nt.doesTokenExist(infoHandle) )
269 {
270 routerName=nt.getTokenString(0,nt.getTokenPosition(infoHandle)-1);
271 }
272 else
273 {
274 routerName=nt.getTokenString(nt.getTokenPosition(lsaHandle)+1,
275 nt.getTokenNumber()-3);
276 }
277 }
278 return routerName;
279 }
280
281 std::string
282 KeyManager::getSiteName(const std::string name)
283 {
284 std::string siteName;
285 std::string routerName;
286 std::string rkp(nlsrRootKeyPrefix);
287 nlsrTokenizer ntRkp(rkp,"/");
288 nlsrTokenizer nt(name,"/");
289 std::string KEY("KEY");
290 std::string opHandle("O.Start");
291 std::string routerHandle("R.Start");
292
293 if ( nt.doesTokenExist(routerHandle) )
294 {
295 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
296 nt.getTokenPosition(routerHandle) -1);
297 }
298 else if ( nt.doesTokenExist(opHandle) )
299 {
300 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
301 nt.getTokenPosition(opHandle) -1);
302 }
303 else
304 {
305 siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(),
306 nt.getTokenPosition(KEY) -1);
307 }
308 return siteName;
309 }
akmhoqueba094742014-02-28 11:47:21 -0600310}
311
312
313