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