8 #ifndef NDN_SECURITY_CONF_KEY_LOCATOR_CHECKER_HPP
9 #define NDN_SECURITY_CONF_KEY_LOCATOR_CHECKER_HPP
11 #include "../../common.hpp"
12 #include "../../data.hpp"
13 #include "../../interest.hpp"
14 #include <boost/algorithm/string.hpp>
22 class KeyLocatorCheckerFactory;
42 std::string& failInfo)
50 std::string& failInfo)
54 failInfo =
"No Signature";
59 return check(signedName, keyLocator, failInfo);
67 std::string& failInfo) = 0;
75 return (name1 == name2);
79 return (name1.
isPrefixOf(name2) && name1 != name2);
92 , m_relation(relation)
100 std::string& failInfo)
107 failInfo =
"KeyLocatorChecker failed!";
112 failInfo =
"KeyLocator does not have name";
134 std::string& failInfo)
141 failInfo =
"KeyLocatorChecker failed!";
146 failInfo =
"KeyLocator does not have name";
159 const std::string& kExpr,
const std::string kExpand,
161 : m_hyperPRegex(new
Regex(pExpr, pExpand))
162 , m_hyperKRegex(new
Regex(kExpr, kExpand))
163 , m_hyperRelation(hyperRelation)
171 std::string& failInfo)
175 if (m_hyperPRegex->match(packetName) &&
176 m_hyperKRegex->match(keyLocator.
getName()) &&
178 m_hyperKRegex->expand(),
179 m_hyperPRegex->expand()))
182 failInfo =
"KeyLocatorChecker failed!";
187 failInfo =
"KeyLocator does not have name";
194 shared_ptr<Regex> m_hyperPRegex;
195 shared_ptr<Regex> m_hyperKRegex;
203 static shared_ptr<KeyLocatorChecker>
206 ConfigSection::const_iterator propertyIt = configSection.begin();
209 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
210 throw Error(
"Expect <checker.key-locator.type>!");
212 std::string type = propertyIt->second.data();
214 if (boost::iequals(type,
"name"))
215 return createKeyLocatorNameChecker(configSection, filename);
217 throw Error(
"Unsupported checker.key-locator.type: " + type);
221 static shared_ptr<KeyLocatorChecker>
222 createKeyLocatorNameChecker(
const ConfigSection& configSection,
223 const std::string& filename)
225 ConfigSection::const_iterator propertyIt = configSection.begin();
228 if (propertyIt == configSection.end())
229 throw Error(
"Expect more checker.key-locator properties");
231 if (boost::iequals(propertyIt->first,
"name"))
236 name =
Name(propertyIt->second.data());
240 throw Error(
"Invalid checker.key-locator.name: "
241 + propertyIt->second.data());
245 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"relation"))
246 throw Error(
"Expect <checker.key-locator.relation>!");
248 std::string relationString = propertyIt->second.data();
252 if (boost::iequals(relationString,
"equal"))
254 else if (boost::iequals(relationString,
"is-prefix-of"))
256 else if (boost::iequals(relationString,
"is-strict-prefix-of"))
259 throw Error(
"Unsupported relation: " + relationString);
261 if (propertyIt != configSection.end())
262 throw Error(
"Expect the end of checker.key-locator!");
264 return shared_ptr<RelationKeyLocatorNameChecker>
265 (
new RelationKeyLocatorNameChecker(name, relation));
267 else if (boost::iequals(propertyIt->first,
"regex"))
269 std::string regexString = propertyIt->second.data();
272 if (propertyIt != configSection.end())
273 throw Error(
"Expect the end of checker.key-locator!");
277 return shared_ptr<RegexKeyLocatorNameChecker>
278 (
new RegexKeyLocatorNameChecker(regexString));
280 catch (Regex::Error& e)
282 throw Error(
"Invalid checker.key-locator.regex: " + regexString);
285 else if (boost::iequals(propertyIt->first,
"hyper-relation"))
289 ConfigSection::const_iterator hPropertyIt = hSection.begin();
292 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-regex"))
293 throw Error(
"Expect <checker.key-locator.hyper-relation.k-regex>!");
295 std::string kRegex = hPropertyIt->second.data();
299 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-expand"))
300 throw Error(
"Expect <checker.key-locator.hyper-relation.k-expand>!");
302 std::string kExpand = hPropertyIt->second.data();
306 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"h-relation"))
307 throw Error(
"Expect <checker.key-locator.hyper-relation.h-relation>!");
309 std::string hRelation = hPropertyIt->second.data();
313 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-regex"))
314 throw Error(
"Expect <checker.key-locator.hyper-relation.p-regex>!");
316 std::string pRegex = hPropertyIt->second.data();
320 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-expand"))
321 throw Error(
"Expect <checker.key-locator.hyper-relation.p-expand>!");
323 std::string pExpand = hPropertyIt->second.data();
326 if (hPropertyIt != hSection.end())
327 throw Error(
"Expect the end of checker.key-locator.hyper-relation!");
330 if (boost::iequals(hRelation,
"equal"))
332 else if (boost::iequals(hRelation,
"is-prefix-of"))
334 else if (boost::iequals(hRelation,
"is-strict-prefix-of"))
337 throw Error(
"Unsupported checker.key-locator.hyper-relation.h-relation: "
342 return shared_ptr<HyperKeyLocatorNameChecker>
343 (
new HyperKeyLocatorNameChecker(pRegex, pExpand,
347 catch (Regex::Error& e)
349 throw Error(
"Invalid regex for key-locator.hyper-relation");
353 throw Error(
"Unsupported checker.key-locator");
362 #endif // NDN_SECURITY_CONF_KEY_LOCATOR_CHECKER_HPP
RegexKeyLocatorNameChecker(const Regex ®ex)
virtual bool check(const Name &packetName, const KeyLocator &keyLocator, std::string &failInfo)
const Name & getName() const
virtual bool check(const Name &packetName, const KeyLocator &keyLocator, std::string &failInfo)
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
An Interest holds a Name and other fields for an interest.
RelationKeyLocatorNameChecker(const Name &name, const KeyLocatorChecker::Relation &relation)
const Name & getName() const
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.
const Name & getName() const
bool check(const Interest &interest, const KeyLocator &keyLocator, std::string &failInfo)
virtual ~KeyLocatorChecker()
size_t size() const
Get the number of components.
HyperKeyLocatorNameChecker(const std::string &pExpr, const std::string pExpand, const std::string &kExpr, const std::string kExpand, const Relation &hyperRelation)
A Name holds an array of Name::Component and represents an NDN name.
boost::property_tree::ptree ConfigSection
bool match(const Name &name)
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...
virtual bool check(const Name &packetName, const KeyLocator &keyLocator, std::string &failInfo)
bool checkRelation(const Relation &relation, const Name &name1, const Name &name2)