Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (c) 2013-2020, Regents of the University of California |
Junxiao Shi | d5798f2 | 2016-08-22 02:33:26 +0000 | [diff] [blame] | 4 | * Yingdi Yu |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 12 | #include "contact-manager.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 13 | #include <QStringList> |
Yingdi Yu | 06f572b | 2014-03-11 15:46:10 -0700 | [diff] [blame] | 14 | #include <QFile> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 15 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 16 | #ifndef Q_MOC_RUN |
Junxiao Shi | d5798f2 | 2016-08-22 02:33:26 +0000 | [diff] [blame] | 17 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 18 | #include <ndn-cxx/face.hpp> |
| 19 | #include <ndn-cxx/security/validator.hpp> |
| 20 | #include <ndn-cxx/security/validator-null.hpp> |
| 21 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 22 | #include <ndn-cxx/security/verification-helpers.hpp> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 23 | #include "cryptopp.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | #include <boost/asio.hpp> |
| 25 | #include <boost/tokenizer.hpp> |
| 26 | #include <boost/filesystem.hpp> |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 27 | #endif |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 29 | namespace fs = boost::filesystem; |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 31 | namespace chronochat { |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | using std::map; |
| 35 | using std::vector; |
| 36 | |
| 37 | using ndn::Face; |
| 38 | using ndn::OBufferStream; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 39 | using ndn::security::Certificate; |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 40 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 41 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 42 | ContactManager::ContactManager(Face& face, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 43 | QObject* parent) |
| 44 | : QObject(parent) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 45 | , m_face(face) |
| 46 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 47 | initializeSecurity(); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ContactManager::~ContactManager() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 51 | { |
| 52 | } |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 53 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 54 | void |
| 55 | ContactManager::initializeSecurity() |
| 56 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 57 | m_validator = make_shared<ndn::security::ValidatorConfig>(m_face); |
| 58 | m_validator->load("security/validation-contact-manager.conf"); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 62 | ContactManager::fetchCollectEndorse(const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 63 | { |
| 64 | Name interestName = identity; |
| 65 | interestName.append("DNS").append("ENDORSED"); |
| 66 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 67 | Interest interest(interestName); |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 68 | interest.setInterestLifetime(time::milliseconds(1000)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 69 | interest.setCanBePrefix(true); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 70 | interest.setMustBeFresh(true); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 71 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 72 | ndn::security::DataValidationSuccessCallback onValidated = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 73 | bind(&ContactManager::onDnsCollectEndorseValidated, this, _1, identity); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 74 | ndn::security::DataValidationFailureCallback onValidationFailed = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 75 | bind(&ContactManager::onDnsCollectEndorseValidationFailed, this, _1, _2, identity); |
| 76 | TimeoutNotify timeoutNotify = |
| 77 | bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, _1, identity); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 78 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 79 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 83 | ContactManager::fetchEndorseCertificateInternal(const Name& identity, size_t certIndex) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 84 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 85 | shared_ptr<EndorseCollection> endorseCollection = |
| 86 | m_bufferedContacts[identity].m_endorseCollection; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 87 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 88 | if(certIndex >= endorseCollection->getCollectionEntries().size()) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 89 | return prepareEndorseInfo(identity); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 90 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 91 | Name interestName(endorseCollection->getCollectionEntries()[certIndex].certName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 92 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 93 | Interest interest(interestName); |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 94 | interest.setInterestLifetime(time::milliseconds(1000)); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 95 | interest.setCanBePrefix(true); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 96 | interest.setMustBeFresh(false); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 97 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 98 | m_face.expressInterest(interest, |
| 99 | bind(&ContactManager::onEndorseCertificateInternal, |
| 100 | this, _1, _2, identity, certIndex, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 101 | endorseCollection->getCollectionEntries()[certIndex].hash), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 102 | bind(&ContactManager::onEndorseCertificateInternalTimeout, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 103 | this, _1, identity, certIndex), |
| 104 | bind(&ContactManager::onEndorseCertificateInternalTimeout, |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 105 | this, _1, identity, certIndex)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 108 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 109 | ContactManager::prepareEndorseInfo(const Name& identity) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 110 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 111 | const Profile& profile = m_bufferedContacts[identity].m_selfEndorseCert->getProfile(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 112 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 113 | shared_ptr<EndorseInfo> endorseInfo = make_shared<EndorseInfo>(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 114 | m_bufferedContacts[identity].m_endorseInfo = endorseInfo; |
| 115 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 116 | map<string, size_t> endorseCount; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 117 | for (Profile::const_iterator pIt = profile.begin(); pIt != profile.end(); pIt++) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 118 | endorseCount[pIt->first] = 0; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 119 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 120 | size_t endorseCertCount = 0; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 121 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 122 | vector<shared_ptr<EndorseCertificate> >::const_iterator cIt = |
| 123 | m_bufferedContacts[identity].m_endorseCertList.begin(); |
| 124 | vector<shared_ptr<EndorseCertificate> >::const_iterator cEnd = |
| 125 | m_bufferedContacts[identity].m_endorseCertList.end(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 126 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 127 | for (; cIt != cEnd; cIt++, endorseCertCount++) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 128 | shared_ptr<Contact> contact = getContact((*cIt)->getSigner()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 129 | if (!static_cast<bool>(contact)) |
| 130 | continue; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 132 | if (!contact->isIntroducer() || |
| 133 | !contact->canBeTrustedFor(profile.getIdentityName())) |
| 134 | continue; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 135 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 136 | if (!(*cIt)->isValid()) |
| 137 | continue; |
| 138 | |
| 139 | if (!ndn::security::verifySignature(**cIt, contact->getPublicKey().data(), contact->getPublicKey().size())) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 140 | continue; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 142 | const Profile& tmpProfile = (*cIt)->getProfile(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 143 | const vector<string>& endorseList = (*cIt)->getEndorseList(); |
| 144 | for (vector<string>::const_iterator eIt = endorseList.begin(); eIt != endorseList.end(); eIt++) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 145 | if (tmpProfile.get(*eIt) == profile.get(*eIt)) |
| 146 | endorseCount[*eIt] += 1; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 147 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 148 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 149 | for (Profile::const_iterator pIt = profile.begin(); pIt != profile.end(); pIt++) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 150 | std::stringstream ss; |
| 151 | ss << endorseCount[pIt->first] << "/" << endorseCertCount; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 152 | endorseInfo->addEndorsement(pIt->first, pIt->second, ss.str()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 153 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 154 | |
| 155 | emit contactEndorseInfoReady (*endorseInfo); |
| 156 | } |
| 157 | |
| 158 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 159 | ContactManager::onDnsSelfEndorseCertValidated(const Data& data, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 160 | const Name& identity) |
| 161 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 162 | try { |
| 163 | Data plainData; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 164 | plainData.wireDecode(data.getContent().blockFromValue()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 165 | shared_ptr<EndorseCertificate> selfEndorseCertificate = |
| 166 | make_shared<EndorseCertificate>(boost::cref(plainData)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 167 | |
| 168 | if (ndn::security::verifySignature(plainData, *selfEndorseCertificate)) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 169 | m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate; |
| 170 | fetchCollectEndorse(identity); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 171 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 172 | else |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 173 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": verification failed")); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 174 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 175 | catch (const Block::Error& e) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 176 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": block error " + e.what())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 177 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 178 | catch (const EndorseCertificate::Error& e) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 179 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": cert error " + e.what())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 180 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 181 | catch (const Data::Error& e) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 182 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": data error " + e.what())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 183 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 187 | ContactManager::onDnsSelfEndorseCertValidationFailed(const Data& data, |
| 188 | const ndn::security::ValidationError& error, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 189 | const Name& identity) |
| 190 | { |
| 191 | // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert, |
| 192 | // but let's stay with failure for now. |
| 193 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | ContactManager::onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, |
| 198 | const Name& identity) |
| 199 | { |
| 200 | // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert, |
| 201 | // but let's stay with failure for now. |
| 202 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 203 | } |
| 204 | |
| 205 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 206 | ContactManager::onDnsCollectEndorseValidated(const Data& data, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 207 | const Name& identity) |
| 208 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 209 | try { |
| 210 | shared_ptr<EndorseCollection> endorseCollection = |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 211 | make_shared<EndorseCollection>(data.getContent().blockFromValue()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 212 | m_bufferedContacts[identity].m_endorseCollection = endorseCollection; |
| 213 | fetchEndorseCertificateInternal(identity, 0); |
| 214 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 215 | catch (const std::runtime_error&) { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 216 | prepareEndorseInfo(identity); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 217 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 221 | ContactManager::onDnsCollectEndorseValidationFailed(const Data& data, |
| 222 | const ndn::security::ValidationError& error, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 223 | const Name& identity) |
| 224 | { |
| 225 | prepareEndorseInfo(identity); |
| 226 | } |
| 227 | |
| 228 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 229 | ContactManager::onDnsCollectEndorseTimeoutNotify(const Interest& interest, const Name& identity) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 230 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 231 | prepareEndorseInfo(identity); |
| 232 | } |
| 233 | |
| 234 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 235 | ContactManager::onEndorseCertificateInternal(const Interest& interest, const Data& data, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 236 | const Name& identity, size_t certIndex, |
| 237 | string hash) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 238 | { |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 239 | std::stringstream ss; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 240 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 241 | using namespace CryptoPP; |
| 242 | |
| 243 | SHA256 hash; |
| 244 | StringSource(data.wireEncode().wire(), data.wireEncode().size(), true, |
| 245 | new HashFilter(hash, new FileSink(ss))); |
| 246 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 247 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 248 | if (ss.str() == hash) { |
| 249 | shared_ptr<EndorseCertificate> endorseCertificate = |
| 250 | make_shared<EndorseCertificate>(boost::cref(data)); |
| 251 | m_bufferedContacts[identity].m_endorseCertList.push_back(endorseCertificate); |
| 252 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 253 | |
| 254 | fetchEndorseCertificateInternal(identity, certIndex+1); |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | ContactManager::onEndorseCertificateInternalTimeout(const Interest& interest, |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 259 | const Name& identity, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 260 | size_t certIndex) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 261 | { |
| 262 | fetchEndorseCertificateInternal(identity, certIndex+1); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | ContactManager::collectEndorsement() |
| 267 | { |
| 268 | { |
| 269 | boost::recursive_mutex::scoped_lock lock(m_collectCountMutex); |
| 270 | m_collectCount = m_contactList.size(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 271 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 272 | for (ContactList::iterator it = m_contactList.begin(); it != m_contactList.end(); it++) { |
| 273 | Name interestName = (*it)->getNameSpace(); |
| 274 | interestName.append("DNS").append(m_identity.wireEncode()).append("ENDORSEE"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 275 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 276 | Interest interest(interestName); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 277 | interest.setMustBeFresh(true); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 278 | interest.setCanBePrefix(true); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 279 | interest.setInterestLifetime(time::milliseconds(1000)); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 280 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 281 | ndn::security::DataValidationSuccessCallback onValidated = |
| 282 | bind(&ContactManager::onDnsEndorseeValidated, this, _1); |
| 283 | ndn::security::DataValidationFailureCallback onValidationFailed = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 284 | bind(&ContactManager::onDnsEndorseeValidationFailed, this, _1, _2); |
| 285 | TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsEndorseeTimeoutNotify, this, _1); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 286 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 287 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 288 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 293 | ContactManager::onDnsEndorseeValidated(const Data& data) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 294 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 295 | Data endorseData; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 296 | endorseData.wireDecode(data.getContent().blockFromValue()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 297 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 298 | EndorseCertificate endorseCertificate(endorseData); |
| 299 | m_contactStorage->updateCollectEndorse(endorseCertificate); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 300 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 301 | decreaseCollectStatus(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 302 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 303 | |
| 304 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 305 | ContactManager::onDnsEndorseeValidationFailed(const Data& data, |
| 306 | const ndn::security::ValidationError& error) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 307 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 308 | decreaseCollectStatus(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 311 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 312 | ContactManager::onDnsEndorseeTimeoutNotify(const Interest& interest) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 313 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 314 | decreaseCollectStatus(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 317 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 318 | ContactManager::decreaseCollectStatus() |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 319 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 320 | size_t count; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 321 | { |
| 322 | boost::recursive_mutex::scoped_lock lock(m_collectCountMutex); |
| 323 | m_collectCount--; |
| 324 | count = m_collectCount; |
| 325 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 326 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 327 | if(count == 0) |
| 328 | publishCollectEndorsedDataInDNS(); |
| 329 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 330 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 331 | void |
| 332 | ContactManager::publishCollectEndorsedDataInDNS() |
| 333 | { |
| 334 | Name dnsName = m_identity; |
| 335 | dnsName.append("DNS").append("ENDORSED").appendVersion(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 336 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 337 | shared_ptr<Data> data = make_shared<Data>(); |
| 338 | data->setName(dnsName); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 339 | data->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 340 | |
| 341 | EndorseCollection endorseCollection; |
| 342 | m_contactStorage->getCollectEndorse(endorseCollection); |
| 343 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 344 | data->setContent(endorseCollection.wireEncode()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 345 | |
| 346 | m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 347 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 348 | m_contactStorage->updateDnsOthersEndorse(*data); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 349 | m_face.put(*data); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 353 | ContactManager::onIdentityCertValidated(const Data& data) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 354 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 355 | shared_ptr<Certificate> cert = make_shared<Certificate>(boost::cref(data)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 356 | m_bufferedIdCerts[cert->getName()] = cert; |
| 357 | decreaseIdCertCount(); |
| 358 | } |
| 359 | |
| 360 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 361 | ContactManager::onIdentityCertValidationFailed(const Data& data, |
| 362 | const ndn::security::ValidationError& error) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 363 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 364 | decreaseIdCertCount(); |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | ContactManager::onIdentityCertTimeoutNotify(const Interest& interest) |
| 369 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 370 | decreaseIdCertCount(); |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | ContactManager::decreaseIdCertCount() |
| 375 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 376 | size_t count; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 377 | { |
| 378 | boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex); |
| 379 | m_idCertCount--; |
| 380 | count = m_idCertCount; |
| 381 | } |
| 382 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 383 | if (count == 0) { |
| 384 | QStringList certNameList; |
| 385 | QStringList nameList; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 386 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 387 | for(BufferedIdCerts::const_iterator it = m_bufferedIdCerts.begin(); |
| 388 | it != m_bufferedIdCerts.end(); it++) { |
| 389 | certNameList << QString::fromStdString(it->second->getName().toUri()); |
| 390 | Profile profile(*(it->second)); |
| 391 | nameList << QString::fromStdString(profile.get("name")); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 392 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 393 | |
| 394 | emit idCertNameListReady(certNameList); |
| 395 | emit nameListReady(nameList); |
| 396 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 399 | shared_ptr<EndorseCertificate> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 400 | ContactManager::getSignedSelfEndorseCertificate(const Profile& profile) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 401 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 402 | auto signCert = m_keyChain.getPib().getIdentity(m_identity) |
| 403 | .getDefaultKey().getDefaultCertificate(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 404 | vector<string> endorseList; |
| 405 | for (Profile::const_iterator it = profile.begin(); it != profile.end(); it++) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 406 | endorseList.push_back(it->first); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 407 | |
| 408 | shared_ptr<EndorseCertificate> selfEndorseCertificate = |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 409 | make_shared<EndorseCertificate>(boost::cref(signCert), |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 410 | boost::cref(profile), |
| 411 | boost::cref(endorseList)); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 412 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 413 | m_keyChain.sign(*selfEndorseCertificate, |
| 414 | ndn::security::signingByIdentity(m_identity).setSignatureInfo( |
| 415 | selfEndorseCertificate->getSignatureInfo())); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 416 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 417 | return selfEndorseCertificate; |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 420 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 421 | ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 422 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 423 | Name dnsName = m_identity; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 424 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 425 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 426 | shared_ptr<Data> data = make_shared<Data>(); |
| 427 | data->setName(dnsName); |
| 428 | data->setContent(selfEndorseCertificate.wireEncode()); |
| 429 | data->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 430 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 431 | m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 432 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 433 | m_contactStorage->updateDnsSelfProfileData(*data); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 434 | m_face.put(*data); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 437 | shared_ptr<EndorseCertificate> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 438 | ContactManager::generateEndorseCertificate(const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 439 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 440 | auto signCert = m_keyChain.getPib().getIdentity(m_identity) |
| 441 | .getDefaultKey().getDefaultCertificate(); |
| 442 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 443 | shared_ptr<Contact> contact = getContact(identity); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 444 | if (!static_cast<bool>(contact)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 445 | return shared_ptr<EndorseCertificate>(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 446 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 447 | Name signerKeyName = m_identity; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 448 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 449 | vector<string> endorseList; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 450 | m_contactStorage->getEndorseList(identity, endorseList); |
| 451 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 452 | shared_ptr<EndorseCertificate> cert = |
| 453 | shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 454 | contact->getPublicKey(), |
| 455 | contact->getNotBefore(), |
| 456 | contact->getNotAfter(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 457 | signCert.getKeyId(), |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 458 | signerKeyName, |
| 459 | contact->getProfile(), |
| 460 | endorseList)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 461 | m_keyChain.sign(*cert, |
| 462 | ndn::security::signingByIdentity(m_identity) |
| 463 | .setSignatureInfo(cert->getSignatureInfo())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 464 | return cert; |
| 465 | |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate) |
| 470 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 471 | Name endorsee = endorseCertificate.getKeyName().getPrefix(-4); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 472 | Name dnsName = m_identity; |
| 473 | dnsName.append("DNS") |
| 474 | .append(endorsee.wireEncode()) |
| 475 | .append("ENDORSEE") |
| 476 | .appendVersion(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 477 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 478 | shared_ptr<Data> data = make_shared<Data>(); |
| 479 | data->setName(dnsName); |
| 480 | data->setContent(endorseCertificate.wireEncode()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 481 | data->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 482 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 483 | m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 484 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 485 | m_contactStorage->updateDnsEndorseOthers(*data, dnsName.get(-3).toUri()); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 486 | m_face.put(*data); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 490 | ContactManager::sendInterest(const Interest& interest, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 491 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 492 | const ndn::security::DataValidationFailureCallback& onValidationFailed, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 493 | const TimeoutNotify& timeoutNotify, |
| 494 | int retry /* = 1 */) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 495 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 496 | m_face.expressInterest(interest, |
| 497 | bind(&ContactManager::onTargetData, |
| 498 | this, _1, _2, onValidated, onValidationFailed), |
| 499 | bind(&ContactManager::onTargetTimeout, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 500 | this, _1, retry, onValidated, onValidationFailed, timeoutNotify), |
| 501 | bind(&ContactManager::onTargetTimeout, |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 502 | this, _1, retry, onValidated, onValidationFailed, timeoutNotify)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 503 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 504 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 505 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 506 | ContactManager::onTargetData(const Interest& interest, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 507 | const Data& data, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 508 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 509 | const ndn::security::DataValidationFailureCallback& onValidationFailed) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 510 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 511 | m_validator->validate(data, onValidated, onValidationFailed); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 512 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 513 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 514 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 515 | ContactManager::onTargetTimeout(const Interest& interest, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 516 | int retry, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 517 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 518 | const ndn::security::DataValidationFailureCallback& onValidationFailed, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 519 | const TimeoutNotify& timeoutNotify) |
| 520 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 521 | if (retry > 0) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 522 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1); |
| 523 | else |
| 524 | timeoutNotify(interest); |
| 525 | } |
| 526 | |
| 527 | void |
| 528 | ContactManager::onDnsInterest(const Name& prefix, const Interest& interest) |
| 529 | { |
| 530 | const Name& interestName = interest.getName(); |
| 531 | shared_ptr<Data> data; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 532 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 533 | if (interestName.size() <= prefix.size()) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 534 | return; |
| 535 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 536 | if (interestName.size() == (prefix.size()+1)) { |
| 537 | data = m_contactStorage->getDnsData("N/A", interestName.get(prefix.size()).toUri()); |
| 538 | if (static_cast<bool>(data)) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 539 | m_face.put(*data); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 540 | return; |
| 541 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 542 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 543 | if (interestName.size() == (prefix.size()+2)) { |
| 544 | data = m_contactStorage->getDnsData(interestName.get(prefix.size()).toUri(), |
| 545 | interestName.get(prefix.size()+1).toUri()); |
| 546 | if (static_cast<bool>(data)) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 547 | m_face.put(*data); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 548 | return; |
| 549 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 550 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 551 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 552 | void |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 553 | ContactManager::onDnsRegisterFailed(const Name& prefix, const std::string& failInfo) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 554 | { |
| 555 | emit warning(QString(failInfo.c_str())); |
| 556 | } |
| 557 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 558 | void |
| 559 | ContactManager::onKeyInterest(const Name& prefix, const Interest& interest) |
| 560 | { |
| 561 | const Name& interestName = interest.getName(); |
| 562 | shared_ptr<Certificate> data; |
| 563 | |
| 564 | try { |
| 565 | ndn::security::Certificate cert = m_keyChain.getPib() |
| 566 | .getIdentity(m_identity) |
| 567 | .getDefaultKey() |
| 568 | .getDefaultCertificate(); |
| 569 | if (cert.getKeyName() == interestName) |
| 570 | return m_face.put(cert); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 571 | } catch (const ndn::security::Pib::Error&) {} |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 572 | |
| 573 | data = m_contactStorage->getSelfEndorseCertificate(); |
| 574 | if (static_cast<bool>(data) && data->getKeyName().equals(interestName)) |
| 575 | return m_face.put(*data); |
| 576 | |
| 577 | data = m_contactStorage->getCollectEndorseByName(interestName); |
| 578 | if (static_cast<bool>(data)) |
| 579 | return m_face.put(*data); |
| 580 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 581 | |
| 582 | // public slots |
| 583 | void |
| 584 | ContactManager::onIdentityUpdated(const QString& identity) |
| 585 | { |
| 586 | m_identity = Name(identity.toStdString()); |
| 587 | |
| 588 | m_contactStorage = make_shared<ContactStorage>(m_identity); |
| 589 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 590 | m_dnsListenerHandle = m_face.setInterestFilter( |
| 591 | Name(m_identity).append("DNS"), |
| 592 | bind(&ContactManager::onDnsInterest, this, _1, _2), |
| 593 | bind(&ContactManager::onDnsRegisterFailed, this, _1, _2)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 594 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 595 | m_keyListenerHandle = m_face.setInterestFilter( |
| 596 | Name(m_identity).append("KEY"), |
| 597 | bind(&ContactManager::onKeyInterest, this, _1, _2), |
| 598 | bind(&ContactManager::onDnsRegisterFailed, this, _1, _2)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 599 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 600 | m_profileCertListenerHandle = m_face.setInterestFilter( |
| 601 | Name(m_identity).append("PROFILE-CERT"), |
| 602 | bind(&ContactManager::onKeyInterest, this, _1, _2), |
| 603 | bind(&ContactManager::onDnsRegisterFailed, this, _1, _2)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 604 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 605 | m_contactList.clear(); |
| 606 | m_contactStorage->getAllContacts(m_contactList); |
| 607 | |
| 608 | m_bufferedContacts.clear(); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 609 | onWaitForContactList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 610 | |
| 611 | collectEndorsement(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 614 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 615 | ContactManager::onFetchContactInfo(const QString& identity) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 616 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 617 | // try to fetch self-endorse-certificate via DNS PROFILE first. |
| 618 | Name identityName(identity.toStdString()); |
| 619 | Name interestName; |
| 620 | interestName.append(identityName).append("DNS").append("PROFILE"); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 621 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 622 | Interest interest(interestName); |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 623 | interest.setInterestLifetime(time::milliseconds(1000)); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 624 | interest.setCanBePrefix(true); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 625 | interest.setMustBeFresh(true); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 626 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 627 | ndn::security::DataValidationSuccessCallback onValidated = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 628 | bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 629 | ndn::security::DataValidationFailureCallback onValidationFailed = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 630 | bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, _2, identityName); |
| 631 | TimeoutNotify timeoutNotify = |
| 632 | bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 633 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 634 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 635 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 636 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 637 | void |
| 638 | ContactManager::onAddFetchedContact(const QString& identity) |
| 639 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 640 | Name identityName(identity.toStdString()); |
| 641 | |
| 642 | BufferedContacts::const_iterator it = m_bufferedContacts.find(identityName); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 643 | if (it != m_bufferedContacts.end()) { |
| 644 | Contact contact(*(it->second.m_selfEndorseCert)); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 645 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 646 | try { |
| 647 | m_contactStorage->addContact(contact); |
| 648 | m_bufferedContacts.erase(identityName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 649 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 650 | m_contactList.clear(); |
| 651 | m_contactStorage->getAllContacts(m_contactList); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 652 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 653 | onWaitForContactList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 654 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 655 | catch (const ContactStorage::Error& e) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 656 | emit warning(QString::fromStdString(e.what())); |
| 657 | } |
| 658 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 659 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 660 | emit warning(QString("Failure: no information of %1").arg(identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | void |
| 664 | ContactManager::onUpdateProfile() |
| 665 | { |
| 666 | // Get current profile; |
| 667 | shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 668 | if (!static_cast<bool>(newProfile)) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 669 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 670 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 671 | shared_ptr<EndorseCertificate> newEndorseCertificate = |
| 672 | getSignedSelfEndorseCertificate(*newProfile); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 673 | |
| 674 | m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate); |
| 675 | |
| 676 | publishSelfEndorseCertificateInDNS(*newEndorseCertificate); |
| 677 | } |
| 678 | |
| 679 | void |
| 680 | ContactManager::onRefreshBrowseContact() |
| 681 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 682 | return; |
| 683 | |
| 684 | #if 0 |
| 685 | // The following no longer works as we don't serve such a list anymore |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 686 | vector<string> bufferedIdCertNames; |
| 687 | try { |
| 688 | using namespace boost::asio::ip; |
| 689 | tcp::iostream request_stream; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 690 | request_stream.expires_from_now(std::chrono::milliseconds(5000)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 691 | request_stream.connect("ndncert.named-data.net","80"); |
| 692 | if (!request_stream) { |
| 693 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #1")); |
| 694 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 695 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 696 | |
| 697 | request_stream << "GET /cert/list/ HTTP/1.0\r\n"; |
| 698 | request_stream << "Host: ndncert.named-data.net\r\n\r\n"; |
| 699 | request_stream.flush(); |
| 700 | |
| 701 | string line1; |
| 702 | std::getline(request_stream,line1); |
| 703 | if (!request_stream) { |
| 704 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #2")); |
| 705 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 708 | std::stringstream response_stream(line1); |
| 709 | string http_version; |
| 710 | response_stream >> http_version; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 711 | size_t status_code; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 712 | response_stream >> status_code; |
| 713 | string status_message; |
| 714 | std::getline(response_stream,status_message); |
| 715 | |
| 716 | if (!response_stream || |
| 717 | http_version.substr(0,5) != "HTTP/") { |
| 718 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #3")); |
| 719 | return; |
| 720 | } |
| 721 | if (status_code!=200) { |
| 722 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #4")); |
| 723 | return; |
| 724 | } |
| 725 | vector<string> headers; |
| 726 | string header; |
| 727 | while (std::getline(request_stream, header) && header != "\r") |
| 728 | headers.push_back(header); |
| 729 | |
| 730 | std::istreambuf_iterator<char> stream_iter (request_stream); |
| 731 | std::istreambuf_iterator<char> end_of_stream; |
| 732 | |
| 733 | typedef boost::tokenizer< boost::escaped_list_separator<char>, |
| 734 | std::istreambuf_iterator<char> > tokenizer_t; |
| 735 | tokenizer_t certItems (stream_iter, |
| 736 | end_of_stream, |
| 737 | boost::escaped_list_separator<char>('\\', '\n', '"')); |
| 738 | |
| 739 | for (tokenizer_t::iterator it = certItems.begin(); it != certItems.end (); it++) |
| 740 | if (!it->empty()) |
| 741 | bufferedIdCertNames.push_back(*it); |
| 742 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 743 | catch (const std::exception& e) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 744 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #N")); |
| 745 | } |
| 746 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 747 | { |
| 748 | boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex); |
| 749 | m_idCertCount = bufferedIdCertNames.size(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 750 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 751 | m_bufferedIdCerts.clear(); |
| 752 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 753 | for (vector<string>::const_iterator it = bufferedIdCertNames.begin(); |
| 754 | it != bufferedIdCertNames.end(); it++) { |
| 755 | Name certName(*it); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 756 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 757 | Interest interest(certName); |
| 758 | interest.setInterestLifetime(time::milliseconds(1000)); |
| 759 | interest.setMustBeFresh(true); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 760 | interest.setCanBePrefix(true); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 761 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 762 | ndn::security::DataValidationSuccessCallback onValidated = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 763 | bind(&ContactManager::onIdentityCertValidated, this, _1); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 764 | ndn::security::DataValidationFailureCallback onValidationFailed = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 765 | bind(&ContactManager::onIdentityCertValidationFailed, this, _1, _2); |
| 766 | TimeoutNotify timeoutNotify = |
| 767 | bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 768 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 769 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 770 | } |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 771 | #endif |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 774 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 775 | ContactManager::onFetchIdCert(const QString& qCertName) |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 776 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 777 | Name certName(qCertName.toStdString()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 778 | if (m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end()) |
| 779 | emit idCertReady(*m_bufferedIdCerts[certName]); |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 780 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 781 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 782 | void |
| 783 | ContactManager::onAddFetchedContactIdCert(const QString& qCertName) |
| 784 | { |
| 785 | Name certName(qCertName.toStdString()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 786 | Name identity = certName.getPrefix(-1); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 787 | |
| 788 | BufferedIdCerts::const_iterator it = m_bufferedIdCerts.find(certName); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 789 | if (it != m_bufferedIdCerts.end()) { |
| 790 | Contact contact(*it->second); |
| 791 | try { |
| 792 | m_contactStorage->addContact(contact); |
| 793 | m_bufferedIdCerts.erase(certName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 794 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 795 | m_contactList.clear(); |
| 796 | m_contactStorage->getAllContacts(m_contactList); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 797 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 798 | onWaitForContactList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 799 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 800 | catch (const ContactStorage::Error& e) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 801 | emit warning(QString::fromStdString(e.what())); |
| 802 | } |
| 803 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 804 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 805 | emit warning(QString("Failure: no information of %1") |
| 806 | .arg(QString::fromStdString(identity.toUri()))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | void |
| 810 | ContactManager::onWaitForContactList() |
| 811 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 812 | QStringList aliasList; |
| 813 | QStringList idList; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 814 | for (ContactList::const_iterator it = m_contactList.begin(); it != m_contactList.end(); it++) { |
| 815 | aliasList << QString((*it)->getAlias().c_str()); |
| 816 | idList << QString((*it)->getNameSpace().toUri().c_str()); |
| 817 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 818 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 819 | emit contactAliasListReady(aliasList); |
| 820 | emit contactIdListReady(idList); |
| 821 | } |
| 822 | |
| 823 | void |
| 824 | ContactManager::onWaitForContactInfo(const QString& identity) |
| 825 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 826 | for (ContactList::const_iterator it = m_contactList.begin(); it != m_contactList.end(); it++) |
| 827 | if ((*it)->getNameSpace().toUri() == identity.toStdString()) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 828 | emit contactInfoReady(QString((*it)->getNameSpace().toUri().c_str()), |
| 829 | QString((*it)->getName().c_str()), |
| 830 | QString((*it)->getInstitution().c_str()), |
| 831 | (*it)->isIntroducer()); |
| 832 | } |
| 833 | |
| 834 | void |
| 835 | ContactManager::onRemoveContact(const QString& identity) |
| 836 | { |
| 837 | m_contactStorage->removeContact(Name(identity.toStdString())); |
| 838 | m_contactList.clear(); |
| 839 | m_contactStorage->getAllContacts(m_contactList); |
| 840 | |
| 841 | onWaitForContactList(); |
| 842 | } |
| 843 | |
| 844 | void |
| 845 | ContactManager::onUpdateAlias(const QString& identity, const QString& alias) |
| 846 | { |
| 847 | m_contactStorage->updateAlias(Name(identity.toStdString()), alias.toStdString()); |
| 848 | m_contactList.clear(); |
| 849 | m_contactStorage->getAllContacts(m_contactList); |
| 850 | |
| 851 | onWaitForContactList(); |
| 852 | } |
| 853 | |
| 854 | void |
| 855 | ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer) |
| 856 | { |
| 857 | m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer); |
| 858 | } |
| 859 | |
| 860 | void |
| 861 | ContactManager::onUpdateEndorseCertificate(const QString& identity) |
| 862 | { |
| 863 | Name identityName(identity.toStdString()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 864 | shared_ptr<Certificate> newEndorseCertificate = generateEndorseCertificate(identityName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 865 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 866 | if (!static_cast<bool>(newEndorseCertificate)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 867 | return; |
| 868 | |
| 869 | m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName); |
| 870 | |
| 871 | publishEndorseCertificateInDNS(*newEndorseCertificate); |
| 872 | } |
| 873 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 874 | } // namespace chronochat |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 875 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 876 | |
| 877 | #if WAF |
| 878 | #include "contact-manager.moc" |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 879 | #endif |