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 | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 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/validation-policy-config.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/security/transform/base64-encode.hpp" |
| 25 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 26 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 28 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 29 | #include "tests/unit/security/validator-config/common.hpp" |
| 30 | #include "tests/unit/security/validator-fixture.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 32 | #include <boost/mp11/list.hpp> |
| 33 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 34 | namespace ndn::tests { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 36 | using ndn::security::ValidationPolicyConfig; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(TestValidationPolicyConfig) |
| 40 | |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 41 | BOOST_FIXTURE_TEST_CASE(EmptyConfig, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 42 | { |
| 43 | this->policy.load(ConfigSection{}, "<empty>"); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 46 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 47 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 48 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 49 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 50 | Data d("/Security/ValidationPolicyConfig/D"); |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 51 | this->m_keyChain.sign(d, signingByIdentity(this->identity)); |
| 52 | VALIDATE_FAILURE(d, "Empty policy should reject everything"); |
| 53 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 54 | Interest i("/Security/ValidationPolicyConfig/I"); |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 55 | this->m_keyChain.sign(i, signingByIdentity(this->identity)); |
| 56 | VALIDATE_FAILURE(i, "Empty policy should reject everything"); |
| 57 | } |
| 58 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 59 | template<typename PacketType> |
| 60 | class ValidationPolicyConfigFixture : public HierarchicalValidatorFixture<ValidationPolicyConfig> |
| 61 | { |
| 62 | public: |
| 63 | ValidationPolicyConfigFixture() |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 64 | { |
| 65 | boost::filesystem::create_directories(path); |
| 66 | baseConfig = R"CONF( |
| 67 | rule |
| 68 | { |
| 69 | id test-rule-id |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 70 | for )CONF" + std::string(getPacketName()) + R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 71 | filter |
| 72 | { |
| 73 | type name |
| 74 | name )CONF" + identity.getName().toUri() + R"CONF( |
| 75 | relation is-prefix-of |
| 76 | } |
| 77 | checker |
| 78 | { |
| 79 | type hierarchical |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 80 | sig-type ecdsa-sha256 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | )CONF"; |
| 84 | } |
| 85 | |
| 86 | ~ValidationPolicyConfigFixture() |
| 87 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 88 | boost::filesystem::remove_all(path); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 91 | private: |
| 92 | static constexpr std::string_view |
| 93 | getPacketName() |
| 94 | { |
| 95 | if constexpr (std::is_same_v<PacketType, Interest>) |
| 96 | return "interest"; |
| 97 | else if constexpr (std::is_same_v<PacketType, Data>) |
| 98 | return "data"; |
| 99 | else |
| 100 | return ""; |
| 101 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 102 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 103 | protected: |
| 104 | const boost::filesystem::path path{boost::filesystem::path(UNIT_TESTS_TMPDIR) / "security" / "validation-policy-config"}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 105 | std::string baseConfig; |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 106 | |
| 107 | using Packet = PacketType; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | template<typename PacketType> |
| 111 | class LoadStringWithFileAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 112 | { |
| 113 | public: |
| 114 | LoadStringWithFileAnchor() |
| 115 | { |
| 116 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 117 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 118 | this->saveIdentityCert(this->identity, (this->path / "identity.ndncert").string()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 119 | this->policy.load(this->baseConfig + R"CONF( |
| 120 | trust-anchor |
| 121 | { |
| 122 | type file |
| 123 | file-name "trust-anchor.ndncert" |
| 124 | } |
| 125 | )CONF", (this->path / "test-config").string()); |
| 126 | |
| 127 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 128 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | template<typename PacketType> |
| 133 | class LoadFileWithFileAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 134 | { |
| 135 | public: |
| 136 | LoadFileWithFileAnchor() |
| 137 | { |
| 138 | std::string configFile = (this->path / "config.conf").string(); |
| 139 | { |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 140 | std::ofstream config(configFile); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 141 | config << this->baseConfig << R"CONF( |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 142 | trust-anchor |
| 143 | { |
| 144 | type file |
| 145 | file-name "trust-anchor.ndncert" |
| 146 | } |
| 147 | )CONF"; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 148 | } |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 149 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 150 | this->saveIdentityCert(this->identity, (this->path / "identity.ndncert").string()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 151 | |
| 152 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 153 | |
| 154 | this->policy.load(configFile); |
| 155 | |
| 156 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 157 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | template<typename PacketType> |
Varun Patil | 574c3f9 | 2020-11-25 09:07:33 +0530 | [diff] [blame] | 162 | class LoadFileWithMultipleFileAnchors : public ValidationPolicyConfigFixture<PacketType> |
| 163 | { |
| 164 | public: |
| 165 | LoadFileWithMultipleFileAnchors() |
| 166 | { |
| 167 | std::string configFile = (this->path / "config.conf").string(); |
| 168 | { |
| 169 | std::ofstream config(configFile); |
| 170 | config << this->baseConfig << R"CONF( |
| 171 | trust-anchor |
| 172 | { |
| 173 | type file |
| 174 | file-name "identity.ndncert" |
| 175 | } |
| 176 | trust-anchor |
| 177 | { |
| 178 | type file |
| 179 | file-name "trust-anchor.ndncert" |
| 180 | } |
| 181 | )CONF"; |
| 182 | } |
| 183 | |
| 184 | this->saveIdentityCert(this->identity, (this->path / "identity.ndncert").string()); |
| 185 | |
| 186 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 187 | |
| 188 | this->policy.load(configFile); |
| 189 | |
| 190 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 191 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | template<typename PacketType> |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 196 | class LoadSectionWithFileAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 197 | { |
| 198 | public: |
| 199 | LoadSectionWithFileAnchor() |
| 200 | { |
| 201 | auto section = makeSection(this->baseConfig + R"CONF( |
| 202 | trust-anchor |
| 203 | { |
| 204 | type file |
| 205 | file-name "trust-anchor.ndncert" |
| 206 | } |
| 207 | )CONF"); |
| 208 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 209 | this->saveIdentityCert(this->identity, (this->path / "identity.ndncert").string()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 210 | |
| 211 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 212 | |
| 213 | this->policy.load(section, (this->path / "test-config").string()); |
| 214 | |
| 215 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 216 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | template<typename PacketType> |
| 221 | class LoadStringWithBase64Anchor : public ValidationPolicyConfigFixture<PacketType> |
| 222 | { |
| 223 | public: |
| 224 | LoadStringWithBase64Anchor() |
| 225 | { |
| 226 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 227 | |
| 228 | std::ostringstream os; |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 229 | { |
| 230 | using namespace ndn::security::transform; |
| 231 | const auto& cert = this->identity.getDefaultKey().getDefaultCertificate().wireEncode(); |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 232 | bufferSource(cert) >> base64Encode(false) >> streamSink(os); |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 233 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 234 | |
| 235 | this->policy.load(this->baseConfig + R"CONF( |
| 236 | trust-anchor |
| 237 | { |
| 238 | type base64 |
| 239 | base64-string ")CONF" + os.str() + R"CONF(" |
| 240 | } |
| 241 | )CONF", (this->path / "test-config").string()); |
| 242 | |
| 243 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 244 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 245 | } |
| 246 | }; |
| 247 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 248 | struct NoRefresh |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 249 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 250 | static std::string |
| 251 | getRefreshString() |
| 252 | { |
| 253 | return ""; |
| 254 | } |
| 255 | }; |
| 256 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 257 | struct Refresh1h |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 258 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 259 | static std::string |
| 260 | getRefreshString() |
| 261 | { |
| 262 | return "refresh 1h"; |
| 263 | } |
| 264 | |
| 265 | static time::milliseconds |
| 266 | getRefreshTime() |
| 267 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 268 | return 1_h; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 269 | } |
| 270 | }; |
| 271 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 272 | struct Refresh1m |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 273 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 274 | static std::string |
| 275 | getRefreshString() |
| 276 | { |
| 277 | return "refresh 1m"; |
| 278 | } |
| 279 | |
| 280 | static time::milliseconds |
| 281 | getRefreshTime() |
| 282 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 283 | return 1_min; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 284 | } |
| 285 | }; |
| 286 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 287 | struct Refresh1s |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 288 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 289 | static std::string |
| 290 | getRefreshString() |
| 291 | { |
| 292 | return "refresh 1s"; |
| 293 | } |
| 294 | |
| 295 | static time::milliseconds |
| 296 | getRefreshTime() |
| 297 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 298 | return 1_s; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 299 | } |
| 300 | }; |
| 301 | |
| 302 | template<typename PacketType, typename Refresh = NoRefresh> |
| 303 | class LoadStringWithDirAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 304 | { |
| 305 | public: |
| 306 | LoadStringWithDirAnchor() |
| 307 | { |
| 308 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 309 | |
| 310 | boost::filesystem::create_directories(this->path / "keys"); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 311 | this->saveIdentityCert(this->identity, (this->path / "keys" / "identity.ndncert").string()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 312 | |
| 313 | this->policy.load(this->baseConfig + R"CONF( |
| 314 | trust-anchor |
| 315 | { |
| 316 | type dir |
| 317 | dir keys |
| 318 | )CONF" + Refresh::getRefreshString() + R"CONF( |
| 319 | } |
| 320 | )CONF", (this->path / "test-config").string()); |
| 321 | |
| 322 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 323 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 324 | } |
| 325 | }; |
| 326 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 327 | template<typename PacketType> |
| 328 | using Policies = boost::mp11::mp_list< |
| 329 | LoadStringWithFileAnchor<PacketType>, |
| 330 | LoadFileWithFileAnchor<PacketType>, |
| 331 | LoadFileWithMultipleFileAnchors<PacketType>, |
| 332 | LoadSectionWithFileAnchor<PacketType>, |
| 333 | LoadStringWithBase64Anchor<PacketType>, |
| 334 | LoadStringWithDirAnchor<PacketType>, |
| 335 | LoadStringWithDirAnchor<PacketType, Refresh1h>, |
| 336 | LoadStringWithDirAnchor<PacketType, Refresh1m>, |
| 337 | LoadStringWithDirAnchor<PacketType, Refresh1s> |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 338 | >; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 339 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 340 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateData, Policy, Policies<Data>, Policy) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 341 | { |
| 342 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 1); |
| 343 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 344 | |
| 345 | using Packet = typename Policy::Packet; |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 346 | Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 347 | |
| 348 | Packet packet = unsignedPacket; |
| 349 | VALIDATE_FAILURE(packet, "Unsigned"); |
| 350 | |
| 351 | packet = unsignedPacket; |
| 352 | this->m_keyChain.sign(packet, signingWithSha256()); |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 353 | VALIDATE_FAILURE(packet, "Should not be accepted, doesn't pass checker /localhost/identity/digest-sha256"); |
| 354 | |
| 355 | packet = Packet("/localhost/identity/digest-sha256/foobar"); |
| 356 | this->m_keyChain.sign(packet, signingWithSha256()); |
| 357 | VALIDATE_FAILURE(packet, "Should not be accepted, no rule for the name /localhost/identity/digest-sha256"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 358 | |
| 359 | packet = unsignedPacket; |
| 360 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 361 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor"); |
| 362 | |
| 363 | packet = unsignedPacket; |
| 364 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 365 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the policy-compliant cert"); |
| 366 | |
| 367 | packet = unsignedPacket; |
| 368 | this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity)); |
| 369 | VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert"); |
| 370 | |
| 371 | packet = unsignedPacket; |
| 372 | this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity)); |
| 373 | VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor"); |
| 374 | } |
| 375 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 376 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateInterest, Policy, Policies<Interest>, Policy) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 377 | { |
| 378 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 379 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 1); |
| 380 | |
| 381 | using Packet = typename Policy::Packet; |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 382 | Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 383 | |
| 384 | Packet packet = unsignedPacket; |
| 385 | VALIDATE_FAILURE(packet, "Unsigned"); |
| 386 | |
| 387 | packet = unsignedPacket; |
| 388 | this->m_keyChain.sign(packet, signingWithSha256()); |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 389 | VALIDATE_FAILURE(packet, "Should not be accepted, doesn't pass checker /localhost/identity/digest-sha256"); |
| 390 | |
| 391 | packet = Packet("/localhost/identity/digest-sha256/foobar"); |
| 392 | this->m_keyChain.sign(packet, signingWithSha256()); |
| 393 | VALIDATE_FAILURE(packet, "Should not be accepted, no rule for the name /localhost/identity/digest-sha256"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 394 | |
| 395 | packet = unsignedPacket; |
| 396 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 397 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor"); |
| 398 | |
| 399 | packet = unsignedPacket; |
| 400 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 401 | VALIDATE_FAILURE(packet, "Should fail, as there is no matching rule for data"); |
| 402 | |
| 403 | packet = unsignedPacket; |
| 404 | this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity)); |
| 405 | VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert"); |
| 406 | |
| 407 | packet = unsignedPacket; |
| 408 | this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity)); |
| 409 | VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor"); |
| 410 | } |
| 411 | |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 412 | BOOST_FIXTURE_TEST_CASE(DigestSha256, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 413 | { |
| 414 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 415 | this->policy.load(R"CONF( |
| 416 | rule |
| 417 | { |
| 418 | id test-rule-data-id |
| 419 | for data |
| 420 | filter |
| 421 | { |
| 422 | type name |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 423 | name /Security/ValidatorFixture |
| 424 | relation is-prefix-of |
| 425 | } |
| 426 | checker |
| 427 | { |
| 428 | type customized |
| 429 | sig-type sha256 |
| 430 | } |
| 431 | } |
| 432 | rule |
| 433 | { |
| 434 | id test-rule-interest-id |
| 435 | for interest |
| 436 | filter |
| 437 | { |
| 438 | type name |
| 439 | name /Security/ValidatorFixture |
| 440 | relation is-prefix-of |
| 441 | } |
| 442 | checker |
| 443 | { |
| 444 | type customized |
| 445 | sig-type sha256 |
| 446 | } |
| 447 | } |
| 448 | )CONF", "test-config"); |
| 449 | |
| 450 | |
| 451 | Interest interest("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 452 | this->m_keyChain.sign(interest, signingWithSha256()); |
| 453 | VALIDATE_SUCCESS(interest, "Should be accepted"); |
| 454 | |
| 455 | Data data("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
| 456 | this->m_keyChain.sign(data, signingWithSha256()); |
| 457 | VALIDATE_SUCCESS(data, "Should be accepted"); |
| 458 | } |
| 459 | |
| 460 | BOOST_FIXTURE_TEST_CASE(DigestSha256WithKeyLocator, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 461 | { |
| 462 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 463 | this->policy.load(R"CONF( |
| 464 | rule |
| 465 | { |
| 466 | id test-rule-data-id |
| 467 | for data |
| 468 | filter |
| 469 | { |
| 470 | type name |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 471 | name /localhost/identity/digest-sha256 |
| 472 | relation is-prefix-of |
| 473 | } |
| 474 | checker |
| 475 | { |
| 476 | type customized |
| 477 | sig-type sha256 |
| 478 | key-locator |
| 479 | { |
| 480 | type name |
| 481 | hyper-relation |
| 482 | { |
| 483 | k-regex ^(<>*)$ |
| 484 | k-expand \\1 |
| 485 | h-relation is-prefix-of |
| 486 | p-regex ^(<>*)$ |
| 487 | p-expand \\1 |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | rule |
| 493 | { |
| 494 | id test-rule-interest-id |
| 495 | for interest |
| 496 | filter |
| 497 | { |
| 498 | type name |
| 499 | name /localhost/identity/digest-sha256 |
| 500 | relation is-prefix-of |
| 501 | } |
| 502 | checker |
| 503 | { |
| 504 | type customized |
| 505 | sig-type sha256 |
| 506 | key-locator |
| 507 | { |
| 508 | type name |
| 509 | hyper-relation |
| 510 | { |
| 511 | k-regex ^(<>*)$ |
| 512 | k-expand \\1 |
| 513 | h-relation is-prefix-of |
| 514 | p-regex ^(<>*)$ |
| 515 | p-expand \\1 |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | )CONF", "test-config"); |
| 521 | |
| 522 | |
| 523 | Interest interest("/localhost/identity/digest-sha256/foobar"); |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 524 | this->m_keyChain.sign(interest, signingWithSha256()); |
| 525 | VALIDATE_SUCCESS(interest, "Should be accepted"); |
| 526 | |
| 527 | Data data("/localhost/identity/digest-sha256/foobar"); |
| 528 | this->m_keyChain.sign(data, signingWithSha256()); |
| 529 | VALIDATE_SUCCESS(data, "Should be accepted"); |
| 530 | } |
| 531 | |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 532 | BOOST_FIXTURE_TEST_CASE(SigTypeCheck, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 533 | { |
| 534 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 535 | this->policy.load(R"CONF( |
| 536 | rule |
| 537 | { |
| 538 | id test-rule-data-id |
| 539 | for data |
| 540 | filter |
| 541 | { |
| 542 | type name |
| 543 | name /localhost/identity/digest-sha256 |
| 544 | relation is-prefix-of |
| 545 | } |
| 546 | checker |
| 547 | { |
| 548 | type customized |
| 549 | sig-type ecdsa-sha256 |
| 550 | key-locator |
| 551 | { |
| 552 | type name |
| 553 | hyper-relation |
| 554 | { |
| 555 | k-regex ^(<>*)$ |
| 556 | k-expand \\1 |
| 557 | h-relation is-prefix-of |
| 558 | p-regex ^(<>*)$ |
| 559 | p-expand \\1 |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | rule |
| 565 | { |
| 566 | id test-rule-interest-id |
| 567 | for interest |
| 568 | filter |
| 569 | { |
| 570 | type name |
| 571 | name /localhost/identity/digest-sha256 |
| 572 | relation is-prefix-of |
| 573 | } |
| 574 | checker |
| 575 | { |
| 576 | type customized |
| 577 | sig-type ecdsa-sha256 |
| 578 | key-locator |
| 579 | { |
| 580 | type name |
| 581 | hyper-relation |
| 582 | { |
| 583 | k-regex ^(<>*)$ |
| 584 | k-expand \\1 |
| 585 | h-relation is-prefix-of |
| 586 | p-regex ^(<>*)$ |
| 587 | p-expand \\1 |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | )CONF", "test-config"); |
| 593 | |
| 594 | |
| 595 | Interest interest("/localhost/identity/digest-sha256/foobar"); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 596 | this->m_keyChain.sign(interest, signingWithSha256()); |
| 597 | VALIDATE_FAILURE(interest, "Signature type check should fail"); |
| 598 | |
| 599 | Data data("/localhost/identity/digest-sha256/foobar"); |
| 600 | this->m_keyChain.sign(data, signingWithSha256()); |
| 601 | VALIDATE_FAILURE(data, "Signature type check should fail"); |
| 602 | } |
| 603 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 604 | BOOST_FIXTURE_TEST_CASE(Reload, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 605 | { |
| 606 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 607 | this->policy.load(R"CONF( |
| 608 | rule |
| 609 | { |
| 610 | id test-rule-data-id |
| 611 | for data |
| 612 | filter |
| 613 | { |
| 614 | type name |
| 615 | name /foo/bar |
| 616 | relation is-prefix-of |
| 617 | } |
| 618 | checker |
| 619 | { |
| 620 | type hierarchical |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 621 | sig-type ecdsa-sha256 |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | rule |
| 625 | { |
| 626 | id test-rule-interest-id |
| 627 | for interest |
| 628 | filter |
| 629 | { |
| 630 | type name |
| 631 | name /foo/bar |
| 632 | relation is-prefix-of |
| 633 | } |
| 634 | checker |
| 635 | { |
| 636 | type hierarchical |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 637 | sig-type ecdsa-sha256 |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | trust-anchor |
| 641 | { |
| 642 | type dir |
| 643 | dir keys |
| 644 | refresh 1h |
| 645 | } |
| 646 | )CONF", "test-config"); |
| 647 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 648 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 649 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 1); |
| 650 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 1); |
| 651 | |
| 652 | this->policy.load(R"CONF( |
| 653 | trust-anchor |
| 654 | { |
| 655 | type any |
| 656 | } |
| 657 | )CONF", "test-config"); |
| 658 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 659 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, true); |
| 660 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 661 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 662 | } |
| 663 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 664 | using Packets = boost::mp11::mp_list<Interest, Data>; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 665 | |
| 666 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(TrustAnchorWildcard, Packet, Packets, ValidationPolicyConfigFixture<Packet>) |
| 667 | { |
| 668 | this->policy.load(R"CONF( |
| 669 | trust-anchor |
| 670 | { |
| 671 | type any |
| 672 | } |
| 673 | )CONF", "test-config"); |
| 674 | |
| 675 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 676 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, true); |
| 677 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 678 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 679 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 680 | Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 681 | |
| 682 | Packet packet = unsignedPacket; |
| 683 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 684 | |
| 685 | packet = unsignedPacket; |
| 686 | this->m_keyChain.sign(packet, signingWithSha256()); |
| 687 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 688 | |
| 689 | packet = unsignedPacket; |
| 690 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 691 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 692 | |
| 693 | packet = unsignedPacket; |
| 694 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 695 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 696 | |
| 697 | packet = unsignedPacket; |
| 698 | this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity)); |
| 699 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 700 | |
| 701 | packet = unsignedPacket; |
| 702 | this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity)); |
| 703 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 704 | } |
| 705 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 706 | using RefreshPolicies = boost::mp11::mp_list<Refresh1h, Refresh1m, Refresh1s>; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 707 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 708 | template<typename RefreshPolicy> |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 709 | using RefreshPolicyFixture = LoadStringWithDirAnchor<Data, RefreshPolicy>; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 710 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 711 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateRefresh, Refresh, RefreshPolicies, RefreshPolicyFixture<Refresh>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 712 | { |
| 713 | using Packet = Data; |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 714 | Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 715 | |
| 716 | boost::filesystem::remove(this->path / "keys" / "identity.ndncert"); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 717 | this->advanceClocks(Refresh::getRefreshTime(), 3); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 718 | |
| 719 | Packet packet = unsignedPacket; |
| 720 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 721 | VALIDATE_FAILURE(packet, "Should fail, as the trust anchor should no longer exist"); |
| 722 | |
| 723 | packet = unsignedPacket; |
| 724 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 725 | VALIDATE_FAILURE(packet, "Should fail, as the trust anchor should no longer exist"); |
| 726 | } |
| 727 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 728 | BOOST_FIXTURE_TEST_CASE(OrphanedPolicyLoad, HierarchicalValidatorFixture<ValidationPolicyConfig>, |
| 729 | * ut::description("test for bug #4758")) |
Alexander Afanasyev | 7b11246 | 2018-10-17 11:51:52 -0400 | [diff] [blame] | 730 | { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 731 | using ndn::security::validator_config::Error; |
| 732 | |
Alexander Afanasyev | 7b11246 | 2018-10-17 11:51:52 -0400 | [diff] [blame] | 733 | ValidationPolicyConfig policy1; |
| 734 | BOOST_CHECK_THROW(policy1.load("trust-anchor { type any }", "test-config"), Error); |
| 735 | |
| 736 | // Reloading would have triggered a segfault |
| 737 | BOOST_CHECK_THROW(policy1.load("trust-anchor { type any }", "test-config"), Error); |
| 738 | |
| 739 | ValidationPolicyConfig policy2; |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 740 | const std::string config = R"CONF( |
Alexander Afanasyev | 7b11246 | 2018-10-17 11:51:52 -0400 | [diff] [blame] | 741 | trust-anchor |
| 742 | { |
| 743 | type dir |
| 744 | dir keys |
| 745 | refresh 1h |
| 746 | } |
| 747 | )CONF"; |
Alexander Afanasyev | 7b11246 | 2018-10-17 11:51:52 -0400 | [diff] [blame] | 748 | // Inserting trust anchor would have triggered a segfault |
| 749 | BOOST_CHECK_THROW(policy2.load(config, "test-config"), Error); |
| 750 | } |
| 751 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 752 | BOOST_AUTO_TEST_SUITE_END() // TestValidationPolicyConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 753 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 754 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 755 | } // namespace ndn::tests |