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