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> |
| 16 | #include <ndn.cxx/security/identity/basic-identity-storage.h> |
| 17 | #include <ndn.cxx/security/identity/osx-privatekey-storage.h> |
| 18 | #include <ndn.cxx/security/policy/simple-policy-manager.h> |
| 19 | #include <ndn.cxx/security/policy/identity-policy-rule.h> |
| 20 | #include <ndn.cxx/security/cache/ttl-certificate-cache.h> |
| 21 | #include <ndn.cxx/security/encryption/basic-encryption-manager.h> |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 22 | #include <ndn.cxx/helpers/der/der.h> |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 23 | #include <fstream> |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 24 | #include "logging.h" |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 25 | #endif |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 26 | |
| 27 | using namespace ndn; |
| 28 | using namespace ndn::security; |
| 29 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 30 | INIT_LOGGER("ContactManager"); |
| 31 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 32 | ContactManager::ContactManager(Ptr<ContactStorage> contactStorage, |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 33 | Ptr<DnsStorage> dnsStorage, |
| 34 | QObject* parent) |
| 35 | : QObject(parent) |
| 36 | , m_contactStorage(contactStorage) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 37 | , m_dnsStorage(dnsStorage) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 38 | { |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 39 | setKeychain(); |
| 40 | |
| 41 | m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain)); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | ContactManager::~ContactManager() |
| 45 | { |
| 46 | } |
| 47 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 48 | void |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 49 | ContactManager::setKeychain() |
| 50 | { |
| 51 | Ptr<OSXPrivatekeyStorage> privateStorage = Ptr<OSXPrivatekeyStorage>::Create(); |
| 52 | Ptr<IdentityManager> identityManager = Ptr<IdentityManager>(new IdentityManager(Ptr<BasicIdentityStorage>::Create(), privateStorage)); |
| 53 | Ptr<TTLCertificateCache> certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache()); |
| 54 | Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>(new SimplePolicyManager(10, certificateCache)); |
| 55 | Ptr<EncryptionManager> encryptionManager = Ptr<EncryptionManager>(new BasicEncryptionManager(privateStorage, "/tmp/encryption.db")); |
| 56 | Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, encryptionManager)); |
| 57 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 58 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><ENDORSED>", |
| 59 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
| 60 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 61 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 62 | "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 63 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 64 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 65 | "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 66 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 67 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 68 | "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 69 | ">", "\\1\\2", "\\1", true))); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 70 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 71 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 72 | "==", "\\1", "\\1\\2", true))); |
| 73 | |
| 74 | policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
| 75 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>", |
| 76 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 77 | |
| 78 | ifstream is ("trust-anchor.data", ios::binary); |
| 79 | is.seekg (0, ios::end); |
| 80 | ifstream::pos_type size = is.tellg(); |
| 81 | char * memblock = new char [size]; |
| 82 | is.seekg (0, ios::beg); |
| 83 | is.read (memblock, size); |
| 84 | is.close(); |
| 85 | |
| 86 | Ptr<Blob> readBlob = Ptr<Blob>(new Blob(memblock, size)); |
| 87 | Ptr<Data> readData = Data::decodeFromWire (readBlob); |
| 88 | Ptr<IdentityCertificate> anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*readData)); |
| 89 | policyManager->addTrustAnchor(anchor); |
| 90 | |
| 91 | delete memblock; |
| 92 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 93 | m_keychain = keychain; |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 94 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 95 | |
| 96 | |
| 97 | void |
| 98 | ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity) |
| 99 | { |
| 100 | Name interestName = identity; |
| 101 | interestName.append("DNS").append("PROFILE"); |
| 102 | |
| 103 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 104 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 105 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, |
| 106 | this, |
| 107 | _1, |
| 108 | identity), |
| 109 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout, |
| 110 | this, |
| 111 | _1, |
| 112 | _2, |
| 113 | identity, |
| 114 | 0), |
| 115 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified, |
| 116 | this, |
| 117 | _1, |
| 118 | identity))); |
| 119 | m_wrapper->sendInterest(interestPtr, closure); |
| 120 | } |
| 121 | |
| 122 | void |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 123 | ContactManager::fetchCollectEndorse(const ndn::Name& identity) |
| 124 | { |
| 125 | Name interestName = identity; |
| 126 | interestName.append("DNS").append("ENDORSED"); |
| 127 | |
| 128 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
| 129 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
| 130 | interestPtr->setInterestLifetime(1); |
| 131 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsCollectEndorseVerified, |
| 132 | this, |
| 133 | _1, |
| 134 | identity), |
| 135 | boost::bind(&ContactManager::onDnsCollectEndorseTimeout, |
| 136 | this, |
| 137 | _1, |
| 138 | _2, |
| 139 | identity, |
| 140 | 0), |
| 141 | boost::bind(&ContactManager::onDnsCollectEndorseUnverified, |
| 142 | this, |
| 143 | _1, |
| 144 | identity))); |
| 145 | m_wrapper->sendInterest(interestPtr, closure); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | ContactManager::fetchKey(const ndn::Name& certName) |
| 150 | { |
| 151 | Name interestName = certName; |
| 152 | |
| 153 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
| 154 | interestPtr->setChildSelector(Interest::CHILD_RIGHT); |
| 155 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onKeyVerified, |
| 156 | this, |
| 157 | _1, |
| 158 | certName), |
| 159 | boost::bind(&ContactManager::onKeyTimeout, |
| 160 | this, |
| 161 | _1, |
| 162 | _2, |
| 163 | certName, |
| 164 | 0), |
| 165 | boost::bind(&ContactManager::onKeyUnverified, |
| 166 | this, |
| 167 | _1, |
| 168 | certName))); |
| 169 | m_wrapper->sendInterest(interestPtr, closure); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | ContactManager::onDnsCollectEndorseVerified(Ptr<Data> data, const Name& identity) |
| 174 | { emit collectEndorseFetched (*data); } |
| 175 | |
| 176 | void |
| 177 | ContactManager::onDnsCollectEndorseTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 178 | { emit collectEndorseFetchFailed (identity); } |
| 179 | |
| 180 | void |
| 181 | ContactManager::onDnsCollectEndorseUnverified(Ptr<Data> data, const Name& identity) |
| 182 | { emit collectEndorseFetchFailed (identity); } |
| 183 | |
| 184 | void |
| 185 | ContactManager::onKeyVerified(Ptr<Data> data, const Name& identity) |
| 186 | { |
| 187 | IdentityCertificate identityCertificate(*data); |
| 188 | Name keyName = identityCertificate.getPublicKeyName(); |
| 189 | Profile profile(keyName.getPrefix(keyName.size()-1), |
| 190 | keyName.get(-2).toUri(), |
| 191 | keyName.getPrefix(keyName.size()-2).toUri()); |
| 192 | |
| 193 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(keyName.getPrefix(keyName.size()-1), |
| 194 | profile)); |
| 195 | |
| 196 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 197 | Name certificateName = identityManager->getDefaultCertificateName (); |
| 198 | identityManager->signByCertificate(*profileData, certificateName); |
| 199 | |
| 200 | EndorseCertificate endorseCertificate(identityCertificate, profileData); |
| 201 | |
| 202 | identityManager->signByCertificate(endorseCertificate, certificateName); |
| 203 | |
| 204 | emit contactKeyFetched (endorseCertificate); |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | ContactManager::onKeyUnverified(Ptr<Data> data, const Name& identity) |
| 209 | { emit contactKeyFetchFailed (identity); } |
| 210 | |
| 211 | void |
| 212 | ContactManager::onKeyTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 213 | { emit contactKeyFetchFailed(identity); } |
| 214 | |
| 215 | void |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 216 | ContactManager::updateProfileData(const Name& identity) |
| 217 | { |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 218 | _LOG_DEBUG("updateProfileData: " << identity.toUri()); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 219 | // Get current profile; |
| 220 | Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity); |
| 221 | if(NULL == newProfile) |
| 222 | return; |
| 223 | Ptr<Blob> newProfileBlob = newProfile->toDerBlob(); |
| 224 | |
| 225 | // Check if profile exists |
| 226 | Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity); |
| 227 | if(NULL != profileDataBlob) |
| 228 | { |
| 229 | Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob); |
| 230 | EndorseCertificate oldEndorseCertificate(*plainData); |
| 231 | // _LOG_DEBUG("Certificate converted!"); |
| 232 | const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content(); |
| 233 | |
| 234 | if(oldProfileBlob == *newProfileBlob) |
| 235 | return; |
| 236 | |
| 237 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 238 | // _LOG_DEBUG("Signing DONE!"); |
| 239 | if(NULL == newEndorseCertificate) |
| 240 | return; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 241 | _LOG_DEBUG("About to update"); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 242 | m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity); |
| 243 | |
| 244 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 249 | // _LOG_DEBUG("Signing DONE!"); |
| 250 | if(NULL == newEndorseCertificate) |
| 251 | return; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 252 | _LOG_DEBUG("About to Insert"); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 253 | m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity); |
| 254 | |
| 255 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 256 | } |
| 257 | } |
| 258 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 259 | void |
| 260 | ContactManager::updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity) |
| 261 | { |
| 262 | Ptr<Blob> oldEndorseCertificateBlob = m_contactStorage->getEndorseCertificate(identity); |
| 263 | Ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identity, signerIdentity); |
| 264 | if(NULL != oldEndorseCertificateBlob) |
| 265 | { |
| 266 | Ptr<Data> plainData = Data::decodeFromWire(oldEndorseCertificateBlob); |
| 267 | EndorseCertificate oldEndorseCertificate(*plainData); |
| 268 | const Blob& oldEndorseContent = oldEndorseCertificate.content(); |
| 269 | const Blob& newEndorseContent = newEndorseCertificate->content(); |
| 270 | if(oldEndorseContent == newEndorseContent) |
| 271 | return; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | if(NULL == newEndorseCertificate) |
| 276 | return; |
| 277 | } |
| 278 | m_contactStorage->addEndorseCertificate(newEndorseCertificate, identity); |
| 279 | publishEndorseCertificateInDNS(newEndorseCertificate, signerIdentity); |
| 280 | } |
| 281 | |
| 282 | Ptr<EndorseCertificate> |
| 283 | ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity) |
| 284 | { |
| 285 | Ptr<ContactItem> contact = getContact(identity); |
| 286 | |
| 287 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 288 | Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity); |
| 289 | Name signerCertName = identityManager->getDefaultCertificateNameByIdentity(signerIdentity); |
| 290 | |
| 291 | vector<string> endorseList = m_contactStorage->getEndorseList(identity); |
| 292 | |
| 293 | Ptr<EndorseCertificate> cert = Ptr<EndorseCertificate>(new EndorseCertificate(contact->getSelfEndorseCertificate(), signerKeyName, endorseList)); |
| 294 | identityManager->signByCertificate(*cert, signerCertName); |
| 295 | |
| 296 | return cert; |
| 297 | } |
| 298 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 299 | vector<Ptr<ContactItem> > |
| 300 | ContactManager::getContactItemList() |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 301 | { return m_contactStorage->getAllContacts(); } |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 302 | |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 303 | Ptr<ContactItem> |
| 304 | ContactManager::getContact(const ndn::Name& contactNamespace) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 305 | { return m_contactStorage->getContact(contactNamespace); } |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 306 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 307 | Ptr<EndorseCertificate> |
| 308 | ContactManager::getSignedSelfEndorseCertificate(const Name& identity, |
| 309 | const Profile& profile) |
| 310 | { |
| 311 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 312 | Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity); |
| 313 | if(0 == certificateName.size()) |
| 314 | return NULL; |
| 315 | |
| 316 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile)); |
| 317 | identityManager->signByCertificate(*profileData, certificateName); |
| 318 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 319 | Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName); |
| 320 | Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true); |
| 321 | |
| 322 | Ptr<security::IdentityCertificate> kskCert; |
| 323 | if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-")) |
| 324 | { |
| 325 | Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature()); |
| 326 | // HACK! KSK certificate should be retrieved from network. |
| 327 | _LOG_DEBUG("keyLocator: " << dskCertSig->getKeyLocator().getKeyName()); |
| 328 | Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName()); |
| 329 | _LOG_DEBUG("keyName: " << keyName.toUri()); |
| 330 | Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName); |
| 331 | _LOG_DEBUG("ksk cert name: " << kskCertName); |
| 332 | kskCert = identityManager->getCertificate(kskCertName); |
| 333 | |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | kskCert = signingCert; |
| 338 | _LOG_DEBUG("ksk cert name: " << kskCert->getName().toUri()); |
| 339 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 340 | |
| 341 | vector<string> endorseList; |
| 342 | Profile::const_iterator it = profile.begin(); |
| 343 | for(; it != profile.end(); it++) |
| 344 | endorseList.push_back(it->first); |
| 345 | |
| 346 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert, |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 347 | profileData, |
| 348 | endorseList)); |
| 349 | identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 350 | |
| 351 | return selfEndorseCertificate; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | void |
| 356 | ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity) |
| 357 | { |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 358 | 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] | 359 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 360 | Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob); |
| 361 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 362 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 363 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 364 | const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 365 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 366 | if(security::PolicyManager::verifySignature(*plainData, ksk)) |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 367 | { |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 368 | // Profile profile = selfEndorseCertificate->getProfileData()->getProfile(); |
| 369 | // Profile::const_iterator it = profile.getEntries().begin(); |
| 370 | // it++; |
| 371 | // _LOG_DEBUG("Entry Size: " << it->first); |
| 372 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 373 | emit contactFetched (*selfEndorseCertificate); |
| 374 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 375 | else |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 376 | { |
| 377 | emit contactFetchFailed (identity); |
| 378 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void |
| 382 | ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity) |
| 383 | { emit contactFetchFailed (identity); } |
| 384 | |
| 385 | void |
| 386 | 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^] | 387 | { emit contactFetchFailed(identity); } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 388 | |
| 389 | void |
| 390 | ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate) |
| 391 | { |
| 392 | Ptr<Data> data = Ptr<Data>::Create(); |
| 393 | |
| 394 | Name keyName = selfEndorseCertificate->getPublicKeyName(); |
| 395 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 396 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 397 | |
| 398 | Name dnsName = identity; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 399 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 400 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 401 | data->setName(dnsName); |
| 402 | Ptr<Blob> blob = selfEndorseCertificate->encodeToWire(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 403 | |
| 404 | // string encoded; |
| 405 | // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(blob->buf()), blob->size(), true, |
| 406 | // new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), false)); |
| 407 | |
| 408 | // Content content(encoded.c_str(), encoded.size()); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 409 | Content content(blob->buf(), blob->size()); |
| 410 | data->setContent(content); |
| 411 | |
| 412 | m_keychain->signByIdentity(*data, identity); |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 413 | |
| 414 | m_dnsStorage->updateDnsSelfProfileData(*data, identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 415 | |
| 416 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 417 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 418 | m_wrapper->putToNdnd(*dnsBlob); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 421 | void |
| 422 | ContactManager::publishEndorseCertificateInDNS(Ptr<EndorseCertificate> endorseCertificate, const Name& signerIdentity) |
| 423 | { |
| 424 | Ptr<Data> data = Ptr<Data>::Create(); |
| 425 | |
| 426 | Name keyName = endorseCertificate->getPublicKeyName(); |
| 427 | Name endorsee = keyName.getSubName(0, keyName.size()-1); |
| 428 | |
| 429 | |
| 430 | Name dnsName = signerIdentity; |
| 431 | dnsName.append("DNS").append(endorsee).append("ENDORSEE").appendVersion(); |
| 432 | |
| 433 | data->setName(dnsName); |
| 434 | Ptr<Blob> blob = endorseCertificate->encodeToWire(); |
| 435 | |
| 436 | Content content(blob->buf(), blob->size()); |
| 437 | data->setContent(content); |
| 438 | |
| 439 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(signerIdentity); |
| 440 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 441 | |
| 442 | m_dnsStorage->updateDnsEndorseOthers(*data, signerIdentity, endorsee); |
| 443 | |
| 444 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 445 | |
| 446 | m_wrapper->putToNdnd(*dnsBlob); |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | ContactManager::publishEndorsedDataInDns(const Name& identity) |
| 451 | { |
| 452 | Ptr<Data> data = Ptr<Data>::Create(); |
| 453 | |
| 454 | Name dnsName = identity; |
| 455 | dnsName.append("DNS").append("ENDORSED").appendVersion(); |
| 456 | data->setName(dnsName); |
| 457 | |
| 458 | Ptr<vector<Blob> > collectEndorseList = m_contactStorage->getCollectEndorseList(identity); |
| 459 | |
| 460 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 461 | |
| 462 | vector<Blob>::const_iterator it = collectEndorseList->begin(); |
| 463 | for(; it != collectEndorseList->end(); it++) |
| 464 | { |
| 465 | Ptr<der::DerOctetString> entry = Ptr<der::DerOctetString>(new der::DerOctetString(*it)); |
| 466 | root->addChild(entry); |
| 467 | } |
| 468 | |
| 469 | blob_stream blobStream; |
| 470 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 471 | root->encode(start); |
| 472 | |
| 473 | Content content(blobStream.buf()->buf(), blobStream.buf()->size()); |
| 474 | data->setContent(content); |
| 475 | |
| 476 | Name signCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(identity); |
| 477 | m_keychain->getIdentityManager()->signByCertificate(*data, signCertName); |
| 478 | |
| 479 | m_dnsStorage->updateDnsOthersEndorse(*data, identity); |
| 480 | |
| 481 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 482 | |
| 483 | m_wrapper->putToNdnd(*dnsBlob); |
| 484 | } |
| 485 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 486 | |
| 487 | #if WAF |
| 488 | #include "contact-manager.moc" |
| 489 | #include "contact-manager.cpp.moc" |
| 490 | #endif |