blob: a70be49b94a62dc1e00d4d0bbeeeea7c21b457d0 [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
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080011#if __clang__
12#pragma clang diagnostic push
13#pragma clang diagnostic ignored "-Wreorder"
14#pragma clang diagnostic ignored "-Wtautological-compare"
15#pragma clang diagnostic ignored "-Wunused-variable"
16#pragma clang diagnostic ignored "-Wunused-function"
17#elif __GNUC__
18#pragma GCC diagnostic ignored "-Wreorder"
19#pragma GCC diagnostic ignored "-Wunused-variable"
20#pragma GCC diagnostic ignored "-Wunused-function"
21#endif
22
23
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070024#include "contact-manager.h"
25
Yingdi Yuaa8d7692013-10-18 17:05:02 -070026#ifndef Q_MOC_RUN
Yingdi Yu64206112013-12-24 11:16:32 +080027#include <ndn-cpp/face.hpp>
Yingdi Yueaa84e22014-01-16 10:30:26 -080028#include <ndn-cpp/security/signature-sha256-with-rsa.hpp>
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080029#include <ndn-cpp/security/verifier.hpp>
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080030#include <cryptopp/base64.h>
Yingdi Yueaa84e22014-01-16 10:30:26 -080031#include <ndn-cpp-et/policy/sec-rule-identity.hpp>
Yingdi Yu7630f642014-01-16 19:13:03 -080032#include <ndn-cpp-et/policy/sec-policy-simple.hpp>
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070033#include <fstream>
Yingdi Yu64206112013-12-24 11:16:32 +080034#include "endorse-collection.pb.h"
Yingdi Yu590fa5d2013-10-18 18:35:09 -070035#include "logging.h"
Yingdi Yuaa8d7692013-10-18 17:05:02 -070036#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070037
38using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080039using namespace ndn::ptr_lib;
40using namespace std;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070041
Yingdi Yu590fa5d2013-10-18 18:35:09 -070042INIT_LOGGER("ContactManager");
43
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080044ContactManager::ContactManager(shared_ptr<KeyChain> keyChain,
Yingdi Yuaccbda92013-12-27 08:44:12 +080045 shared_ptr<Face> face,
Yingdi Yuaccbda92013-12-27 08:44:12 +080046 QObject* parent)
47 : QObject(parent),
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080048 m_face(face)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070049{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080050 m_keyChain = keyChain;
Yingdi Yu64206112013-12-24 11:16:32 +080051 m_contactStorage = make_shared<ContactStorage>();
52 m_dnsStorage = make_shared<DnsStorage>();
Yingdi Yuaa8d7692013-10-18 17:05:02 -070053
Yingdi Yu64206112013-12-24 11:16:32 +080054 initializeSecurity();
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070055}
56
57ContactManager::~ContactManager()
Yingdi Yuaccbda92013-12-27 08:44:12 +080058{}
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070059
Yingdi Yu64206112013-12-24 11:16:32 +080060void
61ContactManager::initializeSecurity()
62{
Yingdi Yu7630f642014-01-16 19:13:03 -080063 shared_ptr<SecPolicySimple> policy = make_shared<SecPolicySimple>();
64 m_verifier = make_shared<Verifier>(policy);
65 m_verifier->setFace(m_face);
Yingdi Yu64206112013-12-24 11:16:32 +080066
Yingdi Yu7630f642014-01-16 19:13:03 -080067 policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<DNS>]*)<DNS><ENDORSED>",
68 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
69 "==", "\\1", "\\1\\2", true));
70 policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<DNS>]*)<DNS><PROFILE>",
71 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
72 "==", "\\1", "\\1\\2", true));
73 policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
74 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
75 "==", "\\1", "\\1\\2", true));
76 policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
77 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
78 ">", "\\1\\2", "\\1", true));
79 policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
80 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
81 "==", "\\1", "\\1\\2", true));
82 policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^(<>*)$",
83 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
84 ">", "\\1", "\\1\\2", true));
Yingdi Yu8fb16a42013-11-10 18:35:09 -080085
Yingdi Yuaa8d7692013-10-18 17:05:02 -070086
Yingdi Yu7630f642014-01-16 19:13:03 -080087 policy->addSigningPolicyRule(make_shared<SecRuleIdentity>("^([^<DNS>]*)<DNS><PROFILE>",
88 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
89 "==", "\\1", "\\1\\2", true));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070090
Yingdi Yu8fb16a42013-11-10 18:35:09 -080091
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080092 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
93HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
9467biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
955X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
96WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
97Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
98MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
99nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
100c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
101MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
102b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
103yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
104I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
105/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
10672P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
107VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
108iVUF1QIBEQAA");
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700109
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800110 string decoded;
111 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
112 TrustAnchor.size(),
113 true,
114 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
Yingdi Yu64206112013-12-24 11:16:32 +0800115 Data data;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800116 data.wireDecode(Block(reinterpret_cast<const uint8_t*>(decoded.c_str()), decoded.size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800117 shared_ptr<IdentityCertificate> anchor = make_shared<IdentityCertificate>(data);
Yingdi Yu7630f642014-01-16 19:13:03 -0800118 policy->addTrustAnchor(anchor);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800119
120#ifdef _DEBUG
121
122 const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
123zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
124qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
1256M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
126DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
127Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
128MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
129qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
130APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
131MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
132MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
133/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
134sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
135H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
136gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
137OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
138F7Wh5ayeo8NBKDsCAwEAAQAA");
139
140 string decoded2;
141 CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()),
142 FakeAnchor.size(),
143 true,
144 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
Yingdi Yu64206112013-12-24 11:16:32 +0800145 Data data2;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800146 data2.wireDecode(Block(reinterpret_cast<const uint8_t*>(decoded.c_str()), decoded.size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800147 shared_ptr<IdentityCertificate>anchor2 = make_shared<IdentityCertificate>(data2);
Yingdi Yu7630f642014-01-16 19:13:03 -0800148 policy->addTrustAnchor(anchor2);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800149
150#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700151}
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700152
153
154void
155ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
156{
157 Name interestName = identity;
158 interestName.append("DNS").append("PROFILE");
159
Yingdi Yu64206112013-12-24 11:16:32 +0800160 Interest interest(interestName);
Yingdi Yu64206112013-12-24 11:16:32 +0800161
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800162 OnVerified onVerified = func_lib::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, this, _1, identity);
163 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactManager::onDnsSelfEndorseCertificateVerifyFailed, this, _1, identity);
164 TimeoutNotify timeoutNotify = func_lib::bind(&ContactManager::onDnsSelfEndorseCertificateTimeoutNotify, this, identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800165
166 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700167}
168
169void
Yingdi Yu64206112013-12-24 11:16:32 +0800170ContactManager::onDnsSelfEndorseCertificateTimeoutNotify(const Name& identity)
171{ emit contactFetchFailed(identity); }
172
173void
174ContactManager::onDnsSelfEndorseCertificateVerified(const shared_ptr<Data>& data,
175 const Name& identity)
176{
177 try{
178 Data plainData;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800179 plainData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800180 EndorseCertificate selfEndorseCertificate(plainData);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800181 if(Verifier::verifySignature(plainData, plainData.getSignature(), selfEndorseCertificate.getPublicKeyInfo()))
Yingdi Yu64206112013-12-24 11:16:32 +0800182 emit contactFetched (selfEndorseCertificate);
183 else
184 emit contactFetchFailed (identity);
185 }catch(std::exception& e){
186 _LOG_ERROR("Exception: " << e.what());
187 emit contactFetchFailed (identity);
188 }
189}
190
191void
192ContactManager::onDnsSelfEndorseCertificateVerifyFailed(const shared_ptr<Data>& data,
193 const Name& identity)
194{ emit contactFetchFailed (identity); }
195
196void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800197ContactManager::fetchCollectEndorse(const ndn::Name& identity)
198{
199 Name interestName = identity;
200 interestName.append("DNS").append("ENDORSED");
201
Yingdi Yu64206112013-12-24 11:16:32 +0800202 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800203 interest.setInterestLifetime(1000);
Yingdi Yu64206112013-12-24 11:16:32 +0800204
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800205 OnVerified onVerified = func_lib::bind(&ContactManager::onDnsCollectEndorseVerified, this, _1, identity);
206 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactManager::onDnsCollectEndorseVerifyFailed, this, _1, identity);
207 TimeoutNotify timeoutNotify = func_lib::bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800208
209 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800210}
211
212void
Yingdi Yu64206112013-12-24 11:16:32 +0800213ContactManager::onDnsCollectEndorseTimeoutNotify(const Name& identity)
214{
215 emit collectEndorseFetchFailed (identity);
216}
217
218void
219ContactManager::onDnsCollectEndorseVerified(const shared_ptr<Data>& data, const Name& identity)
220{ emit collectEndorseFetched (*data); }
221
222void
223ContactManager::onDnsCollectEndorseVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
224{ emit collectEndorseFetchFailed (identity); }
225
226
227void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800228ContactManager::fetchKey(const ndn::Name& certName)
229{
230 Name interestName = certName;
231
Yingdi Yu64206112013-12-24 11:16:32 +0800232 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800233 interest.setInterestLifetime(1000);
Yingdi Yu64206112013-12-24 11:16:32 +0800234
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800235 OnVerified onVerified = func_lib::bind(&ContactManager::onKeyVerified, this, _1, certName);
236 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactManager::onKeyVerifyFailed, this, _1, certName);
237 TimeoutNotify timeoutNotify = func_lib::bind(&ContactManager::onKeyTimeoutNotify, this, certName);
Yingdi Yu64206112013-12-24 11:16:32 +0800238
239 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
240}
241
242
243void
244ContactManager::onKeyVerified(const shared_ptr<Data>& data, const Name& identity)
245{
246 IdentityCertificate identityCertificate(*data);
247
248 Profile profile(identityCertificate);
249 ProfileData profileData(profile);
250
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800251 Name certificateName = m_keyChain->getDefaultCertificateName();
252 m_keyChain->sign(profileData, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800253
254 try{
255 EndorseCertificate endorseCertificate(identityCertificate, profileData);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800256 m_keyChain->sign(endorseCertificate, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800257 emit contactKeyFetched (endorseCertificate);
258 }catch(std::exception& e){
259 _LOG_ERROR("Exception: " << e.what());
260 return;
261 }
262}
263
264void
265ContactManager::onKeyVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
266{
267 _LOG_DEBUG("Key cannot be verified!");
268 emit contactKeyFetchFailed (identity);
269}
270
271void
272ContactManager::onKeyTimeoutNotify(const Name& identity)
273{
274 _LOG_DEBUG("Key timeout!");
275 emit contactKeyFetchFailed(identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800276}
277
278void
Yingdi Yuaccbda92013-12-27 08:44:12 +0800279ContactManager::fetchIdCertificate(const Name& certName)
Yingdi Yu908f8412013-11-09 00:03:26 -0800280{
281 Name interestName = certName;
282
Yingdi Yu64206112013-12-24 11:16:32 +0800283 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800284 interest.setInterestLifetime(1000);
Yingdi Yu64206112013-12-24 11:16:32 +0800285
286 OnVerified onVerified = boost::bind(&ContactManager::onIdCertificateVerified, this, _1, certName);
287 OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onIdCertificateVerifyFailed, this, _1, certName);
288 TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onIdCertificateTimeoutNotify, this, certName);
289
290 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu908f8412013-11-09 00:03:26 -0800291}
292
293void
Yingdi Yu64206112013-12-24 11:16:32 +0800294ContactManager::onIdCertificateTimeoutNotify(const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800295{
296 emit contactCertificateFetchFailed (identity);
297}
Yingdi Yu64206112013-12-24 11:16:32 +0800298
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800299
300void
Yingdi Yu64206112013-12-24 11:16:32 +0800301ContactManager::onIdCertificateVerified(const shared_ptr<Data>& data, const Name& identity)
Yingdi Yu908f8412013-11-09 00:03:26 -0800302{
303 IdentityCertificate identityCertificate(*data);
304 emit contactCertificateFetched(identityCertificate);
305}
306
307void
Yingdi Yu64206112013-12-24 11:16:32 +0800308ContactManager::onIdCertificateVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800309{
310 emit contactCertificateFetchFailed (identity);
311}
Yingdi Yu908f8412013-11-09 00:03:26 -0800312
313void
Yingdi Yu64206112013-12-24 11:16:32 +0800314ContactManager::onTargetData(const shared_ptr<const ndn::Interest>& interest,
315 const shared_ptr<Data>& data,
Yingdi Yu64206112013-12-24 11:16:32 +0800316 const OnVerified& onVerified,
Yingdi Yu7630f642014-01-16 19:13:03 -0800317 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu64206112013-12-24 11:16:32 +0800318{
Yingdi Yu7630f642014-01-16 19:13:03 -0800319 m_verifier->verifyData(data, onVerified, onVerifyFailed);
Yingdi Yu64206112013-12-24 11:16:32 +0800320}
321
322void
323ContactManager::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
324 int retry,
Yingdi Yu64206112013-12-24 11:16:32 +0800325 const OnVerified& onVerified,
326 const OnVerifyFailed& onVerifyFailed,
327 const TimeoutNotify& timeoutNotify)
328{
329 if(retry > 0)
Yingdi Yu7630f642014-01-16 19:13:03 -0800330 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1);
Yingdi Yu64206112013-12-24 11:16:32 +0800331 else
332 {
333 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
334 timeoutNotify();
335 }
336}
337
338void
Yingdi Yu64206112013-12-24 11:16:32 +0800339ContactManager::sendInterest(const Interest& interest,
340 const OnVerified& onVerified,
341 const OnVerifyFailed& onVerifyFailed,
342 const TimeoutNotify& timeoutNotify,
Yingdi Yu7630f642014-01-16 19:13:03 -0800343 int retry /* = 1 */)
Yingdi Yu64206112013-12-24 11:16:32 +0800344{
Yingdi Yuaccbda92013-12-27 08:44:12 +0800345 uint64_t id = m_face->expressInterest(interest,
Yingdi Yu64206112013-12-24 11:16:32 +0800346 boost::bind(&ContactManager::onTargetData,
347 this,
348 _1,
349 _2,
Yingdi Yu64206112013-12-24 11:16:32 +0800350 onVerified,
Yingdi Yu7630f642014-01-16 19:13:03 -0800351 onVerifyFailed),
Yingdi Yu64206112013-12-24 11:16:32 +0800352 boost::bind(&ContactManager::onTargetTimeout,
353 this,
354 _1,
355 retry,
Yingdi Yu64206112013-12-24 11:16:32 +0800356 onVerified,
357 onVerifyFailed,
358 timeoutNotify));
Yingdi Yuaccbda92013-12-27 08:44:12 +0800359
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800360 // _LOG_DEBUG("id: " << id << " entry id: " << m_face->getNode().getEntryIndexForExpressedInterest(interest.getName()));
Yingdi Yu64206112013-12-24 11:16:32 +0800361}
Yingdi Yu908f8412013-11-09 00:03:26 -0800362
363void
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700364ContactManager::updateProfileData(const Name& identity)
365{
366 // Get current profile;
Yingdi Yu64206112013-12-24 11:16:32 +0800367 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800368 if(static_cast<bool>(newProfile))
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700369 return;
Yingdi Yu64206112013-12-24 11:16:32 +0800370
371 shared_ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
372
Yingdi Yueaa84e22014-01-16 10:30:26 -0800373 if(static_cast<bool>(newEndorseCertificate))
Yingdi Yu64206112013-12-24 11:16:32 +0800374 return;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700375
376 // Check if profile exists
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800377 try{
378 Block profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800379 m_contactStorage->updateSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800380 }catch(ContactStorage::Error &e){
Yingdi Yu64206112013-12-24 11:16:32 +0800381 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800382 }
Yingdi Yub35b8652013-11-07 11:32:40 -0800383
Yingdi Yu64206112013-12-24 11:16:32 +0800384 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700385}
386
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800387void
388ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
389{
Yingdi Yu64206112013-12-24 11:16:32 +0800390 shared_ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
391
Yingdi Yueaa84e22014-01-16 10:30:26 -0800392 if(static_cast<bool>(newEndorseCertificate))
Yingdi Yu64206112013-12-24 11:16:32 +0800393 return;
394
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800395 try{
396 Block oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800397 m_contactStorage->updateEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800398 }catch(ContactStorage::Error &e){
Yingdi Yu64206112013-12-24 11:16:32 +0800399 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800400 }
401
Yingdi Yu64206112013-12-24 11:16:32 +0800402 publishEndorseCertificateInDNS(*newEndorseCertificate, signerIdentity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800403}
404
Yingdi Yu64206112013-12-24 11:16:32 +0800405shared_ptr<EndorseCertificate>
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800406ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
407{
Yingdi Yu64206112013-12-24 11:16:32 +0800408 shared_ptr<ContactItem> contact = getContact(identity);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800409 if(static_cast<bool>(contact))
410 return shared_ptr<EndorseCertificate>();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800411
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800412 Name signerKeyName = m_keyChain->getDefaultKeyNameForIdentity(signerIdentity);
413 Name signerCertName = m_keyChain->getDefaultCertificateNameForIdentity(signerIdentity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800414
Yingdi Yu64206112013-12-24 11:16:32 +0800415 vector<string> endorseList;
416 m_contactStorage->getEndorseList(identity, endorseList);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800417
Yingdi Yu64206112013-12-24 11:16:32 +0800418
Yingdi Yub35b8652013-11-07 11:32:40 -0800419 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800420 shared_ptr<EndorseCertificate> cert = make_shared<EndorseCertificate>(contact->getSelfEndorseCertificate(), signerKeyName, endorseList);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800421 m_keyChain->sign(*cert, signerCertName);
Yingdi Yu64206112013-12-24 11:16:32 +0800422 return cert;
423 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800424 _LOG_ERROR("Exception: " << e.what());
Yingdi Yueaa84e22014-01-16 10:30:26 -0800425 return shared_ptr<EndorseCertificate>();
Yingdi Yub35b8652013-11-07 11:32:40 -0800426 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800427}
428
Yingdi Yu64206112013-12-24 11:16:32 +0800429void
430ContactManager::getContactItemList(vector<shared_ptr<ContactItem> >& contacts)
431{ return m_contactStorage->getAllContacts(contacts); }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700432
Yingdi Yu64206112013-12-24 11:16:32 +0800433shared_ptr<ContactItem>
Yingdi Yud40226b2013-10-23 14:05:12 -0700434ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800435{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yud40226b2013-10-23 14:05:12 -0700436
Yingdi Yu64206112013-12-24 11:16:32 +0800437shared_ptr<EndorseCertificate>
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700438ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
439 const Profile& profile)
440{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800441 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700442 if(0 == certificateName.size())
Yingdi Yueaa84e22014-01-16 10:30:26 -0800443 return shared_ptr<EndorseCertificate>();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700444
Yingdi Yu64206112013-12-24 11:16:32 +0800445 ProfileData profileData(profile);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800446 m_keyChain->sign(profileData, certificateName);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700447
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800448 shared_ptr<IdentityCertificate> signingCert = m_keyChain->getCertificate(certificateName);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800449 if(static_cast<bool>(signingCert))
450 return shared_ptr<EndorseCertificate>();
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800451
Yingdi Yu64206112013-12-24 11:16:32 +0800452 Name signingKeyName = IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName());
Yingdi Yued8cfc42013-11-01 17:37:51 -0700453
Yingdi Yu64206112013-12-24 11:16:32 +0800454 shared_ptr<IdentityCertificate> kskCert;
455 if(signingKeyName.get(-1).toEscapedString().substr(0,4) == string("dsk-"))
Yingdi Yued8cfc42013-11-01 17:37:51 -0700456 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800457 SignatureSha256WithRsa dskCertSig(signingCert->getSignature());
Yingdi Yued8cfc42013-11-01 17:37:51 -0700458 // HACK! KSK certificate should be retrieved from network.
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800459 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(dskCertSig.getKeyLocator().getName());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800460
Yingdi Yu64206112013-12-24 11:16:32 +0800461 // TODO: check null existing cases.
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800462 Name kskCertName = m_keyChain->getDefaultCertificateNameForIdentity(keyName.getPrefix(-1));
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800463
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800464 kskCert = m_keyChain->getCertificate(kskCertName);
Yingdi Yued8cfc42013-11-01 17:37:51 -0700465 }
466 else
467 {
468 kskCert = signingCert;
Yingdi Yued8cfc42013-11-01 17:37:51 -0700469 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700470
Yingdi Yueaa84e22014-01-16 10:30:26 -0800471 if(static_cast<bool>(kskCert))
472 return shared_ptr<EndorseCertificate>();
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800473
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700474 vector<string> endorseList;
475 Profile::const_iterator it = profile.begin();
476 for(; it != profile.end(); it++)
477 endorseList.push_back(it->first);
478
Yingdi Yub35b8652013-11-07 11:32:40 -0800479 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800480 shared_ptr<EndorseCertificate> selfEndorseCertificate = make_shared<EndorseCertificate>(*kskCert, profileData, endorseList);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800481 m_keyChain->sign(*selfEndorseCertificate, kskCert->getName());
Yingdi Yu64206112013-12-24 11:16:32 +0800482
483 return selfEndorseCertificate;
484 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800485 _LOG_ERROR("Exception: " << e.what());
Yingdi Yueaa84e22014-01-16 10:30:26 -0800486 return shared_ptr<EndorseCertificate>();
Yingdi Yub35b8652013-11-07 11:32:40 -0800487 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700488}
489
490
491void
Yingdi Yu64206112013-12-24 11:16:32 +0800492ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700493{
Yingdi Yu64206112013-12-24 11:16:32 +0800494 Data data;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700495
Yingdi Yu64206112013-12-24 11:16:32 +0800496 Name keyName = selfEndorseCertificate.getPublicKeyName();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700497 Name identity = keyName.getSubName(0, keyName.size()-1);
498
Yingdi Yu64206112013-12-24 11:16:32 +0800499 time_t nowSeconds = time(NULL);
500 struct tm current = *gmtime(&nowSeconds);
501 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700502
503 Name dnsName = identity;
Yingdi Yu64206112013-12-24 11:16:32 +0800504 dnsName.append("DNS").append("PROFILE").appendVersion(version);
505 data.setName(dnsName);
506
507 data.setContent(selfEndorseCertificate.wireEncode());
508
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800509 Name signCertName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
510 m_keyChain->sign(data, signCertName);
Yingdi Yu64206112013-12-24 11:16:32 +0800511
512 m_dnsStorage->updateDnsSelfProfileData(data, identity);
Yingdi Yuc29fb982013-10-20 19:43:10 -0700513
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800514 m_face->put(data);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700515}
516
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800517void
Yingdi Yu64206112013-12-24 11:16:32 +0800518ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate, const Name& signerIdentity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800519{
Yingdi Yu64206112013-12-24 11:16:32 +0800520 Data data;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800521
Yingdi Yu64206112013-12-24 11:16:32 +0800522 Name keyName = endorseCertificate.getPublicKeyName();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800523 Name endorsee = keyName.getSubName(0, keyName.size()-1);
524
Yingdi Yu64206112013-12-24 11:16:32 +0800525 time_t nowSeconds = time(NULL);
526 struct tm current = *gmtime(&nowSeconds);
527 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800528
529 Name dnsName = signerIdentity;
Yingdi Yu64206112013-12-24 11:16:32 +0800530 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(version);
531 data.setName(dnsName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800532
Yingdi Yu64206112013-12-24 11:16:32 +0800533 data.setContent(endorseCertificate.wireEncode());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800534
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800535 Name signCertName = m_keyChain->getDefaultCertificateNameForIdentity(signerIdentity);
536 m_keyChain->sign(data, signCertName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800537
Yingdi Yu64206112013-12-24 11:16:32 +0800538 m_dnsStorage->updateDnsEndorseOthers(data, signerIdentity, endorsee);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800539
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800540 m_face->put(data);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800541}
542
543void
544ContactManager::publishEndorsedDataInDns(const Name& identity)
545{
Yingdi Yu64206112013-12-24 11:16:32 +0800546 Data data;
547
548 time_t nowSeconds = time(NULL);
549 struct tm current = *gmtime(&nowSeconds);
550 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800551
552 Name dnsName = identity;
Yingdi Yu64206112013-12-24 11:16:32 +0800553 dnsName.append("DNS").append("ENDORSED").appendVersion(version);
554 data.setName(dnsName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800555
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800556 vector<Buffer> collectEndorseList;
Yingdi Yu64206112013-12-24 11:16:32 +0800557 m_contactStorage->getCollectEndorseList(identity, collectEndorseList);
558
559 Chronos::EndorseCollection endorseCollection;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800560
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800561 vector<Buffer>::const_iterator it = collectEndorseList.begin();
Yingdi Yu64206112013-12-24 11:16:32 +0800562 for(; it != collectEndorseList.end(); it++)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800563 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800564 string entryStr(reinterpret_cast<const char*>(it->buf()), it->size());
Yingdi Yu64206112013-12-24 11:16:32 +0800565 endorseCollection.add_endorsement()->set_blob(entryStr);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800566 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800567
Yingdi Yu64206112013-12-24 11:16:32 +0800568 string encoded;
569 endorseCollection.SerializeToString(&encoded);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800570
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800571 data.setContent(reinterpret_cast<const uint8_t*>(encoded.c_str()), encoded.size());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800572
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800573 Name signCertName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
574 m_keyChain->sign(data, signCertName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800575
Yingdi Yu64206112013-12-24 11:16:32 +0800576 m_dnsStorage->updateDnsOthersEndorse(data, identity);
577
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800578 m_face->put(data);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800579}
580
Yingdi Yu908f8412013-11-09 00:03:26 -0800581void
582ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile)
583{
Yingdi Yu64206112013-12-24 11:16:32 +0800584 ProfileData profileData(profile);
Yingdi Yu908f8412013-11-09 00:03:26 -0800585
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800586 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity (m_defaultIdentity);
587 m_keyChain->sign(profileData, certificateName);
Yingdi Yu908f8412013-11-09 00:03:26 -0800588
Yingdi Yu908f8412013-11-09 00:03:26 -0800589
590 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800591 EndorseCertificate endorseCertificate(identityCertificate, profileData);
592
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800593 m_keyChain->sign(endorseCertificate, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800594
595 ContactItem contactItem(endorseCertificate);
596
Yingdi Yu908f8412013-11-09 00:03:26 -0800597 m_contactStorage->addContact(contactItem);
Yingdi Yu64206112013-12-24 11:16:32 +0800598
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800599 emit contactAdded(contactItem.getNameSpace());
Yingdi Yu64206112013-12-24 11:16:32 +0800600
601 }catch(std::exception& e){
Yingdi Yu908f8412013-11-09 00:03:26 -0800602 emit warning(e.what());
603 _LOG_ERROR("Exception: " << e.what());
604 return;
605 }
606}
607
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800608void
609ContactManager::removeContact(const ndn::Name& contactNameSpace)
610{
Yingdi Yu64206112013-12-24 11:16:32 +0800611 shared_ptr<ContactItem> contact = getContact(contactNameSpace);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800612 if(static_cast<bool>(contact))
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800613 return;
614 m_contactStorage->removeContact(contactNameSpace);
615 emit contactRemoved(contact->getPublicKeyName());
616}
Yingdi Yu908f8412013-11-09 00:03:26 -0800617
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700618
619#if WAF
620#include "contact-manager.moc"
621#include "contact-manager.cpp.moc"
622#endif