akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 1 | #include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp> |
| 2 | #include <ndn-cpp-dev/security/key-chain.hpp> |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 3 | #include "nlsr_cert_store.hpp" |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 4 | #include "nlsr_wle.hpp" |
| 5 | #include "nlsr_km.hpp" |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 6 | |
| 7 | namespace nlsr |
| 8 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 9 | static bool |
| 10 | nlsrCertificateStoreEntryCompare(NlsrCertificateStoreEntry& ncse1, |
| 11 | NlsrCertificateStoreEntry& ncse2) |
| 12 | |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 13 | { |
| 14 | int sizeDiff=ncse1.getCert()->getName().size()- |
| 15 | ncse2.getCert()->getName().size(); |
| 16 | return (ncse2.getCert()->getName().isPrefixOf(ncse1.getCert()->getName()) && |
| 17 | (sizeDiff <= 1 && sizeDiff>= 0)); |
| 18 | |
| 19 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | static bool |
| 23 | nlsrCertificateStoreEntryCompareByName(NlsrCertificateStoreEntry& ncse1, |
| 24 | std::string compCertName) |
| 25 | |
| 26 | { |
| 27 | ndn::Name ccn(compCertName); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 28 | int sizeDiff= ncse1.getCert()->getName().size() -ccn.size(); |
| 29 | return ( ccn.isPrefixOf(ncse1.getCert()->getName()) && |
| 30 | (sizeDiff <= 1 && sizeDiff>= 0)); |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | NlsrCertificateStore::updateWaitingList(std::string respCertName) |
| 35 | { |
| 36 | ndn::Name tmpName(respCertName); |
| 37 | respCertName=tmpName.getPrefix(-1).toUri(); |
| 38 | std::pair<WaitingListEntry, bool> chkWle= |
| 39 | waitingList.getWaitingListEntry(respCertName); |
| 40 | if( chkWle.second ) |
| 41 | { |
| 42 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> sc= |
| 43 | getCertificateFromStore(respCertName); |
| 44 | std::list<std::string> waitees=(chkWle.first).getWaitingCerts(); |
| 45 | for(std::list<std::string>::iterator it = waitees.begin(); |
| 46 | it != waitees.end();++it) |
| 47 | { |
| 48 | KeyManager km; |
| 49 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> wc= |
| 50 | getCertificateFromStore(*(it)); |
| 51 | if( wc.second && sc.second ) |
| 52 | { |
| 53 | if(km.verifySignature(*(wc.first),sc.first->getPublicKeyInfo())) |
| 54 | { |
| 55 | //1. Update Certificate Store |
| 56 | setCertificateIsVerified(*(it),true); |
| 57 | //2. Call updateWaitingList for waitee ( *(it) ) |
| 58 | updateWaitingList(*(it)); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | //remove that entry from waiting list |
| 65 | waitingList.removeFromWaitingList(respCertName); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | NlsrCertificateStore::updateWaitingList(NlsrCertificateStoreEntry& ncse) |
| 70 | { |
| 71 | if( ncse.getIsSignerVerified()) |
| 72 | { |
| 73 | updateWaitingList(ncse.getCert()->getName().toUri()); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | ndn::SignatureSha256WithRsa signature(ncse.getCert()->getSignature()); |
| 78 | waitingList.addtoWaitingList(signature.getKeyLocator().getName().toUri(), |
| 79 | ncse.getCert()->getName().toUri()); |
| 80 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool |
| 84 | NlsrCertificateStore::addCertificate(NlsrCertificateStoreEntry & ncse) |
| 85 | { |
| 86 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 87 | std::find_if( certTable.begin(), certTable.end(), |
| 88 | bind(&nlsrCertificateStoreEntryCompare, _1, ncse)); |
| 89 | if(it == certTable.end()) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 90 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 91 | certTable.push_back(ncse); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 92 | updateWaitingList(ncse); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 93 | return true; |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 94 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 95 | else if( it != certTable.end() ) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 96 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 97 | if ( (*it).getCertSeqNum() < ncse.getCertSeqNum() ) |
| 98 | { |
| 99 | certTable.erase(it); |
| 100 | certTable.push_back(ncse); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 101 | updateWaitingList(ncse); |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 102 | return true; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 103 | } |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 104 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | NlsrCertificateStore::addCertificate( |
| 110 | ndn::shared_ptr<ndn::IdentityCertificate> pcert, uint32_t csn, bool isv) |
| 111 | { |
| 112 | NlsrCertificateStoreEntry ncse(pcert, csn, isv); |
| 113 | return addCertificate(ncse); |
| 114 | } |
| 115 | |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 116 | std::pair<uint32_t, bool> |
| 117 | NlsrCertificateStore::getCertificateSeqNum(std::string certName) |
| 118 | { |
| 119 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 120 | std::find_if( certTable.begin(), certTable.end(), |
| 121 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 122 | if(it == certTable.end()) |
| 123 | { |
| 124 | return std::make_pair(0,false); |
| 125 | } |
| 126 | return std::make_pair((*it).getCertSeqNum(),true); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | void |
| 132 | NlsrCertificateStore::setCertificateIsVerified(std::string certName, |
| 133 | bool isVerified) |
| 134 | { |
| 135 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 136 | std::find_if( certTable.begin(), certTable.end(), |
| 137 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 138 | if(it != certTable.end()) |
| 139 | { |
| 140 | it->setIsSignerVerified(true); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool |
| 145 | NlsrCertificateStore::getCertificateIsVerified( std::string certName ) |
| 146 | { |
| 147 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 148 | std::find_if( certTable.begin(), certTable.end(), |
| 149 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 150 | if(it != certTable.end()) |
| 151 | { |
| 152 | return it->getIsSignerVerified(); |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 158 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 159 | NlsrCertificateStore::getCertificateFromStore(const std::string certName) |
| 160 | { |
| 161 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 162 | std::find_if( certTable.begin(), certTable.end(), |
| 163 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 164 | if(it == certTable.end()) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 165 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 166 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 167 | ndn::make_shared<ndn::IdentityCertificate>(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 168 | return std::make_pair(cert,false); |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 169 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 170 | return std::make_pair((*it).getCert(),true); |
| 171 | } |
| 172 | |
| 173 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 174 | NlsrCertificateStore::getCertificateFromStore( |
| 175 | const std::string certName, int checkSeqNum) |
| 176 | { |
| 177 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 178 | std::find_if( certTable.begin(), certTable.end(), |
| 179 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 180 | if(it == certTable.end()) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 181 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 182 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
| 183 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 184 | return std::make_pair(cert,false); |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 185 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 186 | else |
| 187 | { |
| 188 | if( (*it).getCertSeqNum() == checkSeqNum ) |
| 189 | { |
| 190 | return std::make_pair((*it).getCert(),true); |
| 191 | } |
| 192 | } |
| 193 | return std::make_pair((*it).getCert(),false); |
| 194 | } |
| 195 | |
| 196 | bool |
| 197 | NlsrCertificateStore::isCertificateNewInStore(const std::string certName, |
| 198 | int checkSeqNo) |
| 199 | { |
| 200 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 201 | std::find_if( certTable.begin(), certTable.end(), |
| 202 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 203 | if(it != certTable.end()) |
| 204 | { |
| 205 | return (*it).getCertSeqNum() < checkSeqNo ; |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | bool |
| 211 | NlsrCertificateStore::removeCertificateFromStroe(const std::string certName) |
| 212 | { |
| 213 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 214 | std::find_if( certTable.begin(), certTable.end(), |
| 215 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 216 | if(it != certTable.end()) |
| 217 | { |
| 218 | certTable.erase(it); |
| 219 | return true; |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | NlsrCertificateStore::printCertStore() |
| 226 | { |
| 227 | std::list<NlsrCertificateStoreEntry>::iterator it; |
| 228 | for(it=certTable.begin(); it!=certTable.end(); ++it) |
| 229 | { |
| 230 | std::cout<<(*it)<<std::endl; |
| 231 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 232 | std::cout<<waitingList<<std::endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 233 | } |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 234 | } |