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