blob: 02dfaa7119b194a0b53f5ed19da24809e59557f1 [file] [log] [blame]
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2acdcb2015-04-29 11:14:35 -05003 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070020 **/
21
22#ifndef NLSR_VALIDATOR_HPP
23#define NLSR_VALIDATOR_HPP
24
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050025#include "common.hpp"
26#include "security/certificate-store.hpp"
27
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070028#include <ndn-cxx/security/validator-config.hpp>
29
30namespace nlsr {
31
32class Validator : public ndn::ValidatorConfig
33{
34public:
35 class Error : public ndn::ValidatorConfig::Error
36 {
37 public:
38 explicit
39 Error(const std::string& what)
40 : ndn::ValidatorConfig::Error(what)
41 {
42 }
43 };
44
45 explicit
46 Validator(ndn::Face& face,
47 const ndn::Name broadcastPrefix,
48 const ndn::shared_ptr<ndn::CertificateCache>& cache,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050049 security::CertificateStore& certStore,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070050 const int stepLimit = 10)
Yingdi Yue0248032014-06-19 14:06:13 -070051 : ndn::ValidatorConfig(face, cache, ndn::ValidatorConfig::DEFAULT_GRACE_INTERVAL, stepLimit)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070052 , m_broadcastPrefix(broadcastPrefix)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050053 , m_certStore(certStore)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070054 {
55 m_broadcastPrefix.append("KEYS");
56 }
57
58 virtual
59 ~Validator()
60 {
61 }
62
63 const ndn::Name&
64 getBroadcastPrefix()
65 {
66 return m_broadcastPrefix;
67 }
68
69 void
70 setBroadcastPrefix(const ndn::Name& broadcastPrefix)
71 {
72 m_broadcastPrefix = broadcastPrefix;
73 }
74
75protected:
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050076 typedef std::vector<ndn::shared_ptr<ndn::ValidationRequest>> NextSteps;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070077
78 virtual void
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050079 afterCheckPolicy(const NextSteps& nextSteps,
80 const OnFailure& onFailure)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070081 {
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050082 if (m_face == nullptr) {
83 onFailure("Require more information to validate the packet!");
84 return;
85 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070086
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050087 for (const shared_ptr<ndn::ValidationRequest>& request : nextSteps) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070088
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050089 ndn::Interest& interest = request->m_interest;
90
91 // Look for certificate in permanent storage
92 shared_ptr<const ndn::IdentityCertificate> cert = m_certStore.find(interest.getName());
93
94 if (cert != nullptr) {
95 // If the certificate is found, no reason to express interest
96 shared_ptr<ndn::Data> data = make_shared<ndn::Data>(interest.getName());
97 data->setContent(cert->wireEncode());
98
99 Validator::onData(interest, *data, request);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700100 }
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500101 else {
102 // Prepend broadcast prefix to interest name
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700103 ndn::Name broadcastName = m_broadcastPrefix;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500104 broadcastName.append(interest.getName());
105 interest.setName(broadcastName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700106
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500107 // Attempt to fetch the certificate
108 m_face->expressInterest(interest,
109 bind(&Validator::onData, this, _1, _2, request),
110 bind(&Validator::onTimeout,
111 this, _1, request->m_nRetries,
112 onFailure,
113 request));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700114 }
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500115 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700116 }
117
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700118 virtual ndn::shared_ptr<const ndn::Data>
119 preCertificateValidation(const ndn::Data& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700120 {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700121 ndn::shared_ptr<ndn::Data> internalData = ndn::make_shared<ndn::Data>();
122 internalData->wireDecode(data.getContent().blockFromValue());
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700123 return internalData;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700124 }
125
126private:
127 ndn::Name m_broadcastPrefix;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500128 security::CertificateStore& m_certStore;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700129};
130
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700131} // namespace nlsr
132
133#endif // NLSR_VALIDATOR_HPP