blob: 04700f5df7b4f85625f55672a1bfaf89b3c0aa7d [file] [log] [blame]
Yingdi Yu9236c432013-10-18 11:29:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patilb3d03b92021-01-08 17:09:32 +05303 * Copyright (c) 2013-2021, Regents of the University of California
Junxiao Shid5798f22016-08-22 02:33:26 +00004 * Yingdi Yu
Yingdi Yu9236c432013-10-18 11:29:25 -07005 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yu9236c432013-10-18 11:29:25 -070010 */
11
Yingdi Yu0b0a7362014-08-05 16:31:30 -070012#include "contact-manager.hpp"
Davide Pesavento7676b562020-12-14 00:41:26 -050013
Yingdi Yu348f5ea2014-03-01 14:47:25 -080014#include <QStringList>
Yingdi Yu06f572b2014-03-11 15:46:10 -070015#include <QFile>
Yingdi Yu9236c432013-10-18 11:29:25 -070016
Yingdi Yu4685b1b2013-10-18 17:05:02 -070017#ifndef Q_MOC_RUN
Junxiao Shid5798f22016-08-22 02:33:26 +000018#include <ndn-cxx/encoding/buffer-stream.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053019#include <ndn-cxx/face.hpp>
Davide Pesavento7676b562020-12-14 00:41:26 -050020#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 Patil3d850902020-11-23 12:19:14 +053024#include <ndn-cxx/security/validator.hpp>
25#include <ndn-cxx/security/validator-null.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053026#include <ndn-cxx/security/verification-helpers.hpp>
Davide Pesavento7676b562020-12-14 00:41:26 -050027
Yingdi Yu348f5ea2014-03-01 14:47:25 -080028#include <boost/asio.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080029#include <boost/filesystem.hpp>
Davide Pesavento7676b562020-12-14 00:41:26 -050030#include <boost/tokenizer.hpp>
Yingdi Yu4685b1b2013-10-18 17:05:02 -070031#endif
Yingdi Yu9236c432013-10-18 11:29:25 -070032
Yingdi Yu348f5ea2014-03-01 14:47:25 -080033namespace fs = boost::filesystem;
Yingdi Yu9236c432013-10-18 11:29:25 -070034
Yingdi Yueb692ac2015-02-10 18:46:18 -080035namespace chronochat {
Yingdi Yu4685b1b2013-10-18 17:05:02 -070036
Yingdi Yu0b0a7362014-08-05 16:31:30 -070037using std::string;
38using std::map;
39using std::vector;
40
41using ndn::Face;
42using ndn::OBufferStream;
Varun Patil3d850902020-11-23 12:19:14 +053043using ndn::security::Certificate;
Yingdi Yu17032f82014-03-25 15:48:23 -070044
Yingdi Yu348f5ea2014-03-01 14:47:25 -080045
Yingdi Yu2c9e7712014-10-20 11:55:05 -070046ContactManager::ContactManager(Face& face,
Yingdi Yufa4ce792014-02-06 18:09:22 -080047 QObject* parent)
48 : QObject(parent)
Yingdi Yufa4ce792014-02-06 18:09:22 -080049 , m_face(face)
50{
Yingdi Yu76dd8002013-12-24 11:16:32 +080051 initializeSecurity();
Yingdi Yu9236c432013-10-18 11:29:25 -070052}
53
54ContactManager::~ContactManager()
Yingdi Yu0b0a7362014-08-05 16:31:30 -070055{
56}
Yingdi Yu9236c432013-10-18 11:29:25 -070057
Yingdi Yu76dd8002013-12-24 11:16:32 +080058void
59ContactManager::initializeSecurity()
60{
Varun Patilb3d03b92021-01-08 17:09:32 +053061 m_validator = std::make_shared<ndn::security::ValidatorConfig>(m_face);
Varun Patil3d850902020-11-23 12:19:14 +053062 m_validator->load("security/validation-contact-manager.conf");
Yingdi Yu76dd8002013-12-24 11:16:32 +080063}
64
65void
Yingdi Yufa4ce792014-02-06 18:09:22 -080066ContactManager::fetchCollectEndorse(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -080067{
68 Name interestName = identity;
69 interestName.append("DNS").append("ENDORSED");
70
Yingdi Yu76dd8002013-12-24 11:16:32 +080071 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -070072 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patil3d850902020-11-23 12:19:14 +053073 interest.setCanBePrefix(true);
Yingdi Yufa4ce792014-02-06 18:09:22 -080074 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +080075
Varun Patil3d850902020-11-23 12:19:14 +053076 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -070077 bind(&ContactManager::onDnsCollectEndorseValidated, this, _1, identity);
Varun Patil3d850902020-11-23 12:19:14 +053078 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -070079 bind(&ContactManager::onDnsCollectEndorseValidationFailed, this, _1, _2, identity);
80 TimeoutNotify timeoutNotify =
81 bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, _1, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080082
Yingdi Yu348f5ea2014-03-01 14:47:25 -080083 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
Yingdi Yub2e747d2013-11-05 23:06:43 -080084}
85
86void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080087ContactManager::fetchEndorseCertificateInternal(const Name& identity, size_t certIndex)
Yingdi Yub2e747d2013-11-05 23:06:43 -080088{
Davide Pesavento7676b562020-12-14 00:41:26 -050089 auto endorseCollection = m_bufferedContacts[identity].m_endorseCollection;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080090
Davide Pesavento7676b562020-12-14 00:41:26 -050091 if (certIndex >= endorseCollection->getCollectionEntries().size())
Varun Patil3d850902020-11-23 12:19:14 +053092 return prepareEndorseInfo(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080093
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080094 Name interestName(endorseCollection->getCollectionEntries()[certIndex].certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080095
Yingdi Yu76dd8002013-12-24 11:16:32 +080096 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -070097 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +053098 interest.setCanBePrefix(true);
Varun Patil3d850902020-11-23 12:19:14 +053099 interest.setMustBeFresh(false);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800100
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700101 m_face.expressInterest(interest,
102 bind(&ContactManager::onEndorseCertificateInternal,
103 this, _1, _2, identity, certIndex,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800104 endorseCollection->getCollectionEntries()[certIndex].hash),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700105 bind(&ContactManager::onEndorseCertificateInternalTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530106 this, _1, identity, certIndex),
107 bind(&ContactManager::onEndorseCertificateInternalTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700108 this, _1, identity, certIndex));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800109}
110
Yingdi Yu76dd8002013-12-24 11:16:32 +0800111void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800112ContactManager::prepareEndorseInfo(const Name& identity)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800113{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114 const Profile& profile = m_bufferedContacts[identity].m_selfEndorseCert->getProfile();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800115
Varun Patilb3d03b92021-01-08 17:09:32 +0530116 auto endorseInfo = std::make_shared<EndorseInfo>();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800117 m_bufferedContacts[identity].m_endorseInfo = endorseInfo;
118
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800119 map<string, size_t> endorseCount;
Davide Pesavento7676b562020-12-14 00:41:26 -0500120 for (auto pIt = profile.begin(); pIt != profile.end(); pIt++)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700121 endorseCount[pIt->first] = 0;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800122
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800123 size_t endorseCertCount = 0;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800124
Davide Pesavento7676b562020-12-14 00:41:26 -0500125 auto cIt = m_bufferedContacts[identity].m_endorseCertList.cbegin();
126 auto cEnd = m_bufferedContacts[identity].m_endorseCertList.cend();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800127
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700128 for (; cIt != cEnd; cIt++, endorseCertCount++) {
Varun Patil3d850902020-11-23 12:19:14 +0530129 shared_ptr<Contact> contact = getContact((*cIt)->getSigner());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700130 if (!static_cast<bool>(contact))
131 continue;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700132
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700133 if (!contact->isIntroducer() ||
134 !contact->canBeTrustedFor(profile.getIdentityName()))
135 continue;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800136
Varun Patil3d850902020-11-23 12:19:14 +0530137 if (!(*cIt)->isValid())
138 continue;
139
140 if (!ndn::security::verifySignature(**cIt, contact->getPublicKey().data(), contact->getPublicKey().size()))
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700141 continue;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800142
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700143 const Profile& tmpProfile = (*cIt)->getProfile();
Davide Pesavento7676b562020-12-14 00:41:26 -0500144 const auto& endorseList = (*cIt)->getEndorseList();
145 for (auto eIt = endorseList.begin(); eIt != endorseList.end(); eIt++)
Varun Patil3d850902020-11-23 12:19:14 +0530146 if (tmpProfile.get(*eIt) == profile.get(*eIt))
147 endorseCount[*eIt] += 1;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700148 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800149
Davide Pesavento7676b562020-12-14 00:41:26 -0500150 for (auto pIt = profile.begin(); pIt != profile.end(); pIt++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700151 std::stringstream ss;
152 ss << endorseCount[pIt->first] << "/" << endorseCertCount;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800153 endorseInfo->addEndorsement(pIt->first, pIt->second, ss.str());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700154 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800155
156 emit contactEndorseInfoReady (*endorseInfo);
157}
158
159void
Varun Patil3d850902020-11-23 12:19:14 +0530160ContactManager::onDnsSelfEndorseCertValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800161 const Name& identity)
162{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700163 try {
164 Data plainData;
Varun Patil3d850902020-11-23 12:19:14 +0530165 plainData.wireDecode(data.getContent().blockFromValue());
Varun Patilb3d03b92021-01-08 17:09:32 +0530166 auto selfEndorseCertificate = std::make_shared<EndorseCertificate>(plainData);
Varun Patil3d850902020-11-23 12:19:14 +0530167
168 if (ndn::security::verifySignature(plainData, *selfEndorseCertificate)) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700169 m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate;
170 fetchCollectEndorse(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800171 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700172 else
Varun Patil3d850902020-11-23 12:19:14 +0530173 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": verification failed"));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700174 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530175 catch (const Block::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530176 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": block error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700177 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530178 catch (const EndorseCertificate::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530179 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": cert error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700180 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530181 catch (const Data::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530182 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": data error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700183 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800184}
185
186void
Varun Patil3d850902020-11-23 12:19:14 +0530187ContactManager::onDnsSelfEndorseCertValidationFailed(const Data& data,
188 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800189 const Name& identity)
190{
191 // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert,
192 // but let's stay with failure for now.
193 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri()));
194}
195
196void
197ContactManager::onDnsSelfEndorseCertTimeoutNotify(const Interest& interest,
198 const Name& identity)
199{
200 // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert,
201 // but let's stay with failure for now.
202 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri()));
203}
204
205void
Varun Patil3d850902020-11-23 12:19:14 +0530206ContactManager::onDnsCollectEndorseValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800207 const Name& identity)
208{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800209 try {
Varun Patilb3d03b92021-01-08 17:09:32 +0530210 auto endorseCollection = std::make_shared<EndorseCollection>(data.getContent().blockFromValue());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700211 m_bufferedContacts[identity].m_endorseCollection = endorseCollection;
212 fetchEndorseCertificateInternal(identity, 0);
213 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530214 catch (const std::runtime_error&) {
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800215 prepareEndorseInfo(identity);
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800216 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800217}
218
219void
Varun Patil3d850902020-11-23 12:19:14 +0530220ContactManager::onDnsCollectEndorseValidationFailed(const Data& data,
221 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800222 const Name& identity)
223{
224 prepareEndorseInfo(identity);
225}
226
227void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700228ContactManager::onDnsCollectEndorseTimeoutNotify(const Interest& interest, const Name& identity)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800229{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800230 prepareEndorseInfo(identity);
231}
232
233void
Davide Pesavento7676b562020-12-14 00:41:26 -0500234ContactManager::onEndorseCertificateInternal(const Interest&, const Data& data,
235 const Name& identity, size_t certIndex, string hash)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800236{
Davide Pesavento7676b562020-12-14 00:41:26 -0500237 std::ostringstream ss;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700238 {
Davide Pesavento7676b562020-12-14 00:41:26 -0500239 using namespace ndn::security::transform;
240 bufferSource(data.wireEncode().wire(), data.wireEncode().size())
241 >> digestFilter(ndn::DigestAlgorithm::SHA256)
242 >> streamSink(ss);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800243 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700244
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700245 if (ss.str() == hash) {
Varun Patilb3d03b92021-01-08 17:09:32 +0530246 auto endorseCertificate = std::make_shared<EndorseCertificate>(data);
Davide Pesavento7676b562020-12-14 00:41:26 -0500247 m_bufferedContacts[identity].m_endorseCertList.push_back(std::move(endorseCertificate));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700248 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800249
250 fetchEndorseCertificateInternal(identity, certIndex+1);
251}
252
253void
254ContactManager::onEndorseCertificateInternalTimeout(const Interest& interest,
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700255 const Name& identity,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800256 size_t certIndex)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800257{
258 fetchEndorseCertificateInternal(identity, certIndex+1);
259}
260
261void
262ContactManager::collectEndorsement()
263{
264 {
265 boost::recursive_mutex::scoped_lock lock(m_collectCountMutex);
266 m_collectCount = m_contactList.size();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700267
Davide Pesavento7676b562020-12-14 00:41:26 -0500268 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700269 Name interestName = (*it)->getNameSpace();
270 interestName.append("DNS").append(m_identity.wireEncode()).append("ENDORSEE");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800271
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700272 Interest interest(interestName);
Varun Patil3d850902020-11-23 12:19:14 +0530273 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530274 interest.setCanBePrefix(true);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700275 interest.setInterestLifetime(time::milliseconds(1000));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700276
Varun Patil3d850902020-11-23 12:19:14 +0530277 ndn::security::DataValidationSuccessCallback onValidated =
278 bind(&ContactManager::onDnsEndorseeValidated, this, _1);
279 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700280 bind(&ContactManager::onDnsEndorseeValidationFailed, this, _1, _2);
281 TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsEndorseeTimeoutNotify, this, _1);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700282
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700283 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
284 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800285 }
286}
287
288void
Varun Patil3d850902020-11-23 12:19:14 +0530289ContactManager::onDnsEndorseeValidated(const Data& data)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800290{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800291 Data endorseData;
Varun Patil3d850902020-11-23 12:19:14 +0530292 endorseData.wireDecode(data.getContent().blockFromValue());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800293
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800294 EndorseCertificate endorseCertificate(endorseData);
295 m_contactStorage->updateCollectEndorse(endorseCertificate);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800296
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800297 decreaseCollectStatus();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800298}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800299
300void
Varun Patil3d850902020-11-23 12:19:14 +0530301ContactManager::onDnsEndorseeValidationFailed(const Data& data,
302 const ndn::security::ValidationError& error)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700303{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800304 decreaseCollectStatus();
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700305}
306
Yingdi Yub2e747d2013-11-05 23:06:43 -0800307void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800308ContactManager::onDnsEndorseeTimeoutNotify(const Interest& interest)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800309{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800310 decreaseCollectStatus();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800311}
312
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700313void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800314ContactManager::decreaseCollectStatus()
Yingdi Yub2e747d2013-11-05 23:06:43 -0800315{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800316 size_t count;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800317 {
318 boost::recursive_mutex::scoped_lock lock(m_collectCountMutex);
319 m_collectCount--;
320 count = m_collectCount;
321 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800322
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800323 if(count == 0)
324 publishCollectEndorsedDataInDNS();
325}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800326
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800327void
328ContactManager::publishCollectEndorsedDataInDNS()
329{
330 Name dnsName = m_identity;
331 dnsName.append("DNS").append("ENDORSED").appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800332
Varun Patilb3d03b92021-01-08 17:09:32 +0530333 auto data = std::make_shared<Data>();
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700334 data->setName(dnsName);
Varun Patil3d850902020-11-23 12:19:14 +0530335 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800336
337 EndorseCollection endorseCollection;
338 m_contactStorage->getCollectEndorse(endorseCollection);
339
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800340 data->setContent(endorseCollection.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530341
342 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800343
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700344 m_contactStorage->updateDnsOthersEndorse(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700345 m_face.put(*data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800346}
347
348void
Varun Patil3d850902020-11-23 12:19:14 +0530349ContactManager::onIdentityCertValidated(const Data& data)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800350{
Varun Patilb3d03b92021-01-08 17:09:32 +0530351 auto cert = std::make_shared<Certificate>(data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800352 m_bufferedIdCerts[cert->getName()] = cert;
353 decreaseIdCertCount();
354}
355
356void
Varun Patil3d850902020-11-23 12:19:14 +0530357ContactManager::onIdentityCertValidationFailed(const Data& data,
358 const ndn::security::ValidationError& error)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800359{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800360 decreaseIdCertCount();
361}
362
363void
364ContactManager::onIdentityCertTimeoutNotify(const Interest& interest)
365{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800366 decreaseIdCertCount();
367}
368
369void
370ContactManager::decreaseIdCertCount()
371{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800372 size_t count;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800373 {
374 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
375 m_idCertCount--;
376 count = m_idCertCount;
377 }
378
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700379 if (count == 0) {
380 QStringList certNameList;
381 QStringList nameList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800382
Davide Pesavento7676b562020-12-14 00:41:26 -0500383 for (auto it = m_bufferedIdCerts.begin(); it != m_bufferedIdCerts.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700384 certNameList << QString::fromStdString(it->second->getName().toUri());
385 Profile profile(*(it->second));
386 nameList << QString::fromStdString(profile.get("name"));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800387 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700388
389 emit idCertNameListReady(certNameList);
390 emit nameListReady(nameList);
391 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800392}
393
Yingdi Yu76dd8002013-12-24 11:16:32 +0800394shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800395ContactManager::getSignedSelfEndorseCertificate(const Profile& profile)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700396{
Varun Patil3d850902020-11-23 12:19:14 +0530397 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
398 .getDefaultKey().getDefaultCertificate();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700399 vector<string> endorseList;
Davide Pesavento7676b562020-12-14 00:41:26 -0500400 for (auto it = profile.begin(); it != profile.end(); it++)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700401 endorseList.push_back(it->first);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700402
Varun Patilb3d03b92021-01-08 17:09:32 +0530403 auto selfEndorseCertificate = std::make_shared<EndorseCertificate>(signCert, profile, endorseList);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700404
Varun Patil3d850902020-11-23 12:19:14 +0530405 m_keyChain.sign(*selfEndorseCertificate,
406 ndn::security::signingByIdentity(m_identity).setSignatureInfo(
407 selfEndorseCertificate->getSignatureInfo()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700408
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800409 return selfEndorseCertificate;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700410}
411
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700412void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800413ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700414{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800415 Name dnsName = m_identity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800416 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800417
Varun Patilb3d03b92021-01-08 17:09:32 +0530418 auto data = std::make_shared<Data>();
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700419 data->setName(dnsName);
420 data->setContent(selfEndorseCertificate.wireEncode());
421 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800422
Varun Patil3d850902020-11-23 12:19:14 +0530423 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800424
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700425 m_contactStorage->updateDnsSelfProfileData(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700426 m_face.put(*data);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700427}
428
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700429shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800430ContactManager::generateEndorseCertificate(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800431{
Varun Patil3d850902020-11-23 12:19:14 +0530432 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
433 .getDefaultKey().getDefaultCertificate();
434
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800435 shared_ptr<Contact> contact = getContact(identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700436 if (!static_cast<bool>(contact))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800437 return shared_ptr<EndorseCertificate>();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800438
Varun Patil3d850902020-11-23 12:19:14 +0530439 Name signerKeyName = m_identity;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800440
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700441 vector<string> endorseList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800442 m_contactStorage->getEndorseList(identity, endorseList);
443
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700444 shared_ptr<EndorseCertificate> cert =
445 shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800446 contact->getPublicKey(),
447 contact->getNotBefore(),
448 contact->getNotAfter(),
Varun Patil3d850902020-11-23 12:19:14 +0530449 signCert.getKeyId(),
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700450 signerKeyName,
451 contact->getProfile(),
452 endorseList));
Varun Patil3d850902020-11-23 12:19:14 +0530453 m_keyChain.sign(*cert,
454 ndn::security::signingByIdentity(m_identity)
455 .setSignatureInfo(cert->getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800456 return cert;
457
458}
459
460void
461ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate)
462{
Varun Patil3d850902020-11-23 12:19:14 +0530463 Name endorsee = endorseCertificate.getKeyName().getPrefix(-4);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800464 Name dnsName = m_identity;
465 dnsName.append("DNS")
466 .append(endorsee.wireEncode())
467 .append("ENDORSEE")
468 .appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800469
Varun Patilb3d03b92021-01-08 17:09:32 +0530470 auto data = std::make_shared<Data>();
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700471 data->setName(dnsName);
472 data->setContent(endorseCertificate.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530473 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800474
Varun Patil3d850902020-11-23 12:19:14 +0530475 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800476
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700477 m_contactStorage->updateDnsEndorseOthers(*data, dnsName.get(-3).toUri());
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700478 m_face.put(*data);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800479}
480
481void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800482ContactManager::sendInterest(const Interest& interest,
Varun Patil3d850902020-11-23 12:19:14 +0530483 const ndn::security::DataValidationSuccessCallback& onValidated,
484 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800485 const TimeoutNotify& timeoutNotify,
486 int retry /* = 1 */)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800487{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700488 m_face.expressInterest(interest,
489 bind(&ContactManager::onTargetData,
490 this, _1, _2, onValidated, onValidationFailed),
491 bind(&ContactManager::onTargetTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530492 this, _1, retry, onValidated, onValidationFailed, timeoutNotify),
493 bind(&ContactManager::onTargetTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700494 this, _1, retry, onValidated, onValidationFailed, timeoutNotify));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800495}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800496
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800497void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700498ContactManager::onTargetData(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800499 const Data& data,
Varun Patil3d850902020-11-23 12:19:14 +0530500 const ndn::security::DataValidationSuccessCallback& onValidated,
501 const ndn::security::DataValidationFailureCallback& onValidationFailed)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800502{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700503 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800504}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800505
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800506void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700507ContactManager::onTargetTimeout(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800508 int retry,
Varun Patil3d850902020-11-23 12:19:14 +0530509 const ndn::security::DataValidationSuccessCallback& onValidated,
510 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800511 const TimeoutNotify& timeoutNotify)
512{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700513 if (retry > 0)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800514 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
515 else
516 timeoutNotify(interest);
517}
518
519void
520ContactManager::onDnsInterest(const Name& prefix, const Interest& interest)
521{
522 const Name& interestName = interest.getName();
523 shared_ptr<Data> data;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700524
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700525 if (interestName.size() <= prefix.size())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800526 return;
527
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700528 if (interestName.size() == (prefix.size()+1)) {
529 data = m_contactStorage->getDnsData("N/A", interestName.get(prefix.size()).toUri());
530 if (static_cast<bool>(data))
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700531 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700532 return;
533 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800534
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700535 if (interestName.size() == (prefix.size()+2)) {
536 data = m_contactStorage->getDnsData(interestName.get(prefix.size()).toUri(),
537 interestName.get(prefix.size()+1).toUri());
538 if (static_cast<bool>(data))
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700539 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700540 return;
541 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800542}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700543
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800544void
Yingdi Yu17032f82014-03-25 15:48:23 -0700545ContactManager::onDnsRegisterFailed(const Name& prefix, const std::string& failInfo)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800546{
547 emit warning(QString(failInfo.c_str()));
548}
549
Varun Patil3d850902020-11-23 12:19:14 +0530550void
551ContactManager::onKeyInterest(const Name& prefix, const Interest& interest)
552{
553 const Name& interestName = interest.getName();
554 shared_ptr<Certificate> data;
555
556 try {
557 ndn::security::Certificate cert = m_keyChain.getPib()
558 .getIdentity(m_identity)
559 .getDefaultKey()
560 .getDefaultCertificate();
561 if (cert.getKeyName() == interestName)
562 return m_face.put(cert);
Varun Patila24bd3e2020-11-24 10:08:33 +0530563 } catch (const ndn::security::Pib::Error&) {}
Varun Patil3d850902020-11-23 12:19:14 +0530564
565 data = m_contactStorage->getSelfEndorseCertificate();
566 if (static_cast<bool>(data) && data->getKeyName().equals(interestName))
567 return m_face.put(*data);
568
569 data = m_contactStorage->getCollectEndorseByName(interestName);
570 if (static_cast<bool>(data))
571 return m_face.put(*data);
572}
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800573
574// public slots
575void
576ContactManager::onIdentityUpdated(const QString& identity)
577{
578 m_identity = Name(identity.toStdString());
579
Varun Patilb3d03b92021-01-08 17:09:32 +0530580 m_contactStorage = std::make_shared<ContactStorage>(m_identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800581
Varun Patila24bd3e2020-11-24 10:08:33 +0530582 m_dnsListenerHandle = m_face.setInterestFilter(
583 Name(m_identity).append("DNS"),
584 bind(&ContactManager::onDnsInterest, this, _1, _2),
585 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530586
Varun Patila24bd3e2020-11-24 10:08:33 +0530587 m_keyListenerHandle = m_face.setInterestFilter(
588 Name(m_identity).append("KEY"),
589 bind(&ContactManager::onKeyInterest, this, _1, _2),
590 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530591
Varun Patila24bd3e2020-11-24 10:08:33 +0530592 m_profileCertListenerHandle = m_face.setInterestFilter(
593 Name(m_identity).append("PROFILE-CERT"),
594 bind(&ContactManager::onKeyInterest, this, _1, _2),
595 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530596
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800597 m_contactList.clear();
598 m_contactStorage->getAllContacts(m_contactList);
599
600 m_bufferedContacts.clear();
Varun Patil3d850902020-11-23 12:19:14 +0530601 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800602
603 collectEndorsement();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800604}
605
Yingdi Yuae8217c2013-11-09 00:03:26 -0800606void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800607ContactManager::onFetchContactInfo(const QString& identity)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800608{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800609 // try to fetch self-endorse-certificate via DNS PROFILE first.
610 Name identityName(identity.toStdString());
611 Name interestName;
612 interestName.append(identityName).append("DNS").append("PROFILE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800613
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800614 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -0700615 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +0530616 interest.setCanBePrefix(true);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800617 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800618
Varun Patil3d850902020-11-23 12:19:14 +0530619 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700620 bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName);
Varun Patil3d850902020-11-23 12:19:14 +0530621 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700622 bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, _2, identityName);
623 TimeoutNotify timeoutNotify =
624 bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800625
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800626 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
627}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800628
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800629void
630ContactManager::onAddFetchedContact(const QString& identity)
631{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800632 Name identityName(identity.toStdString());
633
Davide Pesavento7676b562020-12-14 00:41:26 -0500634 auto it = m_bufferedContacts.find(identityName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700635 if (it != m_bufferedContacts.end()) {
636 Contact contact(*(it->second.m_selfEndorseCert));
Varun Patila24bd3e2020-11-24 10:08:33 +0530637
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700638 try {
639 m_contactStorage->addContact(contact);
640 m_bufferedContacts.erase(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800641
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700642 m_contactList.clear();
643 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800644
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700645 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800646 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530647 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700648 emit warning(QString::fromStdString(e.what()));
649 }
650 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800651 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700652 emit warning(QString("Failure: no information of %1").arg(identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800653}
654
655void
656ContactManager::onUpdateProfile()
657{
658 // Get current profile;
659 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700660 if (!static_cast<bool>(newProfile))
Yingdi Yuae8217c2013-11-09 00:03:26 -0800661 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800662
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700663 shared_ptr<EndorseCertificate> newEndorseCertificate =
664 getSignedSelfEndorseCertificate(*newProfile);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800665
666 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate);
667
668 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
669}
670
671void
672ContactManager::onRefreshBrowseContact()
673{
Varun Patil3d850902020-11-23 12:19:14 +0530674 return;
675
676#if 0
677 // The following no longer works as we don't serve such a list anymore
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700678 vector<string> bufferedIdCertNames;
679 try {
680 using namespace boost::asio::ip;
681 tcp::iostream request_stream;
Varun Patil3d850902020-11-23 12:19:14 +0530682 request_stream.expires_from_now(std::chrono::milliseconds(5000));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700683 request_stream.connect("ndncert.named-data.net","80");
684 if (!request_stream) {
685 emit warning(QString::fromStdString("Fail to fetch certificate directory! #1"));
686 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800687 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700688
689 request_stream << "GET /cert/list/ HTTP/1.0\r\n";
690 request_stream << "Host: ndncert.named-data.net\r\n\r\n";
691 request_stream.flush();
692
693 string line1;
694 std::getline(request_stream,line1);
695 if (!request_stream) {
696 emit warning(QString::fromStdString("Fail to fetch certificate directory! #2"));
697 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800698 }
699
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700700 std::stringstream response_stream(line1);
701 string http_version;
702 response_stream >> http_version;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800703 size_t status_code;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700704 response_stream >> status_code;
705 string status_message;
706 std::getline(response_stream,status_message);
707
708 if (!response_stream ||
709 http_version.substr(0,5) != "HTTP/") {
710 emit warning(QString::fromStdString("Fail to fetch certificate directory! #3"));
711 return;
712 }
713 if (status_code!=200) {
714 emit warning(QString::fromStdString("Fail to fetch certificate directory! #4"));
715 return;
716 }
717 vector<string> headers;
718 string header;
719 while (std::getline(request_stream, header) && header != "\r")
720 headers.push_back(header);
721
722 std::istreambuf_iterator<char> stream_iter (request_stream);
723 std::istreambuf_iterator<char> end_of_stream;
724
725 typedef boost::tokenizer< boost::escaped_list_separator<char>,
726 std::istreambuf_iterator<char> > tokenizer_t;
727 tokenizer_t certItems (stream_iter,
728 end_of_stream,
729 boost::escaped_list_separator<char>('\\', '\n', '"'));
730
731 for (tokenizer_t::iterator it = certItems.begin(); it != certItems.end (); it++)
732 if (!it->empty())
733 bufferedIdCertNames.push_back(*it);
734 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530735 catch (const std::exception& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700736 emit warning(QString::fromStdString("Fail to fetch certificate directory! #N"));
737 }
738
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800739 {
740 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
741 m_idCertCount = bufferedIdCertNames.size();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800742 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800743 m_bufferedIdCerts.clear();
744
Davide Pesavento7676b562020-12-14 00:41:26 -0500745 for (auto it = bufferedIdCertNames.begin(); it != bufferedIdCertNames.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700746 Name certName(*it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800747
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700748 Interest interest(certName);
749 interest.setInterestLifetime(time::milliseconds(1000));
750 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530751 interest.setCanBePrefix(true);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700752
Varun Patil3d850902020-11-23 12:19:14 +0530753 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700754 bind(&ContactManager::onIdentityCertValidated, this, _1);
Varun Patil3d850902020-11-23 12:19:14 +0530755 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700756 bind(&ContactManager::onIdentityCertValidationFailed, this, _1, _2);
757 TimeoutNotify timeoutNotify =
758 bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800759
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700760 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
761 }
Varun Patil3d850902020-11-23 12:19:14 +0530762#endif
Yingdi Yuae8217c2013-11-09 00:03:26 -0800763}
764
Yingdi Yu72232692013-11-12 17:50:21 -0800765void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800766ContactManager::onFetchIdCert(const QString& qCertName)
Yingdi Yu72232692013-11-12 17:50:21 -0800767{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800768 Name certName(qCertName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700769 if (m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end())
770 emit idCertReady(*m_bufferedIdCerts[certName]);
Yingdi Yu72232692013-11-12 17:50:21 -0800771}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800772
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800773void
774ContactManager::onAddFetchedContactIdCert(const QString& qCertName)
775{
776 Name certName(qCertName.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530777 Name identity = certName.getPrefix(-1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800778
Davide Pesavento7676b562020-12-14 00:41:26 -0500779 auto it = m_bufferedIdCerts.find(certName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700780 if (it != m_bufferedIdCerts.end()) {
781 Contact contact(*it->second);
782 try {
783 m_contactStorage->addContact(contact);
784 m_bufferedIdCerts.erase(certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800785
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700786 m_contactList.clear();
787 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800788
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700789 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800790 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530791 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700792 emit warning(QString::fromStdString(e.what()));
793 }
794 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800795 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700796 emit warning(QString("Failure: no information of %1")
797 .arg(QString::fromStdString(identity.toUri())));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800798}
799
800void
801ContactManager::onWaitForContactList()
802{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800803 QStringList aliasList;
804 QStringList idList;
Davide Pesavento7676b562020-12-14 00:41:26 -0500805 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700806 aliasList << QString((*it)->getAlias().c_str());
807 idList << QString((*it)->getNameSpace().toUri().c_str());
808 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700809
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800810 emit contactAliasListReady(aliasList);
811 emit contactIdListReady(idList);
812}
813
814void
815ContactManager::onWaitForContactInfo(const QString& identity)
816{
Davide Pesavento7676b562020-12-14 00:41:26 -0500817 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700818 if ((*it)->getNameSpace().toUri() == identity.toStdString())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800819 emit contactInfoReady(QString((*it)->getNameSpace().toUri().c_str()),
820 QString((*it)->getName().c_str()),
821 QString((*it)->getInstitution().c_str()),
822 (*it)->isIntroducer());
823}
824
825void
826ContactManager::onRemoveContact(const QString& identity)
827{
828 m_contactStorage->removeContact(Name(identity.toStdString()));
829 m_contactList.clear();
830 m_contactStorage->getAllContacts(m_contactList);
831
832 onWaitForContactList();
833}
834
835void
836ContactManager::onUpdateAlias(const QString& identity, const QString& alias)
837{
838 m_contactStorage->updateAlias(Name(identity.toStdString()), alias.toStdString());
839 m_contactList.clear();
840 m_contactStorage->getAllContacts(m_contactList);
841
842 onWaitForContactList();
843}
844
845void
846ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer)
847{
848 m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer);
849}
850
851void
852ContactManager::onUpdateEndorseCertificate(const QString& identity)
853{
854 Name identityName(identity.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530855 shared_ptr<Certificate> newEndorseCertificate = generateEndorseCertificate(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800856
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700857 if (!static_cast<bool>(newEndorseCertificate))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800858 return;
859
860 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName);
861
862 publishEndorseCertificateInDNS(*newEndorseCertificate);
863}
864
Yingdi Yueb692ac2015-02-10 18:46:18 -0800865} // namespace chronochat
Yingdi Yufa4ce792014-02-06 18:09:22 -0800866
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700867
868#if WAF
869#include "contact-manager.moc"
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700870#endif