blob: db59560ff96bd93a09e24dec70ae43110060e0ca [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,
Varun Patil074e38d2021-01-05 17:38:53 +053047 ndn::KeyChain& keyChain,
Yingdi Yufa4ce792014-02-06 18:09:22 -080048 QObject* parent)
49 : QObject(parent)
Yingdi Yufa4ce792014-02-06 18:09:22 -080050 , m_face(face)
Varun Patil074e38d2021-01-05 17:38:53 +053051 , m_keyChain(keyChain)
Yingdi Yufa4ce792014-02-06 18:09:22 -080052{
Yingdi Yu76dd8002013-12-24 11:16:32 +080053 initializeSecurity();
Yingdi Yu9236c432013-10-18 11:29:25 -070054}
55
56ContactManager::~ContactManager()
Yingdi Yu0b0a7362014-08-05 16:31:30 -070057{
58}
Yingdi Yu9236c432013-10-18 11:29:25 -070059
Yingdi Yu76dd8002013-12-24 11:16:32 +080060void
61ContactManager::initializeSecurity()
62{
Varun Patilb3d03b92021-01-08 17:09:32 +053063 m_validator = std::make_shared<ndn::security::ValidatorConfig>(m_face);
Varun Patil3d850902020-11-23 12:19:14 +053064 m_validator->load("security/validation-contact-manager.conf");
Yingdi Yu76dd8002013-12-24 11:16:32 +080065}
66
67void
Yingdi Yufa4ce792014-02-06 18:09:22 -080068ContactManager::fetchCollectEndorse(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -080069{
70 Name interestName = identity;
71 interestName.append("DNS").append("ENDORSED");
72
Yingdi Yu76dd8002013-12-24 11:16:32 +080073 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -070074 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patil3d850902020-11-23 12:19:14 +053075 interest.setCanBePrefix(true);
Yingdi Yufa4ce792014-02-06 18:09:22 -080076 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +080077
Varun Patil3d850902020-11-23 12:19:14 +053078 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -070079 bind(&ContactManager::onDnsCollectEndorseValidated, this, _1, identity);
Varun Patil3d850902020-11-23 12:19:14 +053080 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -070081 bind(&ContactManager::onDnsCollectEndorseValidationFailed, this, _1, _2, identity);
82 TimeoutNotify timeoutNotify =
83 bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, _1, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080084
Yingdi Yu348f5ea2014-03-01 14:47:25 -080085 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
Yingdi Yub2e747d2013-11-05 23:06:43 -080086}
87
88void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080089ContactManager::fetchEndorseCertificateInternal(const Name& identity, size_t certIndex)
Yingdi Yub2e747d2013-11-05 23:06:43 -080090{
Davide Pesavento7676b562020-12-14 00:41:26 -050091 auto endorseCollection = m_bufferedContacts[identity].m_endorseCollection;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092
Davide Pesavento7676b562020-12-14 00:41:26 -050093 if (certIndex >= endorseCollection->getCollectionEntries().size())
Varun Patil3d850902020-11-23 12:19:14 +053094 return prepareEndorseInfo(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080095
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080096 Name interestName(endorseCollection->getCollectionEntries()[certIndex].certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080097
Yingdi Yu76dd8002013-12-24 11:16:32 +080098 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -070099 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +0530100 interest.setCanBePrefix(true);
Varun Patil3d850902020-11-23 12:19:14 +0530101 interest.setMustBeFresh(false);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800102
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700103 m_face.expressInterest(interest,
104 bind(&ContactManager::onEndorseCertificateInternal,
105 this, _1, _2, identity, certIndex,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800106 endorseCollection->getCollectionEntries()[certIndex].hash),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700107 bind(&ContactManager::onEndorseCertificateInternalTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530108 this, _1, identity, certIndex),
109 bind(&ContactManager::onEndorseCertificateInternalTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700110 this, _1, identity, certIndex));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800111}
112
Yingdi Yu76dd8002013-12-24 11:16:32 +0800113void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114ContactManager::prepareEndorseInfo(const Name& identity)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800115{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800116 const Profile& profile = m_bufferedContacts[identity].m_selfEndorseCert->getProfile();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800117
Varun Patilb3d03b92021-01-08 17:09:32 +0530118 auto endorseInfo = std::make_shared<EndorseInfo>();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800119 m_bufferedContacts[identity].m_endorseInfo = endorseInfo;
120
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800121 map<string, size_t> endorseCount;
Davide Pesavento7676b562020-12-14 00:41:26 -0500122 for (auto pIt = profile.begin(); pIt != profile.end(); pIt++)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700123 endorseCount[pIt->first] = 0;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800124
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800125 size_t endorseCertCount = 0;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800126
Davide Pesavento7676b562020-12-14 00:41:26 -0500127 auto cIt = m_bufferedContacts[identity].m_endorseCertList.cbegin();
128 auto cEnd = m_bufferedContacts[identity].m_endorseCertList.cend();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800129
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700130 for (; cIt != cEnd; cIt++, endorseCertCount++) {
Varun Patil3d850902020-11-23 12:19:14 +0530131 shared_ptr<Contact> contact = getContact((*cIt)->getSigner());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700132 if (!static_cast<bool>(contact))
133 continue;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700134
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700135 if (!contact->isIntroducer() ||
136 !contact->canBeTrustedFor(profile.getIdentityName()))
137 continue;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800138
Varun Patil3d850902020-11-23 12:19:14 +0530139 if (!(*cIt)->isValid())
140 continue;
141
142 if (!ndn::security::verifySignature(**cIt, contact->getPublicKey().data(), contact->getPublicKey().size()))
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700143 continue;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800144
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700145 const Profile& tmpProfile = (*cIt)->getProfile();
Davide Pesavento7676b562020-12-14 00:41:26 -0500146 const auto& endorseList = (*cIt)->getEndorseList();
147 for (auto eIt = endorseList.begin(); eIt != endorseList.end(); eIt++)
Varun Patil3d850902020-11-23 12:19:14 +0530148 if (tmpProfile.get(*eIt) == profile.get(*eIt))
149 endorseCount[*eIt] += 1;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700150 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800151
Davide Pesavento7676b562020-12-14 00:41:26 -0500152 for (auto pIt = profile.begin(); pIt != profile.end(); pIt++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700153 std::stringstream ss;
154 ss << endorseCount[pIt->first] << "/" << endorseCertCount;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800155 endorseInfo->addEndorsement(pIt->first, pIt->second, ss.str());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700156 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800157
158 emit contactEndorseInfoReady (*endorseInfo);
159}
160
161void
Varun Patil3d850902020-11-23 12:19:14 +0530162ContactManager::onDnsSelfEndorseCertValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800163 const Name& identity)
164{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700165 try {
166 Data plainData;
Varun Patil3d850902020-11-23 12:19:14 +0530167 plainData.wireDecode(data.getContent().blockFromValue());
Varun Patilb3d03b92021-01-08 17:09:32 +0530168 auto selfEndorseCertificate = std::make_shared<EndorseCertificate>(plainData);
Varun Patil3d850902020-11-23 12:19:14 +0530169
170 if (ndn::security::verifySignature(plainData, *selfEndorseCertificate)) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700171 m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate;
172 fetchCollectEndorse(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800173 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700174 else
Varun Patil3d850902020-11-23 12:19:14 +0530175 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": verification failed"));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700176 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530177 catch (const Block::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530178 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": block error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700179 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530180 catch (const EndorseCertificate::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530181 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": cert error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700182 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530183 catch (const Data::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530184 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": data error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700185 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800186}
187
188void
Varun Patil3d850902020-11-23 12:19:14 +0530189ContactManager::onDnsSelfEndorseCertValidationFailed(const Data& data,
190 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800191 const Name& identity)
192{
193 // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert,
194 // but let's stay with failure for now.
195 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri()));
196}
197
198void
199ContactManager::onDnsSelfEndorseCertTimeoutNotify(const Interest& interest,
200 const Name& identity)
201{
202 // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert,
203 // but let's stay with failure for now.
204 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri()));
205}
206
207void
Varun Patil3d850902020-11-23 12:19:14 +0530208ContactManager::onDnsCollectEndorseValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800209 const Name& identity)
210{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800211 try {
Varun Patilb3d03b92021-01-08 17:09:32 +0530212 auto endorseCollection = std::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) {
Varun Patilb3d03b92021-01-08 17:09:32 +0530248 auto endorseCertificate = std::make_shared<EndorseCertificate>(data);
Davide Pesavento7676b562020-12-14 00:41:26 -0500249 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
Varun Patilb3d03b92021-01-08 17:09:32 +0530335 auto data = std::make_shared<Data>();
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700336 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 Patilb3d03b92021-01-08 17:09:32 +0530353 auto cert = std::make_shared<Certificate>(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
Varun Patilb3d03b92021-01-08 17:09:32 +0530405 auto selfEndorseCertificate = std::make_shared<EndorseCertificate>(signCert, profile, endorseList);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700406
Varun Patil3d850902020-11-23 12:19:14 +0530407 m_keyChain.sign(*selfEndorseCertificate,
408 ndn::security::signingByIdentity(m_identity).setSignatureInfo(
409 selfEndorseCertificate->getSignatureInfo()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700410
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800411 return selfEndorseCertificate;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700412}
413
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700414void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800415ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700416{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800417 Name dnsName = m_identity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800418 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800419
Varun Patilb3d03b92021-01-08 17:09:32 +0530420 auto data = std::make_shared<Data>();
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700421 data->setName(dnsName);
422 data->setContent(selfEndorseCertificate.wireEncode());
423 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800424
Varun Patil3d850902020-11-23 12:19:14 +0530425 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800426
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700427 m_contactStorage->updateDnsSelfProfileData(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700428 m_face.put(*data);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700429}
430
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700431shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800432ContactManager::generateEndorseCertificate(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800433{
Varun Patil3d850902020-11-23 12:19:14 +0530434 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
435 .getDefaultKey().getDefaultCertificate();
436
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800437 shared_ptr<Contact> contact = getContact(identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700438 if (!static_cast<bool>(contact))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800439 return shared_ptr<EndorseCertificate>();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800440
Varun Patil3d850902020-11-23 12:19:14 +0530441 Name signerKeyName = m_identity;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800442
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700443 vector<string> endorseList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800444 m_contactStorage->getEndorseList(identity, endorseList);
445
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700446 shared_ptr<EndorseCertificate> cert =
447 shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800448 contact->getPublicKey(),
449 contact->getNotBefore(),
450 contact->getNotAfter(),
Varun Patil3d850902020-11-23 12:19:14 +0530451 signCert.getKeyId(),
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700452 signerKeyName,
453 contact->getProfile(),
454 endorseList));
Varun Patil3d850902020-11-23 12:19:14 +0530455 m_keyChain.sign(*cert,
456 ndn::security::signingByIdentity(m_identity)
457 .setSignatureInfo(cert->getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800458 return cert;
459
460}
461
462void
463ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate)
464{
Varun Patil3d850902020-11-23 12:19:14 +0530465 Name endorsee = endorseCertificate.getKeyName().getPrefix(-4);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800466 Name dnsName = m_identity;
467 dnsName.append("DNS")
468 .append(endorsee.wireEncode())
469 .append("ENDORSEE")
470 .appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800471
Varun Patilb3d03b92021-01-08 17:09:32 +0530472 auto data = std::make_shared<Data>();
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700473 data->setName(dnsName);
474 data->setContent(endorseCertificate.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530475 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800476
Varun Patil3d850902020-11-23 12:19:14 +0530477 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800478
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700479 m_contactStorage->updateDnsEndorseOthers(*data, dnsName.get(-3).toUri());
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700480 m_face.put(*data);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800481}
482
483void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800484ContactManager::sendInterest(const Interest& interest,
Varun Patil3d850902020-11-23 12:19:14 +0530485 const ndn::security::DataValidationSuccessCallback& onValidated,
486 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800487 const TimeoutNotify& timeoutNotify,
488 int retry /* = 1 */)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800489{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700490 m_face.expressInterest(interest,
491 bind(&ContactManager::onTargetData,
492 this, _1, _2, onValidated, onValidationFailed),
493 bind(&ContactManager::onTargetTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530494 this, _1, retry, onValidated, onValidationFailed, timeoutNotify),
495 bind(&ContactManager::onTargetTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700496 this, _1, retry, onValidated, onValidationFailed, timeoutNotify));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800497}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800498
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800499void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700500ContactManager::onTargetData(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800501 const Data& data,
Varun Patil3d850902020-11-23 12:19:14 +0530502 const ndn::security::DataValidationSuccessCallback& onValidated,
503 const ndn::security::DataValidationFailureCallback& onValidationFailed)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800504{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700505 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800506}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800507
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800508void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700509ContactManager::onTargetTimeout(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800510 int retry,
Varun Patil3d850902020-11-23 12:19:14 +0530511 const ndn::security::DataValidationSuccessCallback& onValidated,
512 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800513 const TimeoutNotify& timeoutNotify)
514{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700515 if (retry > 0)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800516 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
517 else
518 timeoutNotify(interest);
519}
520
521void
522ContactManager::onDnsInterest(const Name& prefix, const Interest& interest)
523{
524 const Name& interestName = interest.getName();
525 shared_ptr<Data> data;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700526
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700527 if (interestName.size() <= prefix.size())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800528 return;
529
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700530 if (interestName.size() == (prefix.size()+1)) {
531 data = m_contactStorage->getDnsData("N/A", interestName.get(prefix.size()).toUri());
532 if (static_cast<bool>(data))
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700533 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700534 return;
535 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800536
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700537 if (interestName.size() == (prefix.size()+2)) {
538 data = m_contactStorage->getDnsData(interestName.get(prefix.size()).toUri(),
539 interestName.get(prefix.size()+1).toUri());
540 if (static_cast<bool>(data))
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700541 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700542 return;
543 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800544}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700545
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800546void
Yingdi Yu17032f82014-03-25 15:48:23 -0700547ContactManager::onDnsRegisterFailed(const Name& prefix, const std::string& failInfo)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800548{
549 emit warning(QString(failInfo.c_str()));
550}
551
Varun Patil3d850902020-11-23 12:19:14 +0530552void
553ContactManager::onKeyInterest(const Name& prefix, const Interest& interest)
554{
555 const Name& interestName = interest.getName();
556 shared_ptr<Certificate> data;
557
558 try {
559 ndn::security::Certificate cert = m_keyChain.getPib()
560 .getIdentity(m_identity)
561 .getDefaultKey()
562 .getDefaultCertificate();
563 if (cert.getKeyName() == interestName)
564 return m_face.put(cert);
Varun Patila24bd3e2020-11-24 10:08:33 +0530565 } catch (const ndn::security::Pib::Error&) {}
Varun Patil3d850902020-11-23 12:19:14 +0530566
567 data = m_contactStorage->getSelfEndorseCertificate();
568 if (static_cast<bool>(data) && data->getKeyName().equals(interestName))
569 return m_face.put(*data);
570
571 data = m_contactStorage->getCollectEndorseByName(interestName);
572 if (static_cast<bool>(data))
573 return m_face.put(*data);
574}
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800575
576// public slots
577void
578ContactManager::onIdentityUpdated(const QString& identity)
579{
580 m_identity = Name(identity.toStdString());
581
Varun Patilb3d03b92021-01-08 17:09:32 +0530582 m_contactStorage = std::make_shared<ContactStorage>(m_identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800583
Varun Patila24bd3e2020-11-24 10:08:33 +0530584 m_dnsListenerHandle = m_face.setInterestFilter(
585 Name(m_identity).append("DNS"),
586 bind(&ContactManager::onDnsInterest, this, _1, _2),
587 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530588
Varun Patila24bd3e2020-11-24 10:08:33 +0530589 m_keyListenerHandle = m_face.setInterestFilter(
590 Name(m_identity).append("KEY"),
591 bind(&ContactManager::onKeyInterest, this, _1, _2),
592 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530593
Varun Patila24bd3e2020-11-24 10:08:33 +0530594 m_profileCertListenerHandle = m_face.setInterestFilter(
595 Name(m_identity).append("PROFILE-CERT"),
596 bind(&ContactManager::onKeyInterest, this, _1, _2),
597 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530598
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800599 m_contactList.clear();
600 m_contactStorage->getAllContacts(m_contactList);
601
602 m_bufferedContacts.clear();
Varun Patil3d850902020-11-23 12:19:14 +0530603 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800604
605 collectEndorsement();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800606}
607
Yingdi Yuae8217c2013-11-09 00:03:26 -0800608void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800609ContactManager::onFetchContactInfo(const QString& identity)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800610{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800611 // try to fetch self-endorse-certificate via DNS PROFILE first.
612 Name identityName(identity.toStdString());
613 Name interestName;
614 interestName.append(identityName).append("DNS").append("PROFILE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800615
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800616 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -0700617 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +0530618 interest.setCanBePrefix(true);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800619 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800620
Varun Patil3d850902020-11-23 12:19:14 +0530621 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700622 bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName);
Varun Patil3d850902020-11-23 12:19:14 +0530623 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700624 bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, _2, identityName);
625 TimeoutNotify timeoutNotify =
626 bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800627
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800628 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
629}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800630
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800631void
632ContactManager::onAddFetchedContact(const QString& identity)
633{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800634 Name identityName(identity.toStdString());
635
Davide Pesavento7676b562020-12-14 00:41:26 -0500636 auto it = m_bufferedContacts.find(identityName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700637 if (it != m_bufferedContacts.end()) {
638 Contact contact(*(it->second.m_selfEndorseCert));
Varun Patila24bd3e2020-11-24 10:08:33 +0530639
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700640 try {
641 m_contactStorage->addContact(contact);
642 m_bufferedContacts.erase(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800643
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700644 m_contactList.clear();
645 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800646
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700647 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800648 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530649 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700650 emit warning(QString::fromStdString(e.what()));
651 }
652 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800653 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700654 emit warning(QString("Failure: no information of %1").arg(identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800655}
656
657void
658ContactManager::onUpdateProfile()
659{
660 // Get current profile;
661 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700662 if (!static_cast<bool>(newProfile))
Yingdi Yuae8217c2013-11-09 00:03:26 -0800663 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800664
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700665 shared_ptr<EndorseCertificate> newEndorseCertificate =
666 getSignedSelfEndorseCertificate(*newProfile);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800667
668 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate);
669
670 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
671}
672
673void
674ContactManager::onRefreshBrowseContact()
675{
Varun Patil3d850902020-11-23 12:19:14 +0530676 return;
677
678#if 0
679 // The following no longer works as we don't serve such a list anymore
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700680 vector<string> bufferedIdCertNames;
681 try {
682 using namespace boost::asio::ip;
683 tcp::iostream request_stream;
Varun Patil3d850902020-11-23 12:19:14 +0530684 request_stream.expires_from_now(std::chrono::milliseconds(5000));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700685 request_stream.connect("ndncert.named-data.net","80");
686 if (!request_stream) {
687 emit warning(QString::fromStdString("Fail to fetch certificate directory! #1"));
688 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800689 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700690
691 request_stream << "GET /cert/list/ HTTP/1.0\r\n";
692 request_stream << "Host: ndncert.named-data.net\r\n\r\n";
693 request_stream.flush();
694
695 string line1;
696 std::getline(request_stream,line1);
697 if (!request_stream) {
698 emit warning(QString::fromStdString("Fail to fetch certificate directory! #2"));
699 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800700 }
701
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700702 std::stringstream response_stream(line1);
703 string http_version;
704 response_stream >> http_version;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800705 size_t status_code;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700706 response_stream >> status_code;
707 string status_message;
708 std::getline(response_stream,status_message);
709
710 if (!response_stream ||
711 http_version.substr(0,5) != "HTTP/") {
712 emit warning(QString::fromStdString("Fail to fetch certificate directory! #3"));
713 return;
714 }
715 if (status_code!=200) {
716 emit warning(QString::fromStdString("Fail to fetch certificate directory! #4"));
717 return;
718 }
719 vector<string> headers;
720 string header;
721 while (std::getline(request_stream, header) && header != "\r")
722 headers.push_back(header);
723
724 std::istreambuf_iterator<char> stream_iter (request_stream);
725 std::istreambuf_iterator<char> end_of_stream;
726
727 typedef boost::tokenizer< boost::escaped_list_separator<char>,
728 std::istreambuf_iterator<char> > tokenizer_t;
729 tokenizer_t certItems (stream_iter,
730 end_of_stream,
731 boost::escaped_list_separator<char>('\\', '\n', '"'));
732
733 for (tokenizer_t::iterator it = certItems.begin(); it != certItems.end (); it++)
734 if (!it->empty())
735 bufferedIdCertNames.push_back(*it);
736 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530737 catch (const std::exception& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700738 emit warning(QString::fromStdString("Fail to fetch certificate directory! #N"));
739 }
740
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800741 {
742 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
743 m_idCertCount = bufferedIdCertNames.size();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800744 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800745 m_bufferedIdCerts.clear();
746
Davide Pesavento7676b562020-12-14 00:41:26 -0500747 for (auto it = bufferedIdCertNames.begin(); it != bufferedIdCertNames.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700748 Name certName(*it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800749
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700750 Interest interest(certName);
751 interest.setInterestLifetime(time::milliseconds(1000));
752 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530753 interest.setCanBePrefix(true);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700754
Varun Patil3d850902020-11-23 12:19:14 +0530755 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700756 bind(&ContactManager::onIdentityCertValidated, this, _1);
Varun Patil3d850902020-11-23 12:19:14 +0530757 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700758 bind(&ContactManager::onIdentityCertValidationFailed, this, _1, _2);
759 TimeoutNotify timeoutNotify =
760 bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800761
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700762 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
763 }
Varun Patil3d850902020-11-23 12:19:14 +0530764#endif
Yingdi Yuae8217c2013-11-09 00:03:26 -0800765}
766
Yingdi Yu72232692013-11-12 17:50:21 -0800767void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800768ContactManager::onFetchIdCert(const QString& qCertName)
Yingdi Yu72232692013-11-12 17:50:21 -0800769{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800770 Name certName(qCertName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700771 if (m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end())
772 emit idCertReady(*m_bufferedIdCerts[certName]);
Yingdi Yu72232692013-11-12 17:50:21 -0800773}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800774
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800775void
776ContactManager::onAddFetchedContactIdCert(const QString& qCertName)
777{
778 Name certName(qCertName.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530779 Name identity = certName.getPrefix(-1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800780
Davide Pesavento7676b562020-12-14 00:41:26 -0500781 auto it = m_bufferedIdCerts.find(certName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700782 if (it != m_bufferedIdCerts.end()) {
783 Contact contact(*it->second);
784 try {
785 m_contactStorage->addContact(contact);
786 m_bufferedIdCerts.erase(certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800787
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700788 m_contactList.clear();
789 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800790
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700791 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800792 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530793 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700794 emit warning(QString::fromStdString(e.what()));
795 }
796 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800797 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700798 emit warning(QString("Failure: no information of %1")
799 .arg(QString::fromStdString(identity.toUri())));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800800}
801
802void
803ContactManager::onWaitForContactList()
804{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800805 QStringList aliasList;
806 QStringList idList;
Davide Pesavento7676b562020-12-14 00:41:26 -0500807 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700808 aliasList << QString((*it)->getAlias().c_str());
809 idList << QString((*it)->getNameSpace().toUri().c_str());
810 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700811
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800812 emit contactAliasListReady(aliasList);
813 emit contactIdListReady(idList);
814}
815
816void
817ContactManager::onWaitForContactInfo(const QString& identity)
818{
Davide Pesavento7676b562020-12-14 00:41:26 -0500819 for (auto it = m_contactList.begin(); it != m_contactList.end(); it++)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700820 if ((*it)->getNameSpace().toUri() == identity.toStdString())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800821 emit contactInfoReady(QString((*it)->getNameSpace().toUri().c_str()),
822 QString((*it)->getName().c_str()),
823 QString((*it)->getInstitution().c_str()),
824 (*it)->isIntroducer());
825}
826
827void
828ContactManager::onRemoveContact(const QString& identity)
829{
830 m_contactStorage->removeContact(Name(identity.toStdString()));
831 m_contactList.clear();
832 m_contactStorage->getAllContacts(m_contactList);
833
834 onWaitForContactList();
835}
836
837void
838ContactManager::onUpdateAlias(const QString& identity, const QString& alias)
839{
840 m_contactStorage->updateAlias(Name(identity.toStdString()), alias.toStdString());
841 m_contactList.clear();
842 m_contactStorage->getAllContacts(m_contactList);
843
844 onWaitForContactList();
845}
846
847void
848ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer)
849{
850 m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer);
851}
852
853void
854ContactManager::onUpdateEndorseCertificate(const QString& identity)
855{
856 Name identityName(identity.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530857 shared_ptr<Certificate> newEndorseCertificate = generateEndorseCertificate(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800858
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700859 if (!static_cast<bool>(newEndorseCertificate))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800860 return;
861
862 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName);
863
864 publishEndorseCertificateInDNS(*newEndorseCertificate);
865}
866
Yingdi Yueb692ac2015-02-10 18:46:18 -0800867} // namespace chronochat
Yingdi Yufa4ce792014-02-06 18:09:22 -0800868
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700869
870#if WAF
871#include "contact-manager.moc"
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700872#endif