blob: aea34fc0b5efd131653480d30ddb5ad6f8f2c6e1 [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 Yu0b82a4e2013-10-18 11:29:25 -070032#include <fstream>
Yingdi Yu64206112013-12-24 11:16:32 +080033#include "endorse-collection.pb.h"
Yingdi Yu590fa5d2013-10-18 18:35:09 -070034#include "logging.h"
Yingdi Yuaa8d7692013-10-18 17:05:02 -070035#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070036
37using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080038using namespace ndn::ptr_lib;
39using namespace std;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070040
Yingdi Yu590fa5d2013-10-18 18:35:09 -070041INIT_LOGGER("ContactManager");
42
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080043ContactManager::ContactManager(shared_ptr<KeyChain> keyChain,
Yingdi Yuaccbda92013-12-27 08:44:12 +080044 shared_ptr<Face> face,
Yingdi Yuaccbda92013-12-27 08:44:12 +080045 QObject* parent)
46 : QObject(parent),
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080047 m_face(face)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070048{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080049 m_keyChain = keyChain;
Yingdi Yu64206112013-12-24 11:16:32 +080050 m_contactStorage = make_shared<ContactStorage>();
51 m_dnsStorage = make_shared<DnsStorage>();
Yingdi Yuaa8d7692013-10-18 17:05:02 -070052
Yingdi Yu64206112013-12-24 11:16:32 +080053 initializeSecurity();
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070054}
55
56ContactManager::~ContactManager()
Yingdi Yuaccbda92013-12-27 08:44:12 +080057{}
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070058
Yingdi Yu64206112013-12-24 11:16:32 +080059void
60ContactManager::initializeSecurity()
61{
Yingdi Yueaa84e22014-01-16 10:30:26 -080062 m_policy = make_shared<SecPolicySimple>();
Yingdi Yu64206112013-12-24 11:16:32 +080063
Yingdi Yueaa84e22014-01-16 10:30:26 -080064 m_policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<DNS>]*)<DNS><ENDORSED>",
Yingdi Yu64206112013-12-24 11:16:32 +080065 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
66 "==", "\\1", "\\1\\2", true));
Yingdi Yueaa84e22014-01-16 10:30:26 -080067 m_policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yu64206112013-12-24 11:16:32 +080068 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
69 "==", "\\1", "\\1\\2", true));
Yingdi Yueaa84e22014-01-16 10:30:26 -080070 m_policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yu64206112013-12-24 11:16:32 +080071 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
72 "==", "\\1", "\\1\\2", true));
Yingdi Yueaa84e22014-01-16 10:30:26 -080073 m_policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yu64206112013-12-24 11:16:32 +080074 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
75 ">", "\\1\\2", "\\1", true));
Yingdi Yueaa84e22014-01-16 10:30:26 -080076 m_policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yu64206112013-12-24 11:16:32 +080077 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
78 "==", "\\1", "\\1\\2", true));
Yingdi Yueaa84e22014-01-16 10:30:26 -080079 m_policy->addVerificationPolicyRule(make_shared<SecRuleIdentity>("^(<>*)$",
Yingdi Yu64206112013-12-24 11:16:32 +080080 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
81 ">", "\\1", "\\1\\2", true));
Yingdi Yu8fb16a42013-11-10 18:35:09 -080082
Yingdi Yuaa8d7692013-10-18 17:05:02 -070083
Yingdi Yueaa84e22014-01-16 10:30:26 -080084 m_policy->addSigningPolicyRule(make_shared<SecRuleIdentity>("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yu64206112013-12-24 11:16:32 +080085 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
86 "==", "\\1", "\\1\\2", true));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070087
Yingdi Yu8fb16a42013-11-10 18:35:09 -080088
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080089 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
90HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
9167biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
925X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
93WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
94Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
95MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
96nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
97c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
98MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
99b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
100yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
101I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
102/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
10372P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
104VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
105iVUF1QIBEQAA");
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700106
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800107 string decoded;
108 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
109 TrustAnchor.size(),
110 true,
111 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
Yingdi Yu64206112013-12-24 11:16:32 +0800112 Data data;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800113 data.wireDecode(Block(reinterpret_cast<const uint8_t*>(decoded.c_str()), decoded.size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800114 shared_ptr<IdentityCertificate> anchor = make_shared<IdentityCertificate>(data);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800115 m_policy->addTrustAnchor(anchor);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800116
117#ifdef _DEBUG
118
119 const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
120zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
121qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
1226M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
123DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
124Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
125MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
126qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
127APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
128MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
129MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
130/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
131sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
132H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
133gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
134OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
135F7Wh5ayeo8NBKDsCAwEAAQAA");
136
137 string decoded2;
138 CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()),
139 FakeAnchor.size(),
140 true,
141 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
Yingdi Yu64206112013-12-24 11:16:32 +0800142 Data data2;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800143 data2.wireDecode(Block(reinterpret_cast<const uint8_t*>(decoded.c_str()), decoded.size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800144 shared_ptr<IdentityCertificate>anchor2 = make_shared<IdentityCertificate>(data2);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800145 m_policy->addTrustAnchor(anchor2);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800146
147#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700148}
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700149
150
151void
152ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
153{
154 Name interestName = identity;
155 interestName.append("DNS").append("PROFILE");
156
Yingdi Yu64206112013-12-24 11:16:32 +0800157 Interest interest(interestName);
Yingdi Yu64206112013-12-24 11:16:32 +0800158
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800159 OnVerified onVerified = func_lib::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, this, _1, identity);
160 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactManager::onDnsSelfEndorseCertificateVerifyFailed, this, _1, identity);
161 TimeoutNotify timeoutNotify = func_lib::bind(&ContactManager::onDnsSelfEndorseCertificateTimeoutNotify, this, identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800162
163 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700164}
165
166void
Yingdi Yu64206112013-12-24 11:16:32 +0800167ContactManager::onDnsSelfEndorseCertificateTimeoutNotify(const Name& identity)
168{ emit contactFetchFailed(identity); }
169
170void
171ContactManager::onDnsSelfEndorseCertificateVerified(const shared_ptr<Data>& data,
172 const Name& identity)
173{
174 try{
175 Data plainData;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800176 plainData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800177 EndorseCertificate selfEndorseCertificate(plainData);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800178 if(Verifier::verifySignature(plainData, plainData.getSignature(), selfEndorseCertificate.getPublicKeyInfo()))
Yingdi Yu64206112013-12-24 11:16:32 +0800179 emit contactFetched (selfEndorseCertificate);
180 else
181 emit contactFetchFailed (identity);
182 }catch(std::exception& e){
183 _LOG_ERROR("Exception: " << e.what());
184 emit contactFetchFailed (identity);
185 }
186}
187
188void
189ContactManager::onDnsSelfEndorseCertificateVerifyFailed(const shared_ptr<Data>& data,
190 const Name& identity)
191{ emit contactFetchFailed (identity); }
192
193void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800194ContactManager::fetchCollectEndorse(const ndn::Name& identity)
195{
196 Name interestName = identity;
197 interestName.append("DNS").append("ENDORSED");
198
Yingdi Yu64206112013-12-24 11:16:32 +0800199 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800200 interest.setInterestLifetime(1000);
Yingdi Yu64206112013-12-24 11:16:32 +0800201
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800202 OnVerified onVerified = func_lib::bind(&ContactManager::onDnsCollectEndorseVerified, this, _1, identity);
203 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactManager::onDnsCollectEndorseVerifyFailed, this, _1, identity);
204 TimeoutNotify timeoutNotify = func_lib::bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800205
206 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800207}
208
209void
Yingdi Yu64206112013-12-24 11:16:32 +0800210ContactManager::onDnsCollectEndorseTimeoutNotify(const Name& identity)
211{
212 emit collectEndorseFetchFailed (identity);
213}
214
215void
216ContactManager::onDnsCollectEndorseVerified(const shared_ptr<Data>& data, const Name& identity)
217{ emit collectEndorseFetched (*data); }
218
219void
220ContactManager::onDnsCollectEndorseVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
221{ emit collectEndorseFetchFailed (identity); }
222
223
224void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800225ContactManager::fetchKey(const ndn::Name& certName)
226{
227 Name interestName = certName;
228
Yingdi Yu64206112013-12-24 11:16:32 +0800229 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800230 interest.setInterestLifetime(1000);
Yingdi Yu64206112013-12-24 11:16:32 +0800231
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800232 OnVerified onVerified = func_lib::bind(&ContactManager::onKeyVerified, this, _1, certName);
233 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactManager::onKeyVerifyFailed, this, _1, certName);
234 TimeoutNotify timeoutNotify = func_lib::bind(&ContactManager::onKeyTimeoutNotify, this, certName);
Yingdi Yu64206112013-12-24 11:16:32 +0800235
236 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
237}
238
239
240void
241ContactManager::onKeyVerified(const shared_ptr<Data>& data, const Name& identity)
242{
243 IdentityCertificate identityCertificate(*data);
244
245 Profile profile(identityCertificate);
246 ProfileData profileData(profile);
247
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800248 Name certificateName = m_keyChain->getDefaultCertificateName();
249 m_keyChain->sign(profileData, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800250
251 try{
252 EndorseCertificate endorseCertificate(identityCertificate, profileData);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800253 m_keyChain->sign(endorseCertificate, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800254 emit contactKeyFetched (endorseCertificate);
255 }catch(std::exception& e){
256 _LOG_ERROR("Exception: " << e.what());
257 return;
258 }
259}
260
261void
262ContactManager::onKeyVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
263{
264 _LOG_DEBUG("Key cannot be verified!");
265 emit contactKeyFetchFailed (identity);
266}
267
268void
269ContactManager::onKeyTimeoutNotify(const Name& identity)
270{
271 _LOG_DEBUG("Key timeout!");
272 emit contactKeyFetchFailed(identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800273}
274
275void
Yingdi Yuaccbda92013-12-27 08:44:12 +0800276ContactManager::fetchIdCertificate(const Name& certName)
Yingdi Yu908f8412013-11-09 00:03:26 -0800277{
278 Name interestName = certName;
279
Yingdi Yu64206112013-12-24 11:16:32 +0800280 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800281 interest.setInterestLifetime(1000);
Yingdi Yu64206112013-12-24 11:16:32 +0800282
283 OnVerified onVerified = boost::bind(&ContactManager::onIdCertificateVerified, this, _1, certName);
284 OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onIdCertificateVerifyFailed, this, _1, certName);
285 TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onIdCertificateTimeoutNotify, this, certName);
286
287 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu908f8412013-11-09 00:03:26 -0800288}
289
290void
Yingdi Yu64206112013-12-24 11:16:32 +0800291ContactManager::onIdCertificateTimeoutNotify(const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800292{
293 emit contactCertificateFetchFailed (identity);
294}
Yingdi Yu64206112013-12-24 11:16:32 +0800295
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800296
297void
Yingdi Yu64206112013-12-24 11:16:32 +0800298ContactManager::onIdCertificateVerified(const shared_ptr<Data>& data, const Name& identity)
Yingdi Yu908f8412013-11-09 00:03:26 -0800299{
300 IdentityCertificate identityCertificate(*data);
301 emit contactCertificateFetched(identityCertificate);
302}
303
304void
Yingdi Yu64206112013-12-24 11:16:32 +0800305ContactManager::onIdCertificateVerifyFailed(const shared_ptr<Data>& data, const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800306{
307 emit contactCertificateFetchFailed (identity);
308}
Yingdi Yu908f8412013-11-09 00:03:26 -0800309
310void
Yingdi Yu64206112013-12-24 11:16:32 +0800311ContactManager::onTargetData(const shared_ptr<const ndn::Interest>& interest,
312 const shared_ptr<Data>& data,
313 int stepCount,
314 const OnVerified& onVerified,
315 const OnVerifyFailed& onVerifyFailed,
316 const TimeoutNotify& timeoutNotify)
317{
Yingdi Yueaa84e22014-01-16 10:30:26 -0800318 shared_ptr<ValidationRequest> nextStep = m_policy->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
Yingdi Yu64206112013-12-24 11:16:32 +0800319
320 if (nextStep)
321 m_face->expressInterest
322 (*nextStep->interest_,
323 bind(&ContactManager::onCertData, this, _1, _2, nextStep),
324 bind(&ContactManager::onCertTimeout, this, _1, onVerifyFailed, data, nextStep));
325
326}
327
328void
329ContactManager::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
330 int retry,
331 int stepCount,
332 const OnVerified& onVerified,
333 const OnVerifyFailed& onVerifyFailed,
334 const TimeoutNotify& timeoutNotify)
335{
336 if(retry > 0)
337 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount);
338 else
339 {
340 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
341 timeoutNotify();
342 }
343}
344
345void
346ContactManager::onCertData(const shared_ptr<const ndn::Interest>& interest,
347 const shared_ptr<Data>& cert,
348 shared_ptr<ValidationRequest> previousStep)
349{
Yingdi Yueaa84e22014-01-16 10:30:26 -0800350 shared_ptr<ValidationRequest> nextStep = m_policy->checkVerificationPolicy(cert,
Yingdi Yu64206112013-12-24 11:16:32 +0800351 previousStep->stepCount_,
352 previousStep->onVerified_,
353 previousStep->onVerifyFailed_);
354
355 if (nextStep)
356 m_face->expressInterest
357 (*nextStep->interest_,
358 bind(&ContactManager::onCertData, this, _1, _2, nextStep),
359 bind(&ContactManager::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
360}
361
362void
363ContactManager::onCertTimeout(const shared_ptr<const ndn::Interest>& interest,
364 const OnVerifyFailed& onVerifyFailed,
365 const shared_ptr<Data>& data,
366 shared_ptr<ValidationRequest> nextStep)
367{
368 if(nextStep->retry_ > 0)
369 m_face->expressInterest(*interest,
370 bind(&ContactManager::onCertData,
371 this,
372 _1,
373 _2,
374 nextStep),
375 bind(&ContactManager::onCertTimeout,
376 this,
377 _1,
378 onVerifyFailed,
379 data,
380 nextStep));
381 else
382 onVerifyFailed(data);
383}
384
385void
386ContactManager::sendInterest(const Interest& interest,
387 const OnVerified& onVerified,
388 const OnVerifyFailed& onVerifyFailed,
389 const TimeoutNotify& timeoutNotify,
390 int retry /* = 1 */,
391 int stepCount /* = 0 */)
392{
Yingdi Yuaccbda92013-12-27 08:44:12 +0800393 uint64_t id = m_face->expressInterest(interest,
Yingdi Yu64206112013-12-24 11:16:32 +0800394 boost::bind(&ContactManager::onTargetData,
395 this,
396 _1,
397 _2,
398 stepCount,
399 onVerified,
400 onVerifyFailed,
401 timeoutNotify),
402 boost::bind(&ContactManager::onTargetTimeout,
403 this,
404 _1,
405 retry,
406 stepCount,
407 onVerified,
408 onVerifyFailed,
409 timeoutNotify));
Yingdi Yuaccbda92013-12-27 08:44:12 +0800410
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800411 // _LOG_DEBUG("id: " << id << " entry id: " << m_face->getNode().getEntryIndexForExpressedInterest(interest.getName()));
Yingdi Yu64206112013-12-24 11:16:32 +0800412}
Yingdi Yu908f8412013-11-09 00:03:26 -0800413
414void
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700415ContactManager::updateProfileData(const Name& identity)
416{
417 // Get current profile;
Yingdi Yu64206112013-12-24 11:16:32 +0800418 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800419 if(static_cast<bool>(newProfile))
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700420 return;
Yingdi Yu64206112013-12-24 11:16:32 +0800421
422 shared_ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
423
Yingdi Yueaa84e22014-01-16 10:30:26 -0800424 if(static_cast<bool>(newEndorseCertificate))
Yingdi Yu64206112013-12-24 11:16:32 +0800425 return;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700426
427 // Check if profile exists
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800428 try{
429 Block profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800430 m_contactStorage->updateSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800431 }catch(ContactStorage::Error &e){
Yingdi Yu64206112013-12-24 11:16:32 +0800432 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800433 }
Yingdi Yub35b8652013-11-07 11:32:40 -0800434
Yingdi Yu64206112013-12-24 11:16:32 +0800435 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700436}
437
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800438void
439ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
440{
Yingdi Yu64206112013-12-24 11:16:32 +0800441 shared_ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
442
Yingdi Yueaa84e22014-01-16 10:30:26 -0800443 if(static_cast<bool>(newEndorseCertificate))
Yingdi Yu64206112013-12-24 11:16:32 +0800444 return;
445
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800446 try{
447 Block oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800448 m_contactStorage->updateEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800449 }catch(ContactStorage::Error &e){
Yingdi Yu64206112013-12-24 11:16:32 +0800450 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800451 }
452
Yingdi Yu64206112013-12-24 11:16:32 +0800453 publishEndorseCertificateInDNS(*newEndorseCertificate, signerIdentity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800454}
455
Yingdi Yu64206112013-12-24 11:16:32 +0800456shared_ptr<EndorseCertificate>
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800457ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
458{
Yingdi Yu64206112013-12-24 11:16:32 +0800459 shared_ptr<ContactItem> contact = getContact(identity);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800460 if(static_cast<bool>(contact))
461 return shared_ptr<EndorseCertificate>();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800462
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800463 Name signerKeyName = m_keyChain->getDefaultKeyNameForIdentity(signerIdentity);
464 Name signerCertName = m_keyChain->getDefaultCertificateNameForIdentity(signerIdentity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800465
Yingdi Yu64206112013-12-24 11:16:32 +0800466 vector<string> endorseList;
467 m_contactStorage->getEndorseList(identity, endorseList);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800468
Yingdi Yu64206112013-12-24 11:16:32 +0800469
Yingdi Yub35b8652013-11-07 11:32:40 -0800470 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800471 shared_ptr<EndorseCertificate> cert = make_shared<EndorseCertificate>(contact->getSelfEndorseCertificate(), signerKeyName, endorseList);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800472 m_keyChain->sign(*cert, signerCertName);
Yingdi Yu64206112013-12-24 11:16:32 +0800473 return cert;
474 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800475 _LOG_ERROR("Exception: " << e.what());
Yingdi Yueaa84e22014-01-16 10:30:26 -0800476 return shared_ptr<EndorseCertificate>();
Yingdi Yub35b8652013-11-07 11:32:40 -0800477 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800478}
479
Yingdi Yu64206112013-12-24 11:16:32 +0800480void
481ContactManager::getContactItemList(vector<shared_ptr<ContactItem> >& contacts)
482{ return m_contactStorage->getAllContacts(contacts); }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700483
Yingdi Yu64206112013-12-24 11:16:32 +0800484shared_ptr<ContactItem>
Yingdi Yud40226b2013-10-23 14:05:12 -0700485ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800486{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yud40226b2013-10-23 14:05:12 -0700487
Yingdi Yu64206112013-12-24 11:16:32 +0800488shared_ptr<EndorseCertificate>
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700489ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
490 const Profile& profile)
491{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800492 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700493 if(0 == certificateName.size())
Yingdi Yueaa84e22014-01-16 10:30:26 -0800494 return shared_ptr<EndorseCertificate>();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700495
Yingdi Yu64206112013-12-24 11:16:32 +0800496 ProfileData profileData(profile);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800497 m_keyChain->sign(profileData, certificateName);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700498
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800499 shared_ptr<IdentityCertificate> signingCert = m_keyChain->getCertificate(certificateName);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800500 if(static_cast<bool>(signingCert))
501 return shared_ptr<EndorseCertificate>();
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800502
Yingdi Yu64206112013-12-24 11:16:32 +0800503 Name signingKeyName = IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName());
Yingdi Yued8cfc42013-11-01 17:37:51 -0700504
Yingdi Yu64206112013-12-24 11:16:32 +0800505 shared_ptr<IdentityCertificate> kskCert;
506 if(signingKeyName.get(-1).toEscapedString().substr(0,4) == string("dsk-"))
Yingdi Yued8cfc42013-11-01 17:37:51 -0700507 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800508 SignatureSha256WithRsa dskCertSig(signingCert->getSignature());
Yingdi Yued8cfc42013-11-01 17:37:51 -0700509 // HACK! KSK certificate should be retrieved from network.
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800510 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(dskCertSig.getKeyLocator().getName());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800511
Yingdi Yu64206112013-12-24 11:16:32 +0800512 // TODO: check null existing cases.
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800513 Name kskCertName = m_keyChain->getDefaultCertificateNameForIdentity(keyName.getPrefix(-1));
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800514
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800515 kskCert = m_keyChain->getCertificate(kskCertName);
Yingdi Yued8cfc42013-11-01 17:37:51 -0700516 }
517 else
518 {
519 kskCert = signingCert;
Yingdi Yued8cfc42013-11-01 17:37:51 -0700520 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700521
Yingdi Yueaa84e22014-01-16 10:30:26 -0800522 if(static_cast<bool>(kskCert))
523 return shared_ptr<EndorseCertificate>();
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800524
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700525 vector<string> endorseList;
526 Profile::const_iterator it = profile.begin();
527 for(; it != profile.end(); it++)
528 endorseList.push_back(it->first);
529
Yingdi Yub35b8652013-11-07 11:32:40 -0800530 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800531 shared_ptr<EndorseCertificate> selfEndorseCertificate = make_shared<EndorseCertificate>(*kskCert, profileData, endorseList);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800532 m_keyChain->sign(*selfEndorseCertificate, kskCert->getName());
Yingdi Yu64206112013-12-24 11:16:32 +0800533
534 return selfEndorseCertificate;
535 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800536 _LOG_ERROR("Exception: " << e.what());
Yingdi Yueaa84e22014-01-16 10:30:26 -0800537 return shared_ptr<EndorseCertificate>();
Yingdi Yub35b8652013-11-07 11:32:40 -0800538 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700539}
540
541
542void
Yingdi Yu64206112013-12-24 11:16:32 +0800543ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700544{
Yingdi Yu64206112013-12-24 11:16:32 +0800545 Data data;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700546
Yingdi Yu64206112013-12-24 11:16:32 +0800547 Name keyName = selfEndorseCertificate.getPublicKeyName();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700548 Name identity = keyName.getSubName(0, keyName.size()-1);
549
Yingdi Yu64206112013-12-24 11:16:32 +0800550 time_t nowSeconds = time(NULL);
551 struct tm current = *gmtime(&nowSeconds);
552 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700553
554 Name dnsName = identity;
Yingdi Yu64206112013-12-24 11:16:32 +0800555 dnsName.append("DNS").append("PROFILE").appendVersion(version);
556 data.setName(dnsName);
557
558 data.setContent(selfEndorseCertificate.wireEncode());
559
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800560 Name signCertName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
561 m_keyChain->sign(data, signCertName);
Yingdi Yu64206112013-12-24 11:16:32 +0800562
563 m_dnsStorage->updateDnsSelfProfileData(data, identity);
Yingdi Yuc29fb982013-10-20 19:43:10 -0700564
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800565 m_face->put(data);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700566}
567
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800568void
Yingdi Yu64206112013-12-24 11:16:32 +0800569ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate, const Name& signerIdentity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800570{
Yingdi Yu64206112013-12-24 11:16:32 +0800571 Data data;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800572
Yingdi Yu64206112013-12-24 11:16:32 +0800573 Name keyName = endorseCertificate.getPublicKeyName();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800574 Name endorsee = keyName.getSubName(0, keyName.size()-1);
575
Yingdi Yu64206112013-12-24 11:16:32 +0800576 time_t nowSeconds = time(NULL);
577 struct tm current = *gmtime(&nowSeconds);
578 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800579
580 Name dnsName = signerIdentity;
Yingdi Yu64206112013-12-24 11:16:32 +0800581 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(version);
582 data.setName(dnsName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800583
Yingdi Yu64206112013-12-24 11:16:32 +0800584 data.setContent(endorseCertificate.wireEncode());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800585
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800586 Name signCertName = m_keyChain->getDefaultCertificateNameForIdentity(signerIdentity);
587 m_keyChain->sign(data, signCertName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800588
Yingdi Yu64206112013-12-24 11:16:32 +0800589 m_dnsStorage->updateDnsEndorseOthers(data, signerIdentity, endorsee);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800590
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800591 m_face->put(data);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800592}
593
594void
595ContactManager::publishEndorsedDataInDns(const Name& identity)
596{
Yingdi Yu64206112013-12-24 11:16:32 +0800597 Data data;
598
599 time_t nowSeconds = time(NULL);
600 struct tm current = *gmtime(&nowSeconds);
601 MillisecondsSince1970 version = timegm(&current) * 1000.0;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800602
603 Name dnsName = identity;
Yingdi Yu64206112013-12-24 11:16:32 +0800604 dnsName.append("DNS").append("ENDORSED").appendVersion(version);
605 data.setName(dnsName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800606
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800607 vector<Buffer> collectEndorseList;
Yingdi Yu64206112013-12-24 11:16:32 +0800608 m_contactStorage->getCollectEndorseList(identity, collectEndorseList);
609
610 Chronos::EndorseCollection endorseCollection;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800611
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800612 vector<Buffer>::const_iterator it = collectEndorseList.begin();
Yingdi Yu64206112013-12-24 11:16:32 +0800613 for(; it != collectEndorseList.end(); it++)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800614 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800615 string entryStr(reinterpret_cast<const char*>(it->buf()), it->size());
Yingdi Yu64206112013-12-24 11:16:32 +0800616 endorseCollection.add_endorsement()->set_blob(entryStr);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800617 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800618
Yingdi Yu64206112013-12-24 11:16:32 +0800619 string encoded;
620 endorseCollection.SerializeToString(&encoded);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800621
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800622 data.setContent(reinterpret_cast<const uint8_t*>(encoded.c_str()), encoded.size());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800623
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800624 Name signCertName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
625 m_keyChain->sign(data, signCertName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800626
Yingdi Yu64206112013-12-24 11:16:32 +0800627 m_dnsStorage->updateDnsOthersEndorse(data, identity);
628
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800629 m_face->put(data);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800630}
631
Yingdi Yu908f8412013-11-09 00:03:26 -0800632void
633ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile)
634{
Yingdi Yu64206112013-12-24 11:16:32 +0800635 ProfileData profileData(profile);
Yingdi Yu908f8412013-11-09 00:03:26 -0800636
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800637 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity (m_defaultIdentity);
638 m_keyChain->sign(profileData, certificateName);
Yingdi Yu908f8412013-11-09 00:03:26 -0800639
Yingdi Yu908f8412013-11-09 00:03:26 -0800640
641 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800642 EndorseCertificate endorseCertificate(identityCertificate, profileData);
643
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800644 m_keyChain->sign(endorseCertificate, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800645
646 ContactItem contactItem(endorseCertificate);
647
Yingdi Yu908f8412013-11-09 00:03:26 -0800648 m_contactStorage->addContact(contactItem);
Yingdi Yu64206112013-12-24 11:16:32 +0800649
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800650 emit contactAdded(contactItem.getNameSpace());
Yingdi Yu64206112013-12-24 11:16:32 +0800651
652 }catch(std::exception& e){
Yingdi Yu908f8412013-11-09 00:03:26 -0800653 emit warning(e.what());
654 _LOG_ERROR("Exception: " << e.what());
655 return;
656 }
657}
658
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800659void
660ContactManager::removeContact(const ndn::Name& contactNameSpace)
661{
Yingdi Yu64206112013-12-24 11:16:32 +0800662 shared_ptr<ContactItem> contact = getContact(contactNameSpace);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800663 if(static_cast<bool>(contact))
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800664 return;
665 m_contactStorage->removeContact(contactNameSpace);
666 emit contactRemoved(contact->getPublicKeyName());
667}
Yingdi Yu908f8412013-11-09 00:03:26 -0800668
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700669
670#if WAF
671#include "contact-manager.moc"
672#include "contact-manager.cpp.moc"
673#endif