NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
key-locator-checker.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #ifndef NDN_SECURITY_CONF_KEY_LOCATOR_CHECKER_HPP
25 #define NDN_SECURITY_CONF_KEY_LOCATOR_CHECKER_HPP
26 
27 #include "../../common.hpp"
28 #include "../../data.hpp"
29 #include "../../interest.hpp"
30 #include "../../util/regex.hpp"
31 #include "../security-common.hpp"
32 #include <boost/algorithm/string.hpp>
33 
34 #include "common.hpp"
35 
36 namespace ndn {
37 namespace security {
38 namespace conf {
39 
40 class KeyLocatorCheckerFactory;
41 
52 {
53 public:
54  enum Relation {
58  };
59 
60  virtual
62  {
63  }
64 
65  bool
66  check(const Data& data,
67  const KeyLocator& keyLocator,
68  std::string& failInfo)
69  {
70  return check(data.getName(), keyLocator, failInfo);
71  }
72 
73  bool
74  check(const Interest& interest,
75  const KeyLocator& keyLocator,
76  std::string& failInfo)
77  {
78  if (interest.getName().size() < signed_interest::MIN_LENGTH)
79  {
80  failInfo = "No Signature";
81  return false;
82  }
83 
84  Name signedName = interest.getName().getPrefix(-signed_interest::MIN_LENGTH);
85  return check(signedName, keyLocator, failInfo);
86  }
87 
88 protected:
89 
90  virtual bool
91  check(const Name& packetName,
92  const KeyLocator& keyLocator,
93  std::string& failInfo) = 0;
94 
95  bool
96  checkRelation(const Relation& relation, const Name& name1, const Name& name2)
97  {
98  switch (relation)
99  {
100  case RELATION_EQUAL:
101  return (name1 == name2);
103  return name1.isPrefixOf(name2);
105  return (name1.isPrefixOf(name2) && name1 != name2);
106  default:
107  return false;
108  }
109  }
110 };
111 
113 {
114 public:
116  const KeyLocatorChecker::Relation& relation)
117  : m_name(name)
118  , m_relation(relation)
119  {
120  }
121 
122 protected:
123  virtual bool
124  check(const Name& packetName,
125  const KeyLocator& keyLocator,
126  std::string& failInfo)
127  {
128  try
129  {
130  if (checkRelation(m_relation, m_name, keyLocator.getName()))
131  return true;
132 
133  failInfo = "KeyLocatorChecker failed!";
134  return false;
135  }
136  catch (KeyLocator::Error& e)
137  {
138  failInfo = "KeyLocator does not have name";
139  return false;
140  }
141  }
142 
143 private:
144  Name m_name;
145  KeyLocatorChecker::Relation m_relation;
146 };
147 
149 {
150 public:
151  explicit
153  : m_regex(regex)
154  {
155  }
156 
157 protected:
158  virtual bool
159  check(const Name& packetName,
160  const KeyLocator& keyLocator,
161  std::string& failInfo)
162  {
163  try
164  {
165  if (m_regex.match(keyLocator.getName()))
166  return true;
167 
168  failInfo = "KeyLocatorChecker failed!";
169  return false;
170  }
171  catch (KeyLocator::Error& e)
172  {
173  failInfo = "KeyLocator does not have name";
174  return false;
175  }
176  }
177 
178 private:
179  Regex m_regex;
180 };
181 
183 {
184 public:
185  HyperKeyLocatorNameChecker(const std::string& pExpr, const std::string pExpand,
186  const std::string& kExpr, const std::string kExpand,
187  const Relation& hyperRelation)
188  : m_hyperPRegex(new Regex(pExpr, pExpand))
189  , m_hyperKRegex(new Regex(kExpr, kExpand))
190  , m_hyperRelation(hyperRelation)
191  {
192  }
193 
194 protected:
195  virtual bool
196  check(const Name& packetName,
197  const KeyLocator& keyLocator,
198  std::string& failInfo)
199  {
200  try
201  {
202  if (m_hyperPRegex->match(packetName) &&
203  m_hyperKRegex->match(keyLocator.getName()) &&
204  checkRelation(m_hyperRelation,
205  m_hyperKRegex->expand(),
206  m_hyperPRegex->expand()))
207  return true;
208 
209  failInfo = "KeyLocatorChecker failed!";
210  return false;
211  }
212  catch (KeyLocator::Error& e)
213  {
214  failInfo = "KeyLocator does not have name";
215  return false;
216  }
217 
218  }
219 
220 private:
221  shared_ptr<Regex> m_hyperPRegex;
222  shared_ptr<Regex> m_hyperKRegex;
223  Relation m_hyperRelation;
224 };
225 
226 
228 {
229 public:
230  static shared_ptr<KeyLocatorChecker>
231  create(const ConfigSection& configSection, const std::string& filename)
232  {
233  ConfigSection::const_iterator propertyIt = configSection.begin();
234 
235  // Get checker.key-locator.type
236  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type"))
237  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.type>!"));
238 
239  std::string type = propertyIt->second.data();
240 
241  if (boost::iequals(type, "name"))
242  return createKeyLocatorNameChecker(configSection, filename);
243  else
244  BOOST_THROW_EXCEPTION(Error("Unsupported checker.key-locator.type: " + type));
245  }
246 
247 private:
248  static shared_ptr<KeyLocatorChecker>
249  createKeyLocatorNameChecker(const ConfigSection& configSection,
250  const std::string& filename)
251  {
252  ConfigSection::const_iterator propertyIt = configSection.begin();
253  propertyIt++;
254 
255  if (propertyIt == configSection.end())
256  BOOST_THROW_EXCEPTION(Error("Expect more checker.key-locator properties"));
257 
258  if (boost::iequals(propertyIt->first, "name"))
259  {
260  Name name;
261  try
262  {
263  name = Name(propertyIt->second.data());
264  }
265  catch (Name::Error& e)
266  {
267  BOOST_THROW_EXCEPTION(Error("Invalid checker.key-locator.name: " +
268  propertyIt->second.data()));
269  }
270  propertyIt++;
271 
272  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation"))
273  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.relation>!"));
274 
275  std::string relationString = propertyIt->second.data();
276  propertyIt++;
277 
279  if (boost::iequals(relationString, "equal"))
281  else if (boost::iequals(relationString, "is-prefix-of"))
283  else if (boost::iequals(relationString, "is-strict-prefix-of"))
285  else
286  BOOST_THROW_EXCEPTION(Error("Unsupported relation: " + relationString));
287 
288  if (propertyIt != configSection.end())
289  BOOST_THROW_EXCEPTION(Error("Expect the end of checker.key-locator!"));
290 
291  return shared_ptr<RelationKeyLocatorNameChecker>
292  (new RelationKeyLocatorNameChecker(name, relation));
293  }
294  else if (boost::iequals(propertyIt->first, "regex"))
295  {
296  std::string regexString = propertyIt->second.data();
297  propertyIt++;
298 
299  if (propertyIt != configSection.end())
300  BOOST_THROW_EXCEPTION(Error("Expect the end of checker.key-locator!"));
301 
302  try
303  {
304  return shared_ptr<RegexKeyLocatorNameChecker>
305  (new RegexKeyLocatorNameChecker(regexString));
306  }
307  catch (Regex::Error& e)
308  {
309  BOOST_THROW_EXCEPTION(Error("Invalid checker.key-locator.regex: " + regexString));
310  }
311  }
312  else if (boost::iequals(propertyIt->first, "hyper-relation"))
313  {
314  const ConfigSection& hSection = propertyIt->second;
315 
316  ConfigSection::const_iterator hPropertyIt = hSection.begin();
317 
318  // Get k-regex
319  if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "k-regex"))
320  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.hyper-relation.k-regex>!"));
321 
322  std::string kRegex = hPropertyIt->second.data();
323  hPropertyIt++;
324 
325  // Get k-expand
326  if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "k-expand"))
327  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.hyper-relation.k-expand>!"));
328 
329  std::string kExpand = hPropertyIt->second.data();
330  hPropertyIt++;
331 
332  // Get h-relation
333  if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "h-relation"))
334  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.hyper-relation.h-relation>!"));
335 
336  std::string hRelation = hPropertyIt->second.data();
337  hPropertyIt++;
338 
339  // Get p-regex
340  if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "p-regex"))
341  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.hyper-relation.p-regex>!"));
342 
343  std::string pRegex = hPropertyIt->second.data();
344  hPropertyIt++;
345 
346  // Get p-expand
347  if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "p-expand"))
348  BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator.hyper-relation.p-expand>!"));
349 
350  std::string pExpand = hPropertyIt->second.data();
351  hPropertyIt++;
352 
353  if (hPropertyIt != hSection.end())
354  BOOST_THROW_EXCEPTION(Error("Expect the end of checker.key-locator.hyper-relation!"));
355 
357  if (boost::iequals(hRelation, "equal"))
359  else if (boost::iequals(hRelation, "is-prefix-of"))
361  else if (boost::iequals(hRelation, "is-strict-prefix-of"))
363  else
364  BOOST_THROW_EXCEPTION(Error("Unsupported checker.key-locator.hyper-relation.h-relation: "
365  + hRelation));
366 
367  try
368  {
369  return shared_ptr<HyperKeyLocatorNameChecker>
370  (new HyperKeyLocatorNameChecker(pRegex, pExpand,
371  kRegex, kExpand,
372  relation));
373  }
374  catch (Regex::Error& e)
375  {
376  BOOST_THROW_EXCEPTION(Error("Invalid regex for key-locator.hyper-relation"));
377  }
378  }
379  else
380  BOOST_THROW_EXCEPTION(Error("Unsupported checker.key-locator"));
381  }
382 };
383 
384 
385 } // namespace conf
386 } // namespace security
387 } // namespace ndn
388 
389 #endif // NDN_SECURITY_CONF_KEY_LOCATOR_CHECKER_HPP
virtual bool check(const Name &packetName, const KeyLocator &keyLocator, std::string &failInfo)
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:249
Copyright (c) 2011-2015 Regents of the University of California.
virtual bool check(const Name &packetName, const KeyLocator &keyLocator, std::string &failInfo)
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:360
const Name & getName() const
get Name element
represents an Interest packet
Definition: interest.hpp:45
RelationKeyLocatorNameChecker(const Name &name, const KeyLocatorChecker::Relation &relation)
Copyright (c) 2013-2015 Regents of the University of California.
KeyLocatorChecker is one of the classes used by ValidatorConfig.
static shared_ptr< KeyLocatorChecker > create(const ConfigSection &configSection, const std::string &filename)
bool check(const Data &data, const KeyLocator &keyLocator, std::string &failInfo)
Error that can be thrown from Name.
Definition: name.hpp:52
bool check(const Interest &interest, const KeyLocator &keyLocator, std::string &failInfo)
HyperKeyLocatorNameChecker(const std::string &pExpr, const std::string pExpand, const std::string &kExpr, const std::string kExpand, const Relation &hyperRelation)
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:320
size_t size() const
Get the number of components.
Definition: name.hpp:408
boost::property_tree::ptree ConfigSection
Definition: common.hpp:35
virtual bool check(const Name &packetName, const KeyLocator &keyLocator, std::string &failInfo)
represents a Data packet
Definition: data.hpp:39
bool checkRelation(const Relation &relation, const Name &name1, const Name &name2)
const Name & getName() const
Definition: interest.hpp:218