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 | |
| 11 | #include "contact-manager.h" |
| 12 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 13 | #ifndef Q_MOC_RUN |
Yingdi Yu | 0b82a4e | 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 | 0b82a4e | 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 | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 18 | #include <ndn.cxx/helpers/der/der.h> |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 19 | #include <cryptopp/base64.h> |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 20 | #include <fstream> |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 21 | #include "logging.h" |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 22 | #endif |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 23 | |
| 24 | using namespace ndn; |
| 25 | using namespace ndn::security; |
| 26 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 27 | INIT_LOGGER("ContactManager"); |
| 28 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 29 | ContactManager::ContactManager(Ptr<ContactStorage> contactStorage, |
Yingdi Yu | 590fa5d | 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 | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 34 | , m_dnsStorage(dnsStorage) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 35 | { |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 36 | setKeychain(); |
| 37 | |
| 38 | m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain)); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | ContactManager::~ContactManager() |
| 42 | { |
| 43 | } |
| 44 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 45 | void |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 46 | ContactManager::setKeychain() |
| 47 | { |
Yingdi Yu | 6a5b9f6 | 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 | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 8dacdf2 | 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 | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 56 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 57 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 58 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 59 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 60 | "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 61 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 62 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 63 | "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 64 | ">", "\\1\\2", "\\1", true))); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 65 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 66 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
Yingdi Yu | aa8d769 | 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 | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 6a5b9f6 | 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 | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 6a5b9f6 | 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 | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 99 | policyManager->addTrustAnchor(anchor); |
Yingdi Yu | 6a5b9f6 | 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 | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 132 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 133 | m_keychain = keychain; |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 134 | } |
Yingdi Yu | aa8d769 | 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 | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 144 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
Yingdi Yu | aa8d769 | 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 | 8dacdf2 | 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); |
| 195 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onKeyVerified, |
| 196 | this, |
| 197 | _1, |
| 198 | certName), |
| 199 | boost::bind(&ContactManager::onKeyTimeout, |
| 200 | this, |
| 201 | _1, |
| 202 | _2, |
| 203 | certName, |
| 204 | 0), |
| 205 | boost::bind(&ContactManager::onKeyUnverified, |
| 206 | this, |
| 207 | _1, |
| 208 | certName))); |
| 209 | m_wrapper->sendInterest(interestPtr, closure); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | ContactManager::onDnsCollectEndorseVerified(Ptr<Data> data, const Name& identity) |
| 214 | { emit collectEndorseFetched (*data); } |
| 215 | |
| 216 | void |
| 217 | ContactManager::onDnsCollectEndorseTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 218 | { emit collectEndorseFetchFailed (identity); } |
| 219 | |
| 220 | void |
| 221 | ContactManager::onDnsCollectEndorseUnverified(Ptr<Data> data, const Name& identity) |
| 222 | { emit collectEndorseFetchFailed (identity); } |
| 223 | |
| 224 | void |
| 225 | ContactManager::onKeyVerified(Ptr<Data> data, const Name& identity) |
| 226 | { |
| 227 | IdentityCertificate identityCertificate(*data); |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 228 | |
| 229 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(Profile(identityCertificate))); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 230 | |
| 231 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 232 | Name certificateName = identityManager->getDefaultCertificateName (); |
| 233 | identityManager->signByCertificate(*profileData, certificateName); |
| 234 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 235 | Ptr<EndorseCertificate> endorseCertificate = NULL; |
| 236 | try{ |
| 237 | endorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(identityCertificate, profileData)); |
| 238 | }catch(exception& e){ |
| 239 | _LOG_ERROR("Exception: " << e.what()); |
| 240 | return; |
| 241 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 242 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 243 | identityManager->signByCertificate(*endorseCertificate, certificateName); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 244 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 245 | emit contactKeyFetched (*endorseCertificate); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
| 249 | ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity) |
| 250 | { emit contactKeyFetchFailed (identity); } |
| 251 | |
| 252 | void |
| 253 | ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 254 | { emit contactKeyFetchFailed(identity); } |
| 255 | |
| 256 | void |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 257 | ContactManager::updateProfileData(const Name& identity) |
| 258 | { |
| 259 | // Get current profile; |
| 260 | Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity); |
| 261 | if(NULL == newProfile) |
| 262 | return; |
| 263 | Ptr<Blob> newProfileBlob = newProfile->toDerBlob(); |
| 264 | |
| 265 | // Check if profile exists |
| 266 | Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity); |
| 267 | if(NULL != profileDataBlob) |
| 268 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 269 | |
| 270 | Ptr<EndorseCertificate> oldEndorseCertificate = NULL; |
| 271 | try{ |
| 272 | Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob); |
| 273 | oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
| 274 | }catch(exception& e){ |
| 275 | _LOG_ERROR("Exception: " << e.what()); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | const Blob& oldProfileBlob = oldEndorseCertificate->getProfileData()->content(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 280 | |
| 281 | if(oldProfileBlob == *newProfileBlob) |
| 282 | return; |
| 283 | |
| 284 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 285 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 286 | if(NULL == newEndorseCertificate) |
| 287 | return; |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 288 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 289 | m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity); |
| 290 | |
| 291 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 296 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 297 | if(NULL == newEndorseCertificate) |
| 298 | return; |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 299 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 300 | m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity); |
| 301 | |
| 302 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 303 | } |
| 304 | } |
| 305 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 306 | void |
| 307 | ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity) |
| 308 | { |
| 309 | Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity); |
| 310 | Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity); |
| 311 | if(NULL != oldEndorseCertificateBlob) |
| 312 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 313 | Ptr<EndorseCertificate> oldEndorseCertificate = NULL; |
| 314 | try{ |
| 315 | Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob); |
| 316 | oldEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
| 317 | }catch(exception& e){ |
| 318 | _LOG_ERROR("Exception: " << e.what()); |
| 319 | return; |
| 320 | } |
| 321 | const Blob& oldEndorseContent = oldEndorseCertificate->content(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 322 | const Blob& newEndorseContent = newEndorseCertificate->content(); |
| 323 | if(oldEndorseContent == newEndorseContent) |
| 324 | return; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | if(NULL == newEndorseCertificate) |
| 329 | return; |
| 330 | } |
| 331 | m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity); |
| 332 | publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity); |
| 333 | } |
| 334 | |
| 335 | Ptr<EndorseCertificate> |
| 336 | ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity) |
| 337 | { |
| 338 | Ptr<ContactItem> contact = getContact(identity); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 339 | if(contact == NULL) |
| 340 | return NULL; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 341 | |
| 342 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 343 | Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity); |
| 344 | Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity); |
| 345 | |
| 346 | vector<string> endorseList = m_contactStorage->getEndorseList(identity); |
| 347 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 348 | Ptr<EndorseCertificate> cert = NULL; |
| 349 | try{ |
| 350 | cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList)); |
| 351 | }catch(exception& e){ |
| 352 | _LOG_ERROR("Exception: " << e.what()); |
| 353 | return NULL; |
| 354 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 355 | identityManager->signByCertificate(*cert, signerCertName); |
| 356 | |
| 357 | return cert; |
| 358 | } |
| 359 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 360 | vector<Ptr<ContactItem> > |
| 361 | ContactManager::getContactItemList() |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 362 | { return m_contactStorage->getAllContacts(); } |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 363 | |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 364 | Ptr<ContactItem> |
| 365 | ContactManager::getContact(const ndn::Name& contactNamespace) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 366 | { return m_contactStorage->getContact(contactNamespace); } |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 367 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 368 | Ptr<EndorseCertificate> |
| 369 | ContactManager::getSignedSelfEndorseCertificate(const Name& identity, |
| 370 | const Profile& profile) |
| 371 | { |
| 372 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 373 | Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity); |
| 374 | if(0 == certificateName.size()) |
| 375 | return NULL; |
| 376 | |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 377 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile)); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 378 | identityManager->signByCertificate(*profileData, certificateName); |
| 379 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 380 | Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 381 | if(NULL == signingCert) |
| 382 | return NULL; |
| 383 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 384 | Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true); |
| 385 | |
| 386 | Ptr<security::IdentityCertificate> kskCert; |
| 387 | if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-")) |
| 388 | { |
| 389 | Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature()); |
| 390 | // HACK! KSK certificate should be retrieved from network. |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 391 | Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 392 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 393 | Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 394 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 395 | kskCert = identityManager->getCertificate(kskCertName); |
| 396 | |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | kskCert = signingCert; |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 401 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 402 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 403 | if(NULL == kskCert) |
| 404 | return NULL; |
| 405 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 406 | vector<string> endorseList; |
| 407 | Profile::const_iterator it = profile.begin(); |
| 408 | for(; it != profile.end(); it++) |
| 409 | endorseList.push_back(it->first); |
| 410 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 411 | Ptr<EndorseCertificate> selfEndorseCertificate = NULL; |
| 412 | try{ |
| 413 | selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert, |
| 414 | profileData, |
| 415 | endorseList)); |
| 416 | }catch(exception& e){ |
| 417 | _LOG_ERROR("Exception: " << e.what()); |
| 418 | return NULL; |
| 419 | } |
| 420 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 421 | identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 422 | |
| 423 | return selfEndorseCertificate; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | void |
| 428 | ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity) |
| 429 | { |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 430 | Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size())); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 431 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame^] | 432 | Ptr<Data> plainData = NULL; |
| 433 | Ptr<EndorseCertificate> selfEndorseCertificate = NULL; |
| 434 | try{ |
| 435 | plainData = Data::decodeFromWire(dataContentBlob); |
| 436 | selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
| 437 | }catch(exception& e){ |
| 438 | _LOG_ERROR("Exception: " << e.what()); |
| 439 | return; |
| 440 | } |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 441 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 442 | const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 443 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 444 | if(security::PolicyManager::verifySignature(*plainData, ksk)) |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 445 | emit contactFetched (*selfEndorseCertificate); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 446 | else |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 447 | emit contactFetchFailed (identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void |
| 451 | ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity) |
| 452 | { emit contactFetchFailed (identity); } |
| 453 | |
| 454 | void |
| 455 | ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 456 | { emit contactFetchFailed(identity); } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 457 | |
| 458 | void |
| 459 | ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate) |
| 460 | { |
| 461 | Ptr<Data> data = Ptr<Data>::Create(); |
| 462 | |
| 463 | Name keyName = selfEndorseCertificate->getPublicKeyName(); |
| 464 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 465 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 466 | |
| 467 | Name dnsName = identity; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 468 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 469 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 470 | data->setName(dnsName); |
| 471 | Ptr<Blob> blob = selfEndorseCertificate->encodeToWire(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 472 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 473 | Content content(blob->buf(), blob->size()); |
| 474 | data->setContent(content); |
| 475 | |
| 476 | m_keychain->signByIdentity(*data, identity); |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 477 | |
| 478 | m_dnsStorage->updateDnsSelfProfileData(*data, identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 479 | |
| 480 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 481 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 482 | m_wrapper->putToNdnd(*dnsBlob); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 485 | void |
| 486 | ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity) |
| 487 | { |
| 488 | Ptr<Data> data = Ptr<Data>::Create(); |
| 489 | |
| 490 | Name keyName = endorseCertificate->getPublicKeyName(); |
| 491 | Name endorsee = keyName.getSubName(0, keyName.size()-1); |
| 492 | |
| 493 | |
| 494 | Name dnsName = signerIdentity; |
| 495 | dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(); |
| 496 | |
| 497 | data->setName(dnsName); |
| 498 | Ptr<Blob> blob = endorseCertificate->encodeToWire(); |
| 499 | |
| 500 | Content content(blob->buf(), blob->size()); |
| 501 | data->setContent(content); |
| 502 | |
| 503 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity); |
| 504 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 505 | |
| 506 | m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee); |
| 507 | |
| 508 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 509 | |
| 510 | m_wrapper->putToNdnd(*dnsBlob); |
| 511 | } |
| 512 | |
| 513 | void |
| 514 | ContactManager::publishEndorsedDataInDns(const Name& identity) |
| 515 | { |
| 516 | Ptr<Data> data = Ptr<Data>::Create(); |
| 517 | |
| 518 | Name dnsName = identity; |
| 519 | dnsName.append("DNS").append("ENDORSED").appendVersion(); |
| 520 | data->setName(dnsName); |
| 521 | |
| 522 | Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity); |
| 523 | |
| 524 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 525 | |
| 526 | vector<Blob>::const_iterator it = collectEndorseList->begin(); |
| 527 | for(; it != collectEndorseList->end(); it++) |
| 528 | { |
| 529 | Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it)); |
| 530 | root->addChild(entry); |
| 531 | } |
| 532 | |
| 533 | blob_stream blobStream; |
| 534 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 535 | root->encode(start); |
| 536 | |
| 537 | Content content(blobStream.buf()->buf(), blobStream.buf()->size()); |
| 538 | data->setContent(content); |
| 539 | |
| 540 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity); |
| 541 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 542 | |
| 543 | m_dnsStorage->updateDnsOthersEndorse(*data, identity); |
| 544 | |
| 545 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 546 | |
| 547 | m_wrapper->putToNdnd(*dnsBlob); |
| 548 | } |
| 549 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 550 | |
| 551 | #if WAF |
| 552 | #include "contact-manager.moc" |
| 553 | #include "contact-manager.cpp.moc" |
| 554 | #endif |