blob: 651ab821baff669b30f0bface6d271eb31741315 [file] [log] [blame]
Yingdi Yu0b82a4e2013-10-18 11:29:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "contact-manager.h"
12
Yingdi Yuaa8d7692013-10-18 17:05:02 -070013#ifndef Q_MOC_RUN
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070014#include <ndn.cxx/wrapper/wrapper.h>
15#include <ndn.cxx/security/keychain.h>
Yingdi Yu0b82a4e2013-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 Yu8dacdf22013-11-05 23:06:43 -080018#include <ndn.cxx/helpers/der/der.h>
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080019#include <cryptopp/base64.h>
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070020#include <fstream>
Yingdi Yu590fa5d2013-10-18 18:35:09 -070021#include "logging.h"
Yingdi Yuaa8d7692013-10-18 17:05:02 -070022#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070023
24using namespace ndn;
25using namespace ndn::security;
26
Yingdi Yu590fa5d2013-10-18 18:35:09 -070027INIT_LOGGER("ContactManager");
28
Yingdi Yuaa8d7692013-10-18 17:05:02 -070029ContactManager::ContactManager(Ptr<ContactStorage> contactStorage,
Yingdi Yu590fa5d2013-10-18 18:35:09 -070030 Ptr<DnsStorage> dnsStorage,
31 QObject* parent)
32 : QObject(parent)
33 , m_contactStorage(contactStorage)
Yingdi Yuaa8d7692013-10-18 17:05:02 -070034 , m_dnsStorage(dnsStorage)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070035{
Yingdi Yuaa8d7692013-10-18 17:05:02 -070036 setKeychain();
37
38 m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070039}
40
41ContactManager::~ContactManager()
42{
43}
44
Yingdi Yuaa8d7692013-10-18 17:05:02 -070045void
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070046ContactManager::setKeychain()
47{
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080048 Ptr<IdentityManager> identityManager = Ptr<IdentityManager>::Create();
49 Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>::Create();
50
51 Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, NULL));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070052
Yingdi Yu8dacdf22013-11-05 23:06:43 -080053 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><ENDORSED>",
54 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
55 "==", "\\1", "\\1\\2", true)));
Yingdi Yuaa8d7692013-10-18 17:05:02 -070056 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070057 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070058 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070059 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070060 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070061 "==", "\\1", "\\1\\2", true)));
Yingdi Yu42f66462013-10-31 17:38:22 -070062 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070063 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070064 ">", "\\1\\2", "\\1", true)));
Yingdi Yu42f66462013-10-31 17:38:22 -070065 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070066 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070067 "==", "\\1", "\\1\\2", true)));
68
69 policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
70 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
71 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070072
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080073 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
74HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
7567biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
765X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
77WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
78Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
79MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
80nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
81c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
82MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
83b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
84yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
85I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
86/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
8772P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
88VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
89iVUF1QIBEQAA");
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070090
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080091 string decoded;
92 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
93 TrustAnchor.size(),
94 true,
95 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
96 Ptr<Blob> blob = Ptr<Blob>(new Blob(decoded.c_str(), decoded.size()));
97 Ptr<Data> data = Data::decodeFromWire(blob);
98 Ptr<IdentityCertificate>anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070099 policyManager->addTrustAnchor(anchor);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800100
101#ifdef _DEBUG
102
103 const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
104zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
105qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
1066M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
107DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
108Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
109MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
110qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
111APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
112MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
113MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
114/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
115sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
116H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
117gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
118OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
119F7Wh5ayeo8NBKDsCAwEAAQAA");
120
121 string decoded2;
122 CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()),
123 FakeAnchor.size(),
124 true,
125 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
126 Ptr<Blob> blob2 = Ptr<Blob>(new Blob(decoded2.c_str(), decoded2.size()));
127 Ptr<Data> data2 = Data::decodeFromWire(blob2);
128 Ptr<IdentityCertificate>anchor2 = Ptr<IdentityCertificate>(new IdentityCertificate(*data2));
129 policyManager->addTrustAnchor(anchor2);
130
131#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700132
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700133 m_keychain = keychain;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700134}
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700135
136
137void
138ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
139{
140 Name interestName = identity;
141 interestName.append("DNS").append("PROFILE");
142
143 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
Yingdi Yu42f66462013-10-31 17:38:22 -0700144 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700145 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified,
146 this,
147 _1,
148 identity),
149 boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout,
150 this,
151 _1,
152 _2,
153 identity,
154 0),
155 boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified,
156 this,
157 _1,
158 identity)));
159 m_wrapper->sendInterest(interestPtr, closure);
160}
161
162void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800163ContactManager::fetchCollectEndorse(const ndn::Name& identity)
164{
165 Name interestName = identity;
166 interestName.append("DNS").append("ENDORSED");
167
168 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
169 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
170 interestPtr->setInterestLifetime(1);
171 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsCollectEndorseVerified,
172 this,
173 _1,
174 identity),
175 boost::bind(&ContactManager::onDnsCollectEndorseTimeout,
176 this,
177 _1,
178 _2,
179 identity,
180 0),
181 boost::bind(&ContactManager::onDnsCollectEndorseUnverified,
182 this,
183 _1,
184 identity)));
185 m_wrapper->sendInterest(interestPtr, closure);
186}
187
188void
189ContactManager::fetchKey(const ndn::Name& certName)
190{
191 Name interestName = certName;
192
193 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
194 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
195 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onKeyVerified,
196 this,
197 _1,
198 certName),
199 boost::bind(&ContactManager::onKeyTimeout,
200 this,
201 _1,
202 _2,
203 certName,
204 0),
205 boost::bind(&ContactManager::onKeyUnverified,
206 this,
207 _1,
208 certName)));
209 m_wrapper->sendInterest(interestPtr, closure);
210}
211
212void
213ContactManager::onDnsCollectEndorseVerified(Ptr<Data> data, const Name& identity)
214{ emit collectEndorseFetched (*data); }
215
216void
217ContactManager::onDnsCollectEndorseTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
218{ emit collectEndorseFetchFailed (identity); }
219
220void
221ContactManager::onDnsCollectEndorseUnverified(Ptr<Data> data, const Name& identity)
222{ emit collectEndorseFetchFailed (identity); }
223
224void
225ContactManager::onKeyVerified(Ptr<Data> data, const Name& identity)
226{
227 IdentityCertificate identityCertificate(*data);
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800228
229 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(Profile(identityCertificate)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800230
231 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
232 Name certificateName = identityManager->getDefaultCertificateName ();
233 identityManager->signByCertificate(*profileData, certificateName);
234
Yingdi Yub35b8652013-11-07 11:32:40 -0800235 Ptr<EndorseCertificate> endorseCertificate = NULL;
236 try{
237 endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData));
238 }catch(exception& e){
239 _LOG_ERROR("Exception: " << e.what());
240 return;
241 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800242
Yingdi Yub35b8652013-11-07 11:32:40 -0800243 identityManager->signByCertificate(*endorseCertificate, certificateName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800244
Yingdi Yub35b8652013-11-07 11:32:40 -0800245 emit contactKeyFetched (*endorseCertificate);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800246}
247
248void
249ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity)
250{ emit contactKeyFetchFailed (identity); }
251
252void
253ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
254{ emit contactKeyFetchFailed(identity); }
255
256void
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700257ContactManager::updateProfileData(const Name& identity)
258{
259 // Get current profile;
260 Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
261 if(NULL == newProfile)
262 return;
263 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
264
265 // Check if profile exists
266 Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
267 if(NULL != profileDataBlob)
268 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800269
270 Ptr<EndorseCertificate> oldEndorseCertificate = NULL;
271 try{
272 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
273 oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
274 }catch(exception& e){
275 _LOG_ERROR("Exception: " << e.what());
276 return;
277 }
278
279 const Blob& oldProfileBlob = oldEndorseCertificate->getProfileData()->content();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700280
281 if(oldProfileBlob == *newProfileBlob)
282 return;
283
284 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
Yingdi Yub35b8652013-11-07 11:32:40 -0800285
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700286 if(NULL == newEndorseCertificate)
287 return;
Yingdi Yub35b8652013-11-07 11:32:40 -0800288
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700289 m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
290
291 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
292 }
293 else
294 {
295 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
Yingdi Yub35b8652013-11-07 11:32:40 -0800296
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700297 if(NULL == newEndorseCertificate)
298 return;
Yingdi Yub35b8652013-11-07 11:32:40 -0800299
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700300 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
301
302 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
303 }
304}
305
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800306void
307ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
308{
309 Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
310 Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
311 if(NULL != oldEndorseCertificateBlob)
312 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800313 Ptr<EndorseCertificate> oldEndorseCertificate = NULL;
314 try{
315 Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob);
316 oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
317 }catch(exception& e){
318 _LOG_ERROR("Exception: " << e.what());
319 return;
320 }
321 const Blob& oldEndorseContent = oldEndorseCertificate->content();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800322 const Blob& newEndorseContent = newEndorseCertificate->content();
323 if(oldEndorseContent == newEndorseContent)
324 return;
325 }
326 else
327 {
328 if(NULL == newEndorseCertificate)
329 return;
330 }
331 m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity);
332 publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity);
333}
334
335Ptr<EndorseCertificate>
336ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
337{
338 Ptr<ContactItem> contact = getContact(identity);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800339 if(contact == NULL)
340 return NULL;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800341
342 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
343 Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity);
344 Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity);
345
346 vector<string> endorseList = m_contactStorage->getEndorseList(identity);
347
Yingdi Yub35b8652013-11-07 11:32:40 -0800348 Ptr<EndorseCertificate> cert = NULL;
349 try{
350 cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList));
351 }catch(exception& e){
352 _LOG_ERROR("Exception: " << e.what());
353 return NULL;
354 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800355 identityManager->signByCertificate(*cert, signerCertName);
356
357 return cert;
358}
359
Yingdi Yu79c25a22013-10-21 13:38:38 -0700360vector<Ptr<ContactItem> >
361ContactManager::getContactItemList()
Yingdi Yu813d4e92013-11-03 16:22:05 -0800362{ return m_contactStorage->getAllContacts(); }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700363
Yingdi Yud40226b2013-10-23 14:05:12 -0700364Ptr<ContactItem>
365ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800366{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yud40226b2013-10-23 14:05:12 -0700367
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700368Ptr<EndorseCertificate>
369ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
370 const Profile& profile)
371{
372 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
373 Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity);
374 if(0 == certificateName.size())
375 return NULL;
376
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800377 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700378 identityManager->signByCertificate(*profileData, certificateName);
379
Yingdi Yued8cfc42013-11-01 17:37:51 -0700380 Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800381 if(NULL == signingCert)
382 return NULL;
383
Yingdi Yued8cfc42013-11-01 17:37:51 -0700384 Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
385
386 Ptr<security::IdentityCertificate> kskCert;
387 if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-"))
388 {
389 Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
390 // HACK! KSK certificate should be retrieved from network.
Yingdi Yued8cfc42013-11-01 17:37:51 -0700391 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800392
Yingdi Yued8cfc42013-11-01 17:37:51 -0700393 Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800394
Yingdi Yued8cfc42013-11-01 17:37:51 -0700395 kskCert = identityManager->getCertificate(kskCertName);
396
397 }
398 else
399 {
400 kskCert = signingCert;
Yingdi Yued8cfc42013-11-01 17:37:51 -0700401 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700402
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800403 if(NULL == kskCert)
404 return NULL;
405
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700406 vector<string> endorseList;
407 Profile::const_iterator it = profile.begin();
408 for(; it != profile.end(); it++)
409 endorseList.push_back(it->first);
410
Yingdi Yub35b8652013-11-07 11:32:40 -0800411 Ptr<EndorseCertificate> selfEndorseCertificate = NULL;
412 try{
413 selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
414 profileData,
415 endorseList));
416 }catch(exception& e){
417 _LOG_ERROR("Exception: " << e.what());
418 return NULL;
419 }
420
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700421 identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
422
423 return selfEndorseCertificate;
424}
425
426
427void
428ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
429{
Yingdi Yuc29fb982013-10-20 19:43:10 -0700430 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700431
Yingdi Yub35b8652013-11-07 11:32:40 -0800432 Ptr<Data> plainData = NULL;
433 Ptr<EndorseCertificate> selfEndorseCertificate = NULL;
434 try{
435 plainData = Data::decodeFromWire(dataContentBlob);
436 selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
437 }catch(exception& e){
438 _LOG_ERROR("Exception: " << e.what());
439 return;
440 }
Yingdi Yuc29fb982013-10-20 19:43:10 -0700441
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700442 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700443
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700444 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800445 emit contactFetched (*selfEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700446 else
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800447 emit contactFetchFailed (identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700448}
449
450void
451ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
452{ emit contactFetchFailed (identity); }
453
454void
455ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800456{ emit contactFetchFailed(identity); }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700457
458void
459ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate)
460{
461 Ptr<Data> data = Ptr<Data>::Create();
462
463 Name keyName = selfEndorseCertificate->getPublicKeyName();
464 Name identity = keyName.getSubName(0, keyName.size()-1);
465
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700466
467 Name dnsName = identity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700468 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700469
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700470 data->setName(dnsName);
471 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700472
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700473 Content content(blob->buf(), blob->size());
474 data->setContent(content);
475
476 m_keychain->signByIdentity(*data, identity);
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700477
478 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700479
480 Ptr<Blob> dnsBlob = data->encodeToWire();
481
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700482 m_wrapper->putToNdnd(*dnsBlob);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700483}
484
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800485void
486ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity)
487{
488 Ptr<Data> data = Ptr<Data>::Create();
489
490 Name keyName = endorseCertificate->getPublicKeyName();
491 Name endorsee = keyName.getSubName(0, keyName.size()-1);
492
493
494 Name dnsName = signerIdentity;
495 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion();
496
497 data->setName(dnsName);
498 Ptr<Blob> blob = endorseCertificate->encodeToWire();
499
500 Content content(blob->buf(), blob->size());
501 data->setContent(content);
502
503 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity);
504 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
505
506 m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee);
507
508 Ptr<Blob> dnsBlob = data->encodeToWire();
509
510 m_wrapper->putToNdnd(*dnsBlob);
511}
512
513void
514ContactManager::publishEndorsedDataInDns(const Name& identity)
515{
516 Ptr<Data> data = Ptr<Data>::Create();
517
518 Name dnsName = identity;
519 dnsName.append("DNS").append("ENDORSED").appendVersion();
520 data->setName(dnsName);
521
522 Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity);
523
524 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
525
526 vector<Blob>::const_iterator it = collectEndorseList->begin();
527 for(; it != collectEndorseList->end(); it++)
528 {
529 Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it));
530 root->addChild(entry);
531 }
532
533 blob_stream blobStream;
534 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
535 root->encode(start);
536
537 Content content(blobStream.buf()->buf(), blobStream.buf()->size());
538 data->setContent(content);
539
540 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity);
541 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
542
543 m_dnsStorage->updateDnsOthersEndorse(*data, identity);
544
545 Ptr<Blob> dnsBlob = data->encodeToWire();
546
547 m_wrapper->putToNdnd(*dnsBlob);
548}
549
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700550
551#if WAF
552#include "contact-manager.moc"
553#include "contact-manager.cpp.moc"
554#endif