blob: 2f7536b67b901d0058d523b2a84f981874e70885 [file] [log] [blame]
Yingdi Yu9236c432013-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 Yu4685b1b2013-10-18 17:05:02 -070013#ifndef Q_MOC_RUN
Yingdi Yu9236c432013-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 Yu9236c432013-10-18 11:29:25 -070022#include <fstream>
Yingdi Yuec3d9a32013-10-18 18:35:09 -070023#include "logging.h"
Yingdi Yu4685b1b2013-10-18 17:05:02 -070024#endif
Yingdi Yu9236c432013-10-18 11:29:25 -070025
26using namespace ndn;
27using namespace ndn::security;
28
Yingdi Yuec3d9a32013-10-18 18:35:09 -070029INIT_LOGGER("ContactManager");
30
Yingdi Yu4685b1b2013-10-18 17:05:02 -070031ContactManager::ContactManager(Ptr<ContactStorage> contactStorage,
Yingdi Yuec3d9a32013-10-18 18:35:09 -070032 Ptr<DnsStorage> dnsStorage,
33 QObject* parent)
34 : QObject(parent)
35 , m_contactStorage(contactStorage)
Yingdi Yu4685b1b2013-10-18 17:05:02 -070036 , m_dnsStorage(dnsStorage)
Yingdi Yu9236c432013-10-18 11:29:25 -070037{
Yingdi Yu4685b1b2013-10-18 17:05:02 -070038 setKeychain();
39
40 m_wrapper = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu9236c432013-10-18 11:29:25 -070041}
42
43ContactManager::~ContactManager()
44{
45}
46
Yingdi Yu4685b1b2013-10-18 17:05:02 -070047void
Yingdi Yu9236c432013-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 Yu4685b1b2013-10-18 17:05:02 -070057 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
58 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
59 "==", "\\1", "\\1\\2", true)));
Yingdi Yu9236c432013-10-18 11:29:25 -070060 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
Yingdi Yu7989eb22013-10-31 17:38:22 -070061 "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>",
Yingdi Yu4685b1b2013-10-18 17:05:02 -070062 "==", "\\1", "\\1\\2", true)));
Yingdi Yu7989eb22013-10-31 17:38:22 -070063 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
64 "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
Yingdi Yu4685b1b2013-10-18 17:05:02 -070065 ">", "\\1\\2", "\\1", true)));
Yingdi Yu7989eb22013-10-31 17:38:22 -070066 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
67 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
Yingdi Yu4685b1b2013-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 Yu9236c432013-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 Yu4685b1b2013-10-18 17:05:02 -070089 m_keychain = keychain;
Yingdi Yu9236c432013-10-18 11:29:25 -070090}
Yingdi Yu4685b1b2013-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 Yu7989eb22013-10-31 17:38:22 -0700100 interestPtr->setChildSelector(Interest::CHILD_RIGHT);
Yingdi Yu4685b1b2013-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{
121 // Get current profile;
122 Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
123 if(NULL == newProfile)
124 return;
125 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
126
127 // Check if profile exists
128 Ptr<Blob> profileDataBlob = m_contactStorage->getSelfEndorseCertificate(identity);
129 if(NULL != profileDataBlob)
130 {
131 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
132 EndorseCertificate oldEndorseCertificate(*plainData);
133 // _LOG_DEBUG("Certificate converted!");
134 const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content();
135
136 if(oldProfileBlob == *newProfileBlob)
137 return;
138
139 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
140 // _LOG_DEBUG("Signing DONE!");
141 if(NULL == newEndorseCertificate)
142 return;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700143 _LOG_DEBUG("About to update");
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700144 m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
145
146 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
147 }
148 else
149 {
150 Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
151 // _LOG_DEBUG("Signing DONE!");
152 if(NULL == newEndorseCertificate)
153 return;
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700154 _LOG_DEBUG("About to Insert");
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700155 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
156
157 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
158 }
159}
160
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700161vector<Ptr<ContactItem> >
162ContactManager::getContactItemList()
163{
164 vector<Ptr<ContactItem> > result;
165
166 vector<Ptr<ContactItem> > ncList = m_contactStorage->getAllNormalContacts();
167 vector<Ptr<TrustedContact> > tcList = m_contactStorage->getAllTrustedContacts();
168
169 result.insert(result.end(), tcList.begin(), tcList.end());
170 result.insert(result.end(), ncList.begin(), ncList.end());
171
172 return result;
173}
174
Yingdi Yu4ef8cf62013-10-23 14:05:12 -0700175Ptr<ContactItem>
176ContactManager::getContact(const ndn::Name& contactNamespace)
177{
178 Ptr<ContactItem> contactItem = m_contactStorage->getNormalContact(contactNamespace);
179 if(NULL != contactItem)
180 return contactItem;
181
182 contactItem = m_contactStorage->getTrustedContact(contactNamespace);
183 if(NULL != contactItem)
184 return contactItem;
185
186 return NULL;
187}
188
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700189Ptr<EndorseCertificate>
190ContactManager::getSignedSelfEndorseCertificate(const Name& identity,
191 const Profile& profile)
192{
193 Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
194 Name certificateName = identityManager->getDefaultCertificateNameByIdentity(identity);
195 if(0 == certificateName.size())
196 return NULL;
197
198 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile));
199 identityManager->signByCertificate(*profileData, certificateName);
200
201 Ptr<security::IdentityCertificate> dskCert = identityManager->getCertificate(certificateName);
202 Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(dskCert->getSignature());
203 // HACK! KSK certificate should be retrieved from network.
Yingdi Yu7989eb22013-10-31 17:38:22 -0700204 _LOG_DEBUG("keyLocator: " << dskCertSig->getKeyLocator().getKeyName());
205 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
206 _LOG_DEBUG("keyName: " << keyName.toUri());
207 Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
208 _LOG_DEBUG("ksk cert name: " << kskCertName);
209 Ptr<security::IdentityCertificate> kskCert = identityManager->getCertificate(kskCertName);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700210
211 vector<string> endorseList;
212 Profile::const_iterator it = profile.begin();
213 for(; it != profile.end(); it++)
214 endorseList.push_back(it->first);
215
216 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
217 kskCert->getNotBefore(),
218 kskCert->getNotAfter(),
219 profileData,
220 endorseList));
221 identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
222
223 return selfEndorseCertificate;
224}
225
226
227void
228ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
229{
Yingdi Yud95c5642013-10-20 19:43:10 -0700230 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700231
Yingdi Yud95c5642013-10-20 19:43:10 -0700232 Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob);
233
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700234 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
Yingdi Yud95c5642013-10-20 19:43:10 -0700235
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700236 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yud95c5642013-10-20 19:43:10 -0700237
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700238 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yud95c5642013-10-20 19:43:10 -0700239 {
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700240 // Profile profile = selfEndorseCertificate->getProfileData()->getProfile();
241 // Profile::const_iterator it = profile.getEntries().begin();
242 // it++;
243 // _LOG_DEBUG("Entry Size: " << it->first);
244
Yingdi Yud95c5642013-10-20 19:43:10 -0700245 emit contactFetched (*selfEndorseCertificate);
246 }
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700247 else
Yingdi Yud95c5642013-10-20 19:43:10 -0700248 {
249 emit contactFetchFailed (identity);
250 }
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700251}
252
253void
254ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
255{ emit contactFetchFailed (identity); }
256
257void
258ContactManager::onDnsSelfEndorseCertificateTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
259{
260 if(retry > 0)
261 {
262 Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
263 boost::bind(&ContactManager::onDnsSelfEndorseCertificateTimeout,
264 this,
265 _1,
266 _2,
267 identity,
268 retry - 1),
269 closure->m_unverifiedCallback,
270 closure->m_stepCount)
271 );
272 m_wrapper->sendInterest(interest, newClosure);
273 }
274 else
275 emit contactFetchFailed(identity);
276}
277
278void
279ContactManager::publishSelfEndorseCertificateInDNS(Ptr<EndorseCertificate> selfEndorseCertificate)
280{
281 Ptr<Data> data = Ptr<Data>::Create();
282
283 Name keyName = selfEndorseCertificate->getPublicKeyName();
284 Name identity = keyName.getSubName(0, keyName.size()-1);
285
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700286
287 Name dnsName = identity;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700288 dnsName.append("DNS").append("PROFILE").appendVersion();
Yingdi Yud95c5642013-10-20 19:43:10 -0700289
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700290 data->setName(dnsName);
291 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yud95c5642013-10-20 19:43:10 -0700292
293 // string encoded;
294 // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(blob->buf()), blob->size(), true,
295 // new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), false));
296
297 // Content content(encoded.c_str(), encoded.size());
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700298 Content content(blob->buf(), blob->size());
299 data->setContent(content);
300
301 m_keychain->signByIdentity(*data, identity);
Yingdi Yuec3d9a32013-10-18 18:35:09 -0700302
303 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700304
305 Ptr<Blob> dnsBlob = data->encodeToWire();
306
Yingdi Yueda39aa2013-10-23 23:07:29 -0700307 m_wrapper->putToNdnd(*dnsBlob);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700308}
309
310
311#if WAF
312#include "contact-manager.moc"
313#include "contact-manager.cpp.moc"
314#endif