Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2013-2020, Regents of the University of California |
Junxiao Shi | d5798f2 | 2016-08-22 02:33:26 +0000 | [diff] [blame] | 4 | * Yingdi Yu |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 12 | #include "contact-manager.hpp" |
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 | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 61 | m_validator = make_shared<ndn::security::ValidatorConfig>(m_face); |
| 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 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 116 | shared_ptr<EndorseInfo> endorseInfo = 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()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 166 | shared_ptr<EndorseCertificate> selfEndorseCertificate = |
| 167 | make_shared<EndorseCertificate>(boost::cref(plainData)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 168 | |
| 169 | if (ndn::security::verifySignature(plainData, *selfEndorseCertificate)) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 170 | m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate; |
| 171 | fetchCollectEndorse(identity); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 172 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 173 | else |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 174 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": verification failed")); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 175 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 176 | catch (const Block::Error& e) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 177 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": block error " + e.what())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 178 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 179 | catch (const EndorseCertificate::Error& e) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 180 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": cert error " + e.what())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 181 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 182 | catch (const Data::Error& e) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 183 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": data error " + e.what())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 184 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 188 | ContactManager::onDnsSelfEndorseCertValidationFailed(const Data& data, |
| 189 | const ndn::security::ValidationError& error, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 190 | const Name& identity) |
| 191 | { |
| 192 | // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert, |
| 193 | // but let's stay with failure for now. |
| 194 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | ContactManager::onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, |
| 199 | const Name& identity) |
| 200 | { |
| 201 | // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert, |
| 202 | // but let's stay with failure for now. |
| 203 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 204 | } |
| 205 | |
| 206 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 207 | ContactManager::onDnsCollectEndorseValidated(const Data& data, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 208 | const Name& identity) |
| 209 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 210 | try { |
| 211 | shared_ptr<EndorseCollection> endorseCollection = |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 212 | 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) { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame^] | 248 | auto endorseCertificate = make_shared<EndorseCertificate>(data); |
| 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 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 335 | shared_ptr<Data> data = make_shared<Data>(); |
| 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 | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 353 | shared_ptr<Certificate> cert = make_shared<Certificate>(boost::cref(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 | |
| 405 | shared_ptr<EndorseCertificate> selfEndorseCertificate = |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 406 | make_shared<EndorseCertificate>(boost::cref(signCert), |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 407 | boost::cref(profile), |
| 408 | boost::cref(endorseList)); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 409 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 410 | m_keyChain.sign(*selfEndorseCertificate, |
| 411 | ndn::security::signingByIdentity(m_identity).setSignatureInfo( |
| 412 | selfEndorseCertificate->getSignatureInfo())); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 413 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 414 | return selfEndorseCertificate; |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 417 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 418 | ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 419 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 420 | Name dnsName = m_identity; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 421 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 422 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 423 | shared_ptr<Data> data = make_shared<Data>(); |
| 424 | data->setName(dnsName); |
| 425 | data->setContent(selfEndorseCertificate.wireEncode()); |
| 426 | data->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 427 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 428 | m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 429 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 430 | m_contactStorage->updateDnsSelfProfileData(*data); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 431 | m_face.put(*data); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 434 | shared_ptr<EndorseCertificate> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 435 | ContactManager::generateEndorseCertificate(const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 436 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 437 | auto signCert = m_keyChain.getPib().getIdentity(m_identity) |
| 438 | .getDefaultKey().getDefaultCertificate(); |
| 439 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 440 | shared_ptr<Contact> contact = getContact(identity); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 441 | if (!static_cast<bool>(contact)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 442 | return shared_ptr<EndorseCertificate>(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 443 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 444 | Name signerKeyName = m_identity; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 445 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 446 | vector<string> endorseList; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 447 | m_contactStorage->getEndorseList(identity, endorseList); |
| 448 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 449 | shared_ptr<EndorseCertificate> cert = |
| 450 | shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 451 | contact->getPublicKey(), |
| 452 | contact->getNotBefore(), |
| 453 | contact->getNotAfter(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 454 | signCert.getKeyId(), |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 455 | signerKeyName, |
| 456 | contact->getProfile(), |
| 457 | endorseList)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 458 | m_keyChain.sign(*cert, |
| 459 | ndn::security::signingByIdentity(m_identity) |
| 460 | .setSignatureInfo(cert->getSignatureInfo())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 461 | return cert; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | void |
| 466 | ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate) |
| 467 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 468 | Name endorsee = endorseCertificate.getKeyName().getPrefix(-4); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 469 | Name dnsName = m_identity; |
| 470 | dnsName.append("DNS") |
| 471 | .append(endorsee.wireEncode()) |
| 472 | .append("ENDORSEE") |
| 473 | .appendVersion(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 474 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 475 | shared_ptr<Data> data = make_shared<Data>(); |
| 476 | data->setName(dnsName); |
| 477 | data->setContent(endorseCertificate.wireEncode()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 478 | data->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 479 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 480 | m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 481 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 482 | m_contactStorage->updateDnsEndorseOthers(*data, dnsName.get(-3).toUri()); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 483 | m_face.put(*data); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 487 | ContactManager::sendInterest(const Interest& interest, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 488 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 489 | const ndn::security::DataValidationFailureCallback& onValidationFailed, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 490 | const TimeoutNotify& timeoutNotify, |
| 491 | int retry /* = 1 */) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 492 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 493 | m_face.expressInterest(interest, |
| 494 | bind(&ContactManager::onTargetData, |
| 495 | this, _1, _2, onValidated, onValidationFailed), |
| 496 | bind(&ContactManager::onTargetTimeout, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 497 | this, _1, retry, onValidated, onValidationFailed, timeoutNotify), |
| 498 | bind(&ContactManager::onTargetTimeout, |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 499 | this, _1, retry, onValidated, onValidationFailed, timeoutNotify)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 500 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 501 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 502 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 503 | ContactManager::onTargetData(const Interest& interest, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 504 | const Data& data, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 505 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 506 | const ndn::security::DataValidationFailureCallback& onValidationFailed) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 507 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 508 | m_validator->validate(data, onValidated, onValidationFailed); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 509 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 510 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 511 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 512 | ContactManager::onTargetTimeout(const Interest& interest, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 513 | int retry, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 514 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 515 | const ndn::security::DataValidationFailureCallback& onValidationFailed, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 516 | const TimeoutNotify& timeoutNotify) |
| 517 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 518 | if (retry > 0) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 519 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1); |
| 520 | else |
| 521 | timeoutNotify(interest); |
| 522 | } |
| 523 | |
| 524 | void |
| 525 | ContactManager::onDnsInterest(const Name& prefix, const Interest& interest) |
| 526 | { |
| 527 | const Name& interestName = interest.getName(); |
| 528 | shared_ptr<Data> data; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 529 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 530 | if (interestName.size() <= prefix.size()) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 531 | return; |
| 532 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 533 | if (interestName.size() == (prefix.size()+1)) { |
| 534 | data = m_contactStorage->getDnsData("N/A", interestName.get(prefix.size()).toUri()); |
| 535 | if (static_cast<bool>(data)) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 536 | m_face.put(*data); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 537 | return; |
| 538 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 539 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 540 | if (interestName.size() == (prefix.size()+2)) { |
| 541 | data = m_contactStorage->getDnsData(interestName.get(prefix.size()).toUri(), |
| 542 | interestName.get(prefix.size()+1).toUri()); |
| 543 | if (static_cast<bool>(data)) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 544 | m_face.put(*data); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 545 | return; |
| 546 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 547 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 548 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 549 | void |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 550 | ContactManager::onDnsRegisterFailed(const Name& prefix, const std::string& failInfo) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 551 | { |
| 552 | emit warning(QString(failInfo.c_str())); |
| 553 | } |
| 554 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 555 | void |
| 556 | ContactManager::onKeyInterest(const Name& prefix, const Interest& interest) |
| 557 | { |
| 558 | const Name& interestName = interest.getName(); |
| 559 | shared_ptr<Certificate> data; |
| 560 | |
| 561 | try { |
| 562 | ndn::security::Certificate cert = m_keyChain.getPib() |
| 563 | .getIdentity(m_identity) |
| 564 | .getDefaultKey() |
| 565 | .getDefaultCertificate(); |
| 566 | if (cert.getKeyName() == interestName) |
| 567 | return m_face.put(cert); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 568 | } catch (const ndn::security::Pib::Error&) {} |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 569 | |
| 570 | data = m_contactStorage->getSelfEndorseCertificate(); |
| 571 | if (static_cast<bool>(data) && data->getKeyName().equals(interestName)) |
| 572 | return m_face.put(*data); |
| 573 | |
| 574 | data = m_contactStorage->getCollectEndorseByName(interestName); |
| 575 | if (static_cast<bool>(data)) |
| 576 | return m_face.put(*data); |
| 577 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 578 | |
| 579 | // public slots |
| 580 | void |
| 581 | ContactManager::onIdentityUpdated(const QString& identity) |
| 582 | { |
| 583 | m_identity = Name(identity.toStdString()); |
| 584 | |
| 585 | m_contactStorage = make_shared<ContactStorage>(m_identity); |
| 586 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 587 | m_dnsListenerHandle = m_face.setInterestFilter( |
| 588 | Name(m_identity).append("DNS"), |
| 589 | bind(&ContactManager::onDnsInterest, 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_keyListenerHandle = m_face.setInterestFilter( |
| 593 | Name(m_identity).append("KEY"), |
| 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 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 597 | m_profileCertListenerHandle = m_face.setInterestFilter( |
| 598 | Name(m_identity).append("PROFILE-CERT"), |
| 599 | bind(&ContactManager::onKeyInterest, this, _1, _2), |
| 600 | bind(&ContactManager::onDnsRegisterFailed, this, _1, _2)); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 601 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 602 | m_contactList.clear(); |
| 603 | m_contactStorage->getAllContacts(m_contactList); |
| 604 | |
| 605 | m_bufferedContacts.clear(); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 606 | onWaitForContactList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 607 | |
| 608 | collectEndorsement(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 611 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 612 | ContactManager::onFetchContactInfo(const QString& identity) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 613 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 614 | // try to fetch self-endorse-certificate via DNS PROFILE first. |
| 615 | Name identityName(identity.toStdString()); |
| 616 | Name interestName; |
| 617 | interestName.append(identityName).append("DNS").append("PROFILE"); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 618 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 619 | Interest interest(interestName); |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 620 | interest.setInterestLifetime(time::milliseconds(1000)); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 621 | interest.setCanBePrefix(true); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 622 | interest.setMustBeFresh(true); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 623 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 624 | ndn::security::DataValidationSuccessCallback onValidated = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 625 | bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 626 | ndn::security::DataValidationFailureCallback onValidationFailed = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 627 | bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, _2, identityName); |
| 628 | TimeoutNotify timeoutNotify = |
| 629 | bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName); |
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 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 632 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 633 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 634 | void |
| 635 | ContactManager::onAddFetchedContact(const QString& identity) |
| 636 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 637 | Name identityName(identity.toStdString()); |
| 638 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame^] | 639 | auto it = m_bufferedContacts.find(identityName); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 640 | if (it != m_bufferedContacts.end()) { |
| 641 | Contact contact(*(it->second.m_selfEndorseCert)); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 642 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 643 | try { |
| 644 | m_contactStorage->addContact(contact); |
| 645 | m_bufferedContacts.erase(identityName); |
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 | m_contactList.clear(); |
| 648 | m_contactStorage->getAllContacts(m_contactList); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 649 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 650 | onWaitForContactList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 651 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 652 | catch (const ContactStorage::Error& e) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 653 | emit warning(QString::fromStdString(e.what())); |
| 654 | } |
| 655 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 656 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 657 | emit warning(QString("Failure: no information of %1").arg(identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | void |
| 661 | ContactManager::onUpdateProfile() |
| 662 | { |
| 663 | // Get current profile; |
| 664 | shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 665 | if (!static_cast<bool>(newProfile)) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 666 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 667 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 668 | shared_ptr<EndorseCertificate> newEndorseCertificate = |
| 669 | getSignedSelfEndorseCertificate(*newProfile); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 670 | |
| 671 | m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate); |
| 672 | |
| 673 | publishSelfEndorseCertificateInDNS(*newEndorseCertificate); |
| 674 | } |
| 675 | |
| 676 | void |
| 677 | ContactManager::onRefreshBrowseContact() |
| 678 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 679 | return; |
| 680 | |
| 681 | #if 0 |
| 682 | // 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] | 683 | vector<string> bufferedIdCertNames; |
| 684 | try { |
| 685 | using namespace boost::asio::ip; |
| 686 | tcp::iostream request_stream; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 687 | request_stream.expires_from_now(std::chrono::milliseconds(5000)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 688 | request_stream.connect("ndncert.named-data.net","80"); |
| 689 | if (!request_stream) { |
| 690 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #1")); |
| 691 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 692 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 693 | |
| 694 | request_stream << "GET /cert/list/ HTTP/1.0\r\n"; |
| 695 | request_stream << "Host: ndncert.named-data.net\r\n\r\n"; |
| 696 | request_stream.flush(); |
| 697 | |
| 698 | string line1; |
| 699 | std::getline(request_stream,line1); |
| 700 | if (!request_stream) { |
| 701 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #2")); |
| 702 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 705 | std::stringstream response_stream(line1); |
| 706 | string http_version; |
| 707 | response_stream >> http_version; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 708 | size_t status_code; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 709 | response_stream >> status_code; |
| 710 | string status_message; |
| 711 | std::getline(response_stream,status_message); |
| 712 | |
| 713 | if (!response_stream || |
| 714 | http_version.substr(0,5) != "HTTP/") { |
| 715 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #3")); |
| 716 | return; |
| 717 | } |
| 718 | if (status_code!=200) { |
| 719 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #4")); |
| 720 | return; |
| 721 | } |
| 722 | vector<string> headers; |
| 723 | string header; |
| 724 | while (std::getline(request_stream, header) && header != "\r") |
| 725 | headers.push_back(header); |
| 726 | |
| 727 | std::istreambuf_iterator<char> stream_iter (request_stream); |
| 728 | std::istreambuf_iterator<char> end_of_stream; |
| 729 | |
| 730 | typedef boost::tokenizer< boost::escaped_list_separator<char>, |
| 731 | std::istreambuf_iterator<char> > tokenizer_t; |
| 732 | tokenizer_t certItems (stream_iter, |
| 733 | end_of_stream, |
| 734 | boost::escaped_list_separator<char>('\\', '\n', '"')); |
| 735 | |
| 736 | for (tokenizer_t::iterator it = certItems.begin(); it != certItems.end (); it++) |
| 737 | if (!it->empty()) |
| 738 | bufferedIdCertNames.push_back(*it); |
| 739 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 740 | catch (const std::exception& e) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 741 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #N")); |
| 742 | } |
| 743 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 744 | { |
| 745 | boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex); |
| 746 | m_idCertCount = bufferedIdCertNames.size(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 747 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 748 | m_bufferedIdCerts.clear(); |
| 749 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame^] | 750 | for (auto it = bufferedIdCertNames.begin(); it != bufferedIdCertNames.end(); it++) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 751 | Name certName(*it); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 752 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 753 | Interest interest(certName); |
| 754 | interest.setInterestLifetime(time::milliseconds(1000)); |
| 755 | interest.setMustBeFresh(true); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 756 | interest.setCanBePrefix(true); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 757 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 758 | ndn::security::DataValidationSuccessCallback onValidated = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 759 | bind(&ContactManager::onIdentityCertValidated, this, _1); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 760 | ndn::security::DataValidationFailureCallback onValidationFailed = |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 761 | bind(&ContactManager::onIdentityCertValidationFailed, this, _1, _2); |
| 762 | TimeoutNotify timeoutNotify = |
| 763 | bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 764 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 765 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 766 | } |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 767 | #endif |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 770 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 771 | ContactManager::onFetchIdCert(const QString& qCertName) |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 772 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 773 | Name certName(qCertName.toStdString()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 774 | if (m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end()) |
| 775 | emit idCertReady(*m_bufferedIdCerts[certName]); |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 776 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 777 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 778 | void |
| 779 | ContactManager::onAddFetchedContactIdCert(const QString& qCertName) |
| 780 | { |
| 781 | Name certName(qCertName.toStdString()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 782 | Name identity = certName.getPrefix(-1); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 783 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame^] | 784 | auto it = m_bufferedIdCerts.find(certName); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 785 | if (it != m_bufferedIdCerts.end()) { |
| 786 | Contact contact(*it->second); |
| 787 | try { |
| 788 | m_contactStorage->addContact(contact); |
| 789 | m_bufferedIdCerts.erase(certName); |
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 | m_contactList.clear(); |
| 792 | m_contactStorage->getAllContacts(m_contactList); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 793 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 794 | onWaitForContactList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 795 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 796 | catch (const ContactStorage::Error& e) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 797 | emit warning(QString::fromStdString(e.what())); |
| 798 | } |
| 799 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 800 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 801 | emit warning(QString("Failure: no information of %1") |
| 802 | .arg(QString::fromStdString(identity.toUri()))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | void |
| 806 | ContactManager::onWaitForContactList() |
| 807 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 808 | QStringList aliasList; |
| 809 | QStringList idList; |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame^] | 810 | for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 811 | aliasList << QString((*it)->getAlias().c_str()); |
| 812 | idList << QString((*it)->getNameSpace().toUri().c_str()); |
| 813 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 814 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 815 | emit contactAliasListReady(aliasList); |
| 816 | emit contactIdListReady(idList); |
| 817 | } |
| 818 | |
| 819 | void |
| 820 | ContactManager::onWaitForContactInfo(const QString& identity) |
| 821 | { |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame^] | 822 | for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 823 | if ((*it)->getNameSpace().toUri() == identity.toStdString()) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 824 | emit contactInfoReady(QString((*it)->getNameSpace().toUri().c_str()), |
| 825 | QString((*it)->getName().c_str()), |
| 826 | QString((*it)->getInstitution().c_str()), |
| 827 | (*it)->isIntroducer()); |
| 828 | } |
| 829 | |
| 830 | void |
| 831 | ContactManager::onRemoveContact(const QString& identity) |
| 832 | { |
| 833 | m_contactStorage->removeContact(Name(identity.toStdString())); |
| 834 | m_contactList.clear(); |
| 835 | m_contactStorage->getAllContacts(m_contactList); |
| 836 | |
| 837 | onWaitForContactList(); |
| 838 | } |
| 839 | |
| 840 | void |
| 841 | ContactManager::onUpdateAlias(const QString& identity, const QString& alias) |
| 842 | { |
| 843 | m_contactStorage->updateAlias(Name(identity.toStdString()), alias.toStdString()); |
| 844 | m_contactList.clear(); |
| 845 | m_contactStorage->getAllContacts(m_contactList); |
| 846 | |
| 847 | onWaitForContactList(); |
| 848 | } |
| 849 | |
| 850 | void |
| 851 | ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer) |
| 852 | { |
| 853 | m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer); |
| 854 | } |
| 855 | |
| 856 | void |
| 857 | ContactManager::onUpdateEndorseCertificate(const QString& identity) |
| 858 | { |
| 859 | Name identityName(identity.toStdString()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 860 | shared_ptr<Certificate> newEndorseCertificate = generateEndorseCertificate(identityName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 861 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 862 | if (!static_cast<bool>(newEndorseCertificate)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 863 | return; |
| 864 | |
| 865 | m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName); |
| 866 | |
| 867 | publishEndorseCertificateInDNS(*newEndorseCertificate); |
| 868 | } |
| 869 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 870 | } // namespace chronochat |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 871 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 872 | |
| 873 | #if WAF |
| 874 | #include "contact-manager.moc" |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 875 | #endif |