blob: 5a5f5bb4e80c9b2e06f09ca32d2beb7cddbd1436 [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"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080013#include <QStringList>
Yingdi Yu06f572b2014-03-11 15:46:10 -070014#include <QFile>
Yingdi Yu9236c432013-10-18 11:29:25 -070015
Yingdi Yu4685b1b2013-10-18 17:05:02 -070016#ifndef Q_MOC_RUN
Junxiao Shid5798f22016-08-22 02:33:26 +000017#include <ndn-cxx/encoding/buffer-stream.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053018#include <ndn-cxx/face.hpp>
19#include <ndn-cxx/security/validator.hpp>
20#include <ndn-cxx/security/validator-null.hpp>
21#include <ndn-cxx/security/signing-helpers.hpp>
22#include <ndn-cxx/security/verification-helpers.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070023#include "cryptopp.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080024#include <boost/asio.hpp>
25#include <boost/tokenizer.hpp>
26#include <boost/filesystem.hpp>
Yingdi Yu4685b1b2013-10-18 17:05:02 -070027#endif
Yingdi Yu9236c432013-10-18 11:29:25 -070028
Yingdi Yu348f5ea2014-03-01 14:47:25 -080029namespace fs = boost::filesystem;
Yingdi Yu9236c432013-10-18 11:29:25 -070030
Yingdi Yueb692ac2015-02-10 18:46:18 -080031namespace chronochat {
Yingdi Yu4685b1b2013-10-18 17:05:02 -070032
Yingdi Yu0b0a7362014-08-05 16:31:30 -070033using std::string;
34using std::map;
35using std::vector;
36
37using ndn::Face;
38using ndn::OBufferStream;
Varun Patil3d850902020-11-23 12:19:14 +053039using ndn::security::Certificate;
Yingdi Yu17032f82014-03-25 15:48:23 -070040
Yingdi Yu348f5ea2014-03-01 14:47:25 -080041
Yingdi Yu2c9e7712014-10-20 11:55:05 -070042ContactManager::ContactManager(Face& face,
Yingdi Yufa4ce792014-02-06 18:09:22 -080043 QObject* parent)
44 : QObject(parent)
Yingdi Yufa4ce792014-02-06 18:09:22 -080045 , m_face(face)
46{
Yingdi Yu76dd8002013-12-24 11:16:32 +080047 initializeSecurity();
Yingdi Yu9236c432013-10-18 11:29:25 -070048}
49
50ContactManager::~ContactManager()
Yingdi Yu0b0a7362014-08-05 16:31:30 -070051{
52}
Yingdi Yu9236c432013-10-18 11:29:25 -070053
Yingdi Yu76dd8002013-12-24 11:16:32 +080054void
55ContactManager::initializeSecurity()
56{
Varun Patil3d850902020-11-23 12:19:14 +053057 m_validator = make_shared<ndn::security::ValidatorConfig>(m_face);
58 m_validator->load("security/validation-contact-manager.conf");
Yingdi Yu76dd8002013-12-24 11:16:32 +080059}
60
61void
Yingdi Yufa4ce792014-02-06 18:09:22 -080062ContactManager::fetchCollectEndorse(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -080063{
64 Name interestName = identity;
65 interestName.append("DNS").append("ENDORSED");
66
Yingdi Yu76dd8002013-12-24 11:16:32 +080067 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -070068 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patil3d850902020-11-23 12:19:14 +053069 interest.setCanBePrefix(true);
Yingdi Yufa4ce792014-02-06 18:09:22 -080070 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +080071
Varun Patil3d850902020-11-23 12:19:14 +053072 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -070073 bind(&ContactManager::onDnsCollectEndorseValidated, this, _1, identity);
Varun Patil3d850902020-11-23 12:19:14 +053074 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -070075 bind(&ContactManager::onDnsCollectEndorseValidationFailed, this, _1, _2, identity);
76 TimeoutNotify timeoutNotify =
77 bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, _1, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080078
Yingdi Yu348f5ea2014-03-01 14:47:25 -080079 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
Yingdi Yub2e747d2013-11-05 23:06:43 -080080}
81
82void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080083ContactManager::fetchEndorseCertificateInternal(const Name& identity, size_t certIndex)
Yingdi Yub2e747d2013-11-05 23:06:43 -080084{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070085 shared_ptr<EndorseCollection> endorseCollection =
86 m_bufferedContacts[identity].m_endorseCollection;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080087
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080088 if(certIndex >= endorseCollection->getCollectionEntries().size())
Varun Patil3d850902020-11-23 12:19:14 +053089 return prepareEndorseInfo(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080090
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080091 Name interestName(endorseCollection->getCollectionEntries()[certIndex].certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092
Yingdi Yu76dd8002013-12-24 11:16:32 +080093 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -070094 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +053095 interest.setCanBePrefix(true);
Varun Patil3d850902020-11-23 12:19:14 +053096 interest.setMustBeFresh(false);
Yingdi Yu76dd8002013-12-24 11:16:32 +080097
Yingdi Yu2c9e7712014-10-20 11:55:05 -070098 m_face.expressInterest(interest,
99 bind(&ContactManager::onEndorseCertificateInternal,
100 this, _1, _2, identity, certIndex,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800101 endorseCollection->getCollectionEntries()[certIndex].hash),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700102 bind(&ContactManager::onEndorseCertificateInternalTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530103 this, _1, identity, certIndex),
104 bind(&ContactManager::onEndorseCertificateInternalTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700105 this, _1, identity, certIndex));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800106}
107
Yingdi Yu76dd8002013-12-24 11:16:32 +0800108void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800109ContactManager::prepareEndorseInfo(const Name& identity)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800110{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800111 const Profile& profile = m_bufferedContacts[identity].m_selfEndorseCert->getProfile();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800112
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800113 shared_ptr<EndorseInfo> endorseInfo = make_shared<EndorseInfo>();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114 m_bufferedContacts[identity].m_endorseInfo = endorseInfo;
115
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800116 map<string, size_t> endorseCount;
Varun Patila24bd3e2020-11-24 10:08:33 +0530117 for (Profile::const_iterator pIt = profile.begin(); pIt != profile.end(); pIt++)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700118 endorseCount[pIt->first] = 0;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800119
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800120 size_t endorseCertCount = 0;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800121
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700122 vector<shared_ptr<EndorseCertificate> >::const_iterator cIt =
123 m_bufferedContacts[identity].m_endorseCertList.begin();
124 vector<shared_ptr<EndorseCertificate> >::const_iterator cEnd =
125 m_bufferedContacts[identity].m_endorseCertList.end();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800126
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700127 for (; cIt != cEnd; cIt++, endorseCertCount++) {
Varun Patil3d850902020-11-23 12:19:14 +0530128 shared_ptr<Contact> contact = getContact((*cIt)->getSigner());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700129 if (!static_cast<bool>(contact))
130 continue;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700131
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700132 if (!contact->isIntroducer() ||
133 !contact->canBeTrustedFor(profile.getIdentityName()))
134 continue;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800135
Varun Patil3d850902020-11-23 12:19:14 +0530136 if (!(*cIt)->isValid())
137 continue;
138
139 if (!ndn::security::verifySignature(**cIt, contact->getPublicKey().data(), contact->getPublicKey().size()))
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700140 continue;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700142 const Profile& tmpProfile = (*cIt)->getProfile();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700143 const vector<string>& endorseList = (*cIt)->getEndorseList();
144 for (vector<string>::const_iterator eIt = endorseList.begin(); eIt != endorseList.end(); eIt++)
Varun Patil3d850902020-11-23 12:19:14 +0530145 if (tmpProfile.get(*eIt) == profile.get(*eIt))
146 endorseCount[*eIt] += 1;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700147 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800148
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700149 for (Profile::const_iterator pIt = profile.begin(); pIt != profile.end(); pIt++) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700150 std::stringstream ss;
151 ss << endorseCount[pIt->first] << "/" << endorseCertCount;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800152 endorseInfo->addEndorsement(pIt->first, pIt->second, ss.str());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700153 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800154
155 emit contactEndorseInfoReady (*endorseInfo);
156}
157
158void
Varun Patil3d850902020-11-23 12:19:14 +0530159ContactManager::onDnsSelfEndorseCertValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800160 const Name& identity)
161{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700162 try {
163 Data plainData;
Varun Patil3d850902020-11-23 12:19:14 +0530164 plainData.wireDecode(data.getContent().blockFromValue());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700165 shared_ptr<EndorseCertificate> selfEndorseCertificate =
166 make_shared<EndorseCertificate>(boost::cref(plainData));
Varun Patil3d850902020-11-23 12:19:14 +0530167
168 if (ndn::security::verifySignature(plainData, *selfEndorseCertificate)) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700169 m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate;
170 fetchCollectEndorse(identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800171 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700172 else
Varun Patil3d850902020-11-23 12:19:14 +0530173 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": verification failed"));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700174 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530175 catch (const Block::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530176 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": block error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700177 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530178 catch (const EndorseCertificate::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530179 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": cert error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700180 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530181 catch (const Data::Error& e) {
Varun Patil3d850902020-11-23 12:19:14 +0530182 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri() + ": data error " + e.what()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700183 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800184}
185
186void
Varun Patil3d850902020-11-23 12:19:14 +0530187ContactManager::onDnsSelfEndorseCertValidationFailed(const Data& data,
188 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800189 const Name& identity)
190{
191 // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert,
192 // but let's stay with failure for now.
193 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri()));
194}
195
196void
197ContactManager::onDnsSelfEndorseCertTimeoutNotify(const Interest& interest,
198 const Name& identity)
199{
200 // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert,
201 // but let's stay with failure for now.
202 emit contactInfoFetchFailed(QString::fromStdString(identity.toUri()));
203}
204
205void
Varun Patil3d850902020-11-23 12:19:14 +0530206ContactManager::onDnsCollectEndorseValidated(const Data& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800207 const Name& identity)
208{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800209 try {
210 shared_ptr<EndorseCollection> endorseCollection =
Varun Patil3d850902020-11-23 12:19:14 +0530211 make_shared<EndorseCollection>(data.getContent().blockFromValue());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700212 m_bufferedContacts[identity].m_endorseCollection = endorseCollection;
213 fetchEndorseCertificateInternal(identity, 0);
214 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530215 catch (const std::runtime_error&) {
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800216 prepareEndorseInfo(identity);
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800217 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800218}
219
220void
Varun Patil3d850902020-11-23 12:19:14 +0530221ContactManager::onDnsCollectEndorseValidationFailed(const Data& data,
222 const ndn::security::ValidationError& error,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800223 const Name& identity)
224{
225 prepareEndorseInfo(identity);
226}
227
228void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700229ContactManager::onDnsCollectEndorseTimeoutNotify(const Interest& interest, const Name& identity)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800230{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800231 prepareEndorseInfo(identity);
232}
233
234void
Varun Patil3d850902020-11-23 12:19:14 +0530235ContactManager::onEndorseCertificateInternal(const Interest& interest, const Data& data,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800236 const Name& identity, size_t certIndex,
237 string hash)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800238{
Yingdi Yu17032f82014-03-25 15:48:23 -0700239 std::stringstream ss;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700240 {
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800241 using namespace CryptoPP;
242
243 SHA256 hash;
244 StringSource(data.wireEncode().wire(), data.wireEncode().size(), true,
245 new HashFilter(hash, new FileSink(ss)));
246 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700247
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700248 if (ss.str() == hash) {
249 shared_ptr<EndorseCertificate> endorseCertificate =
250 make_shared<EndorseCertificate>(boost::cref(data));
251 m_bufferedContacts[identity].m_endorseCertList.push_back(endorseCertificate);
252 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800253
254 fetchEndorseCertificateInternal(identity, certIndex+1);
255}
256
257void
258ContactManager::onEndorseCertificateInternalTimeout(const Interest& interest,
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700259 const Name& identity,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800260 size_t certIndex)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800261{
262 fetchEndorseCertificateInternal(identity, certIndex+1);
263}
264
265void
266ContactManager::collectEndorsement()
267{
268 {
269 boost::recursive_mutex::scoped_lock lock(m_collectCountMutex);
270 m_collectCount = m_contactList.size();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700271
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700272 for (ContactList::iterator it = m_contactList.begin(); it != m_contactList.end(); it++) {
273 Name interestName = (*it)->getNameSpace();
274 interestName.append("DNS").append(m_identity.wireEncode()).append("ENDORSEE");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800275
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700276 Interest interest(interestName);
Varun Patil3d850902020-11-23 12:19:14 +0530277 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530278 interest.setCanBePrefix(true);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700279 interest.setInterestLifetime(time::milliseconds(1000));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700280
Varun Patil3d850902020-11-23 12:19:14 +0530281 ndn::security::DataValidationSuccessCallback onValidated =
282 bind(&ContactManager::onDnsEndorseeValidated, this, _1);
283 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700284 bind(&ContactManager::onDnsEndorseeValidationFailed, this, _1, _2);
285 TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsEndorseeTimeoutNotify, this, _1);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700286
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700287 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
288 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800289 }
290}
291
292void
Varun Patil3d850902020-11-23 12:19:14 +0530293ContactManager::onDnsEndorseeValidated(const Data& data)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800294{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800295 Data endorseData;
Varun Patil3d850902020-11-23 12:19:14 +0530296 endorseData.wireDecode(data.getContent().blockFromValue());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800297
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800298 EndorseCertificate endorseCertificate(endorseData);
299 m_contactStorage->updateCollectEndorse(endorseCertificate);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800300
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800301 decreaseCollectStatus();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800302}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800303
304void
Varun Patil3d850902020-11-23 12:19:14 +0530305ContactManager::onDnsEndorseeValidationFailed(const Data& data,
306 const ndn::security::ValidationError& error)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700307{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800308 decreaseCollectStatus();
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700309}
310
Yingdi Yub2e747d2013-11-05 23:06:43 -0800311void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800312ContactManager::onDnsEndorseeTimeoutNotify(const Interest& interest)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800313{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800314 decreaseCollectStatus();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800315}
316
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700317void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800318ContactManager::decreaseCollectStatus()
Yingdi Yub2e747d2013-11-05 23:06:43 -0800319{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800320 size_t count;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800321 {
322 boost::recursive_mutex::scoped_lock lock(m_collectCountMutex);
323 m_collectCount--;
324 count = m_collectCount;
325 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800326
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800327 if(count == 0)
328 publishCollectEndorsedDataInDNS();
329}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800330
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800331void
332ContactManager::publishCollectEndorsedDataInDNS()
333{
334 Name dnsName = m_identity;
335 dnsName.append("DNS").append("ENDORSED").appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800336
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700337 shared_ptr<Data> data = make_shared<Data>();
338 data->setName(dnsName);
Varun Patil3d850902020-11-23 12:19:14 +0530339 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800340
341 EndorseCollection endorseCollection;
342 m_contactStorage->getCollectEndorse(endorseCollection);
343
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800344 data->setContent(endorseCollection.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530345
346 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800347
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700348 m_contactStorage->updateDnsOthersEndorse(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700349 m_face.put(*data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800350}
351
352void
Varun Patil3d850902020-11-23 12:19:14 +0530353ContactManager::onIdentityCertValidated(const Data& data)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800354{
Varun Patil3d850902020-11-23 12:19:14 +0530355 shared_ptr<Certificate> cert = make_shared<Certificate>(boost::cref(data));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800356 m_bufferedIdCerts[cert->getName()] = cert;
357 decreaseIdCertCount();
358}
359
360void
Varun Patil3d850902020-11-23 12:19:14 +0530361ContactManager::onIdentityCertValidationFailed(const Data& data,
362 const ndn::security::ValidationError& error)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800363{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800364 decreaseIdCertCount();
365}
366
367void
368ContactManager::onIdentityCertTimeoutNotify(const Interest& interest)
369{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800370 decreaseIdCertCount();
371}
372
373void
374ContactManager::decreaseIdCertCount()
375{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800376 size_t count;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800377 {
378 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
379 m_idCertCount--;
380 count = m_idCertCount;
381 }
382
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700383 if (count == 0) {
384 QStringList certNameList;
385 QStringList nameList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800386
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700387 for(BufferedIdCerts::const_iterator it = m_bufferedIdCerts.begin();
388 it != m_bufferedIdCerts.end(); it++) {
389 certNameList << QString::fromStdString(it->second->getName().toUri());
390 Profile profile(*(it->second));
391 nameList << QString::fromStdString(profile.get("name"));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800392 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700393
394 emit idCertNameListReady(certNameList);
395 emit nameListReady(nameList);
396 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800397}
398
Yingdi Yu76dd8002013-12-24 11:16:32 +0800399shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800400ContactManager::getSignedSelfEndorseCertificate(const Profile& profile)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700401{
Varun Patil3d850902020-11-23 12:19:14 +0530402 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
403 .getDefaultKey().getDefaultCertificate();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700404 vector<string> endorseList;
405 for (Profile::const_iterator it = profile.begin(); it != profile.end(); it++)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700406 endorseList.push_back(it->first);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700407
408 shared_ptr<EndorseCertificate> selfEndorseCertificate =
Varun Patil3d850902020-11-23 12:19:14 +0530409 make_shared<EndorseCertificate>(boost::cref(signCert),
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700410 boost::cref(profile),
411 boost::cref(endorseList));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700412
Varun Patil3d850902020-11-23 12:19:14 +0530413 m_keyChain.sign(*selfEndorseCertificate,
414 ndn::security::signingByIdentity(m_identity).setSignatureInfo(
415 selfEndorseCertificate->getSignatureInfo()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700416
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800417 return selfEndorseCertificate;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700418}
419
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700420void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800421ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700422{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800423 Name dnsName = m_identity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800424 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800425
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700426 shared_ptr<Data> data = make_shared<Data>();
427 data->setName(dnsName);
428 data->setContent(selfEndorseCertificate.wireEncode());
429 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800430
Varun Patil3d850902020-11-23 12:19:14 +0530431 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800432
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700433 m_contactStorage->updateDnsSelfProfileData(*data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700434 m_face.put(*data);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700435}
436
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700437shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800438ContactManager::generateEndorseCertificate(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800439{
Varun Patil3d850902020-11-23 12:19:14 +0530440 auto signCert = m_keyChain.getPib().getIdentity(m_identity)
441 .getDefaultKey().getDefaultCertificate();
442
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800443 shared_ptr<Contact> contact = getContact(identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700444 if (!static_cast<bool>(contact))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800445 return shared_ptr<EndorseCertificate>();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800446
Varun Patil3d850902020-11-23 12:19:14 +0530447 Name signerKeyName = m_identity;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800448
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700449 vector<string> endorseList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800450 m_contactStorage->getEndorseList(identity, endorseList);
451
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700452 shared_ptr<EndorseCertificate> cert =
453 shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800454 contact->getPublicKey(),
455 contact->getNotBefore(),
456 contact->getNotAfter(),
Varun Patil3d850902020-11-23 12:19:14 +0530457 signCert.getKeyId(),
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700458 signerKeyName,
459 contact->getProfile(),
460 endorseList));
Varun Patil3d850902020-11-23 12:19:14 +0530461 m_keyChain.sign(*cert,
462 ndn::security::signingByIdentity(m_identity)
463 .setSignatureInfo(cert->getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800464 return cert;
465
466}
467
468void
469ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate)
470{
Varun Patil3d850902020-11-23 12:19:14 +0530471 Name endorsee = endorseCertificate.getKeyName().getPrefix(-4);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800472 Name dnsName = m_identity;
473 dnsName.append("DNS")
474 .append(endorsee.wireEncode())
475 .append("ENDORSEE")
476 .appendVersion();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800477
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700478 shared_ptr<Data> data = make_shared<Data>();
479 data->setName(dnsName);
480 data->setContent(endorseCertificate.wireEncode());
Varun Patil3d850902020-11-23 12:19:14 +0530481 data->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800482
Varun Patil3d850902020-11-23 12:19:14 +0530483 m_keyChain.sign(*data, ndn::security::signingByIdentity(m_identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800484
Yingdi Yu6c3c5962014-09-09 16:52:38 -0700485 m_contactStorage->updateDnsEndorseOthers(*data, dnsName.get(-3).toUri());
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700486 m_face.put(*data);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800487}
488
489void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800490ContactManager::sendInterest(const Interest& interest,
Varun Patil3d850902020-11-23 12:19:14 +0530491 const ndn::security::DataValidationSuccessCallback& onValidated,
492 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800493 const TimeoutNotify& timeoutNotify,
494 int retry /* = 1 */)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800495{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700496 m_face.expressInterest(interest,
497 bind(&ContactManager::onTargetData,
498 this, _1, _2, onValidated, onValidationFailed),
499 bind(&ContactManager::onTargetTimeout,
Varun Patil3d850902020-11-23 12:19:14 +0530500 this, _1, retry, onValidated, onValidationFailed, timeoutNotify),
501 bind(&ContactManager::onTargetTimeout,
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700502 this, _1, retry, onValidated, onValidationFailed, timeoutNotify));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800503}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800504
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800505void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700506ContactManager::onTargetData(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800507 const Data& data,
Varun Patil3d850902020-11-23 12:19:14 +0530508 const ndn::security::DataValidationSuccessCallback& onValidated,
509 const ndn::security::DataValidationFailureCallback& onValidationFailed)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800510{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700511 m_validator->validate(data, onValidated, onValidationFailed);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800512}
Yingdi Yub2e747d2013-11-05 23:06:43 -0800513
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800514void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700515ContactManager::onTargetTimeout(const Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800516 int retry,
Varun Patil3d850902020-11-23 12:19:14 +0530517 const ndn::security::DataValidationSuccessCallback& onValidated,
518 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800519 const TimeoutNotify& timeoutNotify)
520{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700521 if (retry > 0)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800522 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
523 else
524 timeoutNotify(interest);
525}
526
527void
528ContactManager::onDnsInterest(const Name& prefix, const Interest& interest)
529{
530 const Name& interestName = interest.getName();
531 shared_ptr<Data> data;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700532
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700533 if (interestName.size() <= prefix.size())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800534 return;
535
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700536 if (interestName.size() == (prefix.size()+1)) {
537 data = m_contactStorage->getDnsData("N/A", interestName.get(prefix.size()).toUri());
538 if (static_cast<bool>(data))
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700539 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700540 return;
541 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800542
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700543 if (interestName.size() == (prefix.size()+2)) {
544 data = m_contactStorage->getDnsData(interestName.get(prefix.size()).toUri(),
545 interestName.get(prefix.size()+1).toUri());
546 if (static_cast<bool>(data))
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700547 m_face.put(*data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700548 return;
549 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800550}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700551
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800552void
Yingdi Yu17032f82014-03-25 15:48:23 -0700553ContactManager::onDnsRegisterFailed(const Name& prefix, const std::string& failInfo)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800554{
555 emit warning(QString(failInfo.c_str()));
556}
557
Varun Patil3d850902020-11-23 12:19:14 +0530558void
559ContactManager::onKeyInterest(const Name& prefix, const Interest& interest)
560{
561 const Name& interestName = interest.getName();
562 shared_ptr<Certificate> data;
563
564 try {
565 ndn::security::Certificate cert = m_keyChain.getPib()
566 .getIdentity(m_identity)
567 .getDefaultKey()
568 .getDefaultCertificate();
569 if (cert.getKeyName() == interestName)
570 return m_face.put(cert);
Varun Patila24bd3e2020-11-24 10:08:33 +0530571 } catch (const ndn::security::Pib::Error&) {}
Varun Patil3d850902020-11-23 12:19:14 +0530572
573 data = m_contactStorage->getSelfEndorseCertificate();
574 if (static_cast<bool>(data) && data->getKeyName().equals(interestName))
575 return m_face.put(*data);
576
577 data = m_contactStorage->getCollectEndorseByName(interestName);
578 if (static_cast<bool>(data))
579 return m_face.put(*data);
580}
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800581
582// public slots
583void
584ContactManager::onIdentityUpdated(const QString& identity)
585{
586 m_identity = Name(identity.toStdString());
587
588 m_contactStorage = make_shared<ContactStorage>(m_identity);
589
Varun Patila24bd3e2020-11-24 10:08:33 +0530590 m_dnsListenerHandle = m_face.setInterestFilter(
591 Name(m_identity).append("DNS"),
592 bind(&ContactManager::onDnsInterest, this, _1, _2),
593 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530594
Varun Patila24bd3e2020-11-24 10:08:33 +0530595 m_keyListenerHandle = m_face.setInterestFilter(
596 Name(m_identity).append("KEY"),
597 bind(&ContactManager::onKeyInterest, this, _1, _2),
598 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530599
Varun Patila24bd3e2020-11-24 10:08:33 +0530600 m_profileCertListenerHandle = m_face.setInterestFilter(
601 Name(m_identity).append("PROFILE-CERT"),
602 bind(&ContactManager::onKeyInterest, this, _1, _2),
603 bind(&ContactManager::onDnsRegisterFailed, this, _1, _2));
Varun Patil3d850902020-11-23 12:19:14 +0530604
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800605 m_contactList.clear();
606 m_contactStorage->getAllContacts(m_contactList);
607
608 m_bufferedContacts.clear();
Varun Patil3d850902020-11-23 12:19:14 +0530609 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800610
611 collectEndorsement();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800612}
613
Yingdi Yuae8217c2013-11-09 00:03:26 -0800614void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800615ContactManager::onFetchContactInfo(const QString& identity)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800616{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800617 // try to fetch self-endorse-certificate via DNS PROFILE first.
618 Name identityName(identity.toStdString());
619 Name interestName;
620 interestName.append(identityName).append("DNS").append("PROFILE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800621
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800622 Interest interest(interestName);
Yingdi Yua7876722014-03-25 14:46:55 -0700623 interest.setInterestLifetime(time::milliseconds(1000));
Varun Patila24bd3e2020-11-24 10:08:33 +0530624 interest.setCanBePrefix(true);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800625 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800626
Varun Patil3d850902020-11-23 12:19:14 +0530627 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700628 bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName);
Varun Patil3d850902020-11-23 12:19:14 +0530629 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700630 bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, _2, identityName);
631 TimeoutNotify timeoutNotify =
632 bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800633
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800634 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
635}
Yingdi Yu76dd8002013-12-24 11:16:32 +0800636
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800637void
638ContactManager::onAddFetchedContact(const QString& identity)
639{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800640 Name identityName(identity.toStdString());
641
642 BufferedContacts::const_iterator it = m_bufferedContacts.find(identityName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700643 if (it != m_bufferedContacts.end()) {
644 Contact contact(*(it->second.m_selfEndorseCert));
Varun Patila24bd3e2020-11-24 10:08:33 +0530645
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700646 try {
647 m_contactStorage->addContact(contact);
648 m_bufferedContacts.erase(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800649
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700650 m_contactList.clear();
651 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800652
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700653 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800654 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530655 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700656 emit warning(QString::fromStdString(e.what()));
657 }
658 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800659 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700660 emit warning(QString("Failure: no information of %1").arg(identity));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800661}
662
663void
664ContactManager::onUpdateProfile()
665{
666 // Get current profile;
667 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700668 if (!static_cast<bool>(newProfile))
Yingdi Yuae8217c2013-11-09 00:03:26 -0800669 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800670
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700671 shared_ptr<EndorseCertificate> newEndorseCertificate =
672 getSignedSelfEndorseCertificate(*newProfile);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800673
674 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate);
675
676 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
677}
678
679void
680ContactManager::onRefreshBrowseContact()
681{
Varun Patil3d850902020-11-23 12:19:14 +0530682 return;
683
684#if 0
685 // The following no longer works as we don't serve such a list anymore
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700686 vector<string> bufferedIdCertNames;
687 try {
688 using namespace boost::asio::ip;
689 tcp::iostream request_stream;
Varun Patil3d850902020-11-23 12:19:14 +0530690 request_stream.expires_from_now(std::chrono::milliseconds(5000));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700691 request_stream.connect("ndncert.named-data.net","80");
692 if (!request_stream) {
693 emit warning(QString::fromStdString("Fail to fetch certificate directory! #1"));
694 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800695 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700696
697 request_stream << "GET /cert/list/ HTTP/1.0\r\n";
698 request_stream << "Host: ndncert.named-data.net\r\n\r\n";
699 request_stream.flush();
700
701 string line1;
702 std::getline(request_stream,line1);
703 if (!request_stream) {
704 emit warning(QString::fromStdString("Fail to fetch certificate directory! #2"));
705 return;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800706 }
707
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700708 std::stringstream response_stream(line1);
709 string http_version;
710 response_stream >> http_version;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800711 size_t status_code;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700712 response_stream >> status_code;
713 string status_message;
714 std::getline(response_stream,status_message);
715
716 if (!response_stream ||
717 http_version.substr(0,5) != "HTTP/") {
718 emit warning(QString::fromStdString("Fail to fetch certificate directory! #3"));
719 return;
720 }
721 if (status_code!=200) {
722 emit warning(QString::fromStdString("Fail to fetch certificate directory! #4"));
723 return;
724 }
725 vector<string> headers;
726 string header;
727 while (std::getline(request_stream, header) && header != "\r")
728 headers.push_back(header);
729
730 std::istreambuf_iterator<char> stream_iter (request_stream);
731 std::istreambuf_iterator<char> end_of_stream;
732
733 typedef boost::tokenizer< boost::escaped_list_separator<char>,
734 std::istreambuf_iterator<char> > tokenizer_t;
735 tokenizer_t certItems (stream_iter,
736 end_of_stream,
737 boost::escaped_list_separator<char>('\\', '\n', '"'));
738
739 for (tokenizer_t::iterator it = certItems.begin(); it != certItems.end (); it++)
740 if (!it->empty())
741 bufferedIdCertNames.push_back(*it);
742 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530743 catch (const std::exception& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700744 emit warning(QString::fromStdString("Fail to fetch certificate directory! #N"));
745 }
746
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800747 {
748 boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex);
749 m_idCertCount = bufferedIdCertNames.size();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800750 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800751 m_bufferedIdCerts.clear();
752
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700753 for (vector<string>::const_iterator it = bufferedIdCertNames.begin();
754 it != bufferedIdCertNames.end(); it++) {
755 Name certName(*it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800756
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700757 Interest interest(certName);
758 interest.setInterestLifetime(time::milliseconds(1000));
759 interest.setMustBeFresh(true);
Varun Patila24bd3e2020-11-24 10:08:33 +0530760 interest.setCanBePrefix(true);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700761
Varun Patil3d850902020-11-23 12:19:14 +0530762 ndn::security::DataValidationSuccessCallback onValidated =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700763 bind(&ContactManager::onIdentityCertValidated, this, _1);
Varun Patil3d850902020-11-23 12:19:14 +0530764 ndn::security::DataValidationFailureCallback onValidationFailed =
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700765 bind(&ContactManager::onIdentityCertValidationFailed, this, _1, _2);
766 TimeoutNotify timeoutNotify =
767 bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800768
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700769 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
770 }
Varun Patil3d850902020-11-23 12:19:14 +0530771#endif
Yingdi Yuae8217c2013-11-09 00:03:26 -0800772}
773
Yingdi Yu72232692013-11-12 17:50:21 -0800774void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800775ContactManager::onFetchIdCert(const QString& qCertName)
Yingdi Yu72232692013-11-12 17:50:21 -0800776{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800777 Name certName(qCertName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700778 if (m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end())
779 emit idCertReady(*m_bufferedIdCerts[certName]);
Yingdi Yu72232692013-11-12 17:50:21 -0800780}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800781
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800782void
783ContactManager::onAddFetchedContactIdCert(const QString& qCertName)
784{
785 Name certName(qCertName.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530786 Name identity = certName.getPrefix(-1);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800787
788 BufferedIdCerts::const_iterator it = m_bufferedIdCerts.find(certName);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700789 if (it != m_bufferedIdCerts.end()) {
790 Contact contact(*it->second);
791 try {
792 m_contactStorage->addContact(contact);
793 m_bufferedIdCerts.erase(certName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800794
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700795 m_contactList.clear();
796 m_contactStorage->getAllContacts(m_contactList);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800797
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700798 onWaitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800799 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530800 catch (const ContactStorage::Error& e) {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700801 emit warning(QString::fromStdString(e.what()));
802 }
803 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800804 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700805 emit warning(QString("Failure: no information of %1")
806 .arg(QString::fromStdString(identity.toUri())));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800807}
808
809void
810ContactManager::onWaitForContactList()
811{
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800812 QStringList aliasList;
813 QStringList idList;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700814 for (ContactList::const_iterator it = m_contactList.begin(); it != m_contactList.end(); it++) {
815 aliasList << QString((*it)->getAlias().c_str());
816 idList << QString((*it)->getNameSpace().toUri().c_str());
817 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700818
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800819 emit contactAliasListReady(aliasList);
820 emit contactIdListReady(idList);
821}
822
823void
824ContactManager::onWaitForContactInfo(const QString& identity)
825{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700826 for (ContactList::const_iterator it = m_contactList.begin(); it != m_contactList.end(); it++)
827 if ((*it)->getNameSpace().toUri() == identity.toStdString())
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800828 emit contactInfoReady(QString((*it)->getNameSpace().toUri().c_str()),
829 QString((*it)->getName().c_str()),
830 QString((*it)->getInstitution().c_str()),
831 (*it)->isIntroducer());
832}
833
834void
835ContactManager::onRemoveContact(const QString& identity)
836{
837 m_contactStorage->removeContact(Name(identity.toStdString()));
838 m_contactList.clear();
839 m_contactStorage->getAllContacts(m_contactList);
840
841 onWaitForContactList();
842}
843
844void
845ContactManager::onUpdateAlias(const QString& identity, const QString& alias)
846{
847 m_contactStorage->updateAlias(Name(identity.toStdString()), alias.toStdString());
848 m_contactList.clear();
849 m_contactStorage->getAllContacts(m_contactList);
850
851 onWaitForContactList();
852}
853
854void
855ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer)
856{
857 m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer);
858}
859
860void
861ContactManager::onUpdateEndorseCertificate(const QString& identity)
862{
863 Name identityName(identity.toStdString());
Varun Patil3d850902020-11-23 12:19:14 +0530864 shared_ptr<Certificate> newEndorseCertificate = generateEndorseCertificate(identityName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800865
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700866 if (!static_cast<bool>(newEndorseCertificate))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800867 return;
868
869 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName);
870
871 publishEndorseCertificateInDNS(*newEndorseCertificate);
872}
873
Yingdi Yueb692ac2015-02-10 18:46:18 -0800874} // namespace chronochat
Yingdi Yufa4ce792014-02-06 18:09:22 -0800875
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700876
877#if WAF
878#include "contact-manager.moc"
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700879#endif