Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "checker.hpp" |
| 23 | #include "security/v2/validation-state.hpp" |
| 24 | #include "security/verification-helpers.hpp" |
| 25 | #include "security/pib/key.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 27 | #include <boost/algorithm/string/predicate.hpp> |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | namespace v2 { |
| 32 | namespace validator_config { |
| 33 | |
| 34 | bool |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 35 | Checker::check(uint32_t pktType, const Name& pktName, const Name& klName, |
| 36 | const shared_ptr<ValidationState>& state) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 37 | { |
| 38 | BOOST_ASSERT(pktType == tlv::Interest || pktType == tlv::Data); |
| 39 | |
| 40 | if (pktType == tlv::Interest) { |
| 41 | if (pktName.size() < signed_interest::MIN_SIZE) |
| 42 | return false; |
| 43 | |
| 44 | return checkNames(pktName.getPrefix(-signed_interest::MIN_SIZE), klName, state); |
| 45 | } |
| 46 | else { |
| 47 | return checkNames(pktName, klName, state); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | NameRelationChecker::NameRelationChecker(const Name& name, const NameRelation& relation) |
| 52 | : m_name(name) |
| 53 | , m_relation(relation) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | bool |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 58 | NameRelationChecker::checkNames(const Name& pktName, const Name& klName, |
| 59 | const shared_ptr<ValidationState>& state) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 60 | { |
| 61 | // pktName not used in this check |
| 62 | Name identity = extractIdentityFromKeyName(klName); |
| 63 | bool result = checkNameRelation(m_relation, m_name, identity); |
| 64 | if (!result) { |
| 65 | std::ostringstream os; |
| 66 | os << "KeyLocator check failed: name relation " << m_name << " " << m_relation |
| 67 | << " for packet " << pktName << " is invalid" |
| 68 | << " (KeyLocator=" << klName << ", identity=" << identity << ")"; |
| 69 | state->fail({ValidationError::POLICY_ERROR, os.str()}); |
| 70 | } |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | RegexChecker::RegexChecker(const Regex& regex) |
| 75 | : m_regex(regex) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | RegexChecker::checkNames(const Name& pktName, const Name& klName, const shared_ptr<ValidationState>& state) |
| 81 | { |
Zhiyi Zhang | c4a0176 | 2017-10-11 12:07:25 -0700 | [diff] [blame] | 82 | bool result = m_regex.match(klName); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 83 | if (!result) { |
| 84 | std::ostringstream os; |
| 85 | os << "KeyLocator check failed: regex " << m_regex << " for packet " << pktName << " is invalid" |
Zhiyi Zhang | c4a0176 | 2017-10-11 12:07:25 -0700 | [diff] [blame] | 86 | << " (KeyLocator=" << klName << ")"; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 87 | state->fail({ValidationError::POLICY_ERROR, os.str()}); |
| 88 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 89 | return result; |
| 90 | } |
| 91 | |
| 92 | HyperRelationChecker::HyperRelationChecker(const std::string& pktNameExpr, const std::string pktNameExpand, |
| 93 | const std::string& klNameExpr, const std::string klNameExpand, |
| 94 | const NameRelation& hyperRelation) |
| 95 | : m_hyperPRegex(pktNameExpr, pktNameExpand) |
| 96 | , m_hyperKRegex(klNameExpr, klNameExpand) |
| 97 | , m_hyperRelation(hyperRelation) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | bool |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 102 | HyperRelationChecker::checkNames(const Name& pktName, const Name& klName, |
| 103 | const shared_ptr<ValidationState>& state) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 104 | { |
| 105 | if (!m_hyperPRegex.match(pktName) || !m_hyperKRegex.match(klName)) { |
| 106 | std::ostringstream os; |
| 107 | os << "Packet " << pktName << " (" << "KeyLocator=" << klName << ") does not match " |
| 108 | << "the hyper relation rule pkt=" << m_hyperPRegex << ", key=" << m_hyperKRegex; |
| 109 | state->fail({ValidationError::POLICY_ERROR, os.str()}); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool result = checkNameRelation(m_hyperRelation, m_hyperKRegex.expand(), m_hyperPRegex.expand()); |
| 114 | if (!result) { |
| 115 | std::ostringstream os; |
| 116 | os << "KeyLocator check failed: hyper relation " << m_hyperRelation |
| 117 | << " pkt=" << m_hyperPRegex << ", key=" << m_hyperKRegex |
| 118 | << " of packet " << pktName << " (KeyLocator=" << klName << ") is invalid"; |
| 119 | state->fail({ValidationError::POLICY_ERROR, os.str()}); |
| 120 | } |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | unique_ptr<Checker> |
| 125 | Checker::create(const ConfigSection& configSection, const std::string& configFilename) |
| 126 | { |
| 127 | auto propertyIt = configSection.begin(); |
| 128 | |
| 129 | // Get checker.type |
| 130 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 131 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.type>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | std::string type = propertyIt->second.data(); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 135 | if (boost::iequals(type, "customized")) { |
| 136 | return createCustomizedChecker(configSection, configFilename); |
| 137 | } |
| 138 | else if (boost::iequals(type, "hierarchical")) { |
| 139 | return createHierarchicalChecker(configSection, configFilename); |
| 140 | } |
| 141 | else { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 142 | BOOST_THROW_EXCEPTION(Error("Unrecognized <checker.type>: " + type)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | unique_ptr<Checker> |
| 147 | Checker::createCustomizedChecker(const ConfigSection& configSection, |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 148 | const std::string& configFilename) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 149 | { |
| 150 | auto propertyIt = configSection.begin(); |
| 151 | propertyIt++; |
| 152 | |
| 153 | // TODO implement restrictions based on signature type (outside this checker) |
| 154 | |
| 155 | if (propertyIt != configSection.end() && boost::iequals(propertyIt->first, "sig-type")) { |
| 156 | // ignore sig-type |
| 157 | propertyIt++; |
| 158 | } |
| 159 | |
| 160 | // Get checker.key-locator |
| 161 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "key-locator")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 162 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | auto checker = createKeyLocatorChecker(propertyIt->second, configFilename); |
| 166 | propertyIt++; |
| 167 | |
| 168 | if (propertyIt != configSection.end()) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 169 | BOOST_THROW_EXCEPTION(Error("Expecting end of <checker>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 170 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 171 | return checker; |
| 172 | } |
| 173 | |
| 174 | unique_ptr<Checker> |
| 175 | Checker::createHierarchicalChecker(const ConfigSection& configSection, |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 176 | const std::string& configFilename) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 177 | { |
| 178 | auto propertyIt = configSection.begin(); |
| 179 | propertyIt++; |
| 180 | |
| 181 | // TODO implement restrictions based on signature type (outside this checker) |
| 182 | |
| 183 | if (propertyIt != configSection.end() && boost::iequals(propertyIt->first, "sig-type")) { |
| 184 | // ignore sig-type |
| 185 | propertyIt++; |
| 186 | } |
| 187 | |
| 188 | if (propertyIt != configSection.end()) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 189 | BOOST_THROW_EXCEPTION(Error("Expecting end of <checker>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 190 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 191 | return make_unique<HyperRelationChecker>("^(<>*)$", "\\1", |
| 192 | "^(<>*)<KEY><>$", "\\1", |
| 193 | NameRelation::IS_PREFIX_OF); |
| 194 | } |
| 195 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 196 | unique_ptr<Checker> |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 197 | Checker::createKeyLocatorChecker(const ConfigSection& configSection, |
| 198 | const std::string& configFilename) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 199 | { |
| 200 | auto propertyIt = configSection.begin(); |
| 201 | |
| 202 | // Get checker.key-locator.type |
| 203 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 204 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.type>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 205 | |
| 206 | std::string type = propertyIt->second.data(); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 207 | if (boost::iequals(type, "name")) |
| 208 | return createKeyLocatorNameChecker(configSection, configFilename); |
| 209 | else |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 210 | BOOST_THROW_EXCEPTION(Error("Unrecognized <checker.key-locator.type>: " + type)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | unique_ptr<Checker> |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 214 | Checker::createKeyLocatorNameChecker(const ConfigSection& configSection, |
| 215 | const std::string& configFilename) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 216 | { |
| 217 | auto propertyIt = configSection.begin(); |
| 218 | propertyIt++; |
| 219 | |
| 220 | if (propertyIt == configSection.end()) |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 221 | BOOST_THROW_EXCEPTION(Error("Unexpected end of <checker.key-locator>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 222 | |
| 223 | if (boost::iequals(propertyIt->first, "name")) { |
| 224 | Name name; |
| 225 | try { |
| 226 | name = Name(propertyIt->second.data()); |
| 227 | } |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 228 | catch (const Name::Error&) { |
| 229 | BOOST_THROW_EXCEPTION(Error("Invalid <checker.key-locator.name>: " + propertyIt->second.data())); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 230 | } |
| 231 | propertyIt++; |
| 232 | |
| 233 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 234 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.relation>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | std::string relationString = propertyIt->second.data(); |
| 238 | propertyIt++; |
| 239 | |
| 240 | NameRelation relation = getNameRelationFromString(relationString); |
| 241 | |
| 242 | if (propertyIt != configSection.end()) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 243 | BOOST_THROW_EXCEPTION(Error("Expecting end of <checker.key-locator>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 244 | } |
| 245 | return make_unique<NameRelationChecker>(name, relation); |
| 246 | } |
| 247 | else if (boost::iequals(propertyIt->first, "regex")) { |
| 248 | std::string regexString = propertyIt->second.data(); |
| 249 | propertyIt++; |
| 250 | |
| 251 | if (propertyIt != configSection.end()) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 252 | BOOST_THROW_EXCEPTION(Error("Expecting end of <checker.key-locator>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | try { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame] | 256 | return make_unique<RegexChecker>(Regex(regexString)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 257 | } |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 258 | catch (const Regex::Error&) { |
| 259 | BOOST_THROW_EXCEPTION(Error("Invalid <checker.key-locator.regex>: " + regexString)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | else if (boost::iequals(propertyIt->first, "hyper-relation")) { |
| 263 | const ConfigSection& hSection = propertyIt->second; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 264 | ConfigSection::const_iterator hPropertyIt = hSection.begin(); |
| 265 | |
| 266 | // Get k-regex |
| 267 | if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "k-regex")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 268 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.hyper-relation.k-regex>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | std::string kRegex = hPropertyIt->second.data(); |
| 272 | hPropertyIt++; |
| 273 | |
| 274 | // Get k-expand |
| 275 | if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "k-expand")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 276 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.hyper-relation.k-expand>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | std::string kExpand = hPropertyIt->second.data(); |
| 280 | hPropertyIt++; |
| 281 | |
| 282 | // Get h-relation |
| 283 | if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "h-relation")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 284 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.hyper-relation.h-relation>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | std::string hRelation = hPropertyIt->second.data(); |
| 288 | hPropertyIt++; |
| 289 | |
| 290 | // Get p-regex |
| 291 | if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "p-regex")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 292 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.hyper-relation.p-regex>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | std::string pRegex = hPropertyIt->second.data(); |
| 296 | hPropertyIt++; |
| 297 | |
| 298 | // Get p-expand |
| 299 | if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "p-expand")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 300 | BOOST_THROW_EXCEPTION(Error("Expecting <checker.key-locator.hyper-relation.p-expand>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | std::string pExpand = hPropertyIt->second.data(); |
| 304 | hPropertyIt++; |
| 305 | |
| 306 | if (hPropertyIt != hSection.end()) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 307 | BOOST_THROW_EXCEPTION(Error("Expecting end of <checker.key-locator.hyper-relation>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | NameRelation relation = getNameRelationFromString(hRelation); |
| 311 | try { |
| 312 | return make_unique<HyperRelationChecker>(pRegex, pExpand, kRegex, kExpand, relation); |
| 313 | } |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 314 | catch (const Regex::Error&) { |
| 315 | BOOST_THROW_EXCEPTION(Error("Invalid regex for <key-locator.hyper-relation>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | else { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame^] | 319 | BOOST_THROW_EXCEPTION(Error("Unrecognized <checker.key-locator>: " + propertyIt->first)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | } // namespace validator_config |
| 324 | } // namespace v2 |
| 325 | } // namespace security |
| 326 | } // namespace ndn |