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