Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [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. |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [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 "ndn-cxx/security/validation-policy-signed-interest.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 23 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 24 | #include "ndn-cxx/security/interest-signer.hpp" |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 25 | #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp" |
| 26 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 27 | #include "tests/test-common.hpp" |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 28 | #include "tests/unit/security/validator-fixture.hpp" |
| 29 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 30 | #include <boost/mp11/list.hpp> |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | namespace ndn::tests { |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 34 | using namespace ndn::security; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Security) |
| 37 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 38 | struct SignedInterestDefaultOptions |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 39 | { |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 40 | static auto |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 41 | getOptions() |
| 42 | { |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 43 | return ValidationPolicySignedInterest::Options{}; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 44 | } |
| 45 | }; |
| 46 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 47 | template<class ValidationOptions, class InnerPolicy> |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 48 | class SignedInterestPolicyWrapper : public ValidationPolicySignedInterest |
| 49 | { |
| 50 | public: |
| 51 | SignedInterestPolicyWrapper() |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 52 | : ValidationPolicySignedInterest(make_unique<InnerPolicy>(), ValidationOptions::getOptions()) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 57 | template<class ValidationOptions = SignedInterestDefaultOptions, |
| 58 | class InnerPolicy = ValidationPolicySimpleHierarchy> |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 59 | class ValidationPolicySignedInterestFixture |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 60 | : public HierarchicalValidatorFixture<SignedInterestPolicyWrapper<ValidationOptions, InnerPolicy>> |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 61 | { |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 62 | protected: |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 63 | Interest |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 64 | makeSignedInterest(const Identity& id, |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 65 | uint32_t signingFlags = InterestSigner::WantNonce | InterestSigner::WantTime) |
| 66 | { |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 67 | Interest interest(Name(id.getName()).append("CMD")); |
| 68 | m_signer.makeSignedInterest(interest, signingByIdentity(id), signingFlags); |
| 69 | return interest; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 72 | protected: |
| 73 | InterestSigner m_signer{this->m_keyChain}; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 74 | static constexpr uint32_t WantAll = InterestSigner::WantNonce | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 75 | InterestSigner::WantTime | |
| 76 | InterestSigner::WantSeqNum; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 79 | BOOST_FIXTURE_TEST_SUITE(TestValidationPolicySignedInterest, ValidationPolicySignedInterestFixture<>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 80 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 81 | BOOST_AUTO_TEST_CASE(Basic) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 82 | { |
| 83 | auto i1 = makeSignedInterest(identity, WantAll); |
| 84 | VALIDATE_SUCCESS(i1, "Should succeed (within grace period)"); |
| 85 | VALIDATE_FAILURE(i1, "Should fail (replay attack)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 86 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 87 | |
| 88 | advanceClocks(5_ms); |
| 89 | auto i2 = makeSignedInterest(identity, WantAll); |
| 90 | VALIDATE_SUCCESS(i2, "Should succeed (timestamp and sequence number larger than previous)"); |
| 91 | |
| 92 | Interest i3(Name(identity.getName()).append("CMD")); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 93 | m_signer.makeSignedInterest(i3, signingWithSha256()); |
| 94 | VALIDATE_FAILURE(i3, "Should fail (Sha256 signature violates policy)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 95 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(DataPassthrough) |
| 99 | { |
| 100 | Data d1("/Security/ValidatorFixture/Sub1"); |
| 101 | m_keyChain.sign(d1); |
| 102 | VALIDATE_SUCCESS(d1, "Should succeed (fallback on inner validation policy for data)"); |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(InnerPolicyReject) |
| 106 | { |
| 107 | auto i1 = makeSignedInterest(otherIdentity); |
| 108 | VALIDATE_FAILURE(i1, "Should fail (inner policy should reject)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 109 | BOOST_TEST(lastError.getCode() == ValidationError::LOOP_DETECTED); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 112 | template<ssize_t count> |
| 113 | struct MaxRecordCount |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 114 | { |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 115 | static auto |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 116 | getOptions() |
| 117 | { |
| 118 | ValidationPolicySignedInterest::Options options; |
| 119 | options.timestampGracePeriod = 15_s; |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 120 | options.maxRecordCount = count; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 121 | return options; |
| 122 | } |
| 123 | }; |
| 124 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 125 | BOOST_FIXTURE_TEST_CASE(LimitedRecords, ValidationPolicySignedInterestFixture<MaxRecordCount<3>>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 126 | { |
| 127 | Identity id1 = addSubCertificate("/Security/ValidatorFixture/Sub1", identity); |
| 128 | cache.insert(id1.getDefaultKey().getDefaultCertificate()); |
| 129 | Identity id2 = addSubCertificate("/Security/ValidatorFixture/Sub2", identity); |
| 130 | cache.insert(id2.getDefaultKey().getDefaultCertificate()); |
| 131 | Identity id3 = addSubCertificate("/Security/ValidatorFixture/Sub3", identity); |
| 132 | cache.insert(id3.getDefaultKey().getDefaultCertificate()); |
| 133 | Identity id4 = addSubCertificate("/Security/ValidatorFixture/Sub4", identity); |
| 134 | cache.insert(id4.getDefaultKey().getDefaultCertificate()); |
| 135 | |
| 136 | auto i1 = makeSignedInterest(id2); |
| 137 | auto i2 = makeSignedInterest(id3); |
| 138 | auto i3 = makeSignedInterest(id4); |
| 139 | auto i00 = makeSignedInterest(id1); // signed at 0s |
| 140 | advanceClocks(1_s); |
| 141 | auto i01 = makeSignedInterest(id1); // signed at 1s |
| 142 | advanceClocks(1_s); |
| 143 | auto i02 = makeSignedInterest(id1); // signed at 2s |
| 144 | |
| 145 | VALIDATE_SUCCESS(i00, "Should succeed"); |
| 146 | rewindClockAfterValidation(); |
| 147 | |
| 148 | VALIDATE_SUCCESS(i02, "Should succeed"); |
| 149 | rewindClockAfterValidation(); |
| 150 | |
| 151 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 152 | rewindClockAfterValidation(); |
| 153 | |
| 154 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 155 | rewindClockAfterValidation(); |
| 156 | |
| 157 | VALIDATE_SUCCESS(i3, "Should succeed, forgets identity id1"); |
| 158 | rewindClockAfterValidation(); |
| 159 | |
| 160 | VALIDATE_SUCCESS(i01, "Should succeed despite timestamp is reordered, because record has been evicted"); |
| 161 | } |
| 162 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 163 | BOOST_FIXTURE_TEST_CASE(UnlimitedRecords, ValidationPolicySignedInterestFixture<MaxRecordCount<-1>>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 164 | { |
| 165 | std::vector<Identity> identities; |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 166 | for (size_t i = 0; i < 20; ++i) { |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 167 | Identity id = addSubCertificate("/Security/ValidatorFixture/Sub" + to_string(i), identity); |
| 168 | cache.insert(id.getDefaultKey().getDefaultCertificate()); |
| 169 | identities.push_back(id); |
| 170 | } |
| 171 | |
| 172 | auto i1 = makeSignedInterest(identities.at(0)); // signed at 0s |
| 173 | advanceClocks(1_s); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 174 | for (size_t i = 0; i < 20; ++i) { |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 175 | auto i2 = makeSignedInterest(identities.at(i)); // signed at +1s |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 176 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 177 | rewindClockAfterValidation(); |
| 178 | } |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 179 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 180 | VALIDATE_FAILURE(i1, "Should fail (timestamp reorder)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 181 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 184 | BOOST_FIXTURE_TEST_CASE(ZeroRecords, ValidationPolicySignedInterestFixture<MaxRecordCount<0>>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 185 | { |
| 186 | auto i1 = makeSignedInterest(identity); // signed at 0s |
| 187 | advanceClocks(1_s); |
| 188 | auto i2 = makeSignedInterest(identity); // signed at +1s |
| 189 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 190 | rewindClockAfterValidation(); |
| 191 | |
| 192 | VALIDATE_SUCCESS(i1, "Should succeed despite timestamp reordering, as records aren't kept"); |
| 193 | } |
| 194 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 195 | struct DisabledTimestampValidation |
| 196 | { |
| 197 | static auto |
| 198 | getOptions() |
| 199 | { |
| 200 | ValidationPolicySignedInterest::Options options; |
| 201 | options.shouldValidateTimestamps = false; |
| 202 | return options; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | template<int secs> |
| 207 | struct GracePeriodSeconds |
| 208 | { |
| 209 | static auto |
| 210 | getOptions() |
| 211 | { |
| 212 | ValidationPolicySignedInterest::Options options; |
| 213 | options.timestampGracePeriod = time::seconds(secs); |
| 214 | return options; |
| 215 | } |
| 216 | }; |
| 217 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 218 | BOOST_AUTO_TEST_SUITE(TimestampValidation) |
| 219 | |
| 220 | BOOST_AUTO_TEST_CASE(MissingTimestamp) |
| 221 | { |
| 222 | auto i1 = makeSignedInterest(identity, InterestSigner::WantSeqNum); |
| 223 | VALIDATE_FAILURE(i1, "Should fail (timestamp missing)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 224 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 227 | BOOST_FIXTURE_TEST_CASE(Disabled, ValidationPolicySignedInterestFixture<DisabledTimestampValidation>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 228 | { |
| 229 | auto i1 = makeSignedInterest(identity); // signed at 0ms |
| 230 | advanceClocks(100_ms); |
| 231 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 232 | |
| 233 | auto i2 = makeSignedInterest(identity); // signed at +100ms |
| 234 | // Set i2 to have same timestamp as i1 |
| 235 | auto si2 = i2.getSignatureInfo(); |
| 236 | si2->setTime(i2.getSignatureInfo()->getTime()); |
| 237 | i2.setSignatureInfo(*si2); |
| 238 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 239 | } |
| 240 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 241 | BOOST_FIXTURE_TEST_CASE(TimestampTooOld, ValidationPolicySignedInterestFixture<GracePeriodSeconds<15>>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 242 | { |
| 243 | auto i1 = makeSignedInterest(identity); // signed at 0s |
| 244 | advanceClocks(16_s); // verifying at +16s |
| 245 | VALIDATE_FAILURE(i1, "Should fail (timestamp outside the grace period)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 246 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 247 | rewindClockAfterValidation(); |
| 248 | |
| 249 | auto i2 = makeSignedInterest(identity); // signed at +16s |
| 250 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 251 | } |
| 252 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 253 | BOOST_FIXTURE_TEST_CASE(TimestampTooNew, ValidationPolicySignedInterestFixture<GracePeriodSeconds<15>>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 254 | { |
| 255 | auto i1 = makeSignedInterest(identity); // signed at 0s |
| 256 | advanceClocks(1_s); |
| 257 | auto i2 = makeSignedInterest(identity); // signed at +1s |
| 258 | advanceClocks(1_s); |
| 259 | auto i3 = makeSignedInterest(identity); // signed at +2s |
| 260 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 261 | m_systemClock->advance(-18_s); // verifying at -16s |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 262 | VALIDATE_FAILURE(i1, "Should fail (timestamp outside the grace period)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 263 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 264 | rewindClockAfterValidation(); |
| 265 | |
| 266 | // SignedInterestValidator should not remember i1's timestamp |
| 267 | VALIDATE_FAILURE(i2, "Should fail (timestamp outside the grace period)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 268 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 269 | rewindClockAfterValidation(); |
| 270 | |
| 271 | // SignedInterestValidator should not remember i2's timestamp, and should treat i3 as initial |
| 272 | advanceClocks(18_s); // verifying at +2s |
| 273 | VALIDATE_SUCCESS(i3, "Should succeed"); |
| 274 | } |
| 275 | |
| 276 | BOOST_AUTO_TEST_CASE(TimestampReorderEqual) |
| 277 | { |
| 278 | auto i1 = makeSignedInterest(identity); // signed at 0s |
| 279 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 280 | |
| 281 | auto i2 = makeSignedInterest(identity); // signed at 0s |
| 282 | auto si2 = i2.getSignatureInfo(); |
| 283 | si2->setTime(i1.getSignatureInfo()->getTime()); |
| 284 | i2.setSignatureInfo(*si2); |
| 285 | VALIDATE_FAILURE(i2, "Should fail (timestamp reordered)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 286 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 287 | |
| 288 | advanceClocks(2_s); |
| 289 | auto i3 = makeSignedInterest(identity); // signed at +2s |
| 290 | VALIDATE_SUCCESS(i3, "Should succeed"); |
| 291 | } |
| 292 | |
| 293 | BOOST_AUTO_TEST_CASE(TimestampReorderNegative) |
| 294 | { |
| 295 | auto i2 = makeSignedInterest(identity); // signed at 0ms |
| 296 | advanceClocks(200_ms); |
| 297 | auto i3 = makeSignedInterest(identity); // signed at +200ms |
| 298 | advanceClocks(900_ms); |
| 299 | auto i1 = makeSignedInterest(identity); // signed at +1100ms |
| 300 | advanceClocks(300_ms); |
| 301 | auto i4 = makeSignedInterest(identity); // signed at +1400ms |
| 302 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 303 | m_systemClock->advance(-300_ms); // verifying at +1100ms |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 304 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 305 | rewindClockAfterValidation(); |
| 306 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 307 | m_systemClock->advance(-1100_ms); // verifying at 0ms |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 308 | VALIDATE_FAILURE(i2, "Should fail (timestamp reordered)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 309 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 310 | rewindClockAfterValidation(); |
| 311 | |
| 312 | // SignedInterestValidator should not remember i2's timestamp |
| 313 | advanceClocks(200_ms); // verifying at +200ms |
| 314 | VALIDATE_FAILURE(i3, "Should fail (timestamp reordered)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 315 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 316 | rewindClockAfterValidation(); |
| 317 | |
| 318 | advanceClocks(1200_ms); // verifying at 1400ms |
| 319 | VALIDATE_SUCCESS(i4, "Should succeed"); |
| 320 | } |
| 321 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 322 | using NonPositiveGracePeriods = boost::mp11::mp_list<GracePeriodSeconds<0>, GracePeriodSeconds<-1>>; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 323 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 324 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(GraceNonPositive, GracePeriod, NonPositiveGracePeriods, |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 325 | ValidationPolicySignedInterestFixture<GracePeriod>) |
| 326 | { |
| 327 | auto i1 = this->makeSignedInterest(this->identity); // signed at 0ms |
| 328 | auto i2 = this->makeSignedInterest(this->subIdentity); // signed at 0ms |
| 329 | // ensure timestamps are exactly 0ms |
| 330 | for (auto interest : {&i1, &i2}) { |
| 331 | auto si = interest->getSignatureInfo(); |
| 332 | si->setTime(time::system_clock::now()); |
| 333 | interest->setSignatureInfo(*si); |
| 334 | } |
| 335 | |
| 336 | VALIDATE_SUCCESS(i1, "Should succeed when validating at 0ms"); |
| 337 | this->rewindClockAfterValidation(); |
| 338 | |
| 339 | this->advanceClocks(1_ms); |
| 340 | VALIDATE_FAILURE(i2, "Should fail when validating at 1ms"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 341 | BOOST_TEST(this->lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | BOOST_AUTO_TEST_SUITE_END() // TimestampValidation |
| 345 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 346 | struct EnabledSeqNumValidation |
| 347 | { |
| 348 | static auto |
| 349 | getOptions() |
| 350 | { |
| 351 | ValidationPolicySignedInterest::Options options; |
| 352 | options.shouldValidateSeqNums = true; |
| 353 | return options; |
| 354 | } |
| 355 | }; |
| 356 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 357 | BOOST_AUTO_TEST_SUITE(SeqNumValidation) |
| 358 | |
| 359 | // By default, sequence number validation is disabled |
| 360 | BOOST_AUTO_TEST_CASE(Disabled) |
| 361 | { |
| 362 | auto i1 = makeSignedInterest(identity, WantAll); // signed at 0ms |
| 363 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 364 | |
| 365 | auto i2 = makeSignedInterest(identity, WantAll); // signed at +100ms |
| 366 | // Set i2 to have same seq num as i1 |
| 367 | auto si2 = i2.getSignatureInfo(); |
| 368 | si2->setSeqNum(i2.getSignatureInfo()->getSeqNum()); |
| 369 | i2.setSignatureInfo(*si2); |
| 370 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 371 | } |
| 372 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 373 | BOOST_FIXTURE_TEST_CASE(MissingSeqNum, ValidationPolicySignedInterestFixture<EnabledSeqNumValidation>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 374 | { |
| 375 | auto i1 = makeSignedInterest(identity, InterestSigner::WantTime); |
| 376 | VALIDATE_FAILURE(i1, "Should fail (sequence number missing"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 377 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 380 | BOOST_FIXTURE_TEST_CASE(SeqNumReorder, ValidationPolicySignedInterestFixture<EnabledSeqNumValidation>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 381 | { |
| 382 | auto i1 = makeSignedInterest(identity, WantAll); // seq num is i |
| 383 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 384 | |
| 385 | auto i2 = makeSignedInterest(identity, WantAll); // seq num is i+1 |
| 386 | auto si2 = i2.getSignatureInfo(); |
| 387 | si2->setSeqNum(i1.getSignatureInfo()->getSeqNum()); |
| 388 | i2.setSignatureInfo(*si2); |
| 389 | VALIDATE_FAILURE(i2, "Should fail (sequence number reordered)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 390 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 391 | |
| 392 | auto i3 = makeSignedInterest(identity, WantAll); // seq num is i+2 |
| 393 | VALIDATE_SUCCESS(i3, "Should succeed"); |
| 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_SUITE_END() // SeqNumValidation |
| 397 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 398 | struct DisabledNonceValidation |
| 399 | { |
| 400 | static auto |
| 401 | getOptions() |
| 402 | { |
| 403 | ValidationPolicySignedInterest::Options options; |
| 404 | options.shouldValidateNonces = false; |
| 405 | return options; |
| 406 | } |
| 407 | }; |
| 408 | |
| 409 | template<ssize_t count> |
| 410 | struct MaxNonceRecordCount |
| 411 | { |
| 412 | static auto |
| 413 | getOptions() |
| 414 | { |
| 415 | ValidationPolicySignedInterest::Options options; |
| 416 | options.shouldValidateTimestamps = false; |
| 417 | options.shouldValidateSeqNums = false; |
| 418 | options.maxNonceRecordCount = count; |
| 419 | return options; |
| 420 | } |
| 421 | }; |
| 422 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 423 | BOOST_AUTO_TEST_SUITE(NonceValidation) |
| 424 | |
| 425 | BOOST_AUTO_TEST_CASE(MissingNonce) |
| 426 | { |
| 427 | auto i1 = makeSignedInterest(identity, InterestSigner::WantTime); // Specifically exclude nonce |
| 428 | VALIDATE_FAILURE(i1, "Should fail (nonce missing)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 429 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | BOOST_AUTO_TEST_CASE(DuplicateNonce) |
| 433 | { |
| 434 | auto i1 = makeSignedInterest(identity, WantAll); |
| 435 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 436 | |
| 437 | auto i2 = makeSignedInterest(identity, WantAll); |
| 438 | auto si2 = i2.getSignatureInfo(); |
| 439 | si2->setNonce(i1.getSignatureInfo()->getNonce()); |
| 440 | i2.setSignatureInfo(*si2); |
| 441 | VALIDATE_FAILURE(i2, "Should fail (duplicate nonce)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 442 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 443 | |
| 444 | auto i3 = makeSignedInterest(identity, WantAll); |
| 445 | // On the off chance that the generated nonce is identical to i1 |
| 446 | while (i3.getSignatureInfo()->getNonce() == i1.getSignatureInfo()->getNonce()) { |
| 447 | i3 = makeSignedInterest(identity, WantAll); |
| 448 | } |
| 449 | VALIDATE_SUCCESS(i3, "Should succeed"); |
| 450 | } |
| 451 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 452 | BOOST_FIXTURE_TEST_CASE(Disabled, ValidationPolicySignedInterestFixture<DisabledNonceValidation>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 453 | { |
| 454 | auto i1 = makeSignedInterest(identity, WantAll ^ InterestSigner::WantNonce); |
| 455 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 456 | |
| 457 | // Ensure still works when a nonce is present |
| 458 | auto i2 = makeSignedInterest(identity, WantAll); |
| 459 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 460 | |
| 461 | // Ensure a duplicate still succeeds |
| 462 | auto i3 = makeSignedInterest(identity, WantAll); |
| 463 | auto si3 = i3.getSignatureInfo(); |
| 464 | si3->setNonce(i2.getSignatureInfo()->getNonce()); |
| 465 | i3.setSignatureInfo(*si3); |
| 466 | m_keyChain.sign(i3, signingByIdentity(identity).setSignedInterestFormat(SignedInterestFormat::V03) |
| 467 | .setSignatureInfo(*si3)); |
| 468 | VALIDATE_SUCCESS(i3, "Should succeed"); |
| 469 | } |
| 470 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 471 | BOOST_FIXTURE_TEST_CASE(NonceRecordLimit, ValidationPolicySignedInterestFixture<MaxNonceRecordCount<2>>) |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 472 | { |
| 473 | auto i1 = makeSignedInterest(identity, WantAll); |
| 474 | VALIDATE_SUCCESS(i1, "Should succeed"); |
| 475 | |
| 476 | auto i2 = makeSignedInterest(identity, WantAll); |
| 477 | // On the off chance that the generated nonce is identical to i1 |
| 478 | while (i2.getSignatureInfo()->getNonce() == i1.getSignatureInfo()->getNonce()) { |
| 479 | i2 = makeSignedInterest(identity, WantAll); |
| 480 | } |
| 481 | VALIDATE_SUCCESS(i2, "Should succeed"); |
| 482 | |
| 483 | auto i3 = makeSignedInterest(identity, WantAll); |
| 484 | auto si3 = i3.getSignatureInfo(); |
| 485 | si3->setNonce(i1.getSignatureInfo()->getNonce()); |
| 486 | i3.setSignatureInfo(*si3); |
| 487 | m_keyChain.sign(i3, signingByIdentity(identity).setSignedInterestFormat(SignedInterestFormat::V03) |
| 488 | .setSignatureInfo(*si3)); |
| 489 | VALIDATE_FAILURE(i3, "Should fail (duplicate nonce)"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 490 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 491 | |
| 492 | // Pop i1's nonce off the list |
| 493 | auto i4 = makeSignedInterest(identity, WantAll); |
| 494 | // On the off chance that the generated nonce is identical to i1 or i2 |
| 495 | while (i4.getSignatureInfo()->getNonce() == i1.getSignatureInfo()->getNonce() || |
| 496 | i4.getSignatureInfo()->getNonce() == i2.getSignatureInfo()->getNonce()) { |
| 497 | i4 = makeSignedInterest(identity, WantAll); |
| 498 | } |
| 499 | VALIDATE_SUCCESS(i4, "Should succeed"); |
| 500 | |
| 501 | // Now i3 should succeed because i1's nonce has been popped off the list |
| 502 | auto i5 = makeSignedInterest(identity, WantAll); |
| 503 | auto si5 = i5.getSignatureInfo(); |
| 504 | si5->setNonce(i1.getSignatureInfo()->getNonce()); |
| 505 | i5.setSignatureInfo(*si5); |
| 506 | m_keyChain.sign(i5, signingByIdentity(identity).setSignedInterestFormat(SignedInterestFormat::V03) |
| 507 | .setSignatureInfo(*si5)); |
| 508 | VALIDATE_SUCCESS(i5, "Should succeed"); |
| 509 | } |
| 510 | |
| 511 | BOOST_AUTO_TEST_SUITE_END() // NonceValidation |
| 512 | |
| 513 | BOOST_AUTO_TEST_SUITE_END() // TestValidationPolicySignedInterest |
| 514 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 515 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 516 | } // namespace ndn::tests |