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 | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 22 | #include <fstream> |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 23 | #include "logging.h" |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 24 | #endif |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace ndn; |
| 27 | using namespace ndn::security; |
| 28 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 29 | INIT_LOGGER("ContactManager"); |
| 30 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 31 | ContactManager::ContactManager(Ptr<ContactStorage> contactStorage, |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 32 | Ptr<DnsStorage> dnsStorage, |
| 33 | QObject* parent) |
| 34 | : QObject(parent) |
| 35 | , m_contactStorage(contactStorage) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 36 | , m_dnsStorage(dnsStorage) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 37 | { |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 38 | setKeychain(); |
| 39 | |
| 40 | m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain)); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ContactManager::~ContactManager() |
| 44 | { |
| 45 | } |
| 46 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 47 | void |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 48 | ContactManager::setKeychain() |
| 49 | { |
| 50 | Ptr<OSXPrivatekeyStorage> privateStorage = Ptr<OSXPrivatekeyStorage>::Create(); |
| 51 | Ptr<IdentityManager> identityManager = Ptr<IdentityManager>(new IdentityManager(Ptr<BasicIdentityStorage>::Create(), privateStorage)); |
| 52 | Ptr<TTLCertificateCache> certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache()); |
| 53 | Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>(new SimplePolicyManager(10, certificateCache)); |
| 54 | Ptr<EncryptionManager> encryptionManager = Ptr<EncryptionManager>(new BasicEncryptionManager(privateStorage, "/tmp/encryption.db")); |
| 55 | Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, encryptionManager)); |
| 56 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 57 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
| 58 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>", |
| 59 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 60 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
| 61 | "^([^<KEY>]*)<KEY>(<>*<KSK-.*>)<ID-CERT>", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 62 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 63 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>", |
| 64 | "^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 65 | ">", "\\1\\2", "\\1", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 66 | policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>", |
| 67 | "^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>", |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 68 | "==", "\\1", "\\1\\2", true))); |
| 69 | |
| 70 | policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>", |
| 71 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>", |
| 72 | "==", "\\1", "\\1\\2", true))); |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 73 | |
| 74 | ifstream is ("trust-anchor.data", ios::binary); |
| 75 | is.seekg (0, ios::end); |
| 76 | ifstream::pos_type size = is.tellg(); |
| 77 | char * memblock = new char [size]; |
| 78 | is.seekg (0, ios::beg); |
| 79 | is.read (memblock, size); |
| 80 | is.close(); |
| 81 | |
| 82 | Ptr<Blob> readBlob = Ptr<Blob>(new Blob(memblock, size)); |
| 83 | Ptr<Data> readData = Data::decodeFromWire (readBlob); |
| 84 | Ptr<IdentityCertificate> anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*readData)); |
| 85 | policyManager->addTrustAnchor(anchor); |
| 86 | |
| 87 | delete memblock; |
| 88 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 89 | m_keychain = keychain; |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 90 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | void |
| 94 | ContactManager::fetchSelfEndorseCertificate(const ndn::Name& identity) |
| 95 | { |
| 96 | Name interestName = identity; |
| 97 | interestName.append("DNS").append("PROFILE"); |
| 98 | |
| 99 | Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName)); |
| 100 | Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, |
| 101 | this, |
| 102 | _1, |
| 103 | identity), |
| 104 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout, |
| 105 | this, |
| 106 | _1, |
| 107 | _2, |
| 108 | identity, |
| 109 | 0), |
| 110 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified, |
| 111 | this, |
| 112 | _1, |
| 113 | identity))); |
| 114 | m_wrapper->sendInterest(interestPtr, closure); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | ContactManager::updateProfileData(const Name& identity) |
| 119 | { |
| 120 | // Get current profile; |
| 121 | Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity); |
| 122 | if(NULL == newProfile) |
| 123 | return; |
| 124 | Ptr<Blob> newProfileBlob = newProfile->toDerBlob(); |
| 125 | |
| 126 | // Check if profile exists |
| 127 | Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity); |
| 128 | if(NULL != profileDataBlob) |
| 129 | { |
| 130 | Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob); |
| 131 | EndorseCertificate oldEndorseCertificate(*plainData); |
| 132 | // _LOG_DEBUG("Certificate converted!"); |
| 133 | const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content(); |
| 134 | |
| 135 | if(oldProfileBlob == *newProfileBlob) |
| 136 | return; |
| 137 | |
| 138 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 139 | // _LOG_DEBUG("Signing DONE!"); |
| 140 | if(NULL == newEndorseCertificate) |
| 141 | return; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 142 | _LOG_DEBUG("About to update"); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 143 | m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity); |
| 144 | |
| 145 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
| 150 | // _LOG_DEBUG("Signing DONE!"); |
| 151 | if(NULL == newEndorseCertificate) |
| 152 | return; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 153 | _LOG_DEBUG("About to Insert"); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 154 | m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity); |
| 155 | |
| 156 | publishSelfEndorseCertificateInDNS(newEndorseCertificate); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | Ptr<EndorseCertificate> |
| 161 | ContactManager::getSignedSelfEndorseCertificate(const Name& identity, |
| 162 | const Profile& profile) |
| 163 | { |
| 164 | Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager(); |
| 165 | Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity); |
| 166 | if(0 == certificateName.size()) |
| 167 | return NULL; |
| 168 | |
| 169 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile)); |
| 170 | identityManager->signByCertificate(*profileData, certificateName); |
| 171 | |
| 172 | Ptr<security::IdentityCertificate> dskCert = identityManager->getCertificate(certificateName); |
| 173 | Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(dskCert->getSignature()); |
| 174 | // HACK! KSK certificate should be retrieved from network. |
| 175 | Ptr<security::IdentityCertificate> kskCert = identityManager->getCertificate(dskCertSig->getKeyLocator().getKeyName()); |
| 176 | |
| 177 | vector<string> endorseList; |
| 178 | Profile::const_iterator it = profile.begin(); |
| 179 | for(; it != profile.end(); it++) |
| 180 | endorseList.push_back(it->first); |
| 181 | |
| 182 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert, |
| 183 | kskCert->getNotBefore(), |
| 184 | kskCert->getNotAfter(), |
| 185 | profileData, |
| 186 | endorseList)); |
| 187 | identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 188 | |
| 189 | return selfEndorseCertificate; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void |
| 194 | ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity) |
| 195 | { |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 196 | 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] | 197 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 198 | Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob); |
| 199 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 200 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData)); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 201 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 202 | const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 203 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 204 | if(security::PolicyManager::verifySignature(*plainData, ksk)) |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 205 | { |
| 206 | emit contactFetched (*selfEndorseCertificate); |
| 207 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 208 | else |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 209 | { |
| 210 | emit contactFetchFailed (identity); |
| 211 | } |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
| 215 | ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity) |
| 216 | { emit contactFetchFailed (identity); } |
| 217 | |
| 218 | void |
| 219 | ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry) |
| 220 | { |
| 221 | if(retry > 0) |
| 222 | { |
| 223 | Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback, |
| 224 | boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout, |
| 225 | this, |
| 226 | _1, |
| 227 | _2, |
| 228 | identity, |
| 229 | retry - 1), |
| 230 | closure->m_unverifiedCallback, |
| 231 | closure->m_stepCount) |
| 232 | ); |
| 233 | m_wrapper->sendInterest(interest, newClosure); |
| 234 | } |
| 235 | else |
| 236 | emit contactFetchFailed(identity); |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate) |
| 241 | { |
| 242 | Ptr<Data> data = Ptr<Data>::Create(); |
| 243 | |
| 244 | Name keyName = selfEndorseCertificate->getPublicKeyName(); |
| 245 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 246 | |
| 247 | TimeInterval ti = time::NowUnixTimestamp(); |
| 248 | ostringstream oss; |
| 249 | oss << ti.total_seconds(); |
| 250 | |
| 251 | Name dnsName = identity; |
| 252 | dnsName.append("DNS").append("PROFILE").append(oss.str()); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 253 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 254 | data->setName(dnsName); |
| 255 | Ptr<Blob> blob = selfEndorseCertificate->encodeToWire(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame^] | 256 | |
| 257 | // string encoded; |
| 258 | // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(blob->buf()), blob->size(), true, |
| 259 | // new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), false)); |
| 260 | |
| 261 | // Content content(encoded.c_str(), encoded.size()); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 262 | Content content(blob->buf(), blob->size()); |
| 263 | data->setContent(content); |
| 264 | |
| 265 | m_keychain->signByIdentity(*data, identity); |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 266 | |
| 267 | m_dnsStorage->updateDnsSelfProfileData(*data, identity); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 268 | |
| 269 | Ptr<Blob> dnsBlob = data->encodeToWire(); |
| 270 | |
| 271 | m_wrapper->putToCcnd(*dnsBlob); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | #if WAF |
| 276 | #include "contact-manager.moc" |
| 277 | #include "contact-manager.cpp.moc" |
| 278 | #endif |