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