blob: f7559651b1fdcd33af6753e87201a4674ff73f05 [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
Yingdi Yuf8f572d2014-01-13 11:19:47 -080011#if __clang__
Yingdi Yuf8f572d2014-01-13 11:19:47 -080012#pragma clang diagnostic ignored "-Wtautological-compare"
Yingdi Yuf8f572d2014-01-13 11:19:47 -080013#endif
14
15
Yingdi Yu9236c432013-10-18 11:29:25 -070016#include "contact-manager.h"
17
Yingdi Yu4685b1b2013-10-18 17:05:02 -070018#ifndef Q_MOC_RUN
Yingdi Yub6fb0302014-01-21 11:05:11 -080019#include <ndn-cpp-dev/face.hpp>
20#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080021
22#ifndef WITH_SECURITY
23#include <ndn-cpp-dev/security/validator-null.hpp>
24#else
25#include <ndn-cpp-dev/security/validator-regex.hpp>
Yingdi Yu72781e52013-11-06 23:00:21 -080026#include <cryptopp/base64.h>
Yingdi Yufa4ce792014-02-06 18:09:22 -080027#include <ndn-cpp-dev/security/sec-rule-relative.hpp>
28#endif
29
Yingdi Yu76dd8002013-12-24 11:16:32 +080030#include "endorse-collection.pb.h"
Yingdi Yuec3d9a32013-10-18 18:35:09 -070031#include "logging.h"
Yingdi Yu4685b1b2013-10-18 17:05:02 -070032#endif
Yingdi Yu9236c432013-10-18 11:29:25 -070033
34using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080035using namespace std;
Yingdi Yu9236c432013-10-18 11:29:25 -070036
Yingdi Yuec3d9a32013-10-18 18:35:09 -070037INIT_LOGGER("ContactManager");
38
Yingdi Yufa4ce792014-02-06 18:09:22 -080039namespace chronos{
Yingdi Yu4685b1b2013-10-18 17:05:02 -070040
Yingdi Yufa4ce792014-02-06 18:09:22 -080041ContactManager::ContactManager(shared_ptr<Face> face,
42 QObject* parent)
43 : QObject(parent)
44 , m_contactStorage(new ContactStorage())
45 , m_dnsStorage(new DnsStorage())
46 , m_face(face)
47{
Yingdi Yu76dd8002013-12-24 11:16:32 +080048 initializeSecurity();
Yingdi Yu9236c432013-10-18 11:29:25 -070049}
50
51ContactManager::~ContactManager()
Yingdi Yu6eabbd72013-12-27 08:44:12 +080052{}
Yingdi Yu9236c432013-10-18 11:29:25 -070053
Yingdi Yu76dd8002013-12-24 11:16:32 +080054void
55ContactManager::initializeSecurity()
56{
Yingdi Yufa4ce792014-02-06 18:09:22 -080057
58#ifndef WITH_SECURITY
59
60 m_keyChain = make_shared<KeyChain>();
61 m_validator = make_shared<ValidatorNull>();
62
63#else
64
Yingdi Yu68de61d2014-01-16 19:13:03 -080065 shared_ptr<SecPolicySimple> policy = make_shared<SecPolicySimple>();
66 m_verifier = make_shared<Verifier>(policy);
67 m_verifier->setFace(m_face);
Yingdi Yu76dd8002013-12-24 11:16:32 +080068
Yingdi Yub6fb0302014-01-21 11:05:11 -080069 policy->addVerificationPolicyRule(make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><ENDORSED>",
Yingdi Yu68de61d2014-01-16 19:13:03 -080070 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
71 "==", "\\1", "\\1\\2", true));
Yingdi Yub6fb0302014-01-21 11:05:11 -080072 policy->addVerificationPolicyRule(make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yu68de61d2014-01-16 19:13:03 -080073 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
74 "==", "\\1", "\\1\\2", true));
Yingdi Yub6fb0302014-01-21 11:05:11 -080075 policy->addVerificationPolicyRule(make_shared<SecRuleRelative>("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yu68de61d2014-01-16 19:13:03 -080076 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
77 "==", "\\1", "\\1\\2", true));
Yingdi Yub6fb0302014-01-21 11:05:11 -080078 policy->addVerificationPolicyRule(make_shared<SecRuleRelative>("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yu68de61d2014-01-16 19:13:03 -080079 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
80 ">", "\\1\\2", "\\1", true));
Yingdi Yub6fb0302014-01-21 11:05:11 -080081 policy->addVerificationPolicyRule(make_shared<SecRuleRelative>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yu68de61d2014-01-16 19:13:03 -080082 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
83 "==", "\\1", "\\1\\2", true));
Yingdi Yub6fb0302014-01-21 11:05:11 -080084 policy->addVerificationPolicyRule(make_shared<SecRuleRelative>("^(<>*)$",
Yingdi Yu68de61d2014-01-16 19:13:03 -080085 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
86 ">", "\\1", "\\1\\2", true));
Yingdi Yu785f56f2013-11-10 18:35:09 -080087
Yingdi Yu4685b1b2013-10-18 17:05:02 -070088
Yingdi Yub6fb0302014-01-21 11:05:11 -080089 policy->addSigningPolicyRule(make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yu68de61d2014-01-16 19:13:03 -080090 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
91 "==", "\\1", "\\1\\2", true));
Yingdi Yu9236c432013-10-18 11:29:25 -070092
Yingdi Yu785f56f2013-11-10 18:35:09 -080093
Yingdi Yu72781e52013-11-06 23:00:21 -080094 const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
95HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
9667biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
975X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
98WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
99Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
100MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
101nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
102c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
103MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
104b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
105yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
106I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
107/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
10872P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
109VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
110iVUF1QIBEQAA");
Yingdi Yu9236c432013-10-18 11:29:25 -0700111
Yingdi Yu72781e52013-11-06 23:00:21 -0800112 string decoded;
113 CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()),
114 TrustAnchor.size(),
115 true,
116 new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800117 Data data;
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800118 data.wireDecode(Block(reinterpret_cast<const uint8_t*>(decoded.c_str()), decoded.size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800119 shared_ptr<IdentityCertificate> anchor = make_shared<IdentityCertificate>(data);
Yingdi Yu68de61d2014-01-16 19:13:03 -0800120 policy->addTrustAnchor(anchor);
Yingdi Yu72781e52013-11-06 23:00:21 -0800121#endif
Yingdi Yu9236c432013-10-18 11:29:25 -0700122}
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700123
124
125void
126ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity)
127{
128 Name interestName = identity;
129 interestName.append("DNS").append("PROFILE");
130
Yingdi Yu76dd8002013-12-24 11:16:32 +0800131 Interest interest(interestName);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800132 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800133
Yingdi Yufa4ce792014-02-06 18:09:22 -0800134 OnDataValidated onValidated = bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identity);
135 OnDataValidationFailed onValidationFailed = bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, identity);
136 TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800137
Yingdi Yufa4ce792014-02-06 18:09:22 -0800138 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700139}
140
141void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800142ContactManager::onDnsSelfEndorseCertValidated(const shared_ptr<const Data>& data,
143 const Name& identity)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800144{
145 try{
146 Data plainData;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800147 plainData.wireDecode(data->getContent().blockFromValue());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800148 EndorseCertificate selfEndorseCertificate(plainData);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800149 if(Validator::verifySignature(plainData, plainData.getSignature(), selfEndorseCertificate.getPublicKeyInfo()))
150 emit contactFetched(selfEndorseCertificate);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800151 else
Yingdi Yufa4ce792014-02-06 18:09:22 -0800152 emit contactFetchFailed(identity);
153 }catch(...){
Yingdi Yu76dd8002013-12-24 11:16:32 +0800154 emit contactFetchFailed (identity);
155 }
156}
157
158void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800159ContactManager::fetchCollectEndorse(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800160{
161 Name interestName = identity;
162 interestName.append("DNS").append("ENDORSED");
163
Yingdi Yu76dd8002013-12-24 11:16:32 +0800164 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800165 interest.setInterestLifetime(1000);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800166 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800167
Yingdi Yufa4ce792014-02-06 18:09:22 -0800168 OnDataValidated onValidated = bind(&ContactManager::onDnsCollectEndorseValidated, this, _1, identity);
169 OnDataValidationFailed onValidationFailed = bind(&ContactManager::onDnsCollectEndorseValidationFailed, this, _1, identity);
170 TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800171
Yingdi Yufa4ce792014-02-06 18:09:22 -0800172 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800173}
174
175void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800176ContactManager::fetchKey(const Name& certName)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800177{
178 Name interestName = certName;
179
Yingdi Yu76dd8002013-12-24 11:16:32 +0800180 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800181 interest.setInterestLifetime(1000);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800182 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800183
Yingdi Yufa4ce792014-02-06 18:09:22 -0800184 OnDataValidated onValidated = bind(&ContactManager::onKeyValidated, this, _1, certName);
185 OnDataValidationFailed onValidationFailed = bind(&ContactManager::onKeyValidationFailed, this, _1, certName);
186 TimeoutNotify timeoutNotify = bind(&ContactManager::onKeyTimeoutNotify, this, certName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800187
Yingdi Yufa4ce792014-02-06 18:09:22 -0800188 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800189}
190
Yingdi Yu76dd8002013-12-24 11:16:32 +0800191void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800192ContactManager::onKeyValidated(const shared_ptr<const Data>& data, const Name& identity)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800193{
194 IdentityCertificate identityCertificate(*data);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800195 Profile profile(identityCertificate);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800196
197 try{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800198 EndorseCertificate endorseCertificate(identityCertificate, profile);
199 m_keyChain->sign(endorseCertificate);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800200 emit contactKeyFetched (endorseCertificate);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800201 }catch(...){
Yingdi Yu76dd8002013-12-24 11:16:32 +0800202 return;
203 }
204}
205
206void
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800207ContactManager::fetchIdCertificate(const Name& certName)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800208{
209 Name interestName = certName;
210
Yingdi Yu76dd8002013-12-24 11:16:32 +0800211 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800212 interest.setInterestLifetime(1000);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800213 interest.setMustBeFresh(true);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800214
Yingdi Yufa4ce792014-02-06 18:09:22 -0800215 OnDataValidated onValidated = bind(&ContactManager::onIdCertValidated, this, _1, certName);
216 OnDataValidationFailed onValidationFailed = bind(&ContactManager::onIdCertValidationFailed, this, _1, certName);
217 TimeoutNotify timeoutNotify = bind(&ContactManager::onIdCertTimeoutNotify, this, certName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800218
Yingdi Yufa4ce792014-02-06 18:09:22 -0800219 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800220}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800221
222void
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700223ContactManager::updateProfileData(const Name& identity)
224{
225 // Get current profile;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800226 shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800227 if(!static_cast<bool>(newProfile))
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700228 return;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800229
230 shared_ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
231
Yingdi Yufa4ce792014-02-06 18:09:22 -0800232 if(!static_cast<bool>(newEndorseCertificate))
Yingdi Yu76dd8002013-12-24 11:16:32 +0800233 return;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700234
Yingdi Yufa4ce792014-02-06 18:09:22 -0800235 m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yue35bdb82013-11-07 11:32:40 -0800236
Yingdi Yu76dd8002013-12-24 11:16:32 +0800237 publishSelfEndorseCertificateInDNS(*newEndorseCertificate);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700238}
239
Yingdi Yub2e747d2013-11-05 23:06:43 -0800240void
241ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity)
242{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800243 shared_ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity);
244
Yingdi Yufa4ce792014-02-06 18:09:22 -0800245 if(!static_cast<bool>(newEndorseCertificate))
Yingdi Yu76dd8002013-12-24 11:16:32 +0800246 return;
247
Yingdi Yufa4ce792014-02-06 18:09:22 -0800248 m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identity);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800249
Yingdi Yu76dd8002013-12-24 11:16:32 +0800250 publishEndorseCertificateInDNS(*newEndorseCertificate, signerIdentity);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800251}
252
Yingdi Yu76dd8002013-12-24 11:16:32 +0800253shared_ptr<EndorseCertificate>
Yingdi Yub2e747d2013-11-05 23:06:43 -0800254ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
255{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800256 shared_ptr<ContactItem> contact = getContact(identity);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800257 if(!static_cast<bool>(contact))
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800258 return shared_ptr<EndorseCertificate>();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800259
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800260 Name signerKeyName = m_keyChain->getDefaultKeyNameForIdentity(signerIdentity);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800261
Yingdi Yu76dd8002013-12-24 11:16:32 +0800262 vector<string> endorseList;
263 m_contactStorage->getEndorseList(identity, endorseList);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800264
Yingdi Yue35bdb82013-11-07 11:32:40 -0800265 try{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800266 shared_ptr<EndorseCertificate> cert = make_shared<EndorseCertificate>(contact->getSelfEndorseCertificate(), signerKeyName, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800267 m_keyChain->signByIdentity(*cert, signerIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800268 return cert;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800269 }catch(...){
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800270 return shared_ptr<EndorseCertificate>();
Yingdi Yue35bdb82013-11-07 11:32:40 -0800271 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800272}
273
Yingdi Yu76dd8002013-12-24 11:16:32 +0800274shared_ptr<EndorseCertificate>
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700275ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
276 const Profile& profile)
277{
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800278 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700279 if(0 == certificateName.size())
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800280 return shared_ptr<EndorseCertificate>();
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700281
Yingdi Yufa4ce792014-02-06 18:09:22 -0800282 Name signingKeyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800283 shared_ptr<IdentityCertificate> kskCert;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800284
285 if(signingKeyName.get(-1).toEscapedString().substr(0,4) == "dsk-")
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700286 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800287 shared_ptr<IdentityCertificate> signingCert = m_keyChain->getCertificate(certificateName);
288 if(!static_cast<bool>(signingCert))
289 return shared_ptr<EndorseCertificate>();
Yingdi Yu72781e52013-11-06 23:00:21 -0800290
Yingdi Yufa4ce792014-02-06 18:09:22 -0800291 try{
292 SignatureSha256WithRsa dskCertSig(signingCert->getSignature());
293 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(dskCertSig.getKeyLocator().getName());
294 Name kskCertName = m_keyChain->getDefaultCertificateNameForKey(keyName);
295 kskCert = m_keyChain->getCertificate(kskCertName);
296 }catch(...){
297 return shared_ptr<EndorseCertificate>();
298 }
299 }
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700300 else
Yingdi Yufa4ce792014-02-06 18:09:22 -0800301 kskCert = m_keyChain->getCertificate(certificateName);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700302
Yingdi Yufa4ce792014-02-06 18:09:22 -0800303 if(!static_cast<bool>(kskCert))
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800304 return shared_ptr<EndorseCertificate>();
Yingdi Yu72781e52013-11-06 23:00:21 -0800305
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700306 vector<string> endorseList;
307 Profile::const_iterator it = profile.begin();
308 for(; it != profile.end(); it++)
309 endorseList.push_back(it->first);
310
Yingdi Yue35bdb82013-11-07 11:32:40 -0800311 try{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800312 shared_ptr<EndorseCertificate> selfEndorseCertificate = make_shared<EndorseCertificate>(*kskCert, profile, endorseList);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800313 m_keyChain->sign(*selfEndorseCertificate, kskCert->getName());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800314 return selfEndorseCertificate;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800315 }catch(...){
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800316 return shared_ptr<EndorseCertificate>();
Yingdi Yue35bdb82013-11-07 11:32:40 -0800317 }
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700318}
319
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700320void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800321ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700322{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800323 Data data;
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700324
Yingdi Yufa4ce792014-02-06 18:09:22 -0800325 Name identity = selfEndorseCertificate.getPublicKeyName().getPrefix(-1);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700326
327 Name dnsName = identity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800328 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800329 data.setName(dnsName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800330 data.setContent(selfEndorseCertificate.wireEncode());
331
Yingdi Yufa4ce792014-02-06 18:09:22 -0800332 m_keyChain->signByIdentity(data, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800333 m_dnsStorage->updateDnsSelfProfileData(data, identity);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800334 m_face->put(data);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700335}
336
Yingdi Yub2e747d2013-11-05 23:06:43 -0800337void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800338ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate, const Name& signerIdentity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800339{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800340 Data data;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800341
Yingdi Yufa4ce792014-02-06 18:09:22 -0800342 Name endorsee = endorseCertificate.getPublicKeyName().getPrefix(-1);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800343
344 Name dnsName = signerIdentity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800345 dnsName.append("DNS").append(endorsee.wireEncode()).append("ENDORSEE").appendVersion();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800346 data.setName(dnsName);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800347
Yingdi Yu76dd8002013-12-24 11:16:32 +0800348 data.setContent(endorseCertificate.wireEncode());
Yingdi Yub2e747d2013-11-05 23:06:43 -0800349
Yingdi Yufa4ce792014-02-06 18:09:22 -0800350 m_keyChain->signByIdentity(data, signerIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800351 m_dnsStorage->updateDnsEndorseOthers(data, signerIdentity, endorsee);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800352 m_face->put(data);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800353}
354
355void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800356ContactManager::publishCollectEndorsedDataInDNS(const Name& identity)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800357{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800358 Data data;
359
Yingdi Yub2e747d2013-11-05 23:06:43 -0800360 Name dnsName = identity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800361 dnsName.append("DNS").append("ENDORSED").appendVersion();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800362 data.setName(dnsName);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800363
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800364 vector<Buffer> collectEndorseList;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800365 m_contactStorage->getCollectEndorseList(identity, collectEndorseList);
366
367 Chronos::EndorseCollection endorseCollection;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800368
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800369 vector<Buffer>::const_iterator it = collectEndorseList.begin();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800370 for(; it != collectEndorseList.end(); it++)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800371 {
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800372 string entryStr(reinterpret_cast<const char*>(it->buf()), it->size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800373 endorseCollection.add_endorsement()->set_blob(entryStr);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800374 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800375
Yingdi Yu76dd8002013-12-24 11:16:32 +0800376 string encoded;
377 endorseCollection.SerializeToString(&encoded);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800378
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800379 data.setContent(reinterpret_cast<const uint8_t*>(encoded.c_str()), encoded.size());
Yingdi Yub2e747d2013-11-05 23:06:43 -0800380
Yingdi Yufa4ce792014-02-06 18:09:22 -0800381 m_keyChain->signByIdentity(data, identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800382 m_dnsStorage->updateDnsOthersEndorse(data, identity);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800383 m_face->put(data);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800384}
385
Yingdi Yuae8217c2013-11-09 00:03:26 -0800386void
387ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile)
388{
Yingdi Yuae8217c2013-11-09 00:03:26 -0800389 try{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800390 EndorseCertificate endorseCertificate(identityCertificate, profile);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800391
Yingdi Yufa4ce792014-02-06 18:09:22 -0800392 m_keyChain->signByIdentity(endorseCertificate, m_defaultIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800393
394 ContactItem contactItem(endorseCertificate);
395
Yingdi Yuae8217c2013-11-09 00:03:26 -0800396 m_contactStorage->addContact(contactItem);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800397
Yingdi Yu72232692013-11-12 17:50:21 -0800398 emit contactAdded(contactItem.getNameSpace());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800399
Yingdi Yufa4ce792014-02-06 18:09:22 -0800400 }catch(std::runtime_error& e){
Yingdi Yuae8217c2013-11-09 00:03:26 -0800401 emit warning(e.what());
402 _LOG_ERROR("Exception: " << e.what());
403 return;
404 }
405}
406
Yingdi Yu72232692013-11-12 17:50:21 -0800407void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800408ContactManager::removeContact(const Name& contactNameSpace)
Yingdi Yu72232692013-11-12 17:50:21 -0800409{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800410 shared_ptr<ContactItem> contact = getContact(contactNameSpace);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800411 if(!static_cast<bool>(contact))
Yingdi Yu72232692013-11-12 17:50:21 -0800412 return;
413 m_contactStorage->removeContact(contactNameSpace);
414 emit contactRemoved(contact->getPublicKeyName());
415}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800416
Yingdi Yufa4ce792014-02-06 18:09:22 -0800417}//chronos
418
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700419
420#if WAF
421#include "contact-manager.moc"
422#include "contact-manager.cpp.moc"
423#endif