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