26 #include <boost/algorithm/string/predicate.hpp> 27 #include <boost/filesystem/operations.hpp> 28 #include <boost/filesystem/path.hpp> 29 #include <boost/lexical_cast.hpp> 30 #include <boost/property_tree/info_parser.hpp> 37 namespace validator_config {
42 std::ifstream inputFile(filename);
44 NDN_THROW(
Error(
"Failed to read configuration file: " + filename));
46 load(inputFile, filename);
52 std::istringstream inputStream(input);
53 load(inputStream, filename);
61 boost::property_tree::read_info(input, tree);
63 catch (
const boost::property_tree::info_parser_error& e) {
65 " line " +
to_string(e.line()) +
": " + e.message()));
73 BOOST_ASSERT(!filename.empty());
75 if (m_validator ==
nullptr) {
79 m_shouldBypass =
false;
81 m_interestRules.clear();
82 m_validator->resetAnchors();
83 m_validator->resetVerifiedCertificates();
85 m_isConfigured =
true;
87 for (
const auto& subSection : configSection) {
88 const std::string& sectionName = subSection.first;
91 if (boost::iequals(sectionName,
"rule")) {
97 m_interestRules.push_back(
std::move(rule));
100 else if (boost::iequals(sectionName,
"trust-anchor")) {
101 processConfigTrustAnchor(section, filename);
104 NDN_THROW(
Error(
"Error processing configuration file " + filename +
105 ": unrecognized section " + sectionName));
111 ValidationPolicyConfig::processConfigTrustAnchor(
const ConfigSection& configSection,
112 const std::string& filename)
114 using namespace boost::filesystem;
116 auto propertyIt = configSection.begin();
119 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
123 std::string type = propertyIt->second.data();
126 if (boost::iequals(type,
"file")) {
128 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"file-name")) {
132 std::string file = propertyIt->second.data();
136 if (propertyIt != configSection.end())
139 m_validator->loadAnchor(file, absolute(file, path(filename).parent_path()).
string(),
142 else if (boost::iequals(type,
"base64")) {
144 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"base64-string"))
147 std::stringstream ss(propertyIt->second.data());
150 if (propertyIt != configSection.end())
153 auto idCert = io::load<Certificate>(ss);
154 if (idCert !=
nullptr) {
155 m_validator->loadAnchor(
"",
std::move(*idCert));
161 else if (boost::iequals(type,
"dir")) {
162 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"dir"))
165 std::string dirString(propertyIt->second.data());
169 if (propertyIt != configSection.end())
172 path dirPath = absolute(dirString, path(filename).parent_path());
173 m_validator->loadAnchor(dirString, dirPath.string(), refresh,
true);
175 else if (boost::iequals(type,
"any")) {
176 m_shouldBypass =
true;
184 ValidationPolicyConfig::getRefreshPeriod(ConfigSection::const_iterator& it,
185 const ConfigSection::const_iterator& end)
187 auto refresh = time::nanoseconds::max();
192 if (!boost::iequals(it->first,
"refresh")) {
196 std::string inputString = it->second.data();
198 char unit = inputString[inputString.size() - 1];
199 std::string refreshString = inputString.substr(0, inputString.size() - 1);
201 int32_t refreshPeriod = -1;
203 refreshPeriod = boost::lexical_cast<int32_t>(refreshString);
205 catch (
const boost::bad_lexical_cast&) {
208 if (refreshPeriod < 0) {
212 if (refreshPeriod == 0) {
213 return getDefaultRefreshPeriod();
229 ValidationPolicyConfig::getDefaultRefreshPeriod()
238 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
240 if (m_shouldBypass) {
241 return continueValidation(
nullptr, state);
245 if (!state->getOutcome()) {
249 for (
const auto& rule : m_dataRules) {
252 data.
getName(), klName, state)) {
253 return continueValidation(make_shared<CertificateRequest>(klName), state);
261 "No rule matched for data `" + data.
getName().
toUri() +
"`"});
268 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
270 if (m_shouldBypass) {
271 return continueValidation(
nullptr, state);
275 if (!state->getOutcome()) {
279 for (
const auto& rule : m_interestRules) {
300 state->fail({ValidationError::Code::INVALID_KEY_LOCATOR,
301 "Invalid signed Interest: " + std::string(e.what())});
309 return continueValidation(make_shared<CertificateRequest>(klName), state);
317 "No rule matched for interest `" + interest.
getName().
toUri() +
"`"});
boost::chrono::seconds seconds
boost::chrono::minutes minutes
Sign Interest using Packet Specification v0.3 semantics.
Copyright (c) 2011-2015 Regents of the University of California.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
std::string to_string(const T &val)
static unique_ptr< Rule > create(const ConfigSection &configSection, const std::string &configFilename)
create a rule from configuration section
int32_t getSignatureType() const noexcept
Get SignatureType.
const size_t MIN_SIZE
minimal number of components for Signed Interest
Represents an Interest packet.
optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo.
provides a tag type for simple types
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking...
int32_t getSignatureType() const noexcept
Get SignatureType.
std::function< void(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state)> ValidationContinuation
static Name getKeyLocatorName(const SignatureInfo &si, ValidationState &state)
const Name & getName() const noexcept
Get name.
Represents an absolute name.
SignatureTypeValue
SignatureType values.
size_t size() const
Returns the number of components.
boost::property_tree::ptree ConfigSection
bool hasInnerPolicy() const
Check if inner policy is set.
void checkPolicy(const Data &data, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Check data against the policy.
Block blockFromValue() const
Return a new Block constructed from the TLV-VALUE of this Block.
boost::chrono::hours hours
const ssize_t POS_SIG_INFO
const Name & getName() const noexcept
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Represents a Data packet.
void load(const std::string &filename)
Load policy from file filename.
void wireDecode(const Block &wire, Type type=Type::Data)
Decode from wire format.
boost::chrono::nanoseconds nanoseconds
represents an error in TLV encoding or decoding