blob: b23a47c9c41fd8cd3d835f5cfd2c67984da4c63e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu48e8c0c2014-03-19 12:01:55 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070022 */
23
24#include "validator-config.hpp"
25#include "certificate-cache-ttl.hpp"
26#include "../util/io.hpp"
27
28#include <boost/filesystem.hpp>
29#include <boost/property_tree/info_parser.hpp>
30#include <boost/algorithm/string.hpp>
31
32namespace ndn {
33
34const shared_ptr<CertificateCache> ValidatorConfig::DEFAULT_CERTIFICATE_CACHE;
35
Yingdi Yu96e64062014-04-15 19:57:33 -070036ValidatorConfig::ValidatorConfig(Face& face,
37 const shared_ptr<CertificateCache>& certificateCache,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070038 const int stepLimit)
39 : Validator(face)
Yingdi Yu44d190c2014-04-16 17:05:46 -070040 , m_shouldValidate(true)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070041 , m_stepLimit(stepLimit)
42 , m_certificateCache(certificateCache)
43{
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070044 if (!static_cast<bool>(m_certificateCache))
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070045 m_certificateCache = make_shared<CertificateCacheTtl>(ref(m_face.getIoService()));
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070046}
47
48void
49ValidatorConfig::load(const std::string& filename)
50{
51 std::ifstream inputFile;
52 inputFile.open(filename.c_str());
53 if (!inputFile.good() || !inputFile.is_open())
54 {
55 std::string msg = "Failed to read configuration file: ";
56 msg += filename;
57 throw security::conf::Error(msg);
58 }
59 load(inputFile, filename);
60 inputFile.close();
61}
62
63void
64ValidatorConfig::load(const std::string& input, const std::string& filename)
65{
66 std::istringstream inputStream(input);
67 load(inputStream, filename);
68}
69
70
71void
72ValidatorConfig::load(std::istream& input, const std::string& filename)
73{
74 security::conf::ConfigSection tree;
75 try
76 {
77 boost::property_tree::read_info(input, tree);
78 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070079 catch (boost::property_tree::info_parser_error& error)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070080 {
81 std::stringstream msg;
82 msg << "Failed to parse configuration file";
83 msg << " " << filename;
84 msg << " " << error.message() << " line " << error.line();
85 throw security::conf::Error(msg.str());
86 }
87
Yingdi Yudfa9d732014-04-09 09:53:01 -070088 load(tree, filename);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070089}
90
91void
Yingdi Yudfa9d732014-04-09 09:53:01 -070092ValidatorConfig::load(const security::conf::ConfigSection& configSection,
93 const std::string& filename)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070094{
95 BOOST_ASSERT(!filename.empty());
96
Yingdi Yu58f33712014-04-16 16:57:47 -070097 reset();
98
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070099 if (configSection.begin() == configSection.end())
100 {
101 std::string msg = "Error processing configuration file";
102 msg += ": ";
103 msg += filename;
104 msg += " no data";
105 throw security::conf::Error(msg);
106 }
107
108 for (security::conf::ConfigSection::const_iterator i = configSection.begin();
109 i != configSection.end(); ++i)
110 {
111 const std::string& sectionName = i->first;
112 const security::conf::ConfigSection& section = i->second;
113
114 if (boost::iequals(sectionName, "rule"))
115 {
116 onConfigRule(section, filename);
117 }
118 else if (boost::iequals(sectionName, "trust-anchor"))
119 {
120 onConfigTrustAnchor(section, filename);
121 }
122 else
123 {
124 std::string msg = "Error processing configuration file";
125 msg += " ";
126 msg += filename;
127 msg += " unrecognized section: " + sectionName;
128 throw security::conf::Error(msg);
129 }
130 }
131}
132
133void
134ValidatorConfig::onConfigRule(const security::conf::ConfigSection& configSection,
135 const std::string& filename)
136{
137 using namespace ndn::security::conf;
138
139 ConfigSection::const_iterator propertyIt = configSection.begin();
140
141 // Get rule.id
142 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "id"))
143 throw Error("Expect <rule.id>!");
144
145 std::string ruleId = propertyIt->second.data();
146 propertyIt++;
147
148 // Get rule.for
149 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,"for"))
150 throw Error("Expect <rule.for> in rule: " + ruleId + "!");
151
152 std::string usage = propertyIt->second.data();
153 propertyIt++;
154
155 bool isForData;
156 if (boost::iequals(usage, "data"))
157 isForData = true;
158 else if (boost::iequals(usage, "interest"))
159 isForData = false;
160 else
161 throw Error("Unrecognized <rule.for>: " + usage
162 + " in rule: " + ruleId);
163
164 // Get rule.filter(s)
165 std::vector<shared_ptr<Filter> > filters;
166 for (; propertyIt != configSection.end(); propertyIt++)
167 {
168 if (!boost::iequals(propertyIt->first, "filter"))
169 {
170 if (boost::iequals(propertyIt->first, "checker"))
171 break;
172 throw Error("Expect <rule.filter> in rule: " + ruleId);
173 }
174
175 filters.push_back(FilterFactory::create(propertyIt->second));
176 continue;
177 }
178
179 // Get rule.checker(s)
180 std::vector<shared_ptr<Checker> > checkers;
181 for (; propertyIt != configSection.end(); propertyIt++)
182 {
183 if (!boost::iequals(propertyIt->first, "checker"))
184 throw Error("Expect <rule.checker> in rule: " + ruleId);
185
186 checkers.push_back(CheckerFactory::create(propertyIt->second, filename));
187 continue;
188 }
189
190 // Check other stuff
191 if (propertyIt != configSection.end())
192 throw Error("Expect the end of rule: " + ruleId);
193
194 if (checkers.size() == 0)
195 throw Error("No <rule.checker> is specified in rule: " + ruleId);
196
197 if (isForData)
198 {
199 shared_ptr<DataRule> rule(new DataRule(ruleId));
200 for (size_t i = 0; i < filters.size(); i++)
201 rule->addFilter(filters[i]);
202 for (size_t i = 0; i < checkers.size(); i++)
203 rule->addChecker(checkers[i]);
204
205 m_dataRules.push_back(rule);
206 }
207 else
208 {
209 shared_ptr<InterestRule> rule(new InterestRule(ruleId));
210 for (size_t i = 0; i < filters.size(); i++)
211 rule->addFilter(filters[i]);
212 for (size_t i = 0; i < checkers.size(); i++)
213 rule->addChecker(checkers[i]);
214
215 m_interestRules.push_back(rule);
216 }
217}
218
219void
220ValidatorConfig::onConfigTrustAnchor(const security::conf::ConfigSection& configSection,
221 const std::string& filename)
222{
223 using namespace ndn::security::conf;
224 using namespace boost::filesystem;
225
226 ConfigSection::const_iterator propertyIt = configSection.begin();
227
228 // Get trust-anchor.type
229 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type"))
230 throw Error("Expect <trust-anchor.type>!");
231
232 std::string type = propertyIt->second.data();
233 propertyIt++;
234
235 if (boost::iequals(type, "file"))
236 {
237 // Get trust-anchor.file
238 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,"file-name"))
239 throw Error("Expect <trust-anchor.file-name>!");
240
241 std::string file = propertyIt->second.data();
242 propertyIt++;
243
244 // Check other stuff
245 if (propertyIt != configSection.end())
246 throw Error("Expect the end of trust-anchor!");
247
248 path certfilePath = absolute(file, path(filename).parent_path());
249 shared_ptr<IdentityCertificate> idCert =
250 io::load<IdentityCertificate>(certfilePath.string());
251
252 if (static_cast<bool>(idCert))
253 {
254 BOOST_ASSERT(idCert->getName().size() >= 1);
255 m_anchors[idCert->getName().getPrefix(-1)] = idCert;
256 }
257 else
258 throw Error("Cannot read certificate from file: " +
259 certfilePath.native());
260
261 return;
262 }
263 else if (boost::iequals(type, "base64"))
264 {
265 // Get trust-anchor.base64-string
266 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "base64-string"))
267 throw Error("Expect <trust-anchor.base64-string>!");
268
269 std::stringstream ss(propertyIt->second.data());
270 propertyIt++;
271
272 // Check other stuff
273 if (propertyIt != configSection.end())
274 throw Error("Expect the end of trust-anchor!");
275
276 shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(ss);
277
278 if (static_cast<bool>(idCert))
279 {
280 BOOST_ASSERT(idCert->getName().size() >= 1);
281 m_anchors[idCert->getName().getPrefix(-1)] = idCert;
282 }
283 else
284 throw Error("Cannot decode certificate from base64-string");
285
286 return;
287 }
Yingdi Yu44d190c2014-04-16 17:05:46 -0700288 else if (boost::iequals(type, "any"))
289 {
290 m_shouldValidate = false;
291 }
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700292 else
293 throw Error("Unsupported trust-anchor.type: " + type);
294}
295
296void
297ValidatorConfig::checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700298 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700299 const OnDataValidated& onValidated,
300 const OnDataValidationFailed& onValidationFailed,
301 std::vector<shared_ptr<ValidationRequest> >& nextSteps)
302{
Yingdi Yu44d190c2014-04-16 17:05:46 -0700303 if (!m_shouldValidate)
304 return onValidated(data.shared_from_this());
305
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700306 if (m_stepLimit == nSteps)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700307 return onValidationFailed(data.shared_from_this(),
308 "Maximum steps of validation reached");
309
310 bool isMatched = false;
311 int8_t checkResult = -1;
312
313 for (DataRuleList::iterator it = m_dataRules.begin();
314 it != m_dataRules.end(); it++)
315 {
316 if ((*it)->match(data))
317 {
318 isMatched = true;
319 checkResult = (*it)->check(data, onValidated, onValidationFailed);
320 break;
321 }
322 }
323
324 if (!isMatched)
325 return onValidationFailed(data.shared_from_this(), "No rule matched!");
326
327 if (checkResult == 0)
328 {
329 const Signature& signature = data.getSignature();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700330 checkSignature(data, signature, nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700331 onValidated, onValidationFailed, nextSteps);
332 }
333}
334
335void
336ValidatorConfig::checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700337 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700338 const OnInterestValidated& onValidated,
339 const OnInterestValidationFailed& onValidationFailed,
340 std::vector<shared_ptr<ValidationRequest> >& nextSteps)
341{
Yingdi Yu44d190c2014-04-16 17:05:46 -0700342 if (!m_shouldValidate)
343 return onValidated(interest.shared_from_this());
344
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700345 if (m_stepLimit == nSteps)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700346 return onValidationFailed(interest.shared_from_this(),
347 "Maximum steps of validation reached");
348
349 bool isMatched = false;
350 int8_t checkResult = -1;
351
352 for (InterestRuleList::iterator it = m_interestRules.begin();
353 it != m_interestRules.end(); it++)
354 {
355 if ((*it)->match(interest))
356 {
357 isMatched = true;
358 checkResult = (*it)->check(interest, onValidated, onValidationFailed);
359 break;
360 }
361 }
362
363 if (!isMatched)
364 return onValidationFailed(interest.shared_from_this(), "No rule matched!");
365
366 if (checkResult == 0)
367 {
368 const Name& interestName = interest.getName();
369 Name signedName = interestName.getPrefix(-2);
370 Signature signature(interestName[-2].blockFromValue(),
371 interestName[-1].blockFromValue());
372
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700373 checkSignature(interest, signature, nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700374 onValidated, onValidationFailed, nextSteps);
375 }
376}
377
378
379} // namespace ndn