blob: 13e4adb99a4a18581d9cb61f0a9f99573f0fa8f3 [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 Yub29f78c2013-11-09 20:12:31 -080029ContactManager::ContactManager(QObject* parent)
Yingdi Yu590fa5d2013-10-18 18:35:09 -070030 : QObject(parent)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070031{
Yingdi Yub29f78c2013-11-09 20:12:31 -080032 m_contactStorage = Ptr<ContactStorage>::Create();
33 m_dnsStorage = Ptr<DnsStorage>::Create();
Yingdi Yuaa8d7692013-10-18 17:05:02 -070034
Yingdi Yub29f78c2013-11-09 20:12:31 -080035 setKeychain();
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070036}
37
38ContactManager::~ContactManager()
39{
40}
41
Yingdi Yuaa8d7692013-10-18 17:05:02 -070042void
Yingdi Yub29f78c2013-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){
Yingdi Yueb98f7d2013-11-10 01:34:57 -080048 emit noNdnConnection(QString::fromStdString("Cannot conect to ndnd!\nHave you started your ndnd?"));
Yingdi Yub29f78c2013-11-09 20:12:31 -080049 }
50}
51
52void
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070053ContactManager::setKeychain()
54{
Yingdi Yu6a5b9f62013-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 Yu0b82a4e2013-10-18 11:29:25 -070059
Yingdi Yu8dacdf22013-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 Yuaa8d7692013-10-18 17:05:02 -070063 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070064 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070065 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070066 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070067 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070068 "==", "\\1", "\\1\\2", true)));
Yingdi Yu42f66462013-10-31 17:38:22 -070069 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070070 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070071 ">", "\\1\\2", "\\1", true)));
Yingdi Yu42f66462013-10-31 17:38:22 -070072 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070073 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070074 "==", "\\1", "\\1\\2", true)));
Yingdi Yu8fb16a42013-11-10 18:35:09 -080075 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^(<>*)$",
76 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
77 ">", "\\1", "\\1\\2", true)));
78
Yingdi Yuaa8d7692013-10-18 17:05:02 -070079
80 policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
81 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
82 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070083
Yingdi Yu8fb16a42013-11-10 18:35:09 -080084
Yingdi Yu6a5b9f62013-11-06 23:00:21 -080085 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
86HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
8767biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
885X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
89WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
90Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
91MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
92nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
93c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
94MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
95b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
96yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
97I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
98/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
9972P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
100VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
101iVUF1QIBEQAA");
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700102
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800103 string decoded;
104 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
105 TrustAnchor.size(),
106 true,
107 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
108 Ptr<Blob> blob = Ptr<Blob>(new Blob(decoded.c_str(), decoded.size()));
109 Ptr<Data> data = Data::decodeFromWire(blob);
110 Ptr<IdentityCertificate>anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700111 policyManager->addTrustAnchor(anchor);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800112
113#ifdef _DEBUG
114
115 const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
116zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
117qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
1186M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
119DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
120Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
121MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
122qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
123APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
124MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
125MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
126/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
127sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
128H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
129gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
130OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
131F7Wh5ayeo8NBKDsCAwEAAQAA");
132
133 string decoded2;
134 CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()),
135 FakeAnchor.size(),
136 true,
137 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
138 Ptr<Blob> blob2 = Ptr<Blob>(new Blob(decoded2.c_str(), decoded2.size()));
139 Ptr<Data> data2 = Data::decodeFromWire(blob2);
140 Ptr<IdentityCertificate>anchor2 = Ptr<IdentityCertificate>(new IdentityCertificate(*data2));
141 policyManager->addTrustAnchor(anchor2);
142
143#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700144
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700145 m_keychain = keychain;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700146}
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700147
148
149void
150ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
151{
152 Name interestName = identity;
153 interestName.append("DNS").append("PROFILE");
154
155 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
Yingdi Yu42f66462013-10-31 17:38:22 -0700156 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700157 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified,
158 this,
159 _1,
160 identity),
161 boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout,
162 this,
163 _1,
164 _2,
165 identity,
166 0),
167 boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified,
168 this,
169 _1,
170 identity)));
171 m_wrapper->sendInterest(interestPtr, closure);
172}
173
174void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800175ContactManager::fetchCollectEndorse(const ndn::Name& identity)
176{
177 Name interestName = identity;
178 interestName.append("DNS").append("ENDORSED");
179
180 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
181 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
182 interestPtr->setInterestLifetime(1);
183 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsCollectEndorseVerified,
184 this,
185 _1,
186 identity),
187 boost::bind(&ContactManager::onDnsCollectEndorseTimeout,
188 this,
189 _1,
190 _2,
191 identity,
192 0),
193 boost::bind(&ContactManager::onDnsCollectEndorseUnverified,
194 this,
195 _1,
196 identity)));
197 m_wrapper->sendInterest(interestPtr, closure);
198}
199
200void
201ContactManager::fetchKey(const ndn::Name& certName)
202{
203 Name interestName = certName;
204
205 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
206 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yu908f8412013-11-09 00:03:26 -0800207 interestPtr->setInterestLifetime(1);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800208 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onKeyVerified,
209 this,
210 _1,
211 certName),
212 boost::bind(&ContactManager::onKeyTimeout,
213 this,
214 _1,
215 _2,
216 certName,
217 0),
218 boost::bind(&ContactManager::onKeyUnverified,
219 this,
220 _1,
221 certName)));
222 m_wrapper->sendInterest(interestPtr, closure);
223}
224
225void
Yingdi Yu908f8412013-11-09 00:03:26 -0800226ContactManager::fetchIdCertificate(const ndn::Name& certName)
227{
228 Name interestName = certName;
229
230 Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
231 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
232 interestPtr->setInterestLifetime(1);
233 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onIdCertificateVerified,
234 this,
235 _1,
236 certName),
237 boost::bind(&ContactManager::onIdCertificateTimeout,
238 this,
239 _1,
240 _2,
241 certName,
242 0),
243 boost::bind(&ContactManager::onIdCertificateUnverified,
244 this,
245 _1,
246 certName)));
247 m_wrapper->sendInterest(interestPtr, closure);
248}
249
250void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800251ContactManager::onDnsCollectEndorseVerified(Ptr<Data> data, const Name& identity)
252{ emit collectEndorseFetched (*data); }
253
254void
255ContactManager::onDnsCollectEndorseTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
256{ emit collectEndorseFetchFailed (identity); }
257
258void
259ContactManager::onDnsCollectEndorseUnverified(Ptr<Data> data, const Name& identity)
260{ emit collectEndorseFetchFailed (identity); }
261
262void
263ContactManager::onKeyVerified(Ptr<Data> data, const Name& identity)
264{
265 IdentityCertificate identityCertificate(*data);
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800266
267 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(Profile(identityCertificate)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800268
269 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
270 Name certificateName = identityManager->getDefaultCertificateName ();
271 identityManager->signByCertificate(*profileData, certificateName);
272
Yingdi Yub35b8652013-11-07 11:32:40 -0800273 Ptr<EndorseCertificate> endorseCertificate = NULL;
274 try{
275 endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData));
276 }catch(exception& e){
277 _LOG_ERROR("Exception: " << e.what());
278 return;
279 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800280
Yingdi Yub35b8652013-11-07 11:32:40 -0800281 identityManager->signByCertificate(*endorseCertificate, certificateName);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800282
Yingdi Yub35b8652013-11-07 11:32:40 -0800283 emit contactKeyFetched (*endorseCertificate);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800284}
285
286void
287ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity)
Yingdi Yu8fb16a42013-11-10 18:35:09 -0800288{
289 _LOG_DEBUG("Key cannot be verified!");
290 emit contactKeyFetchFailed (identity);
291}
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800292
293void
294ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
Yingdi Yu8fb16a42013-11-10 18:35:09 -0800295{
296 _LOG_DEBUG("Key timeout!");
297 emit contactKeyFetchFailed(identity);
298}
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800299
300void
Yingdi Yu908f8412013-11-09 00:03:26 -0800301ContactManager::onIdCertificateVerified(Ptr<Data> data, const Name& identity)
302{
303 IdentityCertificate identityCertificate(*data);
304 emit contactCertificateFetched(identityCertificate);
305}
306
307void
308ContactManager::onIdCertificateUnverified(Ptr<Data> data, const Name& identity)
309{ emit contactCertificateFetchFailed (identity); }
310
311void
312ContactManager::onIdCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
313{ emit contactCertificateFetchFailed (identity); }
314
315void
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700316ContactManager::updateProfileData(const Name& identity)
317{
318 // Get current profile;
319 Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
320 if(NULL == newProfile)
321 return;
322 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
323
324 // Check if profile exists
325 Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
326 if(NULL != profileDataBlob)
327 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800328
329 Ptr<EndorseCertificate> oldEndorseCertificate = NULL;
330 try{
331 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
332 oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
333 }catch(exception& e){
334 _LOG_ERROR("Exception: " << e.what());
335 return;
336 }
337
338 const Blob& oldProfileBlob = oldEndorseCertificate->getProfileData()->content();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700339
340 if(oldProfileBlob == *newProfileBlob)
341 return;
342
343 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
Yingdi Yub35b8652013-11-07 11:32:40 -0800344
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700345 if(NULL == newEndorseCertificate)
346 return;
Yingdi Yub35b8652013-11-07 11:32:40 -0800347
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700348 m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
349
350 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
351 }
352 else
353 {
354 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
Yingdi Yub35b8652013-11-07 11:32:40 -0800355
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700356 if(NULL == newEndorseCertificate)
357 return;
Yingdi Yub35b8652013-11-07 11:32:40 -0800358
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700359 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
360
361 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
362 }
363}
364
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800365void
366ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
367{
368 Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity);
369 Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
370 if(NULL != oldEndorseCertificateBlob)
371 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800372 Ptr<EndorseCertificate> oldEndorseCertificate = NULL;
373 try{
374 Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob);
375 oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
376 }catch(exception& e){
377 _LOG_ERROR("Exception: " << e.what());
378 return;
379 }
380 const Blob& oldEndorseContent = oldEndorseCertificate->content();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800381 const Blob& newEndorseContent = newEndorseCertificate->content();
382 if(oldEndorseContent == newEndorseContent)
383 return;
384 }
385 else
386 {
387 if(NULL == newEndorseCertificate)
388 return;
389 }
390 m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity);
391 publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity);
392}
393
394Ptr<EndorseCertificate>
395ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
396{
397 Ptr<ContactItem> contact = getContact(identity);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800398 if(contact == NULL)
399 return NULL;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800400
401 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
402 Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity);
403 Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity);
404
405 vector<string> endorseList = m_contactStorage->getEndorseList(identity);
406
Yingdi Yub35b8652013-11-07 11:32:40 -0800407 Ptr<EndorseCertificate> cert = NULL;
408 try{
409 cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList));
410 }catch(exception& e){
411 _LOG_ERROR("Exception: " << e.what());
412 return NULL;
413 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800414 identityManager->signByCertificate(*cert, signerCertName);
415
416 return cert;
417}
418
Yingdi Yu79c25a22013-10-21 13:38:38 -0700419vector<Ptr<ContactItem> >
420ContactManager::getContactItemList()
Yingdi Yu813d4e92013-11-03 16:22:05 -0800421{ return m_contactStorage->getAllContacts(); }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700422
Yingdi Yud40226b2013-10-23 14:05:12 -0700423Ptr<ContactItem>
424ContactManager::getContact(const ndn::Name& contactNamespace)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800425{ return m_contactStorage->getContact(contactNamespace); }
Yingdi Yud40226b2013-10-23 14:05:12 -0700426
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700427Ptr<EndorseCertificate>
428ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
429 const Profile& profile)
430{
431 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
432 Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity);
433 if(0 == certificateName.size())
434 return NULL;
435
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800436 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700437 identityManager->signByCertificate(*profileData, certificateName);
438
Yingdi Yued8cfc42013-11-01 17:37:51 -0700439 Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800440 if(NULL == signingCert)
441 return NULL;
442
Yingdi Yued8cfc42013-11-01 17:37:51 -0700443 Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
444
445 Ptr<security::IdentityCertificate> kskCert;
446 if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-"))
447 {
448 Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
449 // HACK! KSK certificate should be retrieved from network.
Yingdi Yued8cfc42013-11-01 17:37:51 -0700450 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800451
Yingdi Yued8cfc42013-11-01 17:37:51 -0700452 Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800453
Yingdi Yued8cfc42013-11-01 17:37:51 -0700454 kskCert = identityManager->getCertificate(kskCertName);
455
456 }
457 else
458 {
459 kskCert = signingCert;
Yingdi Yued8cfc42013-11-01 17:37:51 -0700460 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700461
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800462 if(NULL == kskCert)
463 return NULL;
464
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700465 vector<string> endorseList;
466 Profile::const_iterator it = profile.begin();
467 for(; it != profile.end(); it++)
468 endorseList.push_back(it->first);
469
Yingdi Yub35b8652013-11-07 11:32:40 -0800470 Ptr<EndorseCertificate> selfEndorseCertificate = NULL;
471 try{
472 selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
473 profileData,
474 endorseList));
475 }catch(exception& e){
476 _LOG_ERROR("Exception: " << e.what());
477 return NULL;
478 }
479
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700480 identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
481
482 return selfEndorseCertificate;
483}
484
485
486void
487ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
488{
Yingdi Yuc29fb982013-10-20 19:43:10 -0700489 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700490
Yingdi Yub35b8652013-11-07 11:32:40 -0800491 Ptr<Data> plainData = NULL;
492 Ptr<EndorseCertificate> selfEndorseCertificate = NULL;
493 try{
494 plainData = Data::decodeFromWire(dataContentBlob);
495 selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
496 }catch(exception& e){
497 _LOG_ERROR("Exception: " << e.what());
498 return;
499 }
Yingdi Yuc29fb982013-10-20 19:43:10 -0700500
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700501 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700502
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700503 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800504 emit contactFetched (*selfEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700505 else
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800506 emit contactFetchFailed (identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700507}
508
509void
510ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
511{ emit contactFetchFailed (identity); }
512
513void
514ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800515{ emit contactFetchFailed(identity); }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700516
517void
518ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate)
519{
520 Ptr<Data> data = Ptr<Data>::Create();
521
522 Name keyName = selfEndorseCertificate->getPublicKeyName();
523 Name identity = keyName.getSubName(0, keyName.size()-1);
524
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700525
526 Name dnsName = identity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700527 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700528
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700529 data->setName(dnsName);
530 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700531
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700532 Content content(blob->buf(), blob->size());
533 data->setContent(content);
534
535 m_keychain->signByIdentity(*data, identity);
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700536
537 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700538
539 Ptr<Blob> dnsBlob = data->encodeToWire();
540
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700541 m_wrapper->putToNdnd(*dnsBlob);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700542}
543
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800544void
545ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity)
546{
547 Ptr<Data> data = Ptr<Data>::Create();
548
549 Name keyName = endorseCertificate->getPublicKeyName();
550 Name endorsee = keyName.getSubName(0, keyName.size()-1);
551
552
553 Name dnsName = signerIdentity;
554 dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion();
555
556 data->setName(dnsName);
557 Ptr<Blob> blob = endorseCertificate->encodeToWire();
558
559 Content content(blob->buf(), blob->size());
560 data->setContent(content);
561
562 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity);
563 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
564
565 m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee);
566
567 Ptr<Blob> dnsBlob = data->encodeToWire();
568
569 m_wrapper->putToNdnd(*dnsBlob);
570}
571
572void
573ContactManager::publishEndorsedDataInDns(const Name& identity)
574{
575 Ptr<Data> data = Ptr<Data>::Create();
576
577 Name dnsName = identity;
578 dnsName.append("DNS").append("ENDORSED").appendVersion();
579 data->setName(dnsName);
580
581 Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity);
582
583 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
584
585 vector<Blob>::const_iterator it = collectEndorseList->begin();
586 for(; it != collectEndorseList->end(); it++)
587 {
588 Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it));
589 root->addChild(entry);
590 }
591
592 blob_stream blobStream;
593 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
594 root->encode(start);
595
596 Content content(blobStream.buf()->buf(), blobStream.buf()->size());
597 data->setContent(content);
598
599 Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity);
600 m_keychain->getIdentityManager()->signByCertificate(*data, signCertName);
601
602 m_dnsStorage->updateDnsOthersEndorse(*data, identity);
603
604 Ptr<Blob> dnsBlob = data->encodeToWire();
605
606 m_wrapper->putToNdnd(*dnsBlob);
607}
608
Yingdi Yu908f8412013-11-09 00:03:26 -0800609void
610ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile)
611{
612 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile));
613
614 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
615 Name certificateName = identityManager->getDefaultCertificateNameByIdentity (m_defaultIdentity);
616 identityManager->signByCertificate(*profileData, certificateName);
617
618 Ptr<EndorseCertificate> endorseCertificate = NULL;
619 try{
620 endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData));
621 }catch(exception& e){
622 _LOG_ERROR("Exception: " << e.what());
623 return;
624 }
625
626 identityManager->signByCertificate(*endorseCertificate, certificateName);
627
628 ContactItem contactItem(*endorseCertificate);
629
630 try{
631 m_contactStorage->addContact(contactItem);
632 }catch(exception& e){
633 emit warning(e.what());
634 _LOG_ERROR("Exception: " << e.what());
635 return;
636 }
637}
638
639
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700640
641#if WAF
642#include "contact-manager.moc"
643#include "contact-manager.cpp.moc"
644#endif