blob: 2b834c65bcbdd0532836156cf5d747db110392a5 [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
235 EndorseCertificate endorseCertificate(identityCertificate, profileData);
236
237 identityManager->signByCertificate(endorseCertificate, certificateName);
238
239 emit contactKeyFetched (endorseCertificate);
240}
241
242void
243ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity)
244{ emit contactKeyFetchFailed (identity); }
245
246void
247ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
248{ emit contactKeyFetchFailed(identity); }
249
250void
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700251ContactManager::updateProfileData(const Name& identity)
252{
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800253 // _LOG_DEBUG("updateProfileData: " << identity.toUri());
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700254 // Get current profile;
255 Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
256 if(NULL == newProfile)
257 return;
258 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
259
260 // Check if profile exists
261 Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
262 if(NULL != profileDataBlob)
263 {
264 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
265 EndorseCertificate oldEndorseCertificate(*plainData);
266 // _LOG_DEBUG("Certificate converted!");
267 const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content();
268
269 if(oldProfileBlob == *newProfileBlob)
270 return;
271
272 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
273 // _LOG_DEBUG("Signing DONE!");
274 if(NULL == newEndorseCertificate)
275 return;
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800276 // _LOG_DEBUG("About to update");
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700277 m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
278
279 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
280 }
281 else
282 {
283 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
284 // _LOG_DEBUG("Signing DONE!");
285 if(NULL == newEndorseCertificate)
286 return;
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800287 // _LOG_DEBUG("About to Insert");
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700288 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
289
290 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
291 }
292}
293
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800294void
295ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
296{
297 Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
298 Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
299 if(NULL != oldEndorseCertificateBlob)
300 {
301 Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob);
302 EndorseCertificate oldEndorseCertificate(*plainData);
303 const Blob& oldEndorseContent = oldEndorseCertificate.content();
304 const Blob& newEndorseContent = newEndorseCertificate->content();
305 if(oldEndorseContent == newEndorseContent)
306 return;
307 }
308 else
309 {
310 if(NULL == newEndorseCertificate)
311 return;
312 }
313 m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity);
314 publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity);
315}
316
317Ptr<EndorseCertificate>
318ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
319{
320 Ptr<ContactItem> contact = getContact(identity);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800321 if(contact == NULL)
322 return NULL;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800323
324 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
325 Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity);
326 Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity);
327
328 vector<string> endorseList = m_contactStorage->getEndorseList(identity);
329
330 Ptr<EndorseCertificate> cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList));
331 identityManager->signByCertificate(*cert, signerCertName);
332
333 return cert;
334}
335
Yingdi Yu79c25a22013-10-21 13:38:38 -0700336vector<Ptr<ContactItem> >
337ContactManager::getContactItemList()
Yingdi Yu813d4e92013-11-03 16:22:05 -0800338{ return m_contactStorage->getAllContacts(); }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700339
Yingdi Yud40226b2013-10-23 14:05:12 -0700340Ptr<ContactItem>
341ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800342{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yud40226b2013-10-23 14:05:12 -0700343
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700344Ptr<EndorseCertificate>
345ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
346 const Profile& profile)
347{
348 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
349 Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity);
350 if(0 == certificateName.size())
351 return NULL;
352
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800353 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700354 identityManager->signByCertificate(*profileData, certificateName);
355
Yingdi Yued8cfc42013-11-01 17:37:51 -0700356 Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800357 if(NULL == signingCert)
358 return NULL;
359
Yingdi Yued8cfc42013-11-01 17:37:51 -0700360 Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
361
362 Ptr<security::IdentityCertificate> kskCert;
363 if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-"))
364 {
365 Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
366 // HACK! KSK certificate should be retrieved from network.
Yingdi Yued8cfc42013-11-01 17:37:51 -0700367 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800368
Yingdi Yued8cfc42013-11-01 17:37:51 -0700369 Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800370
Yingdi Yued8cfc42013-11-01 17:37:51 -0700371 kskCert = identityManager->getCertificate(kskCertName);
372
373 }
374 else
375 {
376 kskCert = signingCert;
Yingdi Yued8cfc42013-11-01 17:37:51 -0700377 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700378
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800379 if(NULL == kskCert)
380 return NULL;
381
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700382 vector<string> endorseList;
383 Profile::const_iterator it = profile.begin();
384 for(; it != profile.end(); it++)
385 endorseList.push_back(it->first);
386
387 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700388 profileData,
389 endorseList));
390 identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
391
392 return selfEndorseCertificate;
393}
394
395
396void
397ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
398{
Yingdi Yuc29fb982013-10-20 19:43:10 -0700399 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700400
Yingdi Yuc29fb982013-10-20 19:43:10 -0700401 Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob);
402
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700403 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
Yingdi Yuc29fb982013-10-20 19:43:10 -0700404
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700405 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700406
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700407 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800408 emit contactFetched (*selfEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700409 else
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800410 emit contactFetchFailed (identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700411}
412
413void
414ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
415{ emit contactFetchFailed (identity); }
416
417void
418ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800419{ emit contactFetchFailed(identity); }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700420
421void
422ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate)
423{
424 Ptr<Data> data = Ptr<Data>::Create();
425
426 Name keyName = selfEndorseCertificate->getPublicKeyName();
427 Name identity = keyName.getSubName(0, keyName.size()-1);
428
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700429
430 Name dnsName = identity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700431 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700432
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700433 data->setName(dnsName);
434 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700435
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700436 Content content(blob->buf(), blob->size());
437 data->setContent(content);
438
439 m_keychain->signByIdentity(*data, identity);
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700440
441 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700442
443 Ptr<Blob> dnsBlob = data->encodeToWire();
444
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700445 m_wrapper->putToNdnd(*dnsBlob);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700446}
447
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800448void
449ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity)
450{
451 Ptr<Data> data = Ptr<Data>::Create();
452
453 Name keyName = endorseCertificate->getPublicKeyName();
454 Name endorsee = keyName.getSubName(0, keyName.size()-1);
455
456
457 Name dnsName = signerIdentity;
458 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion();
459
460 data->setName(dnsName);
461 Ptr<Blob> blob = endorseCertificate->encodeToWire();
462
463 Content content(blob->buf(), blob->size());
464 data->setContent(content);
465
466 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity);
467 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
468
469 m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee);
470
471 Ptr<Blob> dnsBlob = data->encodeToWire();
472
473 m_wrapper->putToNdnd(*dnsBlob);
474}
475
476void
477ContactManager::publishEndorsedDataInDns(const Name& identity)
478{
479 Ptr<Data> data = Ptr<Data>::Create();
480
481 Name dnsName = identity;
482 dnsName.append("DNS").append("ENDORSED").appendVersion();
483 data->setName(dnsName);
484
485 Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity);
486
487 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
488
489 vector<Blob>::const_iterator it = collectEndorseList->begin();
490 for(; it != collectEndorseList->end(); it++)
491 {
492 Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it));
493 root->addChild(entry);
494 }
495
496 blob_stream blobStream;
497 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
498 root->encode(start);
499
500 Content content(blobStream.buf()->buf(), blobStream.buf()->size());
501 data->setContent(content);
502
503 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity);
504 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
505
506 m_dnsStorage->updateDnsOthersEndorse(*data, identity);
507
508 Ptr<Blob> dnsBlob = data->encodeToWire();
509
510 m_wrapper->putToNdnd(*dnsBlob);
511}
512
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700513
514#if WAF
515#include "contact-manager.moc"
516#include "contact-manager.cpp.moc"
517#endif