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