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