blob: 5a6b4b93ff6ac356e0e61bc126cbe120b3a0f52d [file] [log] [blame]
Yingdi Yu9236c432013-10-18 11:29:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2013-2020, 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 Patil3d850902020-11-23 12:19:14 +053061 m_validator = make_shared<ndn::security::ValidatorConfig>(m_face);
62 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
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800116 shared_ptr<EndorseInfo> endorseInfo = 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());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700166 shared_ptr<EndorseCertificate> selfEndorseCertificate =
167 make_shared<EndorseCertificate>(boost::cref(plainData));
Varun Patil3d850902020-11-23 12:19:14 +0530168
169 if (ndn::security::verifySignature(plainData, *selfEndorseCertificate)) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700170 m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate;
171 fetchCollectEndorse(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800172 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700173 else
Varun Patil3d850902020-11-23 12:19:14 +0530174 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": verification failed"));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700175 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530176 catch (const Block::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530177 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": block error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700178 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530179 catch (const EndorseCertificate::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530180 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": cert error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700181 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530182 catch (const Data::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530183 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": data error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700184 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800185}
186
187void
Varun Patil3d850902020-11-23 12:19:14 +0530188ContactManager::onDnsSelfEndorseCertValidationFailed(const Data& data,
189 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800190 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
197void
198ContactManager::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
206void
Varun Patil3d850902020-11-23 12:19:14 +0530207ContactManager::onDnsCollectEndorseValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800208 const Name& identity)
209{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800210 try {
211 shared_ptr<EndorseCollection> endorseCollection =
Varun Patil3d850902020-11-23 12:19:14 +0530212 make_shared<EndorseCollection>(data.getContent().blockFromValue());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700213 m_bufferedContacts[identity].m_endorseCollection = endorseCollection;
214 fetchEndorseCertificateInternal(identity, 0);
215 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530216 catch (const std::runtime_error&) {
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800217 prepareEndorseInfo(identity);
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800218 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800219}
220
221void
Varun Patil3d850902020-11-23 12:19:14 +0530222ContactManager::onDnsCollectEndorseValidationFailed(const Data& data,
223 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800224 const Name& identity)
225{
226 prepareEndorseInfo(identity);
227}
228
229void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700230ContactManager::onDnsCollectEndorseTimeoutNotify(const Interest& interest, const Name& identity)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800231{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800232 prepareEndorseInfo(identity);
233}
234
235void
Davide Pesavento7676b562020-12-14 00:41:26 -0500236ContactManager::onEndorseCertificateInternal(const Interest&, const Data& data,
237 const Name& identity, size_t certIndex, string hash)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800238{
Davide Pesavento7676b562020-12-14 00:41:26 -0500239 std::ostringstream ss;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700240 {
Davide Pesavento7676b562020-12-14 00:41:26 -0500241 using namespace ndn::security::transform;
242 bufferSource(data.wireEncode().wire(), data.wireEncode().size())
243 >> digestFilter(ndn::DigestAlgorithm::SHA256)
244 >> streamSink(ss);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800245 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700246
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700247 if (ss.str() == hash) {
Davide Pesavento7676b562020-12-14 00:41:26 -0500248 auto endorseCertificate = make_shared<EndorseCertificate>(data);
249 m_bufferedContacts[identity].m_endorseCertList.push_back(std::move(endorseCertificate));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700250 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800251
252 fetchEndorseCertificateInternal(identity, certIndex+1);
253}
254
255void
256ContactManager::onEndorseCertificateInternalTimeout(const Interest& interest,
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700257 const Name& identity,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800258 size_t certIndex)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800259{
260 fetchEndorseCertificateInternal(identity, certIndex+1);
261}
262
263void
264ContactManager::collectEndorsement()
265{
266 {
267 boost::recursive_mutex::scoped_lock lock(m_collectCountMutex);
268 m_collectCount = m_contactList.size();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700269
Davide Pesavento7676b562020-12-14 00:41:26 -0500270 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700271 Name interestName = (*it)->getNameSpace();
272 interestName.append("DNS").append(m_identity.wireEncode()).append("ENDORSEE");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800273
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700274 Interest interest(interestName);
Varun Patil3d850902020-11-23 12:19:14 +0530275 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530276 interest.setCanBePrefix(true);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700277 interest.setInterestLifetime(time::milliseconds(1000));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700278
Varun Patil3d850902020-11-23 12:19:14 +0530279 ndn::security::DataValidationSuccessCallback onValidated =
280 bind(&ContactManager::onDnsEndorseeValidated, this, _1);
281 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700282 bind(&ContactManager::onDnsEndorseeValidationFailed, this, _1, _2);
283 TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsEndorseeTimeoutNotify, this, _1);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700284
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700285 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
286 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800287 }
288}
289
290void
Varun Patil3d850902020-11-23 12:19:14 +0530291ContactManager::onDnsEndorseeValidated(const Data& data)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800292{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800293 Data endorseData;
Varun Patil3d850902020-11-23 12:19:14 +0530294 endorseData.wireDecode(data.getContent().blockFromValue());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800295
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800296 EndorseCertificate endorseCertificate(endorseData);
297 m_contactStorage->updateCollectEndorse(endorseCertificate);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800298
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800299 decreaseCollectStatus();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800300}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800301
302void
Varun Patil3d850902020-11-23 12:19:14 +0530303ContactManager::onDnsEndorseeValidationFailed(const Data& data,
304 const ndn::security::ValidationError& error)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700305{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800306 decreaseCollectStatus();
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700307}
308
Yingdi Yub2e747d2013-11-05 23:06:43 -0800309void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800310ContactManager::onDnsEndorseeTimeoutNotify(const Interest& interest)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800311{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800312 decreaseCollectStatus();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800313}
314
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700315void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800316ContactManager::decreaseCollectStatus()
Yingdi Yub2e747d2013-11-05 23:06:43 -0800317{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800318 size_t count;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800319 {
320 boost::recursive_mutex::scoped_lock lock(m_collectCountMutex);
321 m_collectCount--;
322 count = m_collectCount;
323 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800324
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800325 if(count == 0)
326 publishCollectEndorsedDataInDNS();
327}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800328
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800329void
330ContactManager::publishCollectEndorsedDataInDNS()
331{
332 Name dnsName = m_identity;
333 dnsName.append("DNS").append("ENDORSED").appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800334
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700335 shared_ptr<Data> data = make_shared<Data>();
336 data->setName(dnsName);
Varun Patil3d850902020-11-23 12:19:14 +0530337 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800338
339 EndorseCollection endorseCollection;
340 m_contactStorage->getCollectEndorse(endorseCollection);
341
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800342 data->setContent(endorseCollection.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530343
344 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800345
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700346 m_contactStorage->updateDnsOthersEndorse(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700347 m_face.put(*data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800348}
349
350void
Varun Patil3d850902020-11-23 12:19:14 +0530351ContactManager::onIdentityCertValidated(const Data& data)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800352{
Varun Patil3d850902020-11-23 12:19:14 +0530353 shared_ptr<Certificate> cert = make_shared<Certificate>(boost::cref(data));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800354 m_bufferedIdCerts[cert->getName()] = cert;
355 decreaseIdCertCount();
356}
357
358void
Varun Patil3d850902020-11-23 12:19:14 +0530359ContactManager::onIdentityCertValidationFailed(const Data& data,
360 const ndn::security::ValidationError& error)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800361{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800362 decreaseIdCertCount();
363}
364
365void
366ContactManager::onIdentityCertTimeoutNotify(const Interest& interest)
367{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800368 decreaseIdCertCount();
369}
370
371void
372ContactManager::decreaseIdCertCount()
373{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800374 size_t count;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800375 {
376 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
377 m_idCertCount--;
378 count = m_idCertCount;
379 }
380
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700381 if (count == 0) {
382 QStringList certNameList;
383 QStringList nameList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800384
Davide Pesavento7676b562020-12-14 00:41:26 -0500385 for (auto it = m_bufferedIdCerts.begin(); it != m_bufferedIdCerts.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700386 certNameList << QString::fromStdString(it->second->getName().toUri());
387 Profile profile(*(it->second));
388 nameList << QString::fromStdString(profile.get("name"));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800389 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700390
391 emit idCertNameListReady(certNameList);
392 emit nameListReady(nameList);
393 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800394}
395
Yingdi Yu76dd8002013-12-24 11:16:32 +0800396shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800397ContactManager::getSignedSelfEndorseCertificate(const Profile& profile)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700398{
Varun Patil3d850902020-11-23 12:19:14 +0530399 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
400 .getDefaultKey().getDefaultCertificate();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700401 vector<string> endorseList;
Davide Pesavento7676b562020-12-14 00:41:26 -0500402 for (auto it = profile.begin(); it != profile.end(); it++)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700403 endorseList.push_back(it->first);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700404
405 shared_ptr<EndorseCertificate> selfEndorseCertificate =
Varun Patil3d850902020-11-23 12:19:14 +0530406 make_shared<EndorseCertificate>(boost::cref(signCert),
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700407 boost::cref(profile),
408 boost::cref(endorseList));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700409
Varun Patil3d850902020-11-23 12:19:14 +0530410 m_keyChain.sign(*selfEndorseCertificate,
411 ndn::security::signingByIdentity(m_identity).setSignatureInfo(
412 selfEndorseCertificate->getSignatureInfo()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700413
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800414 return selfEndorseCertificate;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700415}
416
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700417void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800418ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700419{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800420 Name dnsName = m_identity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800421 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800422
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700423 shared_ptr<Data> data = make_shared<Data>();
424 data->setName(dnsName);
425 data->setContent(selfEndorseCertificate.wireEncode());
426 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800427
Varun Patil3d850902020-11-23 12:19:14 +0530428 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800429
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700430 m_contactStorage->updateDnsSelfProfileData(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700431 m_face.put(*data);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700432}
433
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700434shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800435ContactManager::generateEndorseCertificate(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800436{
Varun Patil3d850902020-11-23 12:19:14 +0530437 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
438 .getDefaultKey().getDefaultCertificate();
439
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800440 shared_ptr<Contact> contact = getContact(identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700441 if (!static_cast<bool>(contact))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800442 return shared_ptr<EndorseCertificate>();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800443
Varun Patil3d850902020-11-23 12:19:14 +0530444 Name signerKeyName = m_identity;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800445
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700446 vector<string> endorseList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800447 m_contactStorage->getEndorseList(identity, endorseList);
448
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700449 shared_ptr<EndorseCertificate> cert =
450 shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800451 contact->getPublicKey(),
452 contact->getNotBefore(),
453 contact->getNotAfter(),
Varun Patil3d850902020-11-23 12:19:14 +0530454 signCert.getKeyId(),
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700455 signerKeyName,
456 contact->getProfile(),
457 endorseList));
Varun Patil3d850902020-11-23 12:19:14 +0530458 m_keyChain.sign(*cert,
459 ndn::security::signingByIdentity(m_identity)
460 .setSignatureInfo(cert->getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800461 return cert;
462
463}
464
465void
466ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate)
467{
Varun Patil3d850902020-11-23 12:19:14 +0530468 Name endorsee = endorseCertificate.getKeyName().getPrefix(-4);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800469 Name dnsName = m_identity;
470 dnsName.append("DNS")
471 .append(endorsee.wireEncode())
472 .append("ENDORSEE")
473 .appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800474
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700475 shared_ptr<Data> data = make_shared<Data>();
476 data->setName(dnsName);
477 data->setContent(endorseCertificate.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530478 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800479
Varun Patil3d850902020-11-23 12:19:14 +0530480 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800481
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700482 m_contactStorage->updateDnsEndorseOthers(*data, dnsName.get(-3).toUri());
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700483 m_face.put(*data);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800484}
485
486void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800487ContactManager::sendInterest(const Interest& interest,
Varun Patil3d850902020-11-23 12:19:14 +0530488 const ndn::security::DataValidationSuccessCallback& onValidated,
489 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800490 const TimeoutNotify& timeoutNotify,
491 int retry /* = 1 */)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800492{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700493 m_face.expressInterest(interest,
494 bind(&ContactManager::onTargetData,
495 this, _1, _2, onValidated, onValidationFailed),
496 bind(&ContactManager::onTargetTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530497 this, _1, retry, onValidated, onValidationFailed, timeoutNotify),
498 bind(&ContactManager::onTargetTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700499 this, _1, retry, onValidated, onValidationFailed, timeoutNotify));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800500}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800501
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800502void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700503ContactManager::onTargetData(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800504 const Data& data,
Varun Patil3d850902020-11-23 12:19:14 +0530505 const ndn::security::DataValidationSuccessCallback& onValidated,
506 const ndn::security::DataValidationFailureCallback& onValidationFailed)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800507{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700508 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800509}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800510
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800511void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700512ContactManager::onTargetTimeout(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800513 int retry,
Varun Patil3d850902020-11-23 12:19:14 +0530514 const ndn::security::DataValidationSuccessCallback& onValidated,
515 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800516 const TimeoutNotify& timeoutNotify)
517{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700518 if (retry > 0)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800519 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
520 else
521 timeoutNotify(interest);
522}
523
524void
525ContactManager::onDnsInterest(const Name& prefix, const Interest& interest)
526{
527 const Name& interestName = interest.getName();
528 shared_ptr<Data> data;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700529
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700530 if (interestName.size() <= prefix.size())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800531 return;
532
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700533 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 Yu2c9e7712014-10-20 11:55:05 -0700536 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700537 return;
538 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800539
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700540 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 Yu2c9e7712014-10-20 11:55:05 -0700544 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700545 return;
546 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800547}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700548
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800549void
Yingdi Yu17032f82014-03-25 15:48:23 -0700550ContactManager::onDnsRegisterFailed(const Name& prefix, const std::string& failInfo)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800551{
552 emit warning(QString(failInfo.c_str()));
553}
554
Varun Patil3d850902020-11-23 12:19:14 +0530555void
556ContactManager::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 Patila24bd3e2020-11-24 10:08:33 +0530568 } catch (const ndn::security::Pib::Error&) {}
Varun Patil3d850902020-11-23 12:19:14 +0530569
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 Yu348f5ea2014-03-01 14:47:25 -0800578
579// public slots
580void
581ContactManager::onIdentityUpdated(const QString& identity)
582{
583 m_identity = Name(identity.toStdString());
584
585 m_contactStorage = make_shared<ContactStorage>(m_identity);
586
Varun Patila24bd3e2020-11-24 10:08:33 +0530587 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 Patil3d850902020-11-23 12:19:14 +0530591
Varun Patila24bd3e2020-11-24 10:08:33 +0530592 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 Patil3d850902020-11-23 12:19:14 +0530596
Varun Patila24bd3e2020-11-24 10:08:33 +0530597 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 Patil3d850902020-11-23 12:19:14 +0530601
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800602 m_contactList.clear();
603 m_contactStorage->getAllContacts(m_contactList);
604
605 m_bufferedContacts.clear();
Varun Patil3d850902020-11-23 12:19:14 +0530606 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800607
608 collectEndorsement();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800609}
610
Yingdi Yuae8217c2013-11-09 00:03:26 -0800611void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800612ContactManager::onFetchContactInfo(const QString& identity)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800613{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800614 // 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 Yu76dd8002013-12-24 11:16:32 +0800618
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800619 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -0700620 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +0530621 interest.setCanBePrefix(true);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800622 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800623
Varun Patil3d850902020-11-23 12:19:14 +0530624 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700625 bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName);
Varun Patil3d850902020-11-23 12:19:14 +0530626 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700627 bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, _2, identityName);
628 TimeoutNotify timeoutNotify =
629 bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800630
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800631 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
632}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800633
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800634void
635ContactManager::onAddFetchedContact(const QString& identity)
636{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800637 Name identityName(identity.toStdString());
638
Davide Pesavento7676b562020-12-14 00:41:26 -0500639 auto it = m_bufferedContacts.find(identityName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700640 if (it != m_bufferedContacts.end()) {
641 Contact contact(*(it->second.m_selfEndorseCert));
Varun Patila24bd3e2020-11-24 10:08:33 +0530642
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700643 try {
644 m_contactStorage->addContact(contact);
645 m_bufferedContacts.erase(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800646
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700647 m_contactList.clear();
648 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800649
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700650 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800651 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530652 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700653 emit warning(QString::fromStdString(e.what()));
654 }
655 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800656 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700657 emit warning(QString("Failure: no information of %1").arg(identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800658}
659
660void
661ContactManager::onUpdateProfile()
662{
663 // Get current profile;
664 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700665 if (!static_cast<bool>(newProfile))
Yingdi Yuae8217c2013-11-09 00:03:26 -0800666 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800667
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700668 shared_ptr<EndorseCertificate> newEndorseCertificate =
669 getSignedSelfEndorseCertificate(*newProfile);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800670
671 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate);
672
673 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
674}
675
676void
677ContactManager::onRefreshBrowseContact()
678{
Varun Patil3d850902020-11-23 12:19:14 +0530679 return;
680
681#if 0
682 // The following no longer works as we don't serve such a list anymore
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700683 vector<string> bufferedIdCertNames;
684 try {
685 using namespace boost::asio::ip;
686 tcp::iostream request_stream;
Varun Patil3d850902020-11-23 12:19:14 +0530687 request_stream.expires_from_now(std::chrono::milliseconds(5000));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700688 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 Yu348f5ea2014-03-01 14:47:25 -0800692 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700693
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 Yu348f5ea2014-03-01 14:47:25 -0800703 }
704
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700705 std::stringstream response_stream(line1);
706 string http_version;
707 response_stream >> http_version;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800708 size_t status_code;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700709 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 Patila24bd3e2020-11-24 10:08:33 +0530740 catch (const std::exception& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700741 emit warning(QString::fromStdString("Fail to fetch certificate directory! #N"));
742 }
743
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800744 {
745 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
746 m_idCertCount = bufferedIdCertNames.size();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800747 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800748 m_bufferedIdCerts.clear();
749
Davide Pesavento7676b562020-12-14 00:41:26 -0500750 for (auto it = bufferedIdCertNames.begin(); it != bufferedIdCertNames.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700751 Name certName(*it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800752
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700753 Interest interest(certName);
754 interest.setInterestLifetime(time::milliseconds(1000));
755 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530756 interest.setCanBePrefix(true);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700757
Varun Patil3d850902020-11-23 12:19:14 +0530758 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700759 bind(&ContactManager::onIdentityCertValidated, this, _1);
Varun Patil3d850902020-11-23 12:19:14 +0530760 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700761 bind(&ContactManager::onIdentityCertValidationFailed, this, _1, _2);
762 TimeoutNotify timeoutNotify =
763 bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800764
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700765 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
766 }
Varun Patil3d850902020-11-23 12:19:14 +0530767#endif
Yingdi Yuae8217c2013-11-09 00:03:26 -0800768}
769
Yingdi Yu72232692013-11-12 17:50:21 -0800770void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800771ContactManager::onFetchIdCert(const QString& qCertName)
Yingdi Yu72232692013-11-12 17:50:21 -0800772{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800773 Name certName(qCertName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700774 if (m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end())
775 emit idCertReady(*m_bufferedIdCerts[certName]);
Yingdi Yu72232692013-11-12 17:50:21 -0800776}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800777
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800778void
779ContactManager::onAddFetchedContactIdCert(const QString& qCertName)
780{
781 Name certName(qCertName.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530782 Name identity = certName.getPrefix(-1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800783
Davide Pesavento7676b562020-12-14 00:41:26 -0500784 auto it = m_bufferedIdCerts.find(certName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700785 if (it != m_bufferedIdCerts.end()) {
786 Contact contact(*it->second);
787 try {
788 m_contactStorage->addContact(contact);
789 m_bufferedIdCerts.erase(certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800790
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700791 m_contactList.clear();
792 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800793
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700794 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800795 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530796 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700797 emit warning(QString::fromStdString(e.what()));
798 }
799 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800800 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700801 emit warning(QString("Failure: no information of %1")
802 .arg(QString::fromStdString(identity.toUri())));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800803}
804
805void
806ContactManager::onWaitForContactList()
807{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800808 QStringList aliasList;
809 QStringList idList;
Davide Pesavento7676b562020-12-14 00:41:26 -0500810 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700811 aliasList << QString((*it)->getAlias().c_str());
812 idList << QString((*it)->getNameSpace().toUri().c_str());
813 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700814
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800815 emit contactAliasListReady(aliasList);
816 emit contactIdListReady(idList);
817}
818
819void
820ContactManager::onWaitForContactInfo(const QString& identity)
821{
Davide Pesavento7676b562020-12-14 00:41:26 -0500822 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700823 if ((*it)->getNameSpace().toUri() == identity.toStdString())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800824 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
830void
831ContactManager::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
840void
841ContactManager::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
850void
851ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer)
852{
853 m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer);
854}
855
856void
857ContactManager::onUpdateEndorseCertificate(const QString& identity)
858{
859 Name identityName(identity.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530860 shared_ptr<Certificate> newEndorseCertificate = generateEndorseCertificate(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800861
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700862 if (!static_cast<bool>(newEndorseCertificate))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800863 return;
864
865 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName);
866
867 publishEndorseCertificateInDNS(*newEndorseCertificate);
868}
869
Yingdi Yueb692ac2015-02-10 18:46:18 -0800870} // namespace chronochat
Yingdi Yufa4ce792014-02-06 18:09:22 -0800871
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700872
873#if WAF
874#include "contact-manager.moc"
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700875#endif