blob: 01168a02987495928e556228f2f4f5d9de3d949b [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>",
58 "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
59 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070060 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
61 "^([^<KEY>]*)<KEY>(<>*<KSK-.*>)<ID-CERT>",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070062 "==", "\\1", "\\1\\2", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070063 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>",
64 "^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>",
Yingdi Yuaa8d7692013-10-18 17:05:02 -070065 ">", "\\1\\2", "\\1", true)));
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070066 policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>",
67 "^([^<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));
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
117void
118ContactManager::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 Yu590fa5d2013-10-18 18:35:09 -0700142 _LOG_DEBUG("About to update");
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700143 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 Yu590fa5d2013-10-18 18:35:09 -0700153 _LOG_DEBUG("About to Insert");
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700154 m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
155
156 publishSelfEndorseCertificateInDNS(newEndorseCertificate);
157 }
158}
159
160Ptr<EndorseCertificate>
161ContactManager::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
193void
194ContactManager::onDnsSelfEndorseCertificateVerified(Ptr<Data> data, const Name& identity)
195{
Yingdi Yuc29fb982013-10-20 19:43:10 -0700196 Ptr<Blob> dataContentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700197
Yingdi Yuc29fb982013-10-20 19:43:10 -0700198 Ptr<Data> plainData = Data::decodeFromWire(dataContentBlob);
199
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700200 Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*plainData));
Yingdi Yuc29fb982013-10-20 19:43:10 -0700201
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700202 const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700203
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700204 if(security::PolicyManager::verifySignature(*plainData, ksk))
Yingdi Yuc29fb982013-10-20 19:43:10 -0700205 {
206 emit contactFetched (*selfEndorseCertificate);
207 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700208 else
Yingdi Yuc29fb982013-10-20 19:43:10 -0700209 {
210 emit contactFetchFailed (identity);
211 }
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700212}
213
214void
215ContactManager::onDnsSelfEndorseCertificateUnverified(Ptr<Data> data, const Name& identity)
216{ emit contactFetchFailed (identity); }
217
218void
219ContactManager::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
239void
240ContactManager::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 Yuc29fb982013-10-20 19:43:10 -0700253
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700254 data->setName(dnsName);
255 Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700256
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 Yuaa8d7692013-10-18 17:05:02 -0700262 Content content(blob->buf(), blob->size());
263 data->setContent(content);
264
265 m_keychain->signByIdentity(*data, identity);
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700266
267 m_dnsStorage->updateDnsSelfProfileData(*data, identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700268
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