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" |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 7 | #include "nlsr.hpp" |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 8 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 9 | #define THIS_FILE "nlsr_km.cpp" |
| 10 | |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 11 | namespace nlsr |
| 12 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 13 | bool |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 14 | KeyManager::initialize(ConfParameter &cp) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 15 | { |
| 16 | initCertSeqFromFile(cp.getSeqFileDir()); |
| 17 | if( !loadAllCertificates(cp.getCertDir()) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 18 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 19 | return false; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 20 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 21 | m_nlsrRootKeyPrefix=cp.getRootKeyPrefix(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 22 | 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; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 31 | m_routerIdentity=ri; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 32 | processIdentityName += "/"; |
| 33 | processIdentityName += "nlsr"; |
| 34 | cout<<"Proces Identity Name: "<<processIdentityName<<endl; |
| 35 | ndn::Name identityName(processIdentityName); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 36 | 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; |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 42 | ndn::shared_ptr<ndn::IdentityCertificate> cert = |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 43 | getCertificate(m_processCertName); |
| 44 | signByIdentity(*(cert),m_routerIdentity); |
| 45 | m_certStore.addCertificate(cert, m_certSeqNo, true); |
| 46 | m_certStore.print(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 47 | return true; |
| 48 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 49 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 50 | bool |
| 51 | KeyManager::loadAllCertificates(std::string certDirPath) |
| 52 | { |
| 53 | std::string filePath=certDirPath; |
| 54 | if(filePath.empty()) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 55 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 56 | SequencingManager sm; |
| 57 | filePath=sm.getUserHomeDirectory(); |
| 58 | filePath+="/nlsrCertDir"; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 59 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 65 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 66 | bool |
| 67 | KeyManager::loadCertificate(std::string inputFile, nlsrKeyType keyType) |
| 68 | { |
| 69 | try |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 70 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 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 | switch(keyType) |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 75 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 76 | case KEY_TYPE_ROOT: |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 77 | m_certStore.addCertificate(cert, 10, true); |
| 78 | m_rootCertName=certName; |
| 79 | std::cout<<"Root Cert: "<<m_rootCertName<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 80 | break; |
| 81 | case KEY_TYPE_SITE: |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 82 | m_certStore.addCertificate(cert, 10, true); |
| 83 | m_siteCertName=certName; |
| 84 | std::cout<<"Site Cert: "<<m_siteCertName<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 85 | break; |
| 86 | case KEY_TYPE_OPERATOR: |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 87 | m_certStore.addCertificate(cert, 10, true); |
| 88 | m_opCertName=certName; |
| 89 | std::cout<<"Operator Cert: "<<m_opCertName<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 90 | break; |
| 91 | case KEY_TYPE_ROUTER: |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 92 | m_certStore.addCertificate(cert, m_certSeqNo, true); |
| 93 | m_routerCertName=certName; |
| 94 | std::cout<<"Router Cert: "<<m_routerCertName<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 95 | break; |
| 96 | case KEY_TYPE_PROCESS: |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 97 | m_certStore.addCertificate(cert, m_certSeqNo, true); |
| 98 | m_processCertName=certName; |
| 99 | std::cout<<"Process Cert: "<<m_processCertName<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | break; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 103 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 104 | return true; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 105 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 106 | catch(std::exception& e) |
| 107 | { |
| 108 | return false; |
| 109 | } |
| 110 | return false; |
| 111 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 112 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 113 | ndn::Name |
| 114 | KeyManager::getProcessCertName() |
| 115 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 116 | return m_processCertName; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 117 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 118 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 119 | ndn::Name |
| 120 | KeyManager::getRouterCertName() |
| 121 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 122 | return m_routerCertName; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 123 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 124 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 125 | ndn::Name |
| 126 | KeyManager::getOperatorCertName() |
| 127 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 128 | return m_opCertName; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | ndn::Name |
| 132 | KeyManager::getSiteCertName() |
| 133 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 134 | return m_siteCertName; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ndn::Name |
| 138 | KeyManager::getRootCertName() |
| 139 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 140 | return m_rootCertName; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | uint32_t |
| 144 | KeyManager::getCertSeqNo() |
| 145 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 146 | return m_certSeqNo; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | KeyManager::setCerSeqNo(uint32_t csn) |
| 151 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 152 | m_certSeqNo=csn; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
| 156 | KeyManager::initCertSeqFromFile(string certSeqFileDir) |
| 157 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 158 | m_certSeqFileNameWithPath=certSeqFileDir; |
| 159 | if( m_certSeqFileNameWithPath.empty() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 160 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 161 | SequencingManager sm; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 162 | m_certSeqFileNameWithPath=sm.getUserHomeDirectory(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 163 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 164 | m_certSeqFileNameWithPath += "/nlsrCertSeqNo.txt"; |
| 165 | cout<<"Key Seq File Name: "<< m_certSeqFileNameWithPath<<endl; |
| 166 | std::ifstream inputFile(m_certSeqFileNameWithPath.c_str(),ios::binary); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 167 | if ( inputFile.good() ) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 168 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 169 | inputFile>>m_certSeqNo; |
| 170 | m_certSeqNo++; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 171 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 172 | else |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 173 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 174 | m_certSeqNo=1; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 175 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 176 | writeCertSeqToFile(); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | KeyManager::writeCertSeqToFile() |
| 181 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 182 | std::ofstream outputFile(m_certSeqFileNameWithPath.c_str(),ios::binary); |
| 183 | outputFile<<m_certSeqNo; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 184 | outputFile.close(); |
| 185 | } |
| 186 | |
| 187 | bool |
| 188 | KeyManager::isNewCertificate(std::string certName, int checkSeqNum) |
| 189 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 190 | return m_certStore.isCertificateNewInStore(certName,checkSeqNum); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 194 | KeyManager::getCertificateFromStore(const std::string certName, int checkSeqNum) |
| 195 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 196 | return m_certStore.getCertificateFromStore(certName, checkSeqNum); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 197 | } |
| 198 | |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 199 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 200 | KeyManager::getCertificateFromStore(const std::string certName) |
| 201 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 202 | return m_certStore.getCertificateFromStore(certName); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 203 | } |
| 204 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 205 | bool |
| 206 | KeyManager::addCertificate(ndn::shared_ptr<ndn::IdentityCertificate> pcert |
| 207 | , uint32_t csn, bool isv) |
| 208 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 209 | return m_certStore.addCertificate(pcert, csn, isv); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 210 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 211 | |
| 212 | std::pair<uint32_t, bool> |
| 213 | KeyManager::getCertificateSeqNum(std::string certName) |
| 214 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 215 | return m_certStore.getCertificateSeqNum(certName); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 216 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 217 | |
| 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"); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 226 | if ( nt.getTokenString(0,nt.getTokenPosition(KEY)-1) == m_nlsrRootKeyPrefix) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 227 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 228 | return KEY_TYPE_ROOT; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 229 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 230 | else if ( nt.doesTokenExist(opHandle) ) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 231 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 232 | return KEY_TYPE_OPERATOR; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 233 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 234 | else if ( nt.doesTokenExist(routerHandle) && |
| 235 | nt.doesTokenExist(processHandle)) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 236 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 237 | return KEY_TYPE_PROCESS; |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 238 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 239 | else if ( nt.doesTokenExist(routerHandle) && |
| 240 | !nt.doesTokenExist(processHandle)) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 241 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 242 | 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; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 254 | std::string rkp(m_nlsrRootKeyPrefix); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 255 | 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; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 300 | std::string rkp(m_nlsrRootKeyPrefix); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 301 | 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(), |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 314 | nt.getTokenPosition(opHandle) -1); |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 315 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 316 | else |
| 317 | { |
| 318 | siteName="/ndn" + nt.getTokenString(ntRkp.getTokenNumber(), |
| 319 | nt.getTokenPosition(KEY) -1); |
| 320 | } |
| 321 | return siteName; |
| 322 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 323 | |
| 324 | std::string |
| 325 | KeyManager::getRootName(const std::string name) |
| 326 | { |
| 327 | std::string rName; |
| 328 | nlsrTokenizer nt(name,"/"); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 329 | std::string rkp(m_nlsrRootKeyPrefix); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 330 | 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= |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 361 | m_certStore.getCertificateFromStore(signingCertName); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 362 | |
| 363 | if( signee.second ) |
| 364 | { |
| 365 | switch(paketCertType) |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 366 | { |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 367 | case KEY_TYPE_ROOT: |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 368 | return ((getRootName(packetName) == m_nlsrRootKeyPrefix) && |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 369 | verifySignature(packet,signee.first->getPublicKeyInfo())); |
| 370 | break; |
| 371 | case KEY_TYPE_SITE: |
| 372 | return ((getRootName(packetName) == getRootName(signingCertName)) && |
| 373 | verifySignature(packet,signee.first->getPublicKeyInfo()) && |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 374 | m_certStore.getCertificateIsVerified(signingCertName)); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 375 | break; |
| 376 | case KEY_TYPE_OPERATOR: |
| 377 | return ((getSiteName(packetName) == getSiteName(signingCertName)) && |
| 378 | verifySignature(packet,signee.first->getPublicKeyInfo()) && |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 379 | m_certStore.getCertificateIsVerified(signingCertName)); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 380 | break; |
| 381 | case KEY_TYPE_ROUTER: |
| 382 | return ((getSiteName(packetName) == getSiteName(signingCertName)) && |
| 383 | verifySignature(packet,signee.first->getPublicKeyInfo()) && |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 384 | m_certStore.getCertificateIsVerified(signingCertName)); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 385 | break; |
| 386 | case KEY_TYPE_PROCESS: |
| 387 | return ((getRouterName(packetName) == getRouterName(signingCertName)) && |
| 388 | verifySignature(packet,signee.first->getPublicKeyInfo()) && |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 389 | m_certStore.getCertificateIsVerified(signingCertName)); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 390 | break; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 391 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 392 | } |
| 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
| 405 | |