Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "contact-manager.h" |
| 12 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 13 | #ifndef Q_MOC_RUN |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 14 | #include <ndn.cxx/wrapper/wrapper.h> |
| 15 | #include <ndn.cxx/security/keychain.h> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 16 | #include <ndn.cxx/security/policy/simple-policy-manager.h> |
| 17 | #include <ndn.cxx/security/policy/identity-policy-rule.h> |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 18 | #include <ndn.cxx/helpers/der/der.h> |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 19 | #include <cryptopp/base64.h> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 20 | #include <fstream> |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 21 | #include "logging.h" |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 22 | #endif |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 23 | |
| 24 | using namespace ndn; |
| 25 | using namespace ndn::security; |
| 26 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 27 | INIT_LOGGER("ContactManager"); |
| 28 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 29 | ContactManager::ContactManager(Ptr<ContactStorage> contactStorage, |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 30 | Ptr<DnsStorage> dnsStorage, |
| 31 | QObject* parent) |
| 32 | : QObject(parent) |
| 33 | , m_contactStorage(contactStorage) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 34 | , m_dnsStorage(dnsStorage) |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 35 | { |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 36 | setKeychain(); |
| 37 | |
| 38 | m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain)); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | ContactManager::~ContactManager() |
| 42 | { |
| 43 | } |
| 44 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 45 | void |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 46 | ContactManager::setKeychain() |
| 47 | { |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 48 | Ptr<IdentityManager> identityManager = Ptr<IdentityManager>::Create(); |
| 49 | Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>::Create(); |
| 50 | |
| 51 | Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, NULL)); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 53 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><ENDORSED>", |
| 54 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
| 55 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 56 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 57 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 58 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 59 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 60 | "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 61 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 62 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 63 | "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$", |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 64 | ">", "\\1\\2", "\\1", true))); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 65 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 66 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 67 | "==", "\\1", "\\1\\2", true))); |
| 68 | |
| 69 | policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
| 70 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>", |
| 71 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 73 | const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\ |
| 74 | HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\ |
| 75 | 67biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\ |
| 76 | 5X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\ |
| 77 | WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\ |
| 78 | Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\ |
| 79 | MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\ |
| 80 | nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\ |
| 81 | c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\ |
| 82 | MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\ |
| 83 | b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\ |
| 84 | yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\ |
| 85 | I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\ |
| 86 | /vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\ |
| 87 | 72P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\ |
| 88 | VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\ |
| 89 | iVUF1QIBEQAA"); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 91 | string decoded; |
| 92 | CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()), |
| 93 | TrustAnchor.size(), |
| 94 | true, |
| 95 | new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded))); |
| 96 | Ptr<Blob> blob = Ptr<Blob>(new Blob(decoded.c_str(), decoded.size())); |
| 97 | Ptr<Data> data = Data::decodeFromWire(blob); |
| 98 | Ptr<IdentityCertificate>anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*data)); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 99 | policyManager->addTrustAnchor(anchor); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 100 | |
| 101 | #ifdef _DEBUG |
| 102 | |
| 103 | const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\ |
| 104 | zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\ |
| 105 | qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\ |
| 106 | 6M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\ |
| 107 | DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\ |
| 108 | Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\ |
| 109 | MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\ |
| 110 | qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\ |
| 111 | APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\ |
| 112 | MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\ |
| 113 | MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\ |
| 114 | /PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\ |
| 115 | sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\ |
| 116 | H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\ |
| 117 | gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\ |
| 118 | OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\ |
| 119 | F7Wh5ayeo8NBKDsCAwEAAQAA"); |
| 120 | |
| 121 | string decoded2; |
| 122 | CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()), |
| 123 | FakeAnchor.size(), |
| 124 | true, |
| 125 | new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2))); |
| 126 | Ptr<Blob> blob2 = Ptr<Blob>(new Blob(decoded2.c_str(), decoded2.size())); |
| 127 | Ptr<Data> data2 = Data::decodeFromWire(blob2); |
| 128 | Ptr<IdentityCertificate>anchor2 = Ptr<IdentityCertificate>(new IdentityCertificate(*data2)); |
| 129 | policyManager->addTrustAnchor(anchor2); |
| 130 | |
| 131 | #endif |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 132 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 133 | m_keychain = keychain; |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 134 | } |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 135 | |
| 136 | |
| 137 | void |
| 138 | ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity) |
| 139 | { |
| 140 | Name interestName = identity; |
| 141 | interestName.append("DNS").append("PROFILE"); |
| 142 | |
| 143 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 144 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 145 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, |
| 146 | this, |
| 147 | _1, |
| 148 | identity), |
| 149 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout, |
| 150 | this, |
| 151 | _1, |
| 152 | _2, |
| 153 | identity, |
| 154 | 0), |
| 155 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified, |
| 156 | this, |
| 157 | _1, |
| 158 | identity))); |
| 159 | m_wrapper->sendInterest(interestPtr, closure); |
| 160 | } |
| 161 | |
| 162 | void |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 163 | ContactManager::fetchCollectEndorse(const ndn::Name& identity) |
| 164 | { |
| 165 | Name interestName = identity; |
| 166 | interestName.append("DNS").append("ENDORSED"); |
| 167 | |
| 168 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
| 169 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
| 170 | interestPtr->setInterestLifetime(1); |
| 171 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsCollectEndorseVerified, |
| 172 | this, |
| 173 | _1, |
| 174 | identity), |
| 175 | boost::bind(&ContactManager::onDnsCollectEndorseTimeout, |
| 176 | this, |
| 177 | _1, |
| 178 | _2, |
| 179 | identity, |
| 180 | 0), |
| 181 | boost::bind(&ContactManager::onDnsCollectEndorseUnverified, |
| 182 | this, |
| 183 | _1, |
| 184 | identity))); |
| 185 | m_wrapper->sendInterest(interestPtr, closure); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | ContactManager::fetchKey(const ndn::Name& certName) |
| 190 | { |
| 191 | Name interestName = certName; |
| 192 | |
| 193 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
| 194 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame^] | 195 | interestPtr->setInterestLifetime(1); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 196 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onKeyVerified, |
| 197 | this, |
| 198 | _1, |
| 199 | certName), |
| 200 | boost::bind(&ContactManager::onKeyTimeout, |
| 201 | this, |
| 202 | _1, |
| 203 | _2, |
| 204 | certName, |
| 205 | 0), |
| 206 | boost::bind(&ContactManager::onKeyUnverified, |
| 207 | this, |
| 208 | _1, |
| 209 | certName))); |
| 210 | m_wrapper->sendInterest(interestPtr, closure); |
| 211 | } |
| 212 | |
| 213 | void |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame^] | 214 | ContactManager::fetchIdCertificate(const ndn::Name& certName) |
| 215 | { |
| 216 | Name interestName = certName; |
| 217 | |
| 218 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
| 219 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
| 220 | interestPtr->setInterestLifetime(1); |
| 221 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onIdCertificateVerified, |
| 222 | this, |
| 223 | _1, |
| 224 | certName), |
| 225 | boost::bind(&ContactManager::onIdCertificateTimeout, |
| 226 | this, |
| 227 | _1, |
| 228 | _2, |
| 229 | certName, |
| 230 | 0), |
| 231 | boost::bind(&ContactManager::onIdCertificateUnverified, |
| 232 | this, |
| 233 | _1, |
| 234 | certName))); |
| 235 | m_wrapper->sendInterest(interestPtr, closure); |
| 236 | } |
| 237 | |
| 238 | void |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 239 | ContactManager::onDnsCollectEndorseVerified(Ptr<Data> data, const Name& identity) |
| 240 | { emit collectEndorseFetched (*data); } |
| 241 | |
| 242 | void |
| 243 | ContactManager::onDnsCollectEndorseTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 244 | { emit collectEndorseFetchFailed (identity); } |
| 245 | |
| 246 | void |
| 247 | ContactManager::onDnsCollectEndorseUnverified(Ptr<Data> data, const Name& identity) |
| 248 | { emit collectEndorseFetchFailed (identity); } |
| 249 | |
| 250 | void |
| 251 | ContactManager::onKeyVerified(Ptr<Data> data, const Name& identity) |
| 252 | { |
| 253 | IdentityCertificate identityCertificate(*data); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 254 | |
| 255 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(Profile(identityCertificate))); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 256 | |
| 257 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 258 | Name certificateName = identityManager->getDefaultCertificateName (); |
| 259 | identityManager->signByCertificate(*profileData, certificateName); |
| 260 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 261 | Ptr<EndorseCertificate> endorseCertificate = NULL; |
| 262 | try{ |
| 263 | endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData)); |
| 264 | }catch(exception& e){ |
| 265 | _LOG_ERROR("Exception: " << e.what()); |
| 266 | return; |
| 267 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 268 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 269 | identityManager->signByCertificate(*endorseCertificate, certificateName); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 270 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 271 | emit contactKeyFetched (*endorseCertificate); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void |
| 275 | ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity) |
| 276 | { emit contactKeyFetchFailed (identity); } |
| 277 | |
| 278 | void |
| 279 | ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 280 | { emit contactKeyFetchFailed(identity); } |
| 281 | |
| 282 | void |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame^] | 283 | ContactManager::onIdCertificateVerified(Ptr<Data> data, const Name& identity) |
| 284 | { |
| 285 | IdentityCertificate identityCertificate(*data); |
| 286 | emit contactCertificateFetched(identityCertificate); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | ContactManager::onIdCertificateUnverified(Ptr<Data> data, const Name& identity) |
| 291 | { emit contactCertificateFetchFailed (identity); } |
| 292 | |
| 293 | void |
| 294 | ContactManager::onIdCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 295 | { emit contactCertificateFetchFailed (identity); } |
| 296 | |
| 297 | void |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 298 | ContactManager::updateProfileData(const Name& identity) |
| 299 | { |
| 300 | // Get current profile; |
| 301 | Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity); |
| 302 | if(NULL == newProfile) |
| 303 | return; |
| 304 | Ptr<Blob> newProfileBlob = newProfile->toDerBlob(); |
| 305 | |
| 306 | // Check if profile exists |
| 307 | Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity); |
| 308 | if(NULL != profileDataBlob) |
| 309 | { |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 310 | |
| 311 | Ptr<EndorseCertificate> oldEndorseCertificate = NULL; |
| 312 | try{ |
| 313 | Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob); |
| 314 | oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
| 315 | }catch(exception& e){ |
| 316 | _LOG_ERROR("Exception: " << e.what()); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | const Blob& oldProfileBlob = oldEndorseCertificate->getProfileData()->content(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 321 | |
| 322 | if(oldProfileBlob == *newProfileBlob) |
| 323 | return; |
| 324 | |
| 325 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 326 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 327 | if(NULL == newEndorseCertificate) |
| 328 | return; |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 329 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 330 | m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity); |
| 331 | |
| 332 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 337 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 338 | if(NULL == newEndorseCertificate) |
| 339 | return; |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 340 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 341 | m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity); |
| 342 | |
| 343 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 344 | } |
| 345 | } |
| 346 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 347 | void |
| 348 | ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity) |
| 349 | { |
| 350 | Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity); |
| 351 | Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity); |
| 352 | if(NULL != oldEndorseCertificateBlob) |
| 353 | { |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 354 | Ptr<EndorseCertificate> oldEndorseCertificate = NULL; |
| 355 | try{ |
| 356 | Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob); |
| 357 | oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
| 358 | }catch(exception& e){ |
| 359 | _LOG_ERROR("Exception: " << e.what()); |
| 360 | return; |
| 361 | } |
| 362 | const Blob& oldEndorseContent = oldEndorseCertificate->content(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 363 | const Blob& newEndorseContent = newEndorseCertificate->content(); |
| 364 | if(oldEndorseContent == newEndorseContent) |
| 365 | return; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | if(NULL == newEndorseCertificate) |
| 370 | return; |
| 371 | } |
| 372 | m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity); |
| 373 | publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity); |
| 374 | } |
| 375 | |
| 376 | Ptr<EndorseCertificate> |
| 377 | ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity) |
| 378 | { |
| 379 | Ptr<ContactItem> contact = getContact(identity); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 380 | if(contact == NULL) |
| 381 | return NULL; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 382 | |
| 383 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 384 | Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity); |
| 385 | Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity); |
| 386 | |
| 387 | vector<string> endorseList = m_contactStorage->getEndorseList(identity); |
| 388 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 389 | Ptr<EndorseCertificate> cert = NULL; |
| 390 | try{ |
| 391 | cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList)); |
| 392 | }catch(exception& e){ |
| 393 | _LOG_ERROR("Exception: " << e.what()); |
| 394 | return NULL; |
| 395 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 396 | identityManager->signByCertificate(*cert, signerCertName); |
| 397 | |
| 398 | return cert; |
| 399 | } |
| 400 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 401 | vector<Ptr<ContactItem> > |
| 402 | ContactManager::getContactItemList() |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 403 | { return m_contactStorage->getAllContacts(); } |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 404 | |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 405 | Ptr<ContactItem> |
| 406 | ContactManager::getContact(const ndn::Name& contactNamespace) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 407 | { return m_contactStorage->getContact(contactNamespace); } |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 408 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 409 | Ptr<EndorseCertificate> |
| 410 | ContactManager::getSignedSelfEndorseCertificate(const Name& identity, |
| 411 | const Profile& profile) |
| 412 | { |
| 413 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 414 | Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity); |
| 415 | if(0 == certificateName.size()) |
| 416 | return NULL; |
| 417 | |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 418 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile)); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 419 | identityManager->signByCertificate(*profileData, certificateName); |
| 420 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 421 | Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 422 | if(NULL == signingCert) |
| 423 | return NULL; |
| 424 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 425 | Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true); |
| 426 | |
| 427 | Ptr<security::IdentityCertificate> kskCert; |
| 428 | if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-")) |
| 429 | { |
| 430 | Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature()); |
| 431 | // HACK! KSK certificate should be retrieved from network. |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 432 | Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName()); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 433 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 434 | Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 435 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 436 | kskCert = identityManager->getCertificate(kskCertName); |
| 437 | |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | kskCert = signingCert; |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 442 | } |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 443 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 444 | if(NULL == kskCert) |
| 445 | return NULL; |
| 446 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 447 | vector<string> endorseList; |
| 448 | Profile::const_iterator it = profile.begin(); |
| 449 | for(; it != profile.end(); it++) |
| 450 | endorseList.push_back(it->first); |
| 451 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 452 | Ptr<EndorseCertificate> selfEndorseCertificate = NULL; |
| 453 | try{ |
| 454 | selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert, |
| 455 | profileData, |
| 456 | endorseList)); |
| 457 | }catch(exception& e){ |
| 458 | _LOG_ERROR("Exception: " << e.what()); |
| 459 | return NULL; |
| 460 | } |
| 461 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 462 | identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 463 | |
| 464 | return selfEndorseCertificate; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | void |
| 469 | ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity) |
| 470 | { |
Yingdi Yu | d95c564 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 471 | Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size())); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 472 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 473 | Ptr<Data> plainData = NULL; |
| 474 | Ptr<EndorseCertificate> selfEndorseCertificate = NULL; |
| 475 | try{ |
| 476 | plainData = Data::decodeFromWire(dataContentBlob); |
| 477 | selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
| 478 | }catch(exception& e){ |
| 479 | _LOG_ERROR("Exception: " << e.what()); |
| 480 | return; |
| 481 | } |
Yingdi Yu | d95c564 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 482 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 483 | const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo(); |
Yingdi Yu | d95c564 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 484 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 485 | if(security::PolicyManager::verifySignature(*plainData, ksk)) |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 486 | emit contactFetched (*selfEndorseCertificate); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 487 | else |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 488 | emit contactFetchFailed (identity); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void |
| 492 | ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity) |
| 493 | { emit contactFetchFailed (identity); } |
| 494 | |
| 495 | void |
| 496 | ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 497 | { emit contactFetchFailed(identity); } |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 498 | |
| 499 | void |
| 500 | ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate) |
| 501 | { |
| 502 | Ptr<Data> data = Ptr<Data>::Create(); |
| 503 | |
| 504 | Name keyName = selfEndorseCertificate->getPublicKeyName(); |
| 505 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 506 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 507 | |
| 508 | Name dnsName = identity; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 509 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | d95c564 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 510 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 511 | data->setName(dnsName); |
| 512 | Ptr<Blob> blob = selfEndorseCertificate->encodeToWire(); |
Yingdi Yu | d95c564 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 513 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 514 | Content content(blob->buf(), blob->size()); |
| 515 | data->setContent(content); |
| 516 | |
| 517 | m_keychain->signByIdentity(*data, identity); |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 518 | |
| 519 | m_dnsStorage->updateDnsSelfProfileData(*data, identity); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 520 | |
| 521 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 522 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 523 | m_wrapper->putToNdnd(*dnsBlob); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 526 | void |
| 527 | ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity) |
| 528 | { |
| 529 | Ptr<Data> data = Ptr<Data>::Create(); |
| 530 | |
| 531 | Name keyName = endorseCertificate->getPublicKeyName(); |
| 532 | Name endorsee = keyName.getSubName(0, keyName.size()-1); |
| 533 | |
| 534 | |
| 535 | Name dnsName = signerIdentity; |
| 536 | dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(); |
| 537 | |
| 538 | data->setName(dnsName); |
| 539 | Ptr<Blob> blob = endorseCertificate->encodeToWire(); |
| 540 | |
| 541 | Content content(blob->buf(), blob->size()); |
| 542 | data->setContent(content); |
| 543 | |
| 544 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity); |
| 545 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 546 | |
| 547 | m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee); |
| 548 | |
| 549 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 550 | |
| 551 | m_wrapper->putToNdnd(*dnsBlob); |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | ContactManager::publishEndorsedDataInDns(const Name& identity) |
| 556 | { |
| 557 | Ptr<Data> data = Ptr<Data>::Create(); |
| 558 | |
| 559 | Name dnsName = identity; |
| 560 | dnsName.append("DNS").append("ENDORSED").appendVersion(); |
| 561 | data->setName(dnsName); |
| 562 | |
| 563 | Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity); |
| 564 | |
| 565 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 566 | |
| 567 | vector<Blob>::const_iterator it = collectEndorseList->begin(); |
| 568 | for(; it != collectEndorseList->end(); it++) |
| 569 | { |
| 570 | Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it)); |
| 571 | root->addChild(entry); |
| 572 | } |
| 573 | |
| 574 | blob_stream blobStream; |
| 575 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 576 | root->encode(start); |
| 577 | |
| 578 | Content content(blobStream.buf()->buf(), blobStream.buf()->size()); |
| 579 | data->setContent(content); |
| 580 | |
| 581 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity); |
| 582 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 583 | |
| 584 | m_dnsStorage->updateDnsOthersEndorse(*data, identity); |
| 585 | |
| 586 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 587 | |
| 588 | m_wrapper->putToNdnd(*dnsBlob); |
| 589 | } |
| 590 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame^] | 591 | void |
| 592 | ContactManager::addContact(const IdentityCertificate& identityCertificate, const Profile& profile) |
| 593 | { |
| 594 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile)); |
| 595 | |
| 596 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 597 | Name certificateName = identityManager->getDefaultCertificateNameByIdentity (m_defaultIdentity); |
| 598 | identityManager->signByCertificate(*profileData, certificateName); |
| 599 | |
| 600 | Ptr<EndorseCertificate> endorseCertificate = NULL; |
| 601 | try{ |
| 602 | endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData)); |
| 603 | }catch(exception& e){ |
| 604 | _LOG_ERROR("Exception: " << e.what()); |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | identityManager->signByCertificate(*endorseCertificate, certificateName); |
| 609 | |
| 610 | ContactItem contactItem(*endorseCertificate); |
| 611 | |
| 612 | try{ |
| 613 | m_contactStorage->addContact(contactItem); |
| 614 | }catch(exception& e){ |
| 615 | emit warning(e.what()); |
| 616 | _LOG_ERROR("Exception: " << e.what()); |
| 617 | return; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 622 | |
| 623 | #if WAF |
| 624 | #include "contact-manager.moc" |
| 625 | #include "contact-manager.cpp.moc" |
| 626 | #endif |