blob: 9bd8ca2d3ead035167f5b6728c35809795a4f5b0 [file] [log] [blame]
Yingdi Yu0b82a4e2013-10-18 11:29:25 -07001/* -*- 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 Yuaa8d7692013-10-18 17:05:02 -070013#ifndef Q_MOC_RUN
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070014#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 Yu0b82a4e2013-10-18 11:29:25 -070022#include <fstream>
Yingdi Yu590fa5d2013-10-18 18:35:09 -070023#include "logging.h"
Yingdi Yuaa8d7692013-10-18 17:05:02 -070024#endif
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070025
26using namespace ndn;
27using namespace ndn::security;
28
Yingdi Yu590fa5d2013-10-18 18:35:09 -070029INIT_LOGGER("ContactManager");
30
Yingdi Yuaa8d7692013-10-18 17:05:02 -070031ContactManager::ContactManager(Ptr<ContactStorage> contactStorage,
Yingdi Yu590fa5d2013-10-18 18:35:09 -070032 Ptr<DnsStorage> dnsStorage,
33 QObject* parent)
34 : QObject(parent)
35 , m_contactStorage(contactStorage)
Yingdi Yuaa8d7692013-10-18 17:05:02 -070036 , m_dnsStorage(dnsStorage)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070037{
Yingdi Yuaa8d7692013-10-18 17:05:02 -070038 setKeychain();
39
40 m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070041}
42
43ContactManager::~ContactManager()
44{
45}
46
Yingdi Yuaa8d7692013-10-18 17:05:02 -070047void
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070048ContactManager::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 Yuaa8d7692013-10-18 17:05:02 -070057 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070058 "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070059 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070060 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070061 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070062 "==", "\\1", "\\1\\2", true)));
Yingdi Yu42f66462013-10-31 17:38:22 -070063 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070064 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070065 ">", "\\1\\2", "\\1", true)));
Yingdi Yu42f66462013-10-31 17:38:22 -070066 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yued8cfc42013-11-01 17:37:51 -070067 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070068 "==", "\\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 Yu0b82a4e2013-10-18 11:29:25 -070073
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 Yuaa8d7692013-10-18 17:05:02 -070089 m_keychain = keychain;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070090}
Yingdi Yuaa8d7692013-10-18 17:05:02 -070091
92
93void
94ContactManager::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));
Yingdi Yu42f66462013-10-31 17:38:22 -0700100 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700101 Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified,
102 this,
103 _1,
104 identity),
105 boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout,
106 this,
107 _1,
108 _2,
109 identity,
110 0),
111 boost::bind(&ContactManager::onDnsSelfEndorseCertificateUnverified,
112 this,
113 _1,
114 identity)));
115 m_wrapper->sendInterest(interestPtr, closure);
116}
117
118void
119ContactManager::updateProfileData(const Name& identity)
120{
Yingdi Yued8cfc42013-11-01 17:37:51 -0700121 _LOG_DEBUG("updateProfileData: " << identity.toUri());
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700122 // Get current profile;
123 Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
124 if(NULL == newProfile)
125 return;
126 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
127
128 // Check if profile exists
129 Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
130 if(NULL != profileDataBlob)
131 {
132 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
133 EndorseCertificate oldEndorseCertificate(*plainData);
134 // _LOG_DEBUG("Certificate converted!");
135 const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content();
136
137 if(oldProfileBlob == *newProfileBlob)
138 return;
139
140 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
141 // _LOG_DEBUG("Signing DONE!");
142 if(NULL == newEndorseCertificate)
143 return;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700144 _LOG_DEBUG("About to update");
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700145 m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
146
147 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
148 }
149 else
150 {
151 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
152 // _LOG_DEBUG("Signing DONE!");
153 if(NULL == newEndorseCertificate)
154 return;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700155 _LOG_DEBUG("About to Insert");
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700156 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
157
158 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
159 }
160}
161
Yingdi Yu79c25a22013-10-21 13:38:38 -0700162vector<Ptr<ContactItem> >
163ContactManager::getContactItemList()
164{
165 vector<Ptr<ContactItem> > result;
166
167 vector<Ptr<ContactItem> > ncList = m_contactStorage->getAllNormalContacts();
168 vector<Ptr<TrustedContact> > tcList = m_contactStorage->getAllTrustedContacts();
169
170 result.insert(result.end(), tcList.begin(), tcList.end());
171 result.insert(result.end(), ncList.begin(), ncList.end());
172
173 return result;
174}
175
Yingdi Yud40226b2013-10-23 14:05:12 -0700176Ptr<ContactItem>
177ContactManager::getContact(const ndn::Name& contactNamespace)
178{
179 Ptr<ContactItem> contactItem = m_contactStorage->getNormalContact(contactNamespace);
180 if(NULL != contactItem)
181 return contactItem;
182
183 contactItem = m_contactStorage->getTrustedContact(contactNamespace);
184 if(NULL != contactItem)
185 return contactItem;
186
187 return NULL;
188}
189
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700190Ptr<EndorseCertificate>
191ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
192 const Profile& profile)
193{
194 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
195 Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity);
196 if(0 == certificateName.size())
197 return NULL;
198
199 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile));
200 identityManager->signByCertificate(*profileData, certificateName);
201
Yingdi Yued8cfc42013-11-01 17:37:51 -0700202 Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
203 Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
204
205 Ptr<security::IdentityCertificate> kskCert;
206 if(signingKeyName.get(-1).toUri().substr(0,4) == string("dsk-"))
207 {
208 Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
209 // HACK! KSK certificate should be retrieved from network.
210 _LOG_DEBUG("keyLocator: " << dskCertSig->getKeyLocator().getKeyName());
211 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
212 _LOG_DEBUG("keyName: " << keyName.toUri());
213 Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
214 _LOG_DEBUG("ksk cert name: " << kskCertName);
215 kskCert = identityManager->getCertificate(kskCertName);
216
217 }
218 else
219 {
220 kskCert = signingCert;
221 _LOG_DEBUG("ksk cert name: " << kskCert->getName().toUri());
222 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700223
224 vector<string> endorseList;
225 Profile::const_iterator it = profile.begin();
226 for(; it != profile.end(); it++)
227 endorseList.push_back(it->first);
228
229 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
230 kskCert->getNotBefore(),
231 kskCert->getNotAfter(),
232 profileData,
233 endorseList));
234 identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
235
236 return selfEndorseCertificate;
237}
238
239
240void
241ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
242{
Yingdi Yuc29fb982013-10-20 19:43:10 -0700243 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700244
Yingdi Yuc29fb982013-10-20 19:43:10 -0700245 Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob);
246
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700247 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
Yingdi Yuc29fb982013-10-20 19:43:10 -0700248
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700249 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700250
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700251 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yuc29fb982013-10-20 19:43:10 -0700252 {
Yingdi Yu79c25a22013-10-21 13:38:38 -0700253 // Profile profile = selfEndorseCertificate->getProfileData()->getProfile();
254 // Profile::const_iterator it = profile.getEntries().begin();
255 // it++;
256 // _LOG_DEBUG("Entry Size: " << it->first);
257
Yingdi Yuc29fb982013-10-20 19:43:10 -0700258 emit contactFetched (*selfEndorseCertificate);
259 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700260 else
Yingdi Yuc29fb982013-10-20 19:43:10 -0700261 {
262 emit contactFetchFailed (identity);
263 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700264}
265
266void
267ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
268{ emit contactFetchFailed (identity); }
269
270void
271ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
272{
273 if(retry > 0)
274 {
275 Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
276 boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout,
277 this,
278 _1,
279 _2,
280 identity,
281 retry - 1),
282 closure->m_unverifiedCallback,
283 closure->m_stepCount)
284 );
285 m_wrapper->sendInterest(interest, newClosure);
286 }
287 else
288 emit contactFetchFailed(identity);
289}
290
291void
292ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate)
293{
294 Ptr<Data> data = Ptr<Data>::Create();
295
296 Name keyName = selfEndorseCertificate->getPublicKeyName();
297 Name identity = keyName.getSubName(0, keyName.size()-1);
298
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700299
300 Name dnsName = identity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700301 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700302
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700303 data->setName(dnsName);
304 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700305
306 // string encoded;
307 // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(blob->buf()), blob->size(), true,
308 // new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), false));
309
310 // Content content(encoded.c_str(), encoded.size());
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700311 Content content(blob->buf(), blob->size());
312 data->setContent(content);
313
314 m_keychain->signByIdentity(*data, identity);
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700315
316 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700317
318 Ptr<Blob> dnsBlob = data->encodeToWire();
319
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700320 m_wrapper->putToNdnd(*dnsBlob);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700321}
322
323
324#if WAF
325#include "contact-manager.moc"
326#include "contact-manager.cpp.moc"
327#endif