blob: 0f4db3e35d6482e1a37180ad5f88ea44114fbbf9 [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -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
Yingdi Yu43e71612013-10-30 22:19:31 -070011#include "sync-intro-certificate.h"
12#include "sync-logging.h"
Yingdi Yue8154712014-01-21 10:20:14 -080013#include <ndn-cpp-dev/security/verifier.hpp>
14#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080015
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080016#include "sec-policy-sync.h"
Yingdi Yu43e71612013-10-30 22:19:31 -070017
18using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080019using namespace ndn::ptr_lib;
Yingdi Yu43e71612013-10-30 22:19:31 -070020using namespace std;
21
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080022INIT_LOGGER("SecPolicySync");
Yingdi Yu43e71612013-10-30 22:19:31 -070023
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080024SecPolicySync::SecPolicySync(const Name& signingIdentity,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080025 const Name& signingCertificateName,
26 const Name& syncPrefix,
27 shared_ptr<Face> face,
28 int stepLimit)
Yingdi Yu43e71612013-10-30 22:19:31 -070029 : m_signingIdentity(signingIdentity)
30 , m_signingCertificateName(signingCertificateName.getPrefix(signingCertificateName.size()-1))
31 , m_syncPrefix(syncPrefix)
32 , m_stepLimit(stepLimit)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080033 , m_keyChain(new KeyChain())
34{
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080035 m_introCertPrefix = syncPrefix;
36 m_introCertPrefix.append("WOT");
37
38 m_syncDataPolicy = make_shared<SecRuleRelative>("^[^<%F0\\.>]*<%F0\\.>([^<chronos>]*)<chronos><>",
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080039 "^([^<KEY>]*)<KEY>(<>*)[<dsk-.*><ksk-.*>]<ID-CERT>$",
40 "==", "\\1", "\\1", true);
Yingdi Yu43e71612013-10-30 22:19:31 -070041}
42
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080043SecPolicySync::~SecPolicySync()
Yingdi Yu43e71612013-10-30 22:19:31 -070044{}
45
46bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080047SecPolicySync::skipVerifyAndTrust (const Data& data)
Yingdi Yu43e71612013-10-30 22:19:31 -070048{ return false; }
49
50bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080051SecPolicySync::requireVerify (const Data& data)
Yingdi Yu43e71612013-10-30 22:19:31 -070052{ return true; }
53
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080054shared_ptr<ValidationRequest>
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080055SecPolicySync::checkVerificationPolicy(const shared_ptr<Data>& data,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080056 int stepCount,
57 const OnVerified& onVerified,
58 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -070059{
Yingdi Yu43e71612013-10-30 22:19:31 -070060 if(stepCount > m_stepLimit)
61 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080062 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080063 return shared_ptr<ValidationRequest>();
Yingdi Yu43e71612013-10-30 22:19:31 -070064 }
65
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080066 try{
67 SignatureSha256WithRsa sig(data->getSignature());
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080068 const Name& keyLocatorName = sig.getKeyLocator().getName();
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080069
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080070 // if data is intro cert
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080071 if(m_introCertPrefix.isPrefixOf(data->getName()))
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080072 {
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080073 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080074 map<Name, PublicKey>::const_iterator it = m_trustedIntroducers.find(keyName);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080075 if(m_trustedIntroducers.end() != it)
76 {
77 if(Verifier::verifySignature(*data, sig, it->second))
78 onVerified(data);
79 else
80 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080081 return shared_ptr<ValidationRequest>();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080082 }
83 else
84 return prepareRequest(keyName, true, data, stepCount, onVerified, onVerifyFailed);
85 }
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080086
87 // if data is diff data or sync data
88 if(m_syncPrefix.isPrefixOf(data->getName()) || m_syncDataPolicy->satisfy(*data))
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080089 {
90 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
Yingdi Yu43e71612013-10-30 22:19:31 -070091
Yingdi Yu57fdb5f2014-01-23 22:33:43 -080092 map<Name, PublicKey>::const_iterator it = m_trustedIntroducers.find(keyName);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080093 if(m_trustedIntroducers.end() != it)
94 {
95 if(Verifier::verifySignature(*data, sig, it->second))
96 onVerified(data);
97 else
98 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080099 return shared_ptr<ValidationRequest>();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800100 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700101
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800102 it = m_trustedProducers.find(keyName);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800103 if(m_trustedProducers.end() != it)
104 {
105 if(Verifier::verifySignature(*data, sig, it->second))
106 onVerified(data);
107 else
108 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800109 return shared_ptr<ValidationRequest>();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800110 }
Yingdi Yu0b3bd482013-11-01 16:11:20 -0700111
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800112 return prepareRequest(keyName, false, data, stepCount, onVerified, onVerifyFailed);
113 }
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800114
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800115 }catch(SignatureSha256WithRsa::Error &e){
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800116 _LOG_DEBUG("SecPolicySync Error: " << e.what());
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800117 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800118 return shared_ptr<ValidationRequest>();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800119 }catch(KeyLocator::Error &e){
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800120 _LOG_DEBUG("SecPolicySync Error: " << e.what());
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800121 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800122 return shared_ptr<ValidationRequest>();
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800123 }
124
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800125 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800126 return shared_ptr<ValidationRequest>();
Yingdi Yu43e71612013-10-30 22:19:31 -0700127}
128
129bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800130SecPolicySync::checkSigningPolicy(const Name& dataName,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800131 const Name& certificateName)
Yingdi Yu43e71612013-10-30 22:19:31 -0700132{
Yingdi Yu43e71612013-10-30 22:19:31 -0700133 return true;
Yingdi Yu43e71612013-10-30 22:19:31 -0700134}
135
136Name
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800137SecPolicySync::inferSigningIdentity(const ndn::Name& dataName)
Yingdi Yu43e71612013-10-30 22:19:31 -0700138{ return m_signingIdentity; }
139
140void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800141SecPolicySync::addTrustAnchor(const IdentityCertificate& identityCertificate, bool isIntroducer)
Yingdi Yu43e71612013-10-30 22:19:31 -0700142{
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800143 Name publicKeyName = identityCertificate.getPublicKeyName();
144
145 _LOG_DEBUG("Add intro/producer: " << publicKeyName);
146
Yingdi Yu43e71612013-10-30 22:19:31 -0700147 if(isIntroducer)
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800148 m_trustedIntroducers[publicKeyName] = identityCertificate.getPublicKeyInfo();
Yingdi Yu43e71612013-10-30 22:19:31 -0700149 else
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800150 m_trustedProducers[publicKeyName] = identityCertificate.getPublicKeyInfo();
Yingdi Yu43e71612013-10-30 22:19:31 -0700151}
152
153void
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800154SecPolicySync::addSyncDataRule(const Name& prefix,
155 const IdentityCertificate& identityCertificate,
156 bool isIntroducer)
157{ addTrustAnchor(identityCertificate, isIntroducer); }
Yingdi Yu43e71612013-10-30 22:19:31 -0700158
159
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800160shared_ptr<const vector<Name> >
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800161SecPolicySync::getAllIntroducerName()
Yingdi Yu43e71612013-10-30 22:19:31 -0700162{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800163 shared_ptr<vector<Name> > nameList = make_shared<vector<Name> >();
Yingdi Yu43e71612013-10-30 22:19:31 -0700164
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800165 map<Name, PublicKey>::iterator it = m_trustedIntroducers.begin();
Yingdi Yu43e71612013-10-30 22:19:31 -0700166 for(; it != m_trustedIntroducers.end(); it++)
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800167 nameList->push_back(it->first);
Yingdi Yu43e71612013-10-30 22:19:31 -0700168
169 return nameList;
170}
171
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800172shared_ptr<ValidationRequest>
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800173SecPolicySync::prepareRequest(const Name& keyName,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800174 bool forIntroducer,
175 shared_ptr<Data> data,
176 const int & stepCount,
177 const OnVerified& onVerified,
178 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700179{
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800180 Name interestPrefix = m_syncPrefix;
181 interestPrefix.append("WOT").append(keyName.wireEncode()).append("INTRO-CERT");
Yingdi Yu43e71612013-10-30 22:19:31 -0700182
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800183 shared_ptr<const vector<Name> > nameList = getAllIntroducerName();
Yingdi Yu0b3bd482013-11-01 16:11:20 -0700184 if(0 == nameList->size())
185 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800186 onVerifyFailed(data);
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800187 return shared_ptr<ValidationRequest>();
Yingdi Yu0b3bd482013-11-01 16:11:20 -0700188 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700189
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800190 Name interestName = interestPrefix;
191 interestName.append(nameList->at(0).wireEncode());
Yingdi Yu43e71612013-10-30 22:19:31 -0700192
193 if(forIntroducer)
194 interestName.append("INTRODUCER");
195
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800196 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700197
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800198 OnVerified introCertVerified = func_lib::bind(&SecPolicySync::onIntroCertVerified,
199 this,
200 _1,
201 forIntroducer,
202 data,
203 onVerified,
204 onVerifyFailed);
Yingdi Yu43e71612013-10-30 22:19:31 -0700205
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800206 OnVerifyFailed introCertVerifyFailed = func_lib::bind(&SecPolicySync::onIntroCertVerifyFailed,
207 this,
208 _1,
209 interestPrefix,
210 forIntroducer,
211 nameList,
212 1,
213 data,
214 onVerified,
215 onVerifyFailed);
Yingdi Yu43e71612013-10-30 22:19:31 -0700216
217
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800218 shared_ptr<ValidationRequest> nextStep = make_shared<ValidationRequest>(interest,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800219 introCertVerified,
220 introCertVerifyFailed,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800221 1,
222 m_stepLimit-1);
Yingdi Yu43e71612013-10-30 22:19:31 -0700223 return nextStep;
224}
225
226void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800227SecPolicySync::OnIntroCertInterest(const shared_ptr<const Name>& prefix,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800228 const shared_ptr<const ndn::Interest>& interest,
229 Transport& transport,
230 uint64_t registeredPrefixId)
Yingdi Yu43e71612013-10-30 22:19:31 -0700231{
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800232 map<Name, Data>::const_iterator it = m_introCert.find(*prefix);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800233
234 if(m_introCert.end() != it)
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800235 m_face->put(it->second);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800236}
237
238void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800239SecPolicySync::OnIntroCertRegisterFailed(const shared_ptr<const Name>& prefix)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800240{
241}
242
243void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800244SecPolicySync::onIntroCertVerified(const shared_ptr<Data>& introCertificateData,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800245 bool forIntroducer,
246 shared_ptr<Data> originalData,
247 const OnVerified& onVerified,
248 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800249{
250 shared_ptr<SyncIntroCertificate> introCertificate = make_shared<SyncIntroCertificate>(*introCertificateData);
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800251 Name subjectKeyName = introCertificate->getPublicKeyName();
252
Yingdi Yu43e71612013-10-30 22:19:31 -0700253 if(forIntroducer)
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800254 {
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800255 //Add the intro cert subject as trusted introducer.
256 m_trustedIntroducers[subjectKeyName] = introCertificate->getPublicKeyInfo();
257
258 //Generate another intro cert for the cert subject.
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800259 SyncIntroCertificate syncIntroCertificate(m_syncPrefix,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800260 subjectKeyName,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800261 m_keyChain->getDefaultKeyNameForIdentity(m_signingIdentity),
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800262 introCertificate->getNotBefore(),
263 introCertificate->getNotAfter(),
264 introCertificate->getPublicKeyInfo(),
265 SyncIntroCertificate::INTRODUCER);
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800266 m_keyChain->signByIdentity(syncIntroCertificate, m_signingIdentity);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800267 m_face->put(syncIntroCertificate);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800268
269 // Name prefix = syncIntroCertificate.getName().getPrefix(syncIntroCertificate.getName().size()-1);
270
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800271 // map<string, Data>::const_iterator it = m_introCert.find(prefix);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800272 // if(m_introCert.end() != it)
273 // {
274 // it->second = syncIntroCertificate;
275 // }
276 // else
277 // {
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800278 // m_introCert.insert(pair <Name, Data> (prefix, syncIntroCertificate));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800279 // m_face->registerPrefix(prefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800280 // boost::bind(&SecPolicySync::onIntroCertInterest, this, _1, _2, _3, _4),
281 // boost::bind(&SecPolicySync::onIntroCertRegisterFailed, this, _1));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800282 // }
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800283 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700284 else
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800285 {
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800286 //Add the intro cert subject as trusted producer.
287 m_trustedProducers[subjectKeyName] = introCertificate->getPublicKeyInfo();
288
289 //Generate another intro cert for the cert subject.
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800290 SyncIntroCertificate syncIntroCertificate(m_syncPrefix,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800291 subjectKeyName,
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800292 m_keyChain->getDefaultKeyNameForIdentity(m_signingIdentity),
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800293 introCertificate->getNotBefore(),
294 introCertificate->getNotAfter(),
295 introCertificate->getPublicKeyInfo(),
296 SyncIntroCertificate::PRODUCER);
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800297 m_keyChain->signByIdentity(syncIntroCertificate, m_signingIdentity);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800298 m_face->put(syncIntroCertificate);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800299
300 // Name prefix = syncIntroCertificate.getName().getPrefix(syncIntroCertificate.getName().size()-1);
301
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800302 // map<string, Data>::const_iterator it = m_introCert.find(prefix);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800303 // if(m_introCert.end() != it)
304 // {
305 // it->second = syncIntroCertificate;
306 // }
307 // else
308 // {
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800309 // m_introCert.insert(pair <Name, Data> (prefix, syncIntroCertificate));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800310 // m_face->registerPrefix(prefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800311 // boost::bind(&SecPolicySync::onIntroCertInterest, this, _1, _2, _3, _4),
312 // boost::bind(&SecPolicySync::onIntroCertRegisterFailed, this, _1));
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800313 // }
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800314 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700315
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800316 try{
317 SignatureSha256WithRsa sig(originalData->getSignature());
318 if(Verifier::verifySignature(*originalData, sig, introCertificate->getPublicKeyInfo()))
319 onVerified(originalData);
320 else
321 onVerifyFailed(originalData);
322 }catch(SignatureSha256WithRsa::Error &e){
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800323 onVerifyFailed(originalData);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800324 }catch(KeyLocator::Error &e){
325 onVerifyFailed(originalData);
326 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700327}
328
329void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800330SecPolicySync::onIntroCertVerifyFailed(const shared_ptr<Data>& introCertificateData,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800331 Name interestPrefix,
332 bool forIntroducer,
333 shared_ptr<const vector<Name> > introNameList,
334 int nextIntroducerIndex,
335 shared_ptr<Data> originalData,
336 const OnVerified& onVerified,
337 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700338{
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800339 Name interestName = interestPrefix;
Yingdi Yu43e71612013-10-30 22:19:31 -0700340 if(nextIntroducerIndex < introNameList->size())
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800341 interestName.append(introNameList->at(nextIntroducerIndex).wireEncode());
Yingdi Yu43e71612013-10-30 22:19:31 -0700342 else
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800343 onVerifyFailed(originalData);
Yingdi Yu43e71612013-10-30 22:19:31 -0700344
345 if(forIntroducer)
346 interestName.append("INTRODUCER");
347
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800348 ndn::Interest interest(interestName);
Yingdi Yu43e71612013-10-30 22:19:31 -0700349
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800350 OnVerified introCertVerified = func_lib::bind(&SecPolicySync::onIntroCertVerified,
351 this,
352 _1,
353 forIntroducer,
354 originalData,
355 onVerified,
356 onVerifyFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800357
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800358 OnVerifyFailed introCertVerifyFailed = func_lib::bind(&SecPolicySync::onIntroCertVerifyFailed,
359 this,
360 _1,
361 interestPrefix,
362 forIntroducer,
363 introNameList,
364 nextIntroducerIndex + 1,
365 originalData,
366 onVerified,
367 onVerifyFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800368
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800369 m_face->expressInterest(interest,
370 func_lib::bind(&SecPolicySync::onIntroCertData,
371 this,
372 _1,
373 _2,
374 m_stepLimit-1,
375 introCertVerified,
376 introCertVerifyFailed),
377 func_lib::bind(&SecPolicySync::onIntroCertTimeout,
378 this,
379 _1,
380 1,
381 m_stepLimit-1,
382 introCertVerified,
383 introCertVerifyFailed)
384 );
Yingdi Yu43e71612013-10-30 22:19:31 -0700385}
386
387void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800388SecPolicySync::onIntroCertData(const shared_ptr<const ndn::Interest> &interest,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800389 const shared_ptr<Data>& introCertificateData,
390 int stepCount,
391 const OnVerified& introCertVerified,
392 const OnVerifyFailed& introCertVerifyFailed)
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800393{
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800394 shared_ptr<ValidationRequest> nextStep = checkVerificationPolicy(introCertificateData, stepCount, introCertVerified, introCertVerifyFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800395 if (nextStep)
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800396 m_face->expressInterest(*nextStep->interest_,
397 func_lib::bind(&SecPolicySync::onIntroCertData,
398 this,
399 _1,
400 _2,
401 nextStep->stepCount_,
402 nextStep->onVerified_,
403 nextStep->onVerifyFailed_),
404 func_lib::bind(&SecPolicySync::onIntroCertTimeout,
405 this,
406 _1,
407 nextStep->retry_,
408 nextStep->stepCount_,
409 nextStep->onVerified_,
410 nextStep->onVerifyFailed_)
411 );
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800412}
413
414void
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800415SecPolicySync::onIntroCertTimeout(const shared_ptr<const ndn::Interest>& interest,
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800416 int retry,
417 int stepCount,
418 const OnVerified& introCertVerified,
419 const OnVerifyFailed& introCertVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700420{
421 if(retry > 0)
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800422 m_face->expressInterest(*interest,
423 func_lib::bind(&SecPolicySync::onIntroCertData,
424 this,
425 _1,
426 _2,
427 stepCount,
428 introCertVerified,
429 introCertVerifyFailed),
430 func_lib::bind(&SecPolicySync::onIntroCertTimeout,
431 this,
432 _1,
433 retry - 1,
434 stepCount,
435 introCertVerified,
436 introCertVerifyFailed)
437 );
Yingdi Yu43e71612013-10-30 22:19:31 -0700438 else
Yingdi Yu57fdb5f2014-01-23 22:33:43 -0800439 introCertVerifyFailed(shared_ptr<Data>());
Yingdi Yu43e71612013-10-30 22:19:31 -0700440}