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 | |
| 235 | EndorseCertificate endorseCertificate(identityCertificate, profileData); |
| 236 | |
| 237 | identityManager->signByCertificate(endorseCertificate, certificateName); |
| 238 | |
| 239 | emit contactKeyFetched (endorseCertificate); |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity) |
| 244 | { emit contactKeyFetchFailed (identity); } |
| 245 | |
| 246 | void |
| 247 | ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 248 | { emit contactKeyFetchFailed(identity); } |
| 249 | |
| 250 | void |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 251 | ContactManager::updateProfileData(const Name& identity) |
| 252 | { |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 253 | // _LOG_DEBUG("updateProfileData: " << identity.toUri()); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 254 | // Get current profile; |
| 255 | Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity); |
| 256 | if(NULL == newProfile) |
| 257 | return; |
| 258 | Ptr<Blob> newProfileBlob = newProfile->toDerBlob(); |
| 259 | |
| 260 | // Check if profile exists |
| 261 | Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity); |
| 262 | if(NULL != profileDataBlob) |
| 263 | { |
| 264 | Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob); |
| 265 | EndorseCertificate oldEndorseCertificate(*plainData); |
| 266 | // _LOG_DEBUG("Certificate converted!"); |
| 267 | const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content(); |
| 268 | |
| 269 | if(oldProfileBlob == *newProfileBlob) |
| 270 | return; |
| 271 | |
| 272 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 273 | // _LOG_DEBUG("Signing DONE!"); |
| 274 | if(NULL == newEndorseCertificate) |
| 275 | return; |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 276 | // _LOG_DEBUG("About to update"); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 277 | m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity); |
| 278 | |
| 279 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 284 | // _LOG_DEBUG("Signing DONE!"); |
| 285 | if(NULL == newEndorseCertificate) |
| 286 | return; |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 287 | // _LOG_DEBUG("About to Insert"); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 288 | m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity); |
| 289 | |
| 290 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 291 | } |
| 292 | } |
| 293 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 294 | void |
| 295 | ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity) |
| 296 | { |
| 297 | Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity); |
| 298 | Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity); |
| 299 | if(NULL != oldEndorseCertificateBlob) |
| 300 | { |
| 301 | Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob); |
| 302 | EndorseCertificate oldEndorseCertificate(*plainData); |
| 303 | const Blob& oldEndorseContent = oldEndorseCertificate.content(); |
| 304 | const Blob& newEndorseContent = newEndorseCertificate->content(); |
| 305 | if(oldEndorseContent == newEndorseContent) |
| 306 | return; |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | if(NULL == newEndorseCertificate) |
| 311 | return; |
| 312 | } |
| 313 | m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity); |
| 314 | publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity); |
| 315 | } |
| 316 | |
| 317 | Ptr<EndorseCertificate> |
| 318 | ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity) |
| 319 | { |
| 320 | Ptr<ContactItem> contact = getContact(identity); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 321 | if(contact == NULL) |
| 322 | return NULL; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 323 | |
| 324 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 325 | Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity); |
| 326 | Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity); |
| 327 | |
| 328 | vector<string> endorseList = m_contactStorage->getEndorseList(identity); |
| 329 | |
| 330 | Ptr<EndorseCertificate> cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList)); |
| 331 | identityManager->signByCertificate(*cert, signerCertName); |
| 332 | |
| 333 | return cert; |
| 334 | } |
| 335 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 336 | vector<Ptr<ContactItem> > |
| 337 | ContactManager::getContactItemList() |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 338 | { return m_contactStorage->getAllContacts(); } |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 339 | |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 340 | Ptr<ContactItem> |
| 341 | ContactManager::getContact(const ndn::Name& contactNamespace) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 342 | { return m_contactStorage->getContact(contactNamespace); } |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 343 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 344 | Ptr<EndorseCertificate> |
| 345 | ContactManager::getSignedSelfEndorseCertificate(const Name& identity, |
| 346 | const Profile& profile) |
| 347 | { |
| 348 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 349 | Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity); |
| 350 | if(0 == certificateName.size()) |
| 351 | return NULL; |
| 352 | |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 353 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(profile)); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 354 | identityManager->signByCertificate(*profileData, certificateName); |
| 355 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 356 | Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 357 | if(NULL == signingCert) |
| 358 | return NULL; |
| 359 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 360 | Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true); |
| 361 | |
| 362 | Ptr<security::IdentityCertificate> kskCert; |
| 363 | if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-")) |
| 364 | { |
| 365 | Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature()); |
| 366 | // HACK! KSK certificate should be retrieved from network. |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 367 | Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 368 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 369 | Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 370 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 371 | kskCert = identityManager->getCertificate(kskCertName); |
| 372 | |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | kskCert = signingCert; |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 377 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 378 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 379 | if(NULL == kskCert) |
| 380 | return NULL; |
| 381 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 382 | vector<string> endorseList; |
| 383 | Profile::const_iterator it = profile.begin(); |
| 384 | for(; it != profile.end(); it++) |
| 385 | endorseList.push_back(it->first); |
| 386 | |
| 387 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert, |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 388 | profileData, |
| 389 | endorseList)); |
| 390 | identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 391 | |
| 392 | return selfEndorseCertificate; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | void |
| 397 | ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity) |
| 398 | { |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 399 | 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] | 400 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 401 | Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob); |
| 402 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 403 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 404 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 405 | const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 406 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 407 | if(security::PolicyManager::verifySignature(*plainData, ksk)) |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 408 | emit contactFetched (*selfEndorseCertificate); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 409 | else |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame^] | 410 | emit contactFetchFailed (identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void |
| 414 | ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity) |
| 415 | { emit contactFetchFailed (identity); } |
| 416 | |
| 417 | void |
| 418 | 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] | 419 | { emit contactFetchFailed(identity); } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 420 | |
| 421 | void |
| 422 | ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate) |
| 423 | { |
| 424 | Ptr<Data> data = Ptr<Data>::Create(); |
| 425 | |
| 426 | Name keyName = selfEndorseCertificate->getPublicKeyName(); |
| 427 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 428 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 429 | |
| 430 | Name dnsName = identity; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 431 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 432 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 433 | data->setName(dnsName); |
| 434 | Ptr<Blob> blob = selfEndorseCertificate->encodeToWire(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 435 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 436 | Content content(blob->buf(), blob->size()); |
| 437 | data->setContent(content); |
| 438 | |
| 439 | m_keychain->signByIdentity(*data, identity); |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 440 | |
| 441 | m_dnsStorage->updateDnsSelfProfileData(*data, identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 442 | |
| 443 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 444 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 445 | m_wrapper->putToNdnd(*dnsBlob); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 448 | void |
| 449 | ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity) |
| 450 | { |
| 451 | Ptr<Data> data = Ptr<Data>::Create(); |
| 452 | |
| 453 | Name keyName = endorseCertificate->getPublicKeyName(); |
| 454 | Name endorsee = keyName.getSubName(0, keyName.size()-1); |
| 455 | |
| 456 | |
| 457 | Name dnsName = signerIdentity; |
| 458 | dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(); |
| 459 | |
| 460 | data->setName(dnsName); |
| 461 | Ptr<Blob> blob = endorseCertificate->encodeToWire(); |
| 462 | |
| 463 | Content content(blob->buf(), blob->size()); |
| 464 | data->setContent(content); |
| 465 | |
| 466 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity); |
| 467 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 468 | |
| 469 | m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee); |
| 470 | |
| 471 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 472 | |
| 473 | m_wrapper->putToNdnd(*dnsBlob); |
| 474 | } |
| 475 | |
| 476 | void |
| 477 | ContactManager::publishEndorsedDataInDns(const Name& identity) |
| 478 | { |
| 479 | Ptr<Data> data = Ptr<Data>::Create(); |
| 480 | |
| 481 | Name dnsName = identity; |
| 482 | dnsName.append("DNS").append("ENDORSED").appendVersion(); |
| 483 | data->setName(dnsName); |
| 484 | |
| 485 | Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity); |
| 486 | |
| 487 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 488 | |
| 489 | vector<Blob>::const_iterator it = collectEndorseList->begin(); |
| 490 | for(; it != collectEndorseList->end(); it++) |
| 491 | { |
| 492 | Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it)); |
| 493 | root->addChild(entry); |
| 494 | } |
| 495 | |
| 496 | blob_stream blobStream; |
| 497 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 498 | root->encode(start); |
| 499 | |
| 500 | Content content(blobStream.buf()->buf(), blobStream.buf()->size()); |
| 501 | data->setContent(content); |
| 502 | |
| 503 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity); |
| 504 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 505 | |
| 506 | m_dnsStorage->updateDnsOthersEndorse(*data, identity); |
| 507 | |
| 508 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 509 | |
| 510 | m_wrapper->putToNdnd(*dnsBlob); |
| 511 | } |
| 512 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 513 | |
| 514 | #if WAF |
| 515 | #include "contact-manager.moc" |
| 516 | #include "contact-manager.cpp.moc" |
| 517 | #endif |