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