Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -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/certificate-bundle-fetcher.hpp" |
| 23 | #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/util/regex/regex-pattern-list-matcher.hpp" |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 26 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 27 | #include "tests/unit/security/validator-fixture.hpp" |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 29 | namespace ndn::tests { |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 30 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 31 | using namespace ndn::security; |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(Security) |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestCertificateBundleFetcher) |
| 35 | |
| 36 | class CertificateBundleFetcherWrapper : public CertificateBundleFetcher |
| 37 | { |
| 38 | public: |
| 39 | CertificateBundleFetcherWrapper(Face& face) |
| 40 | : CertificateBundleFetcher(make_unique<CertificateFetcherFromNetwork>(face), face) |
| 41 | { |
| 42 | } |
| 43 | }; |
| 44 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 45 | struct BundleWithFinalBlockId {}; |
| 46 | struct BundleWithoutFinalBlockId {}; |
| 47 | struct Timeout {}; |
| 48 | struct Nack {}; |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 49 | |
| 50 | template<class Response> |
| 51 | class CertificateBundleFetcherFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 52 | CertificateBundleFetcherWrapper> |
| 53 | { |
| 54 | public: |
| 55 | CertificateBundleFetcherFixture() |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 56 | : data("/Security/ValidatorFixture/Sub1/Sub3/Data") |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 57 | , bundleRegexMatcher(std::make_shared<RegexPatternListMatcher>("<>*<_BUNDLE><>*", nullptr)) |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 59 | subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 60 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 61 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 62 | |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 63 | processInterest = [this] (const Interest& interest) { |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 64 | // check if the interest is for bundle or individual certificates |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 65 | if (bundleRegexMatcher->match(interest.getName(), 0, interest.getName().size())) { |
| 66 | makeResponse(interest); |
| 67 | } |
| 68 | else { |
| 69 | auto cert = cache.find(interest); |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 70 | if (cert) { |
| 71 | face.receive(*cert); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 72 | } |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | } |
| 76 | |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 77 | private: |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 78 | void |
| 79 | makeResponse(const Interest& interest); |
| 80 | |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 81 | shared_ptr<Data> |
| 82 | makeBundle(const Interest& interest) const |
| 83 | { |
| 84 | Block certList(tlv::Content); |
| 85 | Name bundleName(interest.getName()); |
| 86 | |
| 87 | if (!bundleName.get(-1).isSegment() || bundleName.get(-1).toSegment() == 0) { |
| 88 | Block subSubCert = subSubIdentity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 89 | certList.push_back(std::move(subSubCert)); |
| 90 | |
| 91 | if (!bundleName.get(-1).isSegment()) { |
| 92 | bundleName |
| 93 | .appendVersion() |
| 94 | .appendSegment(0); |
| 95 | } |
| 96 | } |
| 97 | else { |
| 98 | Block subCert = subIdentity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 99 | Block anchor = identity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 100 | certList.push_back(std::move(subCert)); |
| 101 | certList.push_back(std::move(anchor)); |
| 102 | } |
| 103 | |
| 104 | auto certBundle = make_shared<Data>(); |
| 105 | certBundle->setName(bundleName); |
| 106 | certBundle->setFreshnessPeriod(100_s); |
| 107 | certBundle->setContent(certList); |
| 108 | return certBundle; |
| 109 | } |
| 110 | |
| 111 | protected: |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 112 | Data data; |
| 113 | Identity subSubIdentity; |
| 114 | shared_ptr<RegexPatternListMatcher> bundleRegexMatcher; |
| 115 | }; |
| 116 | |
| 117 | template<> |
| 118 | void |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 119 | CertificateBundleFetcherFixture<BundleWithFinalBlockId>::makeResponse(const Interest& interest) |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 120 | { |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 121 | auto certBundle = makeBundle(interest); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 122 | certBundle->setFinalBlock(name::Component::fromSegment(1)); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 123 | m_keyChain.sign(*certBundle, signingWithSha256()); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 124 | face.receive(*certBundle); |
| 125 | } |
| 126 | |
| 127 | template<> |
| 128 | void |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 129 | CertificateBundleFetcherFixture<BundleWithoutFinalBlockId>::makeResponse(const Interest& interest) |
| 130 | { |
| 131 | auto certBundle = makeBundle(interest); |
| 132 | m_keyChain.sign(*certBundle, signingWithSha256()); |
| 133 | face.receive(*certBundle); |
| 134 | } |
| 135 | |
| 136 | template<> |
| 137 | void |
| 138 | CertificateBundleFetcherFixture<Timeout>::makeResponse(const Interest&) |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 139 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 140 | this->advanceClocks(200_s); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | template<> |
| 144 | void |
| 145 | CertificateBundleFetcherFixture<Nack>::makeResponse(const Interest& interest) |
| 146 | { |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 147 | face.receive(makeNack(interest, lp::NackReason::NO_ROUTE)); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 150 | using SuccessWithBundle = boost::mpl::vector<BundleWithFinalBlockId, BundleWithoutFinalBlockId>; |
| 151 | |
| 152 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithBundle, T, SuccessWithBundle, |
| 153 | CertificateBundleFetcherFixture<T>) |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 154 | { |
| 155 | VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the bundle segments"); |
| 156 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); // produced bundle has 2 segments |
| 157 | |
| 158 | for (const auto& sentInterest : this->face.sentInterests) { |
| 159 | BOOST_CHECK(this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size())); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | using SuccessWithoutBundle = boost::mpl::vector<Nack, Timeout>; |
| 164 | |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 165 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithoutBundle, T, SuccessWithoutBundle, |
| 166 | CertificateBundleFetcherFixture<T>) |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 167 | { |
| 168 | VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the certs"); |
Davide Pesavento | 2ca5e79 | 2020-06-16 22:52:23 -0400 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); // since interest for bundle fails, each cert is retrieved |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 170 | |
| 171 | bool toggle = true; |
| 172 | for (const auto& sentInterest : this->face.sentInterests) { |
| 173 | if (toggle) { |
| 174 | // every alternate interest is going to be that of a bundle |
| 175 | BOOST_CHECK(this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size())); |
| 176 | } |
| 177 | else { |
| 178 | BOOST_CHECK(!this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size())); |
| 179 | } |
| 180 | toggle = !toggle; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateBundleFetcher |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 185 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 186 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 187 | } // namespace ndn::tests |