Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 13 | #ifndef Q_MOC_RUN |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 14 | #include <ndn-cpp/face.hpp> |
| 15 | #include <ndn-cpp/sha256-with-rsa-signature.hpp> |
| 16 | #include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp> |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 17 | #include <cryptopp/base64.h> |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 18 | #include <ndn-cpp-et/policy-manager/identity-policy-rule.hpp> |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 19 | #include <fstream> |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 20 | #include "endorse-collection.pb.h" |
| 21 | #include "null-ptrs.h" |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 22 | #include "logging.h" |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 23 | #endif |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 24 | |
| 25 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 26 | using namespace ndn::ptr_lib; |
| 27 | using namespace std; |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 29 | INIT_LOGGER("ContactManager"); |
| 30 | |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 31 | ContactManager::ContactManager(shared_ptr<IdentityManager> identityManager, |
| 32 | shared_ptr<Face> face, |
| 33 | shared_ptr<Transport> transport, |
| 34 | QObject* parent) |
| 35 | : QObject(parent), |
| 36 | m_face(face), |
| 37 | m_transport(transport) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 38 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 39 | m_identityManager = identityManager; |
| 40 | m_contactStorage = make_shared<ContactStorage>(); |
| 41 | m_dnsStorage = make_shared<DnsStorage>(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 42 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 43 | initializeSecurity(); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ContactManager::~ContactManager() |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 47 | {} |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 49 | // void |
| 50 | // ContactManager::connectToDaemon() |
| 51 | // { |
| 52 | // //Hack! transport does not connect to daemon unless an interest is expressed. |
| 53 | // Name name("/ndn"); |
| 54 | // shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name); |
| 55 | // m_face->expressInterest(*interest, |
| 56 | // bind(&ContactManager::onConnectionData, this, _1, _2), |
| 57 | // bind(&ContactManager::onConnectionDataTimeout, this, _1)); |
| 58 | // } |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 59 | |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 60 | // void |
| 61 | // ContactManager::onConnectionData(const shared_ptr<const ndn::Interest>& interest, |
| 62 | // const shared_ptr<Data>& data) |
| 63 | // { |
| 64 | // _LOG_DEBUG("onConnectionData"); |
| 65 | // } |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 66 | |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 67 | // void |
| 68 | // ContactManager::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest) |
| 69 | // { |
| 70 | // _LOG_DEBUG("onConnectionDataTimeout"); |
| 71 | // } |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 73 | void |
| 74 | ContactManager::initializeSecurity() |
| 75 | { |
| 76 | m_policyManager = make_shared<SimplePolicyManager>(); |
| 77 | |
| 78 | m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><ENDORSED>", |
| 79 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
| 80 | "==", "\\1", "\\1\\2", true)); |
| 81 | m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><PROFILE>", |
| 82 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
| 83 | "==", "\\1", "\\1\\2", true)); |
| 84 | m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
| 85 | "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", |
| 86 | "==", "\\1", "\\1\\2", true)); |
| 87 | m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
| 88 | "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$", |
| 89 | ">", "\\1\\2", "\\1", true)); |
| 90 | m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
| 91 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 92 | "==", "\\1", "\\1\\2", true)); |
| 93 | m_policyManager->addVerificationPolicyRule(make_shared<IdentityPolicyRule>("^(<>*)$", |
| 94 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 95 | ">", "\\1", "\\1\\2", true)); |
Yingdi Yu | 8fb16a4 | 2013-11-10 18:35:09 -0800 | [diff] [blame] | 96 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 98 | m_policyManager->addSigningPolicyRule(make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><PROFILE>", |
| 99 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>", |
| 100 | "==", "\\1", "\\1\\2", true)); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 101 | |
Yingdi Yu | 8fb16a4 | 2013-11-10 18:35:09 -0800 | [diff] [blame] | 102 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 103 | const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\ |
| 104 | HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\ |
| 105 | 67biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\ |
| 106 | 5X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\ |
| 107 | WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\ |
| 108 | Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\ |
| 109 | MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\ |
| 110 | nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\ |
| 111 | c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\ |
| 112 | MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\ |
| 113 | b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\ |
| 114 | yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\ |
| 115 | I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\ |
| 116 | /vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\ |
| 117 | 72P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\ |
| 118 | VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\ |
| 119 | iVUF1QIBEQAA"); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 120 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 121 | string decoded; |
| 122 | CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()), |
| 123 | TrustAnchor.size(), |
| 124 | true, |
| 125 | new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded))); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 126 | Data data; |
| 127 | data.wireDecode((const uint8_t*)decoded.c_str(), decoded.size()); |
| 128 | shared_ptr<IdentityCertificate> anchor = make_shared<IdentityCertificate>(data); |
| 129 | m_policyManager->addTrustAnchor(anchor); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 130 | |
| 131 | #ifdef _DEBUG |
| 132 | |
| 133 | const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\ |
| 134 | zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\ |
| 135 | qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\ |
| 136 | 6M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\ |
| 137 | DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\ |
| 138 | Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\ |
| 139 | MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\ |
| 140 | qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\ |
| 141 | APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\ |
| 142 | MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\ |
| 143 | MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\ |
| 144 | /PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\ |
| 145 | sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\ |
| 146 | H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\ |
| 147 | gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\ |
| 148 | OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\ |
| 149 | F7Wh5ayeo8NBKDsCAwEAAQAA"); |
| 150 | |
| 151 | string decoded2; |
| 152 | CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()), |
| 153 | FakeAnchor.size(), |
| 154 | true, |
| 155 | new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2))); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 156 | Data data2; |
| 157 | data2.wireDecode((const uint8_t*)decoded2.c_str(), decoded2.size()); |
| 158 | shared_ptr<IdentityCertificate>anchor2 = make_shared<IdentityCertificate>(data2); |
| 159 | m_policyManager->addTrustAnchor(anchor2); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 160 | |
| 161 | #endif |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 162 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 163 | |
| 164 | |
| 165 | void |
| 166 | ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity) |
| 167 | { |
| 168 | Name interestName = identity; |
| 169 | interestName.append("DNS").append("PROFILE"); |
| 170 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 171 | Interest interest(interestName); |
| 172 | interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT); |
| 173 | |
| 174 | OnVerified onVerified = boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, this, _1, identity); |
| 175 | OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerifyFailed, this, _1, identity); |
| 176 | TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeoutNotify, this, identity); |
| 177 | |
| 178 | sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 182 | ContactManager::onDnsSelfEndorseCertificateTimeoutNotify(const Name& identity) |
| 183 | { emit contactFetchFailed(identity); } |
| 184 | |
| 185 | void |
| 186 | ContactManager::onDnsSelfEndorseCertificateVerified(const shared_ptr<Data>& data, |
| 187 | const Name& identity) |
| 188 | { |
| 189 | try{ |
| 190 | Data plainData; |
| 191 | plainData.wireDecode(data->getContent().buf(), data->getContent().size()); |
| 192 | EndorseCertificate selfEndorseCertificate(plainData); |
| 193 | if(Sha256WithRsaHandler::verifySignature(plainData, selfEndorseCertificate.getPublicKeyInfo())) |
| 194 | emit contactFetched (selfEndorseCertificate); |
| 195 | else |
| 196 | emit contactFetchFailed (identity); |
| 197 | }catch(std::exception& e){ |
| 198 | _LOG_ERROR("Exception: " << e.what()); |
| 199 | emit contactFetchFailed (identity); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | ContactManager::onDnsSelfEndorseCertificateVerifyFailed(const shared_ptr<Data>& data, |
| 205 | const Name& identity) |
| 206 | { emit contactFetchFailed (identity); } |
| 207 | |
| 208 | void |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 209 | ContactManager::fetchCollectEndorse(const ndn::Name& identity) |
| 210 | { |
| 211 | Name interestName = identity; |
| 212 | interestName.append("DNS").append("ENDORSED"); |
| 213 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 214 | Interest interest(interestName); |
| 215 | interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT); |
| 216 | interest.setInterestLifetimeMilliseconds(1000); |
| 217 | |
| 218 | OnVerified onVerified = boost::bind(&ContactManager::onDnsCollectEndorseVerified, this, _1, identity); |
| 219 | OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onDnsCollectEndorseVerifyFailed, this, _1, identity); |
| 220 | TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, identity); |
| 221 | |
| 222 | sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 226 | ContactManager::onDnsCollectEndorseTimeoutNotify(const Name& identity) |
| 227 | { |
| 228 | emit collectEndorseFetchFailed (identity); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | ContactManager::onDnsCollectEndorseVerified(const shared_ptr<Data>& data, const Name& identity) |
| 233 | { emit collectEndorseFetched (*data); } |
| 234 | |
| 235 | void |
| 236 | ContactManager::onDnsCollectEndorseVerifyFailed(const shared_ptr<Data>& data, const Name& identity) |
| 237 | { emit collectEndorseFetchFailed (identity); } |
| 238 | |
| 239 | |
| 240 | void |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 241 | ContactManager::fetchKey(const ndn::Name& certName) |
| 242 | { |
| 243 | Name interestName = certName; |
| 244 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 245 | Interest interest(interestName); |
| 246 | interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT); |
| 247 | interest.setInterestLifetimeMilliseconds(1000); |
| 248 | |
| 249 | OnVerified onVerified = boost::bind(&ContactManager::onKeyVerified, this, _1, certName); |
| 250 | OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onKeyVerifyFailed, this, _1, certName); |
| 251 | TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onKeyTimeoutNotify, this, certName); |
| 252 | |
| 253 | sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void |
| 258 | ContactManager::onKeyVerified(const shared_ptr<Data>& data, const Name& identity) |
| 259 | { |
| 260 | IdentityCertificate identityCertificate(*data); |
| 261 | |
| 262 | Profile profile(identityCertificate); |
| 263 | ProfileData profileData(profile); |
| 264 | |
| 265 | Name certificateName = m_identityManager->getDefaultCertificateName(); |
| 266 | m_identityManager->signByCertificate(profileData, certificateName); |
| 267 | |
| 268 | try{ |
| 269 | EndorseCertificate endorseCertificate(identityCertificate, profileData); |
| 270 | m_identityManager->signByCertificate(endorseCertificate, certificateName); |
| 271 | emit contactKeyFetched (endorseCertificate); |
| 272 | }catch(std::exception& e){ |
| 273 | _LOG_ERROR("Exception: " << e.what()); |
| 274 | return; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | ContactManager::onKeyVerifyFailed(const shared_ptr<Data>& data, const Name& identity) |
| 280 | { |
| 281 | _LOG_DEBUG("Key cannot be verified!"); |
| 282 | emit contactKeyFetchFailed (identity); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | ContactManager::onKeyTimeoutNotify(const Name& identity) |
| 287 | { |
| 288 | _LOG_DEBUG("Key timeout!"); |
| 289 | emit contactKeyFetchFailed(identity); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 293 | ContactManager::fetchIdCertificate(const Name& certName) |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 294 | { |
| 295 | Name interestName = certName; |
| 296 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 297 | Interest interest(interestName); |
| 298 | interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT); |
| 299 | interest.setInterestLifetimeMilliseconds(1000); |
| 300 | |
| 301 | OnVerified onVerified = boost::bind(&ContactManager::onIdCertificateVerified, this, _1, certName); |
| 302 | OnVerifyFailed onVerifyFailed = boost::bind(&ContactManager::onIdCertificateVerifyFailed, this, _1, certName); |
| 303 | TimeoutNotify timeoutNotify = boost::bind(&ContactManager::onIdCertificateTimeoutNotify, this, certName); |
| 304 | |
| 305 | sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 309 | ContactManager::onIdCertificateTimeoutNotify(const Name& identity) |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 310 | { |
| 311 | emit contactCertificateFetchFailed (identity); |
| 312 | } |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 313 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 314 | |
| 315 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 316 | ContactManager::onIdCertificateVerified(const shared_ptr<Data>& data, const Name& identity) |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 317 | { |
| 318 | IdentityCertificate identityCertificate(*data); |
| 319 | emit contactCertificateFetched(identityCertificate); |
| 320 | } |
| 321 | |
| 322 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 323 | ContactManager::onIdCertificateVerifyFailed(const shared_ptr<Data>& data, const Name& identity) |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 324 | { |
| 325 | emit contactCertificateFetchFailed (identity); |
| 326 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 327 | |
| 328 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 329 | ContactManager::onTargetData(const shared_ptr<const ndn::Interest>& interest, |
| 330 | const shared_ptr<Data>& data, |
| 331 | int stepCount, |
| 332 | const OnVerified& onVerified, |
| 333 | const OnVerifyFailed& onVerifyFailed, |
| 334 | const TimeoutNotify& timeoutNotify) |
| 335 | { |
| 336 | shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed); |
| 337 | |
| 338 | if (nextStep) |
| 339 | m_face->expressInterest |
| 340 | (*nextStep->interest_, |
| 341 | bind(&ContactManager::onCertData, this, _1, _2, nextStep), |
| 342 | bind(&ContactManager::onCertTimeout, this, _1, onVerifyFailed, data, nextStep)); |
| 343 | |
| 344 | } |
| 345 | |
| 346 | void |
| 347 | ContactManager::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 348 | int retry, |
| 349 | int stepCount, |
| 350 | const OnVerified& onVerified, |
| 351 | const OnVerifyFailed& onVerifyFailed, |
| 352 | const TimeoutNotify& timeoutNotify) |
| 353 | { |
| 354 | if(retry > 0) |
| 355 | sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount); |
| 356 | else |
| 357 | { |
| 358 | _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!"); |
| 359 | timeoutNotify(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | ContactManager::onCertData(const shared_ptr<const ndn::Interest>& interest, |
| 365 | const shared_ptr<Data>& cert, |
| 366 | shared_ptr<ValidationRequest> previousStep) |
| 367 | { |
| 368 | shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert, |
| 369 | previousStep->stepCount_, |
| 370 | previousStep->onVerified_, |
| 371 | previousStep->onVerifyFailed_); |
| 372 | |
| 373 | if (nextStep) |
| 374 | m_face->expressInterest |
| 375 | (*nextStep->interest_, |
| 376 | bind(&ContactManager::onCertData, this, _1, _2, nextStep), |
| 377 | bind(&ContactManager::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep)); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | ContactManager::onCertTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 382 | const OnVerifyFailed& onVerifyFailed, |
| 383 | const shared_ptr<Data>& data, |
| 384 | shared_ptr<ValidationRequest> nextStep) |
| 385 | { |
| 386 | if(nextStep->retry_ > 0) |
| 387 | m_face->expressInterest(*interest, |
| 388 | bind(&ContactManager::onCertData, |
| 389 | this, |
| 390 | _1, |
| 391 | _2, |
| 392 | nextStep), |
| 393 | bind(&ContactManager::onCertTimeout, |
| 394 | this, |
| 395 | _1, |
| 396 | onVerifyFailed, |
| 397 | data, |
| 398 | nextStep)); |
| 399 | else |
| 400 | onVerifyFailed(data); |
| 401 | } |
| 402 | |
| 403 | void |
| 404 | ContactManager::sendInterest(const Interest& interest, |
| 405 | const OnVerified& onVerified, |
| 406 | const OnVerifyFailed& onVerifyFailed, |
| 407 | const TimeoutNotify& timeoutNotify, |
| 408 | int retry /* = 1 */, |
| 409 | int stepCount /* = 0 */) |
| 410 | { |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 411 | uint64_t id = m_face->expressInterest(interest, |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 412 | boost::bind(&ContactManager::onTargetData, |
| 413 | this, |
| 414 | _1, |
| 415 | _2, |
| 416 | stepCount, |
| 417 | onVerified, |
| 418 | onVerifyFailed, |
| 419 | timeoutNotify), |
| 420 | boost::bind(&ContactManager::onTargetTimeout, |
| 421 | this, |
| 422 | _1, |
| 423 | retry, |
| 424 | stepCount, |
| 425 | onVerified, |
| 426 | onVerifyFailed, |
| 427 | timeoutNotify)); |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame^] | 428 | |
| 429 | _LOG_DEBUG("id: " << id << " entry id: " << m_face->getNode().getEntryIndexForExpressedInterest(interest.getName())); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 430 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 431 | |
| 432 | void |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 433 | ContactManager::updateProfileData(const Name& identity) |
| 434 | { |
| 435 | // Get current profile; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 436 | shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity); |
| 437 | if(CHRONOCHAT_NULL_PROFILE_PTR == newProfile) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 438 | return; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 439 | |
| 440 | shared_ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 441 | |
| 442 | if(CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR == newEndorseCertificate) |
| 443 | return; |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 444 | |
| 445 | // Check if profile exists |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 446 | Blob profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity); |
| 447 | if(CHRONOCHAT_NULL_BLOB != profileDataBlob) |
| 448 | m_contactStorage->updateSelfEndorseCertificate(*newEndorseCertificate, identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 449 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 450 | m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate, identity); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 451 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 452 | publishSelfEndorseCertificateInDNS(*newEndorseCertificate); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 455 | void |
| 456 | ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity) |
| 457 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 458 | Blob oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity); |
| 459 | shared_ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity); |
| 460 | |
| 461 | if(CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR == newEndorseCertificate) |
| 462 | return; |
| 463 | |
| 464 | if(CHRONOCHAT_NULL_BLOB != oldEndorseCertificateBlob) |
| 465 | m_contactStorage->updateEndorseCertificate(*newEndorseCertificate, identity); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 466 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 467 | m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identity); |
| 468 | |
| 469 | publishEndorseCertificateInDNS(*newEndorseCertificate, signerIdentity); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 472 | shared_ptr<EndorseCertificate> |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 473 | ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity) |
| 474 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 475 | shared_ptr<ContactItem> contact = getContact(identity); |
| 476 | if(contact == CHRONOCHAT_NULL_CONTACTITEM_PTR) |
| 477 | return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 478 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 479 | Name signerKeyName = m_identityManager->getDefaultKeyNameForIdentity(signerIdentity); |
| 480 | Name signerCertName = m_identityManager->getDefaultCertificateNameForIdentity(signerIdentity); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 481 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 482 | vector<string> endorseList; |
| 483 | m_contactStorage->getEndorseList(identity, endorseList); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 484 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 485 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 486 | try{ |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 487 | shared_ptr<EndorseCertificate> cert = make_shared<EndorseCertificate>(contact->getSelfEndorseCertificate(), signerKeyName, endorseList); |
| 488 | m_identityManager->signByCertificate(*cert, signerCertName); |
| 489 | return cert; |
| 490 | }catch(std::exception& e){ |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 491 | _LOG_ERROR("Exception: " << e.what()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 492 | return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR; |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 493 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 496 | void |
| 497 | ContactManager::getContactItemList(vector<shared_ptr<ContactItem> >& contacts) |
| 498 | { return m_contactStorage->getAllContacts(contacts); } |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 499 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 500 | shared_ptr<ContactItem> |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 501 | ContactManager::getContact(const ndn::Name& contactNamespace) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 502 | { return m_contactStorage->getContact(contactNamespace); } |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 503 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 504 | shared_ptr<EndorseCertificate> |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 505 | ContactManager::getSignedSelfEndorseCertificate(const Name& identity, |
| 506 | const Profile& profile) |
| 507 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 508 | Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 509 | if(0 == certificateName.size()) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 510 | return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR; |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 511 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 512 | ProfileData profileData(profile); |
| 513 | m_identityManager->signByCertificate(profileData, certificateName); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 514 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 515 | shared_ptr<IdentityCertificate> signingCert = m_identityManager->getCertificate(certificateName); |
| 516 | if(CHRONOCHAT_NULL_IDENTITYCERTIFICATE_PTR == signingCert) |
| 517 | return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR; |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 518 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 519 | Name signingKeyName = IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName()); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 520 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 521 | shared_ptr<IdentityCertificate> kskCert; |
| 522 | if(signingKeyName.get(-1).toEscapedString().substr(0,4) == string("dsk-")) |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 523 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 524 | const Sha256WithRsaSignature* dskCertSig = dynamic_cast<const Sha256WithRsaSignature*>(signingCert->getSignature()); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 525 | // HACK! KSK certificate should be retrieved from network. |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 526 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 527 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 528 | // TODO: check null existing cases. |
| 529 | Name kskCertName = m_identityManager->getDefaultCertificateNameForIdentity(keyName.getPrefix(-1)); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 530 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 531 | kskCert = m_identityManager->getCertificate(kskCertName); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 532 | } |
| 533 | else |
| 534 | { |
| 535 | kskCert = signingCert; |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 536 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 537 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 538 | if(CHRONOCHAT_NULL_IDENTITYCERTIFICATE_PTR == kskCert) |
| 539 | return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR; |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 540 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 541 | vector<string> endorseList; |
| 542 | Profile::const_iterator it = profile.begin(); |
| 543 | for(; it != profile.end(); it++) |
| 544 | endorseList.push_back(it->first); |
| 545 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 546 | try{ |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 547 | shared_ptr<EndorseCertificate> selfEndorseCertificate = make_shared<EndorseCertificate>(*kskCert, profileData, endorseList); |
| 548 | m_identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 549 | |
| 550 | return selfEndorseCertificate; |
| 551 | }catch(std::exception& e){ |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 552 | _LOG_ERROR("Exception: " << e.what()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 553 | return CHRONOCHAT_NULL_ENDORSECERTIFICATE_PTR; |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 554 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 559 | ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 560 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 561 | Data data; |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 562 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 563 | Name keyName = selfEndorseCertificate.getPublicKeyName(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 564 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 565 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 566 | time_t nowSeconds = time(NULL); |
| 567 | struct tm current = *gmtime(&nowSeconds); |
| 568 | MillisecondsSince1970 version = timegm(¤t) * 1000.0; |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 569 | |
| 570 | Name dnsName = identity; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 571 | dnsName.append("DNS").append("PROFILE").appendVersion(version); |
| 572 | data.setName(dnsName); |
| 573 | |
| 574 | data.setContent(selfEndorseCertificate.wireEncode()); |
| 575 | |
| 576 | Name signCertName = m_identityManager->getDefaultCertificateNameForIdentity(identity); |
| 577 | m_identityManager->signByCertificate(data, signCertName); |
| 578 | |
| 579 | m_dnsStorage->updateDnsSelfProfileData(data, identity); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 580 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 581 | m_transport->send(*data.wireEncode()); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 584 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 585 | ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate, const Name& signerIdentity) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 586 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 587 | Data data; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 588 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 589 | Name keyName = endorseCertificate.getPublicKeyName(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 590 | Name endorsee = keyName.getSubName(0, keyName.size()-1); |
| 591 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 592 | time_t nowSeconds = time(NULL); |
| 593 | struct tm current = *gmtime(&nowSeconds); |
| 594 | MillisecondsSince1970 version = timegm(¤t) * 1000.0; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 595 | |
| 596 | Name dnsName = signerIdentity; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 597 | dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(version); |
| 598 | data.setName(dnsName); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 599 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 600 | data.setContent(endorseCertificate.wireEncode()); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 601 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 602 | Name signCertName = m_identityManager->getDefaultCertificateNameForIdentity(signerIdentity); |
| 603 | m_identityManager->signByCertificate(data, signCertName); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 604 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 605 | m_dnsStorage->updateDnsEndorseOthers(data, signerIdentity, endorsee); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 606 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 607 | m_transport->send(*data.wireEncode()); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void |
| 611 | ContactManager::publishEndorsedDataInDns(const Name& identity) |
| 612 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 613 | Data data; |
| 614 | |
| 615 | time_t nowSeconds = time(NULL); |
| 616 | struct tm current = *gmtime(&nowSeconds); |
| 617 | MillisecondsSince1970 version = timegm(¤t) * 1000.0; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 618 | |
| 619 | Name dnsName = identity; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 620 | dnsName.append("DNS").append("ENDORSED").appendVersion(version); |
| 621 | data.setName(dnsName); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 622 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 623 | vector<Blob> collectEndorseList; |
| 624 | m_contactStorage->getCollectEndorseList(identity, collectEndorseList); |
| 625 | |
| 626 | Chronos::EndorseCollection endorseCollection; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 627 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 628 | vector<Blob>::const_iterator it = collectEndorseList.begin(); |
| 629 | for(; it != collectEndorseList.end(); it++) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 630 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 631 | string entryStr((const char*)it->buf(), it->size()); |
| 632 | endorseCollection.add_endorsement()->set_blob(entryStr); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 633 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 634 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 635 | string encoded; |
| 636 | endorseCollection.SerializeToString(&encoded); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 637 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 638 | data.setContent((const uint8_t*)encoded.c_str(), encoded.size()); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 639 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 640 | Name signCertName = m_identityManager->getDefaultCertificateNameForIdentity(identity); |
| 641 | m_identityManager->signByCertificate(data, signCertName); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 642 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 643 | m_dnsStorage->updateDnsOthersEndorse(data, identity); |
| 644 | |
| 645 | m_transport->send(*data.wireEncode()); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 648 | void |
| 649 | ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile) |
| 650 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 651 | ProfileData profileData(profile); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 652 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 653 | Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity (m_defaultIdentity); |
| 654 | m_identityManager->signByCertificate(profileData, certificateName); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 655 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 656 | |
| 657 | try{ |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 658 | EndorseCertificate endorseCertificate(identityCertificate, profileData); |
| 659 | |
| 660 | m_identityManager->signByCertificate(endorseCertificate, certificateName); |
| 661 | |
| 662 | ContactItem contactItem(endorseCertificate); |
| 663 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 664 | m_contactStorage->addContact(contactItem); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 665 | |
Yingdi Yu | 6ea54e4 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 666 | emit contactAdded(contactItem.getNameSpace()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 667 | |
| 668 | }catch(std::exception& e){ |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 669 | emit warning(e.what()); |
| 670 | _LOG_ERROR("Exception: " << e.what()); |
| 671 | return; |
| 672 | } |
| 673 | } |
| 674 | |
Yingdi Yu | 6ea54e4 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 675 | void |
| 676 | ContactManager::removeContact(const ndn::Name& contactNameSpace) |
| 677 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 678 | shared_ptr<ContactItem> contact = getContact(contactNameSpace); |
| 679 | if(contact == CHRONOCHAT_NULL_CONTACTITEM_PTR) |
Yingdi Yu | 6ea54e4 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 680 | return; |
| 681 | m_contactStorage->removeContact(contactNameSpace); |
| 682 | emit contactRemoved(contact->getPublicKeyName()); |
| 683 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 684 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 685 | |
| 686 | #if WAF |
| 687 | #include "contact-manager.moc" |
| 688 | #include "contact-manager.cpp.moc" |
| 689 | #endif |