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