26 #include <boost/algorithm/string/predicate.hpp>
35 Checker::Result::Result(std::string error)
36 : m_error(std::move(error))
40 class Checker::NegativeResultBuilder
44 NegativeResultBuilder&
51 operator Checker::Result()
const
53 auto error = m_ss.str();
54 return Checker::Result(error.empty() ?
"checker failed" : std::move(error));
58 std::ostringstream m_ss;
61 Checker::NegativeResultBuilder
64 return NegativeResultBuilder();
74 return reject() <<
"signature type does not match the checker "
86 return reject() <<
"ParametersSha256DigestComponent missing";
92 return reject() <<
"name too short";
111 , m_relation(relation)
124 return reject() <<
"identity " << identity <<
" and packet name do not satisfy "
125 << m_relation <<
" relation";
137 if (m_regex.
match(klName)) {
141 return reject() <<
"KeyLocator does not match regex " << m_regex;
145 const std::string& pktNameExpr,
const std::string& pktNameExpand,
146 const std::string& klNameExpr,
const std::string& klNameExpand,
149 , m_hyperPRegex(pktNameExpr, pktNameExpand)
150 , m_hyperKRegex(klNameExpr, klNameExpand)
151 , m_hyperRelation(hyperRelation)
158 if (!m_hyperPRegex.
match(pktName)) {
159 return reject() <<
"packet name does not match p-regex " << m_hyperPRegex;
162 if (!m_hyperKRegex.
match(klName)) {
163 return reject() <<
"KeyLocator does not match k-regex " << m_hyperKRegex;
166 auto kExpand = m_hyperKRegex.
expand();
167 auto pExpand = m_hyperPRegex.
expand();
172 return reject() <<
"expanded names " << kExpand <<
" and " << pExpand
173 <<
" do not satisfy " << m_hyperRelation <<
" relation";
179 auto propertyIt = configSection.begin();
182 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
186 std::string type = propertyIt->second.data();
187 if (boost::iequals(type,
"customized")) {
188 return createCustomizedChecker(configSection, configFilename);
190 else if (boost::iequals(type,
"hierarchical")) {
191 return createHierarchicalChecker(configSection, configFilename);
199 parseSigType(
const std::string& value)
201 if (boost::iequals(value,
"rsa-sha256")) {
204 else if (boost::iequals(value,
"ecdsa-sha256")) {
211 else if (boost::iequals(value,
"sha256")) {
215 NDN_THROW(Error(
"Unrecognized value of <checker.sig-type>: " + value));
220 Checker::createCustomizedChecker(
const ConfigSection& configSection,
221 const std::string& configFilename)
223 auto propertyIt = configSection.begin();
229 if (propertyIt != configSection.end() && boost::iequals(propertyIt->first,
"sig-type")) {
230 sigType = parseSigType(propertyIt->second.data());
234 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"key-locator")) {
237 return make_unique<Checker>(sigType);
239 NDN_THROW(Error(
"Expecting <checker.key-locator>"));
242 auto checker = createKeyLocatorChecker(sigType, propertyIt->second, configFilename);
245 if (propertyIt != configSection.end()) {
246 NDN_THROW(Error(
"Expecting end of <checker>"));
252 Checker::createHierarchicalChecker(
const ConfigSection& configSection,
253 const std::string& configFilename)
255 auto propertyIt = configSection.begin();
261 if (propertyIt != configSection.end() && boost::iequals(propertyIt->first,
"sig-type")) {
262 sigType = parseSigType(propertyIt->second.data());
266 if (propertyIt != configSection.end()) {
267 NDN_THROW(Error(
"Expecting end of <checker>"));
269 return make_unique<HyperRelationChecker>(sigType,
271 "^(<>*)<KEY><>{1,3}$",
"\\1",
277 const ConfigSection& configSection,
const std::string& configFilename)
279 auto propertyIt = configSection.begin();
282 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
283 NDN_THROW(Error(
"Expecting <checker.key-locator.type>"));
285 std::string type = propertyIt->second.data();
286 if (boost::iequals(type,
"name"))
287 return createKeyLocatorNameChecker(sigType, configSection, configFilename);
289 NDN_THROW(Error(
"Unrecognized <checker.key-locator.type>: " + type));
294 const ConfigSection& configSection,
const std::string& configFilename)
296 auto propertyIt = configSection.begin();
299 if (propertyIt == configSection.end())
300 NDN_THROW(Error(
"Unexpected end of <checker.key-locator>"));
302 if (boost::iequals(propertyIt->first,
"name")) {
305 name =
Name(propertyIt->second.data());
308 NDN_THROW_NESTED(Error(
"Invalid <checker.key-locator.name>: " + propertyIt->second.data()));
312 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"relation")) {
313 NDN_THROW(Error(
"Expecting <checker.key-locator.relation>"));
316 std::string relationString = propertyIt->second.data();
321 if (propertyIt != configSection.end()) {
322 NDN_THROW(Error(
"Expecting end of <checker.key-locator>"));
324 return make_unique<NameRelationChecker>(sigType, name, relation);
326 else if (boost::iequals(propertyIt->first,
"regex")) {
327 std::string regexString = propertyIt->second.data();
330 if (propertyIt != configSection.end()) {
331 NDN_THROW(Error(
"Expecting end of <checker.key-locator>"));
335 return make_unique<RegexChecker>(sigType,
Regex(regexString));
337 catch (
const Regex::Error&) {
338 NDN_THROW_NESTED(Error(
"Invalid <checker.key-locator.regex>: " + regexString));
341 else if (boost::iequals(propertyIt->first,
"hyper-relation")) {
343 auto hPropertyIt = hSection.begin();
346 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-regex")) {
347 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.k-regex>"));
350 std::string kRegex = hPropertyIt->second.data();
354 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-expand")) {
355 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.k-expand>"));
358 std::string kExpand = hPropertyIt->second.data();
362 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"h-relation")) {
363 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.h-relation>"));
366 std::string hRelation = hPropertyIt->second.data();
370 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-regex")) {
371 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.p-regex>"));
374 std::string pRegex = hPropertyIt->second.data();
378 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-expand")) {
379 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.p-expand>"));
382 std::string pExpand = hPropertyIt->second.data();
385 if (hPropertyIt != hSection.end()) {
386 NDN_THROW(Error(
"Expecting end of <checker.key-locator.hyper-relation>"));
391 return make_unique<HyperRelationChecker>(sigType, pRegex, pExpand, kRegex, kExpand, relation);
393 catch (
const Regex::Error&) {
398 NDN_THROW(Error(
"Unrecognized <checker.key-locator>: " + propertyIt->first));
Represents an absolute name.
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
size_t size() const noexcept
Returns the number of components.
bool match(const Name &name)
virtual Name expand(const std::string &expand="")
Provides a tag type for simple types.
std::shared_ptr< T > getTag() const
Get a tag item.
tlv::SignatureTypeValue m_sigType
virtual Result checkNames(const Name &pktName, const Name &klName)
Base version of name checking.
static unique_ptr< Checker > create(const ConfigSection &configSection, const std::string &configFilename)
Create a checker from configuration section.
Result check(uint32_t pktType, tlv::SignatureTypeValue sigType, const Name &pktName, const Name &klName, const ValidationState &state)
Check if packet name and KeyLocator satisfy the checker's conditions.
Checker(tlv::SignatureTypeValue sigType)
static NegativeResultBuilder reject()
HyperRelationChecker(tlv::SignatureTypeValue sigType, const std::string &pktNameExpr, const std::string &pktNameExpand, const std::string &klNameExpr, const std::string &klNameExpand, const NameRelation &hyperRelation)
Result checkNames(const Name &pktName, const Name &klName) override
Base version of name checking.
Result checkNames(const Name &pktName, const Name &klName) override
Base version of name checking.
NameRelationChecker(tlv::SignatureTypeValue sigType, const Name &name, const NameRelation &relation)
Result checkNames(const Name &pktName, const Name &klName) override
Base version of name checking.
RegexChecker(tlv::SignatureTypeValue sigType, const Regex ®ex)
#define NDN_THROW_NESTED(e)
NameRelation getNameRelationFromString(const std::string &relationString)
Convert relationString to NameRelation.
boost::property_tree::ptree ConfigSection
bool checkNameRelation(NameRelation relation, const Name &name1, const Name &name2)
Check whether name1 and name2 satisfies relation.
std::ostream & operator<<(std::ostream &os, NameRelation relation)
Name extractIdentityNameFromKeyLocator(const Name &keyLocator)
Extract identity name from key, version-less certificate, or certificate name.
@ V03
Sign Interest using Packet Specification v0.3 semantics.
constexpr size_t MIN_SIZE
Minimum number of name components for an old-style Signed Interest.
@ ParametersSha256DigestComponent
SignatureTypeValue
SignatureType values.
@ SignatureSha256WithEcdsa