24 #ifndef NDN_SECURITY_CONF_CHECKER_HPP 25 #define NDN_SECURITY_CONF_CHECKER_HPP 30 #include "../../util/io.hpp" 31 #include "../validator.hpp" 32 #include "../v1/identity-certificate.hpp" 34 #include <boost/algorithm/string.hpp> 35 #include <boost/filesystem.hpp> 36 #include <boost/lexical_cast.hpp> 46 typedef function<void(const shared_ptr<const Interest>&,
49 typedef function<void(const shared_ptr<const Data>&,
const std::string&)>
OnDataCheckFailed;
89 shared_ptr<KeyLocatorChecker> keyLocatorChecker)
91 , m_keyLocatorChecker(keyLocatorChecker)
96 if (!static_cast<bool>(m_keyLocatorChecker))
97 BOOST_THROW_EXCEPTION(
Error(
"Strong signature requires KeyLocatorChecker"));
103 BOOST_THROW_EXCEPTION(
Error(
"Unsupported signature type"));
120 return check(interest, signature);
133 template<
class Packet>
137 if (m_sigType != signature.
getType()) {
170 std::string failInfo;
171 if (m_keyLocatorChecker->check(packet, signature.
getKeyLocator(), failInfo))
180 shared_ptr<KeyLocatorChecker> m_keyLocatorChecker;
190 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
201 const std::vector<shared_ptr<v1::IdentityCertificate>>& signers)
204 for (std::vector<shared_ptr<v1::IdentityCertificate>>::const_iterator it = signers.begin();
205 it != signers.end(); it++)
206 m_signers[(*it)->getName().getPrefix(-1)] = (*it);
210 BOOST_THROW_EXCEPTION(
Error(
"FixedSigner is only meaningful for strong signature type"));
227 return check(interest, signature);
240 template<
class Packet>
244 if (m_sigType != signature.
getType()) {
273 if (m_signers.find(keyLocatorName) == m_signers.end()) {
279 m_signers[keyLocatorName]->getPublicKeyInfo())) {
298 typedef std::map<Name, shared_ptr<v1::IdentityCertificate>> SignerList;
300 SignerList m_signers;
313 static shared_ptr<Checker>
316 ConfigSection::const_iterator propertyIt = configSection.begin();
319 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
320 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.type>"));
322 std::string type = propertyIt->second.data();
324 if (boost::iequals(type,
"customized"))
325 return createCustomizedChecker(configSection, configFilename);
326 else if (boost::iequals(type,
"hierarchical"))
327 return createHierarchicalChecker(configSection, configFilename);
328 else if (boost::iequals(type,
"fixed-signer"))
329 return createFixedSignerChecker(configSection, configFilename);
331 BOOST_THROW_EXCEPTION(
Error(
"Unsupported checker type: " + type));
335 static shared_ptr<Checker>
337 const std::string& configFilename)
339 ConfigSection::const_iterator propertyIt = configSection.begin();
343 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"sig-type"))
344 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.sig-type>"));
346 std::string sigType = propertyIt->second.data();
350 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"key-locator"))
351 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.key-locator>"));
353 shared_ptr<KeyLocatorChecker> keyLocatorChecker =
357 if (propertyIt != configSection.end())
358 BOOST_THROW_EXCEPTION(
Error(
"Expect the end of checker"));
360 return make_shared<CustomizedChecker>(getSigType(sigType), keyLocatorChecker);
363 static shared_ptr<Checker>
364 createHierarchicalChecker(
const ConfigSection& configSection,
365 const std::string& configFilename)
367 ConfigSection::const_iterator propertyIt = configSection.begin();
371 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"sig-type"))
372 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.sig-type>"));
374 std::string sigType = propertyIt->second.data();
377 if (propertyIt != configSection.end())
378 BOOST_THROW_EXCEPTION(
Error(
"Expect the end of checker"));
380 return make_shared<HierarchicalChecker>(getSigType(sigType));
383 static shared_ptr<Checker>
385 const std::string& configFilename)
387 ConfigSection::const_iterator propertyIt = configSection.begin();
391 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"sig-type"))
392 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.sig-type>"));
394 std::string sigType = propertyIt->second.data();
397 std::vector<shared_ptr<v1::IdentityCertificate>> signers;
398 for (; propertyIt != configSection.end(); propertyIt++) {
399 if (!boost::iequals(propertyIt->first,
"signer"))
400 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.signer> but get <checker." +
401 propertyIt->first +
">"));
403 signers.push_back(getSigner(propertyIt->second, configFilename));
406 if (propertyIt != configSection.end())
407 BOOST_THROW_EXCEPTION(
Error(
"Expect the end of checker"));
413 static shared_ptr<v1::IdentityCertificate>
414 getSigner(
const ConfigSection& configSection,
const std::string& configFilename)
416 using namespace boost::filesystem;
418 ConfigSection::const_iterator propertyIt = configSection.begin();
421 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
422 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.signer.type>"));
424 std::string type = propertyIt->second.data();
427 if (boost::iequals(type,
"file")) {
429 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"file-name"))
430 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.signer.file-name>"));
432 path certfilePath = absolute(propertyIt->second.data(),
433 path(configFilename).parent_path());
436 if (propertyIt != configSection.end())
437 BOOST_THROW_EXCEPTION(
Error(
"Expect the end of checker.signer"));
439 shared_ptr<v1::IdentityCertificate> idCert
440 = io::load<v1::IdentityCertificate>(certfilePath.c_str());
442 if (static_cast<bool>(idCert))
445 BOOST_THROW_EXCEPTION(
Error(
"Cannot read certificate from file: " +
446 certfilePath.native()));
448 else if (boost::iequals(type,
"base64")) {
450 if (propertyIt == configSection.end() ||
451 !boost::iequals(propertyIt->first,
"base64-string"))
452 BOOST_THROW_EXCEPTION(
Error(
"Expect <checker.signer.base64-string>"));
454 std::stringstream ss(propertyIt->second.data());
457 if (propertyIt != configSection.end())
458 BOOST_THROW_EXCEPTION(
Error(
"Expect the end of checker.signer"));
460 shared_ptr<v1::IdentityCertificate> idCert = io::load<v1::IdentityCertificate>(ss);
462 if (static_cast<bool>(idCert))
465 BOOST_THROW_EXCEPTION(
Error(
"Cannot decode certificate from string"));
468 BOOST_THROW_EXCEPTION(
Error(
"Unsupported checker.signer type: " + type));
472 getSigType(
const std::string& sigType)
474 if (boost::iequals(sigType,
"rsa-sha256"))
476 else if (boost::iequals(sigType,
"ecdsa-sha256"))
478 else if (boost::iequals(sigType,
"sha256"))
481 BOOST_THROW_EXCEPTION(
Error(
"Unsupported signature type"));
489 #endif // NDN_SECURITY_CONF_CHECKER_HPP Copyright (c) 2011-2015 Regents of the University of California.
function< void(const shared_ptr< const Interest > &, const std::string &)> OnInterestCheckFailed
virtual int8_t check(const Interest &interest) override
check if interest satisfies condition defined in the specific checker implementation ...
const Name & getName() const
get Name element
HierarchicalChecker(uint32_t sigType)
represents an Interest packet
virtual int8_t check(const Interest &interest) override
check if interest satisfies condition defined in the specific checker implementation ...
bool hasKeyLocator() const
Check if SignatureInfo block has a KeyLocator.
KeyLocatorChecker is one of the classes used by ValidatorConfig.
function< void(const shared_ptr< const Interest > &)> OnInterestChecked
virtual int8_t check(const Data &data)=0
check if data satisfies condition defined in the specific checker implementation
static shared_ptr< KeyLocatorChecker > create(const ConfigSection &configSection, const std::string &filename)
virtual int8_t check(const Data &data) override
check if data satisfies condition defined in the specific checker implementation
Name abstraction to represent an absolute name.
CustomizedChecker(uint32_t sigType, shared_ptr< KeyLocatorChecker > keyLocatorChecker)
FixedSignerChecker(uint32_t sigType, const std::vector< shared_ptr< v1::IdentityCertificate >> &signers)
boost::property_tree::ptree ConfigSection
function< void(const shared_ptr< const Data > &)> OnDataChecked
uint32_t getType() const
Get signature type.
static bool verifySignature(const Data &data, const v1::PublicKey &publicKey)
Verify the data using the publicKey.
static shared_ptr< Checker > create(const ConfigSection &configSection, const std::string &configFilename)
create a checker from configuration file.
const Signature & getSignature() const
const KeyLocator & getKeyLocator() const
Get KeyLocator.
function< void(const shared_ptr< const Data > &, const std::string &)> OnDataCheckFailed
virtual int8_t check(const Data &data) override
check if data satisfies condition defined in the specific checker implementation
represents an error in TLV encoding or decoding
const Name & getName() const
A Signature is storage for the signature-related information (info and value) in a Data packet...