blob: b940bd89e2f2bd370ffbf2c457c61487903c480d [file] [log] [blame]
Yingdi Yu0b82a4e2013-10-18 11:29:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "contact-manager.h"
12
Yingdi Yuaa8d7692013-10-18 17:05:02 -070013#ifndef Q_MOC_RUN
Yingdi Yu64206112013-12-24 11:16:32 +080014#include <ndn-cpp/face.hpp>
15#include <ndn-cpp/sha256-with-rsa-signature.hpp>
16#include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp>
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080017#include <cryptopp/base64.h>
Yingdi Yu64206112013-12-24 11:16:32 +080018#include <ndn-cpp-et/policy-manager/identity-policy-rule.hpp>
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070019#include <fstream>
Yingdi Yu64206112013-12-24 11:16:32 +080020#include "endorse-collection.pb.h"
21#include "null-ptrs.h"
Yingdi Yu590fa5d2013-10-18 18:35:09 -070022#include "logging.h"
Yingdi Yuaa8d7692013-10-18 17:05:02 -070023#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070024
25using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080026using namespace ndn::ptr_lib;
27using namespace std;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070028
Yingdi Yu590fa5d2013-10-18 18:35:09 -070029INIT_LOGGER("ContactManager");
30
Yingdi Yuaccbda92013-12-27 08:44:12 +080031ContactManager::ContactManager(shared_ptr<IdentityManager> identityManager,
32 shared_ptr<Face> face,
33 shared_ptr<Transport> transport,
34 QObject* parent)
35 : QObject(parent),
36 m_face(face),
37 m_transport(transport)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070038{
Yingdi Yu64206112013-12-24 11:16:32 +080039 m_identityManager = identityManager;
40 m_contactStorage = make_shared<ContactStorage>();
41 m_dnsStorage = make_shared<DnsStorage>();
Yingdi Yuaa8d7692013-10-18 17:05:02 -070042
Yingdi Yu64206112013-12-24 11:16:32 +080043 initializeSecurity();
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070044}
45
46ContactManager::~ContactManager()
Yingdi Yuaccbda92013-12-27 08:44:12 +080047{}
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070048
Yingdi Yuaccbda92013-12-27 08:44:12 +080049// void
50// ContactManager::connectToDaemon()
51// {
52// //Hack! transport does not connect to daemon unless an interest is expressed.
53// Name name("/ndn");
54// shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name);
55// m_face->expressInterest(*interest,
56// bind(&ContactManager::onConnectionData, this, _1, _2),
57// bind(&ContactManager::onConnectionDataTimeout, this, _1));
58// }
Yingdi Yub29f78c2013-11-09 20:12:31 -080059
Yingdi Yuaccbda92013-12-27 08:44:12 +080060// void
61// ContactManager::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
62// const shared_ptr<Data>& data)
63// {
64// _LOG_DEBUG("onConnectionData");
65// }
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080066
Yingdi Yuaccbda92013-12-27 08:44:12 +080067// void
68// ContactManager::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
69// {
70// _LOG_DEBUG("onConnectionDataTimeout");
71// }
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070072
Yingdi Yu64206112013-12-24 11:16:32 +080073void
74ContactManager::initializeSecurity()
75{
76 m_policyManager = make_shared<SimplePolicyManager>();
77
78 m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><ENDORSED>",
79 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
80 "==", "\\1", "\\1\\2", true));
81 m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><PROFILE>",
82 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
83 "==", "\\1", "\\1\\2", true));
84 m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
85 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
86 "==", "\\1", "\\1\\2", true));
87 m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
88 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
89 ">", "\\1\\2", "\\1", true));
90 m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
91 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
92 "==", "\\1", "\\1\\2", true));
93 m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^(<>*)$",
94 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
95 ">", "\\1", "\\1\\2", true));
Yingdi Yu8fb16a42013-11-10 18:35:09 -080096
Yingdi Yuaa8d7692013-10-18 17:05:02 -070097
Yingdi Yu64206112013-12-24 11:16:32 +080098 m_policyManager->addSigningPolicyRule(make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><PROFILE>",
99 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
100 "==", "\\1", "\\1\\2", true));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700101
Yingdi Yu8fb16a42013-11-10 18:35:09 -0800102
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800103 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
104HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
10567biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
1065X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
107WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
108Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
109MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
110nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
111c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
112MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
113b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
114yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
115I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
116/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
11772P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
118VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
119iVUF1QIBEQAA");
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700120
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800121 string decoded;
122 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
123 TrustAnchor.size(),
124 true,
125 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
Yingdi Yu64206112013-12-24 11:16:32 +0800126 Data data;
127 data.wireDecode((const uint8_t*)decoded.c_str(), decoded.size());
128 shared_ptr<IdentityCertificate> anchor = make_shared<IdentityCertificate>(data);
129 m_policyManager->addTrustAnchor(anchor);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800130
131#ifdef _DEBUG
132
133 const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
134zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
135qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
1366M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
137DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
138Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
139MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
140qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
141APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
142MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
143MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
144/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
145sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
146H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
147gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
148OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
149F7Wh5ayeo8NBKDsCAwEAAQAA");
150
151 string decoded2;
152 CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()),
153 FakeAnchor.size(),
154 true,
155 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
Yingdi Yu64206112013-12-24 11:16:32 +0800156 Data data2;
157 data2.wireDecode((const uint8_t*)decoded2.c_str(), decoded2.size());
158 shared_ptr<IdentityCertificate>anchor2 = make_shared<IdentityCertificate>(data2);
159 m_policyManager->addTrustAnchor(anchor2);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800160
161#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700162}
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700163
164
165void
166ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
167{
168 Name interestName = identity;
169 interestName.append("DNS").append("PROFILE");
170
Yingdi Yu64206112013-12-24 11:16:32 +0800171 Interest interest(interestName);
172 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
173
174 OnVerified onVerified = boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, this, _1, identity);
175 OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerifyFailed, this, _1, identity);
176 TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeoutNotify, this, identity);
177
178 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700179}
180
181void
Yingdi Yu64206112013-12-24 11:16:32 +0800182ContactManager::onDnsSelfEndorseCertificateTimeoutNotify(const Name& identity)
183{ emit contactFetchFailed(identity); }
184
185void
186ContactManager::onDnsSelfEndorseCertificateVerified(const shared_ptr<Data>& data,
187 const Name& identity)
188{
189 try{
190 Data plainData;
191 plainData.wireDecode(data->getContent().buf(), data->getContent().size());
192 EndorseCertificate selfEndorseCertificate(plainData);
193 if(Sha256WithRsaHandler::verifySignature(plainData, selfEndorseCertificate.getPublicKeyInfo()))
194 emit contactFetched (selfEndorseCertificate);
195 else
196 emit contactFetchFailed (identity);
197 }catch(std::exception& e){
198 _LOG_ERROR("Exception: " << e.what());
199 emit contactFetchFailed (identity);
200 }
201}
202
203void
204ContactManager::onDnsSelfEndorseCertificateVerifyFailed(const shared_ptr<Data>& data,
205 const Name& identity)
206{ emit contactFetchFailed (identity); }
207
208void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800209ContactManager::fetchCollectEndorse(const ndn::Name& identity)
210{
211 Name interestName = identity;
212 interestName.append("DNS").append("ENDORSED");
213
Yingdi Yu64206112013-12-24 11:16:32 +0800214 Interest interest(interestName);
215 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
216 interest.setInterestLifetimeMilliseconds(1000);
217
218 OnVerified onVerified = boost::bind(&ContactManager::onDnsCollectEndorseVerified, this, _1, identity);
219 OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onDnsCollectEndorseVerifyFailed, this, _1, identity);
220 TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, identity);
221
222 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800223}
224
225void
Yingdi Yu64206112013-12-24 11:16:32 +0800226ContactManager::onDnsCollectEndorseTimeoutNotify(const Name& identity)
227{
228 emit collectEndorseFetchFailed (identity);
229}
230
231void
232ContactManager::onDnsCollectEndorseVerified(const shared_ptr<Data>& data, const Name& identity)
233{ emit collectEndorseFetched (*data); }
234
235void
236ContactManager::onDnsCollectEndorseVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
237{ emit collectEndorseFetchFailed (identity); }
238
239
240void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800241ContactManager::fetchKey(const ndn::Name& certName)
242{
243 Name interestName = certName;
244
Yingdi Yu64206112013-12-24 11:16:32 +0800245 Interest interest(interestName);
246 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
247 interest.setInterestLifetimeMilliseconds(1000);
248
249 OnVerified onVerified = boost::bind(&ContactManager::onKeyVerified, this, _1, certName);
250 OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onKeyVerifyFailed, this, _1, certName);
251 TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onKeyTimeoutNotify, this, certName);
252
253 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
254}
255
256
257void
258ContactManager::onKeyVerified(const shared_ptr<Data>& data, const Name& identity)
259{
260 IdentityCertificate identityCertificate(*data);
261
262 Profile profile(identityCertificate);
263 ProfileData profileData(profile);
264
265 Name certificateName = m_identityManager->getDefaultCertificateName();
266 m_identityManager->signByCertificate(profileData, certificateName);
267
268 try{
269 EndorseCertificate endorseCertificate(identityCertificate, profileData);
270 m_identityManager->signByCertificate(endorseCertificate, certificateName);
271 emit contactKeyFetched (endorseCertificate);
272 }catch(std::exception& e){
273 _LOG_ERROR("Exception: " << e.what());
274 return;
275 }
276}
277
278void
279ContactManager::onKeyVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
280{
281 _LOG_DEBUG("Key cannot be verified!");
282 emit contactKeyFetchFailed (identity);
283}
284
285void
286ContactManager::onKeyTimeoutNotify(const Name& identity)
287{
288 _LOG_DEBUG("Key timeout!");
289 emit contactKeyFetchFailed(identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800290}
291
292void
Yingdi Yuaccbda92013-12-27 08:44:12 +0800293ContactManager::fetchIdCertificate(const Name& certName)
Yingdi Yu908f8412013-11-09 00:03:26 -0800294{
295 Name interestName = certName;
296
Yingdi Yu64206112013-12-24 11:16:32 +0800297 Interest interest(interestName);
298 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
299 interest.setInterestLifetimeMilliseconds(1000);
300
301 OnVerified onVerified = boost::bind(&ContactManager::onIdCertificateVerified, this, _1, certName);
302 OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onIdCertificateVerifyFailed, this, _1, certName);
303 TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onIdCertificateTimeoutNotify, this, certName);
304
305 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu908f8412013-11-09 00:03:26 -0800306}
307
308void
Yingdi Yu64206112013-12-24 11:16:32 +0800309ContactManager::onIdCertificateTimeoutNotify(const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800310{
311 emit contactCertificateFetchFailed (identity);
312}
Yingdi Yu64206112013-12-24 11:16:32 +0800313
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800314
315void
Yingdi Yu64206112013-12-24 11:16:32 +0800316ContactManager::onIdCertificateVerified(const shared_ptr<Data>& data, const Name& identity)
Yingdi Yu908f8412013-11-09 00:03:26 -0800317{
318 IdentityCertificate identityCertificate(*data);
319 emit contactCertificateFetched(identityCertificate);
320}
321
322void
Yingdi Yu64206112013-12-24 11:16:32 +0800323ContactManager::onIdCertificateVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800324{
325 emit contactCertificateFetchFailed (identity);
326}
Yingdi Yu908f8412013-11-09 00:03:26 -0800327
328void
Yingdi Yu64206112013-12-24 11:16:32 +0800329ContactManager::onTargetData(const shared_ptr<const ndn::Interest>& interest,
330 const shared_ptr<Data>& data,
331 int stepCount,
332 const OnVerified& onVerified,
333 const OnVerifyFailed& onVerifyFailed,
334 const TimeoutNotify& timeoutNotify)
335{
336 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
337
338 if (nextStep)
339 m_face->expressInterest
340 (*nextStep->interest_,
341 bind(&ContactManager::onCertData, this, _1, _2, nextStep),
342 bind(&ContactManager::onCertTimeout, this, _1, onVerifyFailed, data, nextStep));
343
344}
345
346void
347ContactManager::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
348 int retry,
349 int stepCount,
350 const OnVerified& onVerified,
351 const OnVerifyFailed& onVerifyFailed,
352 const TimeoutNotify& timeoutNotify)
353{
354 if(retry > 0)
355 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount);
356 else
357 {
358 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
359 timeoutNotify();
360 }
361}
362
363void
364ContactManager::onCertData(const shared_ptr<const ndn::Interest>& interest,
365 const shared_ptr<Data>& cert,
366 shared_ptr<ValidationRequest> previousStep)
367{
368 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert,
369 previousStep->stepCount_,
370 previousStep->onVerified_,
371 previousStep->onVerifyFailed_);
372
373 if (nextStep)
374 m_face->expressInterest
375 (*nextStep->interest_,
376 bind(&ContactManager::onCertData, this, _1, _2, nextStep),
377 bind(&ContactManager::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
378}
379
380void
381ContactManager::onCertTimeout(const shared_ptr<const ndn::Interest>& interest,
382 const OnVerifyFailed& onVerifyFailed,
383 const shared_ptr<Data>& data,
384 shared_ptr<ValidationRequest> nextStep)
385{
386 if(nextStep->retry_ > 0)
387 m_face->expressInterest(*interest,
388 bind(&ContactManager::onCertData,
389 this,
390 _1,
391 _2,
392 nextStep),
393 bind(&ContactManager::onCertTimeout,
394 this,
395 _1,
396 onVerifyFailed,
397 data,
398 nextStep));
399 else
400 onVerifyFailed(data);
401}
402
403void
404ContactManager::sendInterest(const Interest& interest,
405 const OnVerified& onVerified,
406 const OnVerifyFailed& onVerifyFailed,
407 const TimeoutNotify& timeoutNotify,
408 int retry /* = 1 */,
409 int stepCount /* = 0 */)
410{
Yingdi Yuaccbda92013-12-27 08:44:12 +0800411 uint64_t id = m_face->expressInterest(interest,
Yingdi Yu64206112013-12-24 11:16:32 +0800412 boost::bind(&ContactManager::onTargetData,
413 this,
414 _1,
415 _2,
416 stepCount,
417 onVerified,
418 onVerifyFailed,
419 timeoutNotify),
420 boost::bind(&ContactManager::onTargetTimeout,
421 this,
422 _1,
423 retry,
424 stepCount,
425 onVerified,
426 onVerifyFailed,
427 timeoutNotify));
Yingdi Yuaccbda92013-12-27 08:44:12 +0800428
429 _LOG_DEBUG("id: " << id << " entry id: " << m_face->getNode().getEntryIndexForExpressedInterest(interest.getName()));
Yingdi Yu64206112013-12-24 11:16:32 +0800430}
Yingdi Yu908f8412013-11-09 00:03:26 -0800431
432void
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700433ContactManager::updateProfileData(const Name& identity)
434{
435 // Get current profile;
Yingdi Yu64206112013-12-24 11:16:32 +0800436 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
437 if(CHRONOCHAT_NULL_PROFILE_PTR == newProfile)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700438 return;
Yingdi Yu64206112013-12-24 11:16:32 +0800439
440 shared_ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
441
442 if(CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR == newEndorseCertificate)
443 return;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700444
445 // Check if profile exists
Yingdi Yu64206112013-12-24 11:16:32 +0800446 Blob profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
447 if(CHRONOCHAT_NULL_BLOB != profileDataBlob)
448 m_contactStorage->updateSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700449 else
Yingdi Yu64206112013-12-24 11:16:32 +0800450 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yub35b8652013-11-07 11:32:40 -0800451
Yingdi Yu64206112013-12-24 11:16:32 +0800452 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700453}
454
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800455void
456ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
457{
Yingdi Yu64206112013-12-24 11:16:32 +0800458 Blob oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
459 shared_ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
460
461 if(CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR == newEndorseCertificate)
462 return;
463
464 if(CHRONOCHAT_NULL_BLOB != oldEndorseCertificateBlob)
465 m_contactStorage->updateEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800466 else
Yingdi Yu64206112013-12-24 11:16:32 +0800467 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identity);
468
469 publishEndorseCertificateInDNS(*newEndorseCertificate, signerIdentity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800470}
471
Yingdi Yu64206112013-12-24 11:16:32 +0800472shared_ptr<EndorseCertificate>
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800473ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
474{
Yingdi Yu64206112013-12-24 11:16:32 +0800475 shared_ptr<ContactItem> contact = getContact(identity);
476 if(contact == CHRONOCHAT_NULL_CONTACTITEM_PTR)
477 return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800478
Yingdi Yu64206112013-12-24 11:16:32 +0800479 Name signerKeyName = m_identityManager->getDefaultKeyNameForIdentity(signerIdentity);
480 Name signerCertName = m_identityManager->getDefaultCertificateNameForIdentity(signerIdentity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800481
Yingdi Yu64206112013-12-24 11:16:32 +0800482 vector<string> endorseList;
483 m_contactStorage->getEndorseList(identity, endorseList);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800484
Yingdi Yu64206112013-12-24 11:16:32 +0800485
Yingdi Yub35b8652013-11-07 11:32:40 -0800486 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800487 shared_ptr<EndorseCertificate> cert = make_shared<EndorseCertificate>(contact->getSelfEndorseCertificate(), signerKeyName, endorseList);
488 m_identityManager->signByCertificate(*cert, signerCertName);
489 return cert;
490 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800491 _LOG_ERROR("Exception: " << e.what());
Yingdi Yu64206112013-12-24 11:16:32 +0800492 return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR;
Yingdi Yub35b8652013-11-07 11:32:40 -0800493 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800494}
495
Yingdi Yu64206112013-12-24 11:16:32 +0800496void
497ContactManager::getContactItemList(vector<shared_ptr<ContactItem> >& contacts)
498{ return m_contactStorage->getAllContacts(contacts); }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700499
Yingdi Yu64206112013-12-24 11:16:32 +0800500shared_ptr<ContactItem>
Yingdi Yud40226b2013-10-23 14:05:12 -0700501ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800502{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yud40226b2013-10-23 14:05:12 -0700503
Yingdi Yu64206112013-12-24 11:16:32 +0800504shared_ptr<EndorseCertificate>
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700505ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
506 const Profile& profile)
507{
Yingdi Yu64206112013-12-24 11:16:32 +0800508 Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700509 if(0 == certificateName.size())
Yingdi Yu64206112013-12-24 11:16:32 +0800510 return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700511
Yingdi Yu64206112013-12-24 11:16:32 +0800512 ProfileData profileData(profile);
513 m_identityManager->signByCertificate(profileData, certificateName);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700514
Yingdi Yu64206112013-12-24 11:16:32 +0800515 shared_ptr<IdentityCertificate> signingCert = m_identityManager->getCertificate(certificateName);
516 if(CHRONOCHAT_NULL_IDENTITYCERTIFICATE_PTR == signingCert)
517 return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR;
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800518
Yingdi Yu64206112013-12-24 11:16:32 +0800519 Name signingKeyName = IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName());
Yingdi Yued8cfc42013-11-01 17:37:51 -0700520
Yingdi Yu64206112013-12-24 11:16:32 +0800521 shared_ptr<IdentityCertificate> kskCert;
522 if(signingKeyName.get(-1).toEscapedString().substr(0,4) == string("dsk-"))
Yingdi Yued8cfc42013-11-01 17:37:51 -0700523 {
Yingdi Yu64206112013-12-24 11:16:32 +0800524 const Sha256WithRsaSignature* dskCertSig = dynamic_cast<const Sha256WithRsaSignature*>(signingCert->getSignature());
Yingdi Yued8cfc42013-11-01 17:37:51 -0700525 // HACK! KSK certificate should be retrieved from network.
Yingdi Yu64206112013-12-24 11:16:32 +0800526 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800527
Yingdi Yu64206112013-12-24 11:16:32 +0800528 // TODO: check null existing cases.
529 Name kskCertName = m_identityManager->getDefaultCertificateNameForIdentity(keyName.getPrefix(-1));
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800530
Yingdi Yu64206112013-12-24 11:16:32 +0800531 kskCert = m_identityManager->getCertificate(kskCertName);
Yingdi Yued8cfc42013-11-01 17:37:51 -0700532 }
533 else
534 {
535 kskCert = signingCert;
Yingdi Yued8cfc42013-11-01 17:37:51 -0700536 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700537
Yingdi Yu64206112013-12-24 11:16:32 +0800538 if(CHRONOCHAT_NULL_IDENTITYCERTIFICATE_PTR == kskCert)
539 return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR;
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800540
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700541 vector<string> endorseList;
542 Profile::const_iterator it = profile.begin();
543 for(; it != profile.end(); it++)
544 endorseList.push_back(it->first);
545
Yingdi Yub35b8652013-11-07 11:32:40 -0800546 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800547 shared_ptr<EndorseCertificate> selfEndorseCertificate = make_shared<EndorseCertificate>(*kskCert, profileData, endorseList);
548 m_identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
549
550 return selfEndorseCertificate;
551 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800552 _LOG_ERROR("Exception: " << e.what());
Yingdi Yu64206112013-12-24 11:16:32 +0800553 return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR;
Yingdi Yub35b8652013-11-07 11:32:40 -0800554 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700555}
556
557
558void
Yingdi Yu64206112013-12-24 11:16:32 +0800559ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700560{
Yingdi Yu64206112013-12-24 11:16:32 +0800561 Data data;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700562
Yingdi Yu64206112013-12-24 11:16:32 +0800563 Name keyName = selfEndorseCertificate.getPublicKeyName();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700564 Name identity = keyName.getSubName(0, keyName.size()-1);
565
Yingdi Yu64206112013-12-24 11:16:32 +0800566 time_t nowSeconds = time(NULL);
567 struct tm current = *gmtime(&nowSeconds);
568 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700569
570 Name dnsName = identity;
Yingdi Yu64206112013-12-24 11:16:32 +0800571 dnsName.append("DNS").append("PROFILE").appendVersion(version);
572 data.setName(dnsName);
573
574 data.setContent(selfEndorseCertificate.wireEncode());
575
576 Name signCertName = m_identityManager->getDefaultCertificateNameForIdentity(identity);
577 m_identityManager->signByCertificate(data, signCertName);
578
579 m_dnsStorage->updateDnsSelfProfileData(data, identity);
Yingdi Yuc29fb982013-10-20 19:43:10 -0700580
Yingdi Yu64206112013-12-24 11:16:32 +0800581 m_transport->send(*data.wireEncode());
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700582}
583
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800584void
Yingdi Yu64206112013-12-24 11:16:32 +0800585ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate, const Name& signerIdentity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800586{
Yingdi Yu64206112013-12-24 11:16:32 +0800587 Data data;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800588
Yingdi Yu64206112013-12-24 11:16:32 +0800589 Name keyName = endorseCertificate.getPublicKeyName();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800590 Name endorsee = keyName.getSubName(0, keyName.size()-1);
591
Yingdi Yu64206112013-12-24 11:16:32 +0800592 time_t nowSeconds = time(NULL);
593 struct tm current = *gmtime(&nowSeconds);
594 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800595
596 Name dnsName = signerIdentity;
Yingdi Yu64206112013-12-24 11:16:32 +0800597 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(version);
598 data.setName(dnsName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800599
Yingdi Yu64206112013-12-24 11:16:32 +0800600 data.setContent(endorseCertificate.wireEncode());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800601
Yingdi Yu64206112013-12-24 11:16:32 +0800602 Name signCertName = m_identityManager->getDefaultCertificateNameForIdentity(signerIdentity);
603 m_identityManager->signByCertificate(data, signCertName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800604
Yingdi Yu64206112013-12-24 11:16:32 +0800605 m_dnsStorage->updateDnsEndorseOthers(data, signerIdentity, endorsee);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800606
Yingdi Yu64206112013-12-24 11:16:32 +0800607 m_transport->send(*data.wireEncode());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800608}
609
610void
611ContactManager::publishEndorsedDataInDns(const Name& identity)
612{
Yingdi Yu64206112013-12-24 11:16:32 +0800613 Data data;
614
615 time_t nowSeconds = time(NULL);
616 struct tm current = *gmtime(&nowSeconds);
617 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800618
619 Name dnsName = identity;
Yingdi Yu64206112013-12-24 11:16:32 +0800620 dnsName.append("DNS").append("ENDORSED").appendVersion(version);
621 data.setName(dnsName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800622
Yingdi Yu64206112013-12-24 11:16:32 +0800623 vector<Blob> collectEndorseList;
624 m_contactStorage->getCollectEndorseList(identity, collectEndorseList);
625
626 Chronos::EndorseCollection endorseCollection;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800627
Yingdi Yu64206112013-12-24 11:16:32 +0800628 vector<Blob>::const_iterator it = collectEndorseList.begin();
629 for(; it != collectEndorseList.end(); it++)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800630 {
Yingdi Yu64206112013-12-24 11:16:32 +0800631 string entryStr((const char*)it->buf(), it->size());
632 endorseCollection.add_endorsement()->set_blob(entryStr);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800633 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800634
Yingdi Yu64206112013-12-24 11:16:32 +0800635 string encoded;
636 endorseCollection.SerializeToString(&encoded);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800637
Yingdi Yu64206112013-12-24 11:16:32 +0800638 data.setContent((const uint8_t*)encoded.c_str(), encoded.size());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800639
Yingdi Yu64206112013-12-24 11:16:32 +0800640 Name signCertName = m_identityManager->getDefaultCertificateNameForIdentity(identity);
641 m_identityManager->signByCertificate(data, signCertName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800642
Yingdi Yu64206112013-12-24 11:16:32 +0800643 m_dnsStorage->updateDnsOthersEndorse(data, identity);
644
645 m_transport->send(*data.wireEncode());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800646}
647
Yingdi Yu908f8412013-11-09 00:03:26 -0800648void
649ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile)
650{
Yingdi Yu64206112013-12-24 11:16:32 +0800651 ProfileData profileData(profile);
Yingdi Yu908f8412013-11-09 00:03:26 -0800652
Yingdi Yu64206112013-12-24 11:16:32 +0800653 Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity (m_defaultIdentity);
654 m_identityManager->signByCertificate(profileData, certificateName);
Yingdi Yu908f8412013-11-09 00:03:26 -0800655
Yingdi Yu908f8412013-11-09 00:03:26 -0800656
657 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800658 EndorseCertificate endorseCertificate(identityCertificate, profileData);
659
660 m_identityManager->signByCertificate(endorseCertificate, certificateName);
661
662 ContactItem contactItem(endorseCertificate);
663
Yingdi Yu908f8412013-11-09 00:03:26 -0800664 m_contactStorage->addContact(contactItem);
Yingdi Yu64206112013-12-24 11:16:32 +0800665
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800666 emit contactAdded(contactItem.getNameSpace());
Yingdi Yu64206112013-12-24 11:16:32 +0800667
668 }catch(std::exception& e){
Yingdi Yu908f8412013-11-09 00:03:26 -0800669 emit warning(e.what());
670 _LOG_ERROR("Exception: " << e.what());
671 return;
672 }
673}
674
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800675void
676ContactManager::removeContact(const ndn::Name& contactNameSpace)
677{
Yingdi Yu64206112013-12-24 11:16:32 +0800678 shared_ptr<ContactItem> contact = getContact(contactNameSpace);
679 if(contact == CHRONOCHAT_NULL_CONTACTITEM_PTR)
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800680 return;
681 m_contactStorage->removeContact(contactNameSpace);
682 emit contactRemoved(contact->getPublicKeyName());
683}
Yingdi Yu908f8412013-11-09 00:03:26 -0800684
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700685
686#if WAF
687#include "contact-manager.moc"
688#include "contact-manager.cpp.moc"
689#endif