akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 1 | #include "nlsr_cert_store.hpp" |
| 2 | |
| 3 | namespace nlsr |
| 4 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 5 | static bool |
| 6 | nlsrCertificateStoreEntryCompare(NlsrCertificateStoreEntry& ncse1, |
| 7 | NlsrCertificateStoreEntry& ncse2) |
| 8 | |
| 9 | { |
| 10 | return ncse1.getCert()->getName().toUri() == |
| 11 | ncse2.getCert()->getName().toUri() ; |
| 12 | } |
| 13 | |
| 14 | static bool |
| 15 | nlsrCertificateStoreEntryCompareByName(NlsrCertificateStoreEntry& ncse1, |
| 16 | std::string compCertName) |
| 17 | |
| 18 | { |
| 19 | ndn::Name ccn(compCertName); |
| 20 | return ( ncse1.getCert()->getName().toUri() == compCertName || |
| 21 | ccn.isPrefixOf(ncse1.getCert()->getName()) ); |
| 22 | } |
| 23 | |
| 24 | bool |
| 25 | NlsrCertificateStore::addCertificate(NlsrCertificateStoreEntry & ncse) |
| 26 | { |
| 27 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 28 | std::find_if( certTable.begin(), certTable.end(), |
| 29 | bind(&nlsrCertificateStoreEntryCompare, _1, ncse)); |
| 30 | if(it == certTable.end()) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 31 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 32 | certTable.push_back(ncse); |
| 33 | return true; |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 34 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 35 | if( it != certTable.end() ) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 36 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 37 | if ( (*it).getCertSeqNum() < ncse.getCertSeqNum() ) |
| 38 | { |
| 39 | certTable.erase(it); |
| 40 | certTable.push_back(ncse); |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 41 | return true; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 42 | } |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 43 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool |
| 48 | NlsrCertificateStore::addCertificate( |
| 49 | ndn::shared_ptr<ndn::IdentityCertificate> pcert, uint32_t csn, bool isv) |
| 50 | { |
| 51 | NlsrCertificateStoreEntry ncse(pcert, csn, isv); |
| 52 | return addCertificate(ncse); |
| 53 | } |
| 54 | |
| 55 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 56 | NlsrCertificateStore::getCertificateFromStore(const std::string certName) |
| 57 | { |
| 58 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 59 | std::find_if( certTable.begin(), certTable.end(), |
| 60 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 61 | if(it == certTable.end()) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 62 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 63 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
| 64 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 65 | return std::make_pair(cert,false); |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 66 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 67 | return std::make_pair((*it).getCert(),true); |
| 68 | } |
| 69 | |
| 70 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 71 | NlsrCertificateStore::getCertificateFromStore( |
| 72 | const std::string certName, int checkSeqNum) |
| 73 | { |
| 74 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 75 | std::find_if( certTable.begin(), certTable.end(), |
| 76 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 77 | if(it == certTable.end()) |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 78 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 79 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
| 80 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 81 | return std::make_pair(cert,false); |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 82 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 83 | else |
| 84 | { |
| 85 | if( (*it).getCertSeqNum() == checkSeqNum ) |
| 86 | { |
| 87 | return std::make_pair((*it).getCert(),true); |
| 88 | } |
| 89 | } |
| 90 | return std::make_pair((*it).getCert(),false); |
| 91 | } |
| 92 | |
| 93 | bool |
| 94 | NlsrCertificateStore::isCertificateNewInStore(const std::string certName, |
| 95 | int checkSeqNo) |
| 96 | { |
| 97 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 98 | std::find_if( certTable.begin(), certTable.end(), |
| 99 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 100 | if(it != certTable.end()) |
| 101 | { |
| 102 | return (*it).getCertSeqNum() < checkSeqNo ; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool |
| 108 | NlsrCertificateStore::removeCertificateFromStroe(const std::string certName) |
| 109 | { |
| 110 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 111 | std::find_if( certTable.begin(), certTable.end(), |
| 112 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 113 | if(it != certTable.end()) |
| 114 | { |
| 115 | certTable.erase(it); |
| 116 | return true; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | NlsrCertificateStore::printCertStore() |
| 123 | { |
| 124 | std::list<NlsrCertificateStoreEntry>::iterator it; |
| 125 | for(it=certTable.begin(); it!=certTable.end(); ++it) |
| 126 | { |
| 127 | std::cout<<(*it)<<std::endl; |
| 128 | } |
| 129 | } |
akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame] | 130 | } |