blob: eac5c518718e35418ad71ad9996ca6ead51a346e [file] [log] [blame]
Yingdi Yu9236c432013-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 Yu4685b1b2013-10-18 17:05:02 -070013#ifndef Q_MOC_RUN
Yingdi Yu9236c432013-10-18 11:29:25 -070014#include <ndn.cxx/wrapper/wrapper.h>
15#include <ndn.cxx/security/keychain.h>
Yingdi Yu9236c432013-10-18 11:29:25 -070016#include <ndn.cxx/security/policy/simple-policy-manager.h>
17#include <ndn.cxx/security/policy/identity-policy-rule.h>
Yingdi Yub2e747d2013-11-05 23:06:43 -080018#include <ndn.cxx/helpers/der/der.h>
Yingdi Yu72781e52013-11-06 23:00:21 -080019#include <cryptopp/base64.h>
Yingdi Yu9236c432013-10-18 11:29:25 -070020#include <fstream>
Yingdi Yuec3d9a32013-10-18 18:35:09 -070021#include "logging.h"
Yingdi Yu4685b1b2013-10-18 17:05:02 -070022#endif
Yingdi Yu9236c432013-10-18 11:29:25 -070023
24using namespace ndn;
25using namespace ndn::security;
26
Yingdi Yuec3d9a32013-10-18 18:35:09 -070027INIT_LOGGER("ContactManager");
28
Yingdi Yu8e135832013-11-09 20:12:31 -080029ContactManager::ContactManager(QObject* parent)
Yingdi Yuec3d9a32013-10-18 18:35:09 -070030 : QObject(parent)
Yingdi Yu9236c432013-10-18 11:29:25 -070031{
Yingdi Yu8e135832013-11-09 20:12:31 -080032 m_contactStorage = Ptr<ContactStorage>::Create();
33 m_dnsStorage = Ptr<DnsStorage>::Create();
Yingdi Yu4685b1b2013-10-18 17:05:02 -070034
Yingdi Yu8e135832013-11-09 20:12:31 -080035 setKeychain();
Yingdi Yu9236c432013-10-18 11:29:25 -070036}
37
38ContactManager::~ContactManager()
39{
40}
41
Yingdi Yu4685b1b2013-10-18 17:05:02 -070042void
Yingdi Yu8e135832013-11-09 20:12:31 -080043ContactManager::setWrapper()
44{
45 try{
46 m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain));
47 }catch(ndn::Error::ndnOperation& e){
48 emit noNdnConnection(QString::fromStdString("Cannot conect to ndnd!\n Have you started your ndnd?"));
49 }
50}
51
52void
Yingdi Yu9236c432013-10-18 11:29:25 -070053ContactManager::setKeychain()
54{
Yingdi Yu72781e52013-11-06 23:00:21 -080055 Ptr<IdentityManager> identityManager = Ptr<IdentityManager>::Create();
56 Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>::Create();
57
58 Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, NULL));
Yingdi Yu9236c432013-10-18 11:29:25 -070059
Yingdi Yub2e747d2013-11-05 23:06:43 -080060 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><ENDORSED>",
61 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
62 "==", "\\1", "\\1\\2", true)));
Yingdi Yu4685b1b2013-10-18 17:05:02 -070063 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yu9b34b1f2013-11-01 17:37:51 -070064 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
Yingdi Yu4685b1b2013-10-18 17:05:02 -070065 "==", "\\1", "\\1\\2", true)));
Yingdi Yu9236c432013-10-18 11:29:25 -070066 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yu9b34b1f2013-11-01 17:37:51 -070067 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
Yingdi Yu4685b1b2013-10-18 17:05:02 -070068 "==", "\\1", "\\1\\2", true)));
Yingdi Yu7989eb22013-10-31 17:38:22 -070069 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yu9b34b1f2013-11-01 17:37:51 -070070 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
Yingdi Yu4685b1b2013-10-18 17:05:02 -070071 ">", "\\1\\2", "\\1", true)));
Yingdi Yu7989eb22013-10-31 17:38:22 -070072 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yu9b34b1f2013-11-01 17:37:51 -070073 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
Yingdi Yu4685b1b2013-10-18 17:05:02 -070074 "==", "\\1", "\\1\\2", true)));
75
76 policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
77 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
78 "==", "\\1", "\\1\\2", true)));
Yingdi Yu9236c432013-10-18 11:29:25 -070079
Yingdi Yu72781e52013-11-06 23:00:21 -080080 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
81HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
8267biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
835X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
84WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
85Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
86MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
87nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
88c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
89MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
90b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
91yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
92I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
93/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
9472P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
95VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
96iVUF1QIBEQAA");
Yingdi Yu9236c432013-10-18 11:29:25 -070097
Yingdi Yu72781e52013-11-06 23:00:21 -080098 string decoded;
99 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
100 TrustAnchor.size(),
101 true,
102 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
103 Ptr<Blob> blob = Ptr<Blob>(new Blob(decoded.c_str(), decoded.size()));
104 Ptr<Data> data = Data::decodeFromWire(blob);
105 Ptr<IdentityCertificate>anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
Yingdi Yu9236c432013-10-18 11:29:25 -0700106 policyManager->addTrustAnchor(anchor);
Yingdi Yu72781e52013-11-06 23:00:21 -0800107
108#ifdef _DEBUG
109
110 const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
111zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
112qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
1136M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
114DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
115Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
116MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
117qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
118APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
119MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
120MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
121/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
122sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
123H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
124gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
125OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
126F7Wh5ayeo8NBKDsCAwEAAQAA");
127
128 string decoded2;
129 CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()),
130 FakeAnchor.size(),
131 true,
132 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
133 Ptr<Blob> blob2 = Ptr<Blob>(new Blob(decoded2.c_str(), decoded2.size()));
134 Ptr<Data> data2 = Data::decodeFromWire(blob2);
135 Ptr<IdentityCertificate>anchor2 = Ptr<IdentityCertificate>(new IdentityCertificate(*data2));
136 policyManager->addTrustAnchor(anchor2);
137
138#endif
Yingdi Yu9236c432013-10-18 11:29:25 -0700139
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700140 m_keychain = keychain;
Yingdi Yu9236c432013-10-18 11:29:25 -0700141}
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700142
143
144void
145ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
146{
147 Name interestName = identity;
148 interestName.append("DNS").append("PROFILE");
149
150 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700151 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700152 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified,
153 this,
154 _1,
155 identity),
156 boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout,
157 this,
158 _1,
159 _2,
160 identity,
161 0),
162 boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified,
163 this,
164 _1,
165 identity)));
166 m_wrapper->sendInterest(interestPtr, closure);
167}
168
169void
Yingdi Yub2e747d2013-11-05 23:06:43 -0800170ContactManager::fetchCollectEndorse(const ndn::Name& identity)
171{
172 Name interestName = identity;
173 interestName.append("DNS").append("ENDORSED");
174
175 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
176 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
177 interestPtr->setInterestLifetime(1);
178 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsCollectEndorseVerified,
179 this,
180 _1,
181 identity),
182 boost::bind(&ContactManager::onDnsCollectEndorseTimeout,
183 this,
184 _1,
185 _2,
186 identity,
187 0),
188 boost::bind(&ContactManager::onDnsCollectEndorseUnverified,
189 this,
190 _1,
191 identity)));
192 m_wrapper->sendInterest(interestPtr, closure);
193}
194
195void
196ContactManager::fetchKey(const ndn::Name& certName)
197{
198 Name interestName = certName;
199
200 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
201 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800202 interestPtr->setInterestLifetime(1);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800203 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onKeyVerified,
204 this,
205 _1,
206 certName),
207 boost::bind(&ContactManager::onKeyTimeout,
208 this,
209 _1,
210 _2,
211 certName,
212 0),
213 boost::bind(&ContactManager::onKeyUnverified,
214 this,
215 _1,
216 certName)));
217 m_wrapper->sendInterest(interestPtr, closure);
218}
219
220void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800221ContactManager::fetchIdCertificate(const ndn::Name& certName)
222{
223 Name interestName = certName;
224
225 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
226 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
227 interestPtr->setInterestLifetime(1);
228 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onIdCertificateVerified,
229 this,
230 _1,
231 certName),
232 boost::bind(&ContactManager::onIdCertificateTimeout,
233 this,
234 _1,
235 _2,
236 certName,
237 0),
238 boost::bind(&ContactManager::onIdCertificateUnverified,
239 this,
240 _1,
241 certName)));
242 m_wrapper->sendInterest(interestPtr, closure);
243}
244
245void
Yingdi Yub2e747d2013-11-05 23:06:43 -0800246ContactManager::onDnsCollectEndorseVerified(Ptr<Data> data, const Name& identity)
247{ emit collectEndorseFetched (*data); }
248
249void
250ContactManager::onDnsCollectEndorseTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
251{ emit collectEndorseFetchFailed (identity); }
252
253void
254ContactManager::onDnsCollectEndorseUnverified(Ptr<Data> data, const Name& identity)
255{ emit collectEndorseFetchFailed (identity); }
256
257void
258ContactManager::onKeyVerified(Ptr<Data> data, const Name& identity)
259{
260 IdentityCertificate identityCertificate(*data);
Yingdi Yu2e3199c2013-11-06 18:42:34 -0800261
262 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(Profile(identityCertificate)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800263
264 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
265 Name certificateName = identityManager->getDefaultCertificateName ();
266 identityManager->signByCertificate(*profileData, certificateName);
267
Yingdi Yue35bdb82013-11-07 11:32:40 -0800268 Ptr<EndorseCertificate> endorseCertificate = NULL;
269 try{
270 endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData));
271 }catch(exception& e){
272 _LOG_ERROR("Exception: " << e.what());
273 return;
274 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800275
Yingdi Yue35bdb82013-11-07 11:32:40 -0800276 identityManager->signByCertificate(*endorseCertificate, certificateName);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800277
Yingdi Yue35bdb82013-11-07 11:32:40 -0800278 emit contactKeyFetched (*endorseCertificate);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800279}
280
281void
282ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity)
283{ emit contactKeyFetchFailed (identity); }
284
285void
286ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
287{ emit contactKeyFetchFailed(identity); }
288
289void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800290ContactManager::onIdCertificateVerified(Ptr<Data> data, const Name& identity)
291{
292 IdentityCertificate identityCertificate(*data);
293 emit contactCertificateFetched(identityCertificate);
294}
295
296void
297ContactManager::onIdCertificateUnverified(Ptr<Data> data, const Name& identity)
298{ emit contactCertificateFetchFailed (identity); }
299
300void
301ContactManager::onIdCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
302{ emit contactCertificateFetchFailed (identity); }
303
304void
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700305ContactManager::updateProfileData(const Name& identity)
306{
307 // Get current profile;
308 Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
309 if(NULL == newProfile)
310 return;
311 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
312
313 // Check if profile exists
314 Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
315 if(NULL != profileDataBlob)
316 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800317
318 Ptr<EndorseCertificate> oldEndorseCertificate = NULL;
319 try{
320 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
321 oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
322 }catch(exception& e){
323 _LOG_ERROR("Exception: " << e.what());
324 return;
325 }
326
327 const Blob& oldProfileBlob = oldEndorseCertificate->getProfileData()->content();
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700328
329 if(oldProfileBlob == *newProfileBlob)
330 return;
331
332 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
Yingdi Yue35bdb82013-11-07 11:32:40 -0800333
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700334 if(NULL == newEndorseCertificate)
335 return;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800336
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700337 m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
338
339 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
340 }
341 else
342 {
343 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
Yingdi Yue35bdb82013-11-07 11:32:40 -0800344
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700345 if(NULL == newEndorseCertificate)
346 return;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800347
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700348 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
349
350 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
351 }
352}
353
Yingdi Yub2e747d2013-11-05 23:06:43 -0800354void
355ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
356{
357 Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
358 Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
359 if(NULL != oldEndorseCertificateBlob)
360 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800361 Ptr<EndorseCertificate> oldEndorseCertificate = NULL;
362 try{
363 Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob);
364 oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
365 }catch(exception& e){
366 _LOG_ERROR("Exception: " << e.what());
367 return;
368 }
369 const Blob& oldEndorseContent = oldEndorseCertificate->content();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800370 const Blob& newEndorseContent = newEndorseCertificate->content();
371 if(oldEndorseContent == newEndorseContent)
372 return;
373 }
374 else
375 {
376 if(NULL == newEndorseCertificate)
377 return;
378 }
379 m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity);
380 publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity);
381}
382
383Ptr<EndorseCertificate>
384ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
385{
386 Ptr<ContactItem> contact = getContact(identity);
Yingdi Yu72781e52013-11-06 23:00:21 -0800387 if(contact == NULL)
388 return NULL;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800389
390 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
391 Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity);
392 Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity);
393
394 vector<string> endorseList = m_contactStorage->getEndorseList(identity);
395
Yingdi Yue35bdb82013-11-07 11:32:40 -0800396 Ptr<EndorseCertificate> cert = NULL;
397 try{
398 cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList));
399 }catch(exception& e){
400 _LOG_ERROR("Exception: " << e.what());
401 return NULL;
402 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800403 identityManager->signByCertificate(*cert, signerCertName);
404
405 return cert;
406}
407
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700408vector<Ptr<ContactItem> >
409ContactManager::getContactItemList()
Yingdi Yu71c01872013-11-03 16:22:05 -0800410{ return m_contactStorage->getAllContacts(); }
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700411
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700412Ptr<ContactItem>
413ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu71c01872013-11-03 16:22:05 -0800414{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700415
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700416Ptr<EndorseCertificate>
417ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
418 const Profile& profile)
419{
420 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
421 Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity);
422 if(0 == certificateName.size())
423 return NULL;
424
Yingdi Yu2e3199c2013-11-06 18:42:34 -0800425 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile));
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700426 identityManager->signByCertificate(*profileData, certificateName);
427
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700428 Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
Yingdi Yu72781e52013-11-06 23:00:21 -0800429 if(NULL == signingCert)
430 return NULL;
431
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700432 Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
433
434 Ptr<security::IdentityCertificate> kskCert;
435 if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-"))
436 {
437 Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
438 // HACK! KSK certificate should be retrieved from network.
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700439 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
Yingdi Yu72781e52013-11-06 23:00:21 -0800440
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700441 Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
Yingdi Yu72781e52013-11-06 23:00:21 -0800442
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700443 kskCert = identityManager->getCertificate(kskCertName);
444
445 }
446 else
447 {
448 kskCert = signingCert;
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700449 }
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700450
Yingdi Yu72781e52013-11-06 23:00:21 -0800451 if(NULL == kskCert)
452 return NULL;
453
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700454 vector<string> endorseList;
455 Profile::const_iterator it = profile.begin();
456 for(; it != profile.end(); it++)
457 endorseList.push_back(it->first);
458
Yingdi Yue35bdb82013-11-07 11:32:40 -0800459 Ptr<EndorseCertificate> selfEndorseCertificate = NULL;
460 try{
461 selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
462 profileData,
463 endorseList));
464 }catch(exception& e){
465 _LOG_ERROR("Exception: " << e.what());
466 return NULL;
467 }
468
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700469 identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
470
471 return selfEndorseCertificate;
472}
473
474
475void
476ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
477{
Yingdi Yud95c5642013-10-20 19:43:10 -0700478 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700479
Yingdi Yue35bdb82013-11-07 11:32:40 -0800480 Ptr<Data> plainData = NULL;
481 Ptr<EndorseCertificate> selfEndorseCertificate = NULL;
482 try{
483 plainData = Data::decodeFromWire(dataContentBlob);
484 selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
485 }catch(exception& e){
486 _LOG_ERROR("Exception: " << e.what());
487 return;
488 }
Yingdi Yud95c5642013-10-20 19:43:10 -0700489
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700490 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yud95c5642013-10-20 19:43:10 -0700491
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700492 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yu72781e52013-11-06 23:00:21 -0800493 emit contactFetched (*selfEndorseCertificate);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700494 else
Yingdi Yu72781e52013-11-06 23:00:21 -0800495 emit contactFetchFailed (identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700496}
497
498void
499ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
500{ emit contactFetchFailed (identity); }
501
502void
503ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800504{ emit contactFetchFailed(identity); }
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700505
506void
507ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate)
508{
509 Ptr<Data> data = Ptr<Data>::Create();
510
511 Name keyName = selfEndorseCertificate->getPublicKeyName();
512 Name identity = keyName.getSubName(0, keyName.size()-1);
513
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700514
515 Name dnsName = identity;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700516 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yud95c5642013-10-20 19:43:10 -0700517
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700518 data->setName(dnsName);
519 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yud95c5642013-10-20 19:43:10 -0700520
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700521 Content content(blob->buf(), blob->size());
522 data->setContent(content);
523
524 m_keychain->signByIdentity(*data, identity);
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700525
526 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700527
528 Ptr<Blob> dnsBlob = data->encodeToWire();
529
Yingdi Yueda39aa2013-10-23 23:07:29 -0700530 m_wrapper->putToNdnd(*dnsBlob);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700531}
532
Yingdi Yub2e747d2013-11-05 23:06:43 -0800533void
534ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity)
535{
536 Ptr<Data> data = Ptr<Data>::Create();
537
538 Name keyName = endorseCertificate->getPublicKeyName();
539 Name endorsee = keyName.getSubName(0, keyName.size()-1);
540
541
542 Name dnsName = signerIdentity;
543 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion();
544
545 data->setName(dnsName);
546 Ptr<Blob> blob = endorseCertificate->encodeToWire();
547
548 Content content(blob->buf(), blob->size());
549 data->setContent(content);
550
551 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity);
552 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
553
554 m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee);
555
556 Ptr<Blob> dnsBlob = data->encodeToWire();
557
558 m_wrapper->putToNdnd(*dnsBlob);
559}
560
561void
562ContactManager::publishEndorsedDataInDns(const Name& identity)
563{
564 Ptr<Data> data = Ptr<Data>::Create();
565
566 Name dnsName = identity;
567 dnsName.append("DNS").append("ENDORSED").appendVersion();
568 data->setName(dnsName);
569
570 Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity);
571
572 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
573
574 vector<Blob>::const_iterator it = collectEndorseList->begin();
575 for(; it != collectEndorseList->end(); it++)
576 {
577 Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it));
578 root->addChild(entry);
579 }
580
581 blob_stream blobStream;
582 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
583 root->encode(start);
584
585 Content content(blobStream.buf()->buf(), blobStream.buf()->size());
586 data->setContent(content);
587
588 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity);
589 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
590
591 m_dnsStorage->updateDnsOthersEndorse(*data, identity);
592
593 Ptr<Blob> dnsBlob = data->encodeToWire();
594
595 m_wrapper->putToNdnd(*dnsBlob);
596}
597
Yingdi Yuae8217c2013-11-09 00:03:26 -0800598void
599ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile)
600{
601 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile));
602
603 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
604 Name certificateName = identityManager->getDefaultCertificateNameByIdentity (m_defaultIdentity);
605 identityManager->signByCertificate(*profileData, certificateName);
606
607 Ptr<EndorseCertificate> endorseCertificate = NULL;
608 try{
609 endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData));
610 }catch(exception& e){
611 _LOG_ERROR("Exception: " << e.what());
612 return;
613 }
614
615 identityManager->signByCertificate(*endorseCertificate, certificateName);
616
617 ContactItem contactItem(*endorseCertificate);
618
619 try{
620 m_contactStorage->addContact(contactItem);
621 }catch(exception& e){
622 emit warning(e.what());
623 _LOG_ERROR("Exception: " << e.what());
624 return;
625 }
626}
627
628
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700629
630#if WAF
631#include "contact-manager.moc"
632#include "contact-manager.cpp.moc"
633#endif