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 | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 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 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/validator-config/checker.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/security/validation-policy.hpp" |
| 24 | #include "ndn-cxx/security/validation-state.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 27 | #include "tests/unit/security/validator-fixture.hpp" |
| 28 | #include "tests/unit/security/validator-config/common.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::tests { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | using namespace ndn::security::validator_config; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(TestChecker) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 37 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 38 | class CheckerFixture : public KeyChainFixture |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | CheckerFixture() |
| 42 | { |
| 43 | names.push_back("/foo/bar"); |
| 44 | names.push_back("/foo/bar/bar"); |
| 45 | names.push_back("/foo"); |
| 46 | names.push_back("/other/prefix"); |
| 47 | } |
| 48 | |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 49 | static Name |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 50 | makeKeyLocatorKeyName(const Name& name) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 51 | { |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 52 | static PartialName suffix("KEY/keyid"); |
| 53 | return Name(name).append(suffix); |
| 54 | } |
| 55 | |
| 56 | static Name |
| 57 | makeKeyLocatorCertName(const Name& name) |
| 58 | { |
| 59 | static PartialName suffix("KEY/keyid/issuer/v=1"); |
| 60 | return Name(name).append(suffix); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 63 | template<typename PktType, typename C> |
| 64 | static void |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 65 | testChecker(C& checker, tlv::SignatureTypeValue sigType, const Name& pktName, const Name& klName, bool expectedOutcome) |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 66 | { |
| 67 | BOOST_TEST_CONTEXT("pkt=" << pktName << " kl=" << klName) { |
| 68 | auto state = PktType::makeState(); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 69 | auto result = checker.check(PktType::getType(), sigType, pktName, klName, *state); |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(bool(result), expectedOutcome); |
| 71 | BOOST_CHECK(boost::logic::indeterminate(state->getOutcome())); |
| 72 | if (!result) { |
| 73 | BOOST_CHECK_NE(result.getErrorMessage(), ""); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 78 | public: |
| 79 | std::vector<Name> names; |
| 80 | }; |
| 81 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 82 | class NameRelationEqual : public CheckerFixture |
| 83 | { |
| 84 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 85 | NameRelationChecker checker{tlv::SignatureSha256WithRsa, "/foo/bar", NameRelation::EQUAL}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 86 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 87 | {true, false, false, false}, |
| 88 | {true, false, false, false}, |
| 89 | {true, false, false, false}}; |
| 90 | }; |
| 91 | |
| 92 | class NameRelationIsPrefixOf : public CheckerFixture |
| 93 | { |
| 94 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 95 | NameRelationChecker checker{tlv::SignatureSha256WithRsa, "/foo/bar", NameRelation::IS_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 96 | std::vector<std::vector<bool>> outcomes = {{true, true, false, false}, |
| 97 | {true, true, false, false}, |
| 98 | {true, true, false, false}, |
| 99 | {true, true, false, false}}; |
| 100 | }; |
| 101 | |
| 102 | class NameRelationIsStrictPrefixOf : public CheckerFixture |
| 103 | { |
| 104 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 105 | NameRelationChecker checker{tlv::SignatureSha256WithRsa, "/foo/bar", NameRelation::IS_STRICT_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 106 | std::vector<std::vector<bool>> outcomes = {{false, true, false, false}, |
| 107 | {false, true, false, false}, |
| 108 | {false, true, false, false}, |
| 109 | {false, true, false, false}}; |
| 110 | }; |
| 111 | |
| 112 | class RegexEqual : public CheckerFixture |
| 113 | { |
| 114 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 115 | RegexChecker checker{tlv::SignatureSha256WithRsa, Regex("^<foo><bar><KEY><>{1,3}$")}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 116 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 117 | {true, false, false, false}, |
| 118 | {true, false, false, false}, |
| 119 | {true, false, false, false}}; |
| 120 | }; |
| 121 | |
| 122 | class RegexIsPrefixOf : public CheckerFixture |
| 123 | { |
| 124 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 125 | RegexChecker checker{tlv::SignatureSha256WithRsa, Regex("^<foo><bar><>*<KEY><>{1,3}$")}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 126 | std::vector<std::vector<bool>> outcomes = {{true, true, false, false}, |
| 127 | {true, true, false, false}, |
| 128 | {true, true, false, false}, |
| 129 | {true, true, false, false}}; |
| 130 | }; |
| 131 | |
| 132 | class RegexIsStrictPrefixOf : public CheckerFixture |
| 133 | { |
| 134 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 135 | RegexChecker checker{tlv::SignatureSha256WithRsa, Regex("^<foo><bar><>+<KEY><>{1,3}$")}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 136 | std::vector<std::vector<bool>> outcomes = {{false, true, false, false}, |
| 137 | {false, true, false, false}, |
| 138 | {false, true, false, false}, |
| 139 | {false, true, false, false}}; |
| 140 | }; |
| 141 | |
| 142 | class HyperRelationEqual : public CheckerFixture |
| 143 | { |
| 144 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 145 | HyperRelationChecker checker{tlv::SignatureSha256WithRsa, |
| 146 | "^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::EQUAL}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 147 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 148 | {false, true, false, false}, |
| 149 | {false, false, true, false}, |
| 150 | {false, false, false, true}}; |
| 151 | }; |
| 152 | |
| 153 | class HyperRelationIsPrefixOf : public CheckerFixture |
| 154 | { |
| 155 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 156 | HyperRelationChecker checker{tlv::SignatureSha256WithRsa, |
| 157 | "^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 158 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 159 | {true, true, true, false}, |
| 160 | {false, false, true, false}, |
| 161 | {false, false, false, true}}; |
| 162 | }; |
| 163 | |
| 164 | class HyperRelationIsStrictPrefixOf : public CheckerFixture |
| 165 | { |
| 166 | public: |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 167 | HyperRelationChecker checker{tlv::SignatureSha256WithRsa, |
| 168 | "^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_STRICT_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 169 | std::vector<std::vector<bool>> outcomes = {{false, false, true, false}, |
| 170 | {true, false, true, false}, |
| 171 | {false, false, false, false}, |
| 172 | {false, false, false, false}}; |
| 173 | }; |
| 174 | |
| 175 | class Hierarchical : public CheckerFixture |
| 176 | { |
| 177 | public: |
| 178 | Hierarchical() |
| 179 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 180 | type hierarchical |
| 181 | sig-type rsa-sha256 |
| 182 | )CONF"), "test-config")) |
| 183 | , checker(*checkerPtr) |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | public: |
| 188 | std::unique_ptr<Checker> checkerPtr; |
| 189 | Checker& checker; |
| 190 | |
| 191 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 192 | {true, true, true, false}, |
| 193 | {false, false, true, false}, |
| 194 | {false, false, false, true}}; |
| 195 | }; |
| 196 | |
| 197 | class CustomizedNameRelation : public CheckerFixture |
| 198 | { |
| 199 | public: |
| 200 | CustomizedNameRelation() |
| 201 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 202 | type customized |
| 203 | sig-type rsa-sha256 |
| 204 | key-locator |
| 205 | { |
| 206 | type name |
| 207 | name /foo/bar |
| 208 | relation equal |
| 209 | } |
| 210 | )CONF"), "test-config")) |
| 211 | , checker(*checkerPtr) |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | public: |
| 216 | std::unique_ptr<Checker> checkerPtr; |
| 217 | Checker& checker; |
| 218 | |
| 219 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 220 | {true, false, false, false}, |
| 221 | {true, false, false, false}, |
| 222 | {true, false, false, false}}; |
| 223 | }; |
| 224 | |
| 225 | class CustomizedRegex : public CheckerFixture |
| 226 | { |
| 227 | public: |
| 228 | CustomizedRegex() |
| 229 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 230 | type customized |
| 231 | sig-type rsa-sha256 |
| 232 | key-locator |
| 233 | { |
| 234 | type name |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 235 | regex ^<foo><bar><KEY><>{1,3}$ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 236 | } |
| 237 | )CONF"), "test-config")) |
| 238 | , checker(*checkerPtr) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | public: |
| 243 | std::unique_ptr<Checker> checkerPtr; |
| 244 | Checker& checker; |
| 245 | |
| 246 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 247 | {true, false, false, false}, |
| 248 | {true, false, false, false}, |
| 249 | {true, false, false, false}}; |
| 250 | }; |
| 251 | |
| 252 | class CustomizedHyperRelation : public CheckerFixture |
| 253 | { |
| 254 | public: |
| 255 | CustomizedHyperRelation() |
| 256 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 257 | type customized |
| 258 | sig-type rsa-sha256 |
| 259 | key-locator |
| 260 | { |
| 261 | type name |
| 262 | hyper-relation |
| 263 | { |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 264 | k-regex ^(<>+)<KEY><>{1,3}$ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 265 | k-expand \\1 |
| 266 | h-relation is-prefix-of |
| 267 | p-regex ^(<>+)$ |
| 268 | p-expand \\1 |
| 269 | } |
| 270 | } |
| 271 | )CONF"), "test-config")) |
| 272 | , checker(*checkerPtr) |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | public: |
| 277 | std::unique_ptr<Checker> checkerPtr; |
| 278 | Checker& checker; |
| 279 | |
| 280 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 281 | {true, true, true, false}, |
| 282 | {false, false, true, false}, |
| 283 | {false, false, false, true}}; |
| 284 | }; |
| 285 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 286 | using CheckerFixtures = boost::mpl::vector< |
| 287 | NameRelationEqual, |
| 288 | NameRelationIsPrefixOf, |
| 289 | NameRelationIsStrictPrefixOf, |
| 290 | RegexEqual, |
| 291 | RegexIsPrefixOf, |
| 292 | RegexIsStrictPrefixOf, |
| 293 | HyperRelationEqual, |
| 294 | HyperRelationIsPrefixOf, |
| 295 | HyperRelationIsStrictPrefixOf, |
| 296 | Hierarchical, |
| 297 | CustomizedNameRelation, |
| 298 | CustomizedRegex, |
| 299 | CustomizedHyperRelation |
| 300 | >; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 301 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 302 | // Cartesian product of [DataPkt, InterestV02Pkt, InterestV03Pkt] and CheckerFixtures. |
| 303 | // Each element is a boost::mpl::pair<PktType, CheckerFixture>. |
| 304 | using Tests = boost::mpl::fold< |
| 305 | CheckerFixtures, |
| 306 | boost::mpl::vector<>, |
| 307 | boost::mpl::push_back<boost::mpl::push_back<boost::mpl::push_back<boost::mpl::_1, |
| 308 | boost::mpl::pair<DataPkt, boost::mpl::_2>>, |
| 309 | boost::mpl::pair<InterestV02Pkt, boost::mpl::_2>>, |
| 310 | boost::mpl::pair<InterestV03Pkt, boost::mpl::_2>> |
| 311 | >::type; |
| 312 | |
| 313 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T::second) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 314 | { |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame] | 315 | using PktType = typename T::first; |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 316 | |
| 317 | BOOST_REQUIRE_EQUAL(this->outcomes.size(), this->names.size()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 318 | for (size_t i = 0; i < this->names.size(); ++i) { |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 319 | BOOST_REQUIRE_EQUAL(this->outcomes[i].size(), this->names.size()); |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 320 | |
| 321 | auto pktName = PktType::makeName(this->names[i], this->m_keyChain); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 322 | for (size_t j = 0; j < this->names.size(); ++j) { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 323 | bool expectedOutcome = this->outcomes[i][j]; |
| 324 | |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 325 | auto klName = this->makeKeyLocatorKeyName(this->names[j]); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 326 | this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithRsa, pktName, klName, expectedOutcome); |
| 327 | this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithEcdsa, pktName, klName, false); |
| 328 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 329 | |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 330 | klName = this->makeKeyLocatorCertName(this->names[j]); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 331 | this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithRsa, pktName, klName, expectedOutcome); |
| 332 | this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithEcdsa, pktName, klName, false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | BOOST_AUTO_TEST_SUITE_END() // TestChecker |
| 338 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 339 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 340 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 341 | } // namespace ndn::tests |