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