akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #include <ndn-cpp-dev/security/identity-certificate.hpp> |
| 2 | #include <ndn-cpp-dev/encoding/block.hpp> |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 3 | #include <ndn-cpp-dev/util/io.hpp> |
| 4 | #include <stdexcept> |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 5 | #include "nlsr_sm.hpp" |
| 6 | #include "nlsr_km.hpp" |
| 7 | |
| 8 | namespace nlsr |
| 9 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 10 | bool |
| 11 | KeyManager::initKeyManager(ConfParameter &cp) |
| 12 | { |
| 13 | initCertSeqFromFile(cp.getSeqFileDir()); |
| 14 | if( !loadAllCertificates(cp.getCertDir()) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 15 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 16 | return false; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 17 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 18 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 46 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 47 | bool |
| 48 | KeyManager::loadAllCertificates(std::string certDirPath) |
| 49 | { |
| 50 | std::string filePath=certDirPath; |
| 51 | if(filePath.empty()) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 52 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 53 | SequencingManager sm; |
| 54 | filePath=sm.getUserHomeDirectory(); |
| 55 | filePath+="/nlsrCertDir"; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 56 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 57 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 62 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 63 | bool |
| 64 | KeyManager::loadCertificate(std::string inputFile, nlsrKeyType keyType) |
| 65 | { |
| 66 | try |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 67 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 68 | 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; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 97 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 98 | catch(std::exception& e) |
| 99 | { |
| 100 | return false; |
| 101 | } |
| 102 | return false; |
| 103 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 104 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 105 | ndn::Name |
| 106 | KeyManager::getProcessCertName() |
| 107 | { |
| 108 | return processCertName; |
| 109 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 110 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 111 | ndn::Name |
| 112 | KeyManager::getRouterCertName() |
| 113 | { |
| 114 | return routerCertName; |
| 115 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 116 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 117 | 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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 152 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 153 | SequencingManager sm; |
| 154 | certSeqFileNameWithPath=sm.getUserHomeDirectory(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 155 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 156 | certSeqFileNameWithPath += "/nlsrCertSeqNo.txt"; |
| 157 | cout<<"Key Seq File Name: "<< certSeqFileNameWithPath<<endl; |
| 158 | std::ifstream inputFile(certSeqFileNameWithPath.c_str(),ios::binary); |
| 159 | if ( inputFile.good() ) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 160 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 161 | inputFile>>certSeqNo; |
| 162 | certSeqNo++; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 163 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 164 | else |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 165 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 166 | certSeqNo=1; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 167 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 168 | 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) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 207 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 208 | return KEY_TYPE_ROOT; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 209 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 210 | else if ( nt.doesTokenExist(opHandle) ) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 211 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 212 | return KEY_TYPE_OPERATOR; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 213 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 214 | else if ( nt.doesTokenExist(routerHandle) && |
| 215 | nt.doesTokenExist(processHandle)) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 216 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 217 | return KEY_TYPE_PROCESS; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 218 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 219 | else if ( nt.doesTokenExist(routerHandle) && |
| 220 | !nt.doesTokenExist(processHandle)) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 221 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 222 | 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(), |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 294 | nt.getTokenPosition(opHandle) -1); |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 295 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 296 | else |
| 297 | { |
| 298 | siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(), |
| 299 | nt.getTokenPosition(KEY) -1); |
| 300 | } |
| 301 | return siteName; |
| 302 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
| 306 | |