8 #ifndef NDN_SECURITY_CONF_CHECKER_HPP
9 #define NDN_SECURITY_CONF_CHECKER_HPP
12 #include "../../util/io.hpp"
13 #include <boost/algorithm/string.hpp>
27 typedef function<void(const shared_ptr<const Data>&,
const std::string&)>
OnDataCheckFailed;
70 INTEREST_SIG_VALUE = -1,
71 INTEREST_SIG_INFO = -2
76 shared_ptr<KeyLocatorChecker> keyLocatorChecker)
78 , m_keyLocatorChecker(keyLocatorChecker)
81 throw Error(
"Strong signature requires KeyLocatorChecker");
100 Signature signature(interestName[INTEREST_SIG_INFO].blockFromValue(),
101 interestName[INTEREST_SIG_VALUE].blockFromValue());
102 return check(interest, signature, onValidated, onValidationFailed);
106 onValidationFailed(interest.shared_from_this(),
"Invalid signature");
111 onValidationFailed(interest.shared_from_this(),
"Cannot decode signature related TLVs");
117 template<
class Packet,
class OnVal
idated,
class OnFailed>
120 const OnValidated& onValidated,
121 const OnFailed& onValidationFailed)
123 if (m_sigType != signature.
getType())
125 onValidationFailed(packet.shared_from_this(),
126 "Signature type does not match: " +
127 boost::lexical_cast<std::string>(m_sigType) +
129 boost::lexical_cast<std::string>(signature.
getType()));
141 std::string failInfo;
142 if (m_keyLocatorChecker->check(packet, sig.getKeyLocator(), failInfo))
146 onValidationFailed(packet.shared_from_this(), failInfo);
150 catch (SignatureSha256WithRsa::Error& e)
152 onValidationFailed(packet.shared_from_this(),
153 "Cannot decode Sha256WithRsa signature!");
161 onValidationFailed(packet.shared_from_this(),
162 "Unsupported signature type: " +
163 boost::lexical_cast<std::string>(signature.
getType()));
171 shared_ptr<KeyLocatorChecker> m_keyLocatorChecker;
180 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
191 INTEREST_SIG_VALUE = -1,
192 INTEREST_SIG_INFO = -2
196 const std::vector<shared_ptr<IdentityCertificate> >& signers)
199 for (std::vector<shared_ptr<IdentityCertificate> >::const_iterator it = signers.begin();
200 it != signers.end(); it++)
201 m_signers[(*it)->getName().getPrefix(-1)] = (*it);
220 Signature signature(interestName[INTEREST_SIG_INFO].blockFromValue(),
221 interestName[INTEREST_SIG_VALUE].blockFromValue());
222 return check(interest, signature, onValidated, onValidationFailed);
226 onValidationFailed(interest.shared_from_this(),
"Invalid signature");
231 onValidationFailed(interest.shared_from_this(),
"Cannot decode signature related TLVs");
237 template<
class Packet,
class OnVal
idated,
class OnFailed>
240 const OnValidated& onValidated,
241 const OnFailed& onValidationFailed)
243 if (m_sigType != signature.
getType())
245 onValidationFailed(packet.shared_from_this(),
246 "Signature type does not match: "
247 + boost::lexical_cast<std::string>(m_sigType)
249 + boost::lexical_cast<std::string>(signature.
getType()));
261 const Name& keyLocatorName = sig.getKeyLocator().getName();
262 if (m_signers.find(keyLocatorName) == m_signers.end())
264 onValidationFailed(packet.shared_from_this(),
265 "Signer is not in the fixed signer list: "
266 + keyLocatorName.
toUri());
271 m_signers[keyLocatorName]->getPublicKeyInfo()))
273 onValidated(packet.shared_from_this());
278 onValidationFailed(packet.shared_from_this(),
279 "Signature cannot be validated!");
283 catch (KeyLocator::Error& e)
285 onValidationFailed(packet.shared_from_this(),
286 "KeyLocator does not have name!");
289 catch (SignatureSha256WithRsa::Error& e)
291 onValidationFailed(packet.shared_from_this(),
292 "Cannot decode signature!");
298 onValidationFailed(packet.shared_from_this(),
299 "FixedSigner does not allow Sha256 signature type!");
304 onValidationFailed(packet.shared_from_this(),
305 "Unsupported signature type: "
306 + boost::lexical_cast<std::string>(signature.
getType()));
313 typedef std::map<Name, shared_ptr<IdentityCertificate> > SignerList;
316 SignerList m_signers;
329 static shared_ptr<Checker>
332 ConfigSection::const_iterator propertyIt = configSection.begin();
335 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
336 throw Error(
"Expect <checker.type>!");
338 std::string type = propertyIt->second.data();
340 if (boost::iequals(type,
"customized"))
341 return createCustomizedChecker(configSection, configFilename);
342 else if (boost::iequals(type,
"hierarchical"))
343 return createHierarchicalChecker(configSection, configFilename);
344 else if (boost::iequals(type,
"fixed-signer"))
345 return createFixedSignerChecker(configSection, configFilename);
347 throw Error(
"Unsupported checker type: " + type);
351 static shared_ptr<Checker>
353 const std::string& configFilename)
355 ConfigSection::const_iterator propertyIt = configSection.begin();
359 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"sig-type"))
360 throw Error(
"Expect <checker.sig-type>!");
362 std::string sigType = propertyIt->second.data();
366 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"key-locator"))
367 throw Error(
"Expect <checker.key-locator>!");
369 shared_ptr<KeyLocatorChecker> keyLocatorChecker =
373 if (propertyIt != configSection.end())
374 throw Error(
"Expect the end of checker!");
376 return make_shared<CustomizedChecker>(boost::cref(getSigType(sigType)),
377 boost::cref(keyLocatorChecker));
380 static shared_ptr<Checker>
381 createHierarchicalChecker(
const ConfigSection& configSection,
382 const std::string& configFilename)
384 ConfigSection::const_iterator propertyIt = configSection.begin();
388 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"sig-type"))
389 throw Error(
"Expect <checker.sig-type>!");
391 std::string sigType = propertyIt->second.data();
394 if (propertyIt != configSection.end())
395 throw Error(
"Expect the end of checker!");
397 return make_shared<HierarchicalChecker>(boost::cref(getSigType(sigType)));
400 static shared_ptr<Checker>
402 const std::string& configFilename)
404 ConfigSection::const_iterator propertyIt = configSection.begin();
408 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"sig-type"))
409 throw Error(
"Expect <checker.sig-type>!");
411 std::string sigType = propertyIt->second.data();
414 std::vector<shared_ptr<IdentityCertificate> > signers;
415 for (; propertyIt != configSection.end(); propertyIt++)
417 if (!boost::iequals(propertyIt->first,
"signer"))
418 throw Error(
"Expect <checker.signer> but get <checker."
419 + propertyIt->first +
">");
421 signers.push_back(getSigner(propertyIt->second, configFilename));
424 if (propertyIt != configSection.end())
425 throw Error(
"Expect the end of checker!");
427 return shared_ptr<FixedSignerChecker>(
new FixedSignerChecker(getSigType(sigType),
431 static shared_ptr<IdentityCertificate>
432 getSigner(
const ConfigSection& configSection,
const std::string& configFilename)
434 using namespace boost::filesystem;
436 ConfigSection::const_iterator propertyIt = configSection.begin();
439 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
440 throw Error(
"Expect <checker.signer.type>!");
442 std::string type = propertyIt->second.data();
445 if (boost::iequals(type,
"file"))
448 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"file-name"))
449 throw Error(
"Expect <checker.signer.file-name>!");
451 path certfilePath = absolute(propertyIt->second.data(),
452 path(configFilename).parent_path());
455 if (propertyIt != configSection.end())
456 throw Error(
"Expect the end of checker.signer");
458 shared_ptr<IdentityCertificate> idCert
459 = io::load<IdentityCertificate>(certfilePath.c_str());
461 if (static_cast<bool>(idCert))
464 throw Error(
"Cannot read certificate from file: "
465 + certfilePath.native());
467 else if (boost::iequals(type,
"base64"))
470 if (propertyIt == configSection.end() ||
471 !boost::iequals(propertyIt->first,
"base64-string"))
472 throw Error(
"Expect <checker.signer.base64-string>!");
474 std::stringstream ss(propertyIt->second.data());
477 if (propertyIt != configSection.end())
478 throw Error(
"Expect the end of checker.signer");
480 shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(ss);
482 if (static_cast<bool>(idCert))
485 throw Error(
"Cannot decode certificate from string");
488 throw Error(
"Unsupported checker.signer type: " + type);
492 getSigType(
const std::string& sigType)
494 if (boost::iequals(sigType,
"rsa-sha256"))
496 else if (boost::iequals(sigType,
"sha256"))
507 #endif // NDN_SECURITY_SEC_CONF_RULE_SIGNER_HPP
Representing of SHA256-with-RSA signature in a data packet.
const Name & getName() const
function< void(const shared_ptr< const Interest > &, const std::string &)> OnInterestCheckFailed
FixedSignerChecker(uint32_t sigType, const std::vector< shared_ptr< IdentityCertificate > > &signers)
virtual int8_t check(const Data &data, const OnDataChecked &onValidated, const OnDataCheckFailed &onValidationFailed)
check if data satisfies condition defined in the specific checker implementation
HierarchicalChecker(uint32_t sigType)
An Interest holds a Name and other fields for an interest.
virtual int8_t check(const Data &data, const OnDataChecked &onValidated, const OnDataCheckFailed &onValidationFailed)=0
check if data satisfies condition defined in the specific checker implementation
virtual int8_t check(const Interest &interest, const OnInterestChecked &onValidated, const OnInterestCheckFailed &onValidationFailed)
check if interest satisfies condition defined in the specific checker implementation ...
function< void(const shared_ptr< const Interest > &)> OnInterestChecked
virtual int8_t check(const Interest &interest, const OnInterestChecked &onValidated, const OnInterestCheckFailed &onValidationFailed)
check if interest satisfies condition defined in the specific checker implementation ...
static shared_ptr< KeyLocatorChecker > create(const ConfigSection &configSection, const std::string &filename)
std::string toUri() const
Encode this name as a URI.
A Name holds an array of Name::Component and represents an NDN name.
CustomizedChecker(uint32_t sigType, shared_ptr< KeyLocatorChecker > keyLocatorChecker)
const Signature & getSignature() const
virtual int8_t check(const Data &data, const OnDataChecked &onValidated, const OnDataCheckFailed &onValidationFailed)
check if data satisfies condition defined in the specific checker implementation
boost::property_tree::ptree ConfigSection
static bool verifySignature(const Data &data, const PublicKey &publicKey)
Verify the data using the publicKey.
function< void(const shared_ptr< const Data > &)> OnDataChecked
static shared_ptr< Checker > create(const ConfigSection &configSection, const std::string &configFilename)
create a checker from configuration file.
function< void(const shared_ptr< const Data > &, const std::string &)> OnDataCheckFailed
A Signature is storage for the signature-related information (info and value) in a Data packet...