blob: 1c7b8b8fdfdae1a660419a9c684126bb38eebbd4 [file] [log] [blame]
Manika Mittal95c80702017-01-27 09:54:41 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Manika Mittal95c80702017-01-27 09:54:41 -08004 *
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 Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/certificate-bundle-fetcher.hpp"
23#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/util/regex/regex-pattern-list-matcher.hpp"
Manika Mittal95c80702017-01-27 09:54:41 -080025
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026#include "tests/test-common.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040027#include "tests/unit/security/validator-fixture.hpp"
Manika Mittal95c80702017-01-27 09:54:41 -080028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Manika Mittal95c80702017-01-27 09:54:41 -080030
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031using namespace ndn::security;
Manika Mittal95c80702017-01-27 09:54:41 -080032
33BOOST_AUTO_TEST_SUITE(Security)
Manika Mittal95c80702017-01-27 09:54:41 -080034BOOST_AUTO_TEST_SUITE(TestCertificateBundleFetcher)
35
36class CertificateBundleFetcherWrapper : public CertificateBundleFetcher
37{
38public:
39 CertificateBundleFetcherWrapper(Face& face)
40 : CertificateBundleFetcher(make_unique<CertificateFetcherFromNetwork>(face), face)
41 {
42 }
43};
44
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040045struct BundleWithFinalBlockId {};
46struct BundleWithoutFinalBlockId {};
47struct Timeout {};
48struct Nack {};
Manika Mittal95c80702017-01-27 09:54:41 -080049
50template<class Response>
51class CertificateBundleFetcherFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
52 CertificateBundleFetcherWrapper>
53{
54public:
55 CertificateBundleFetcherFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040056 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
Davide Pesavento2ca5e792020-06-16 22:52:23 -040057 , bundleRegexMatcher(std::make_shared<RegexPatternListMatcher>("<>*<_BUNDLE><>*", nullptr))
Manika Mittal95c80702017-01-27 09:54:41 -080058 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040059 subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Manika Mittal95c80702017-01-27 09:54:41 -080060 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
Manika Mittal95c80702017-01-27 09:54:41 -080061 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
Davide Pesavento2ca5e792020-06-16 22:52:23 -040062
Manika Mittal95c80702017-01-27 09:54:41 -080063 processInterest = [this] (const Interest& interest) {
Davide Pesavento2ca5e792020-06-16 22:52:23 -040064 // check if the interest is for bundle or individual certificates
Manika Mittal95c80702017-01-27 09:54:41 -080065 if (bundleRegexMatcher->match(interest.getName(), 0, interest.getName().size())) {
66 makeResponse(interest);
67 }
68 else {
69 auto cert = cache.find(interest);
Davide Pesavento2ca5e792020-06-16 22:52:23 -040070 if (cert) {
71 face.receive(*cert);
Manika Mittal95c80702017-01-27 09:54:41 -080072 }
Manika Mittal95c80702017-01-27 09:54:41 -080073 }
74 };
75 }
76
Davide Pesavento2ca5e792020-06-16 22:52:23 -040077private:
Manika Mittal95c80702017-01-27 09:54:41 -080078 void
79 makeResponse(const Interest& interest);
80
Davide Pesavento2ca5e792020-06-16 22:52:23 -040081 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
111protected:
Manika Mittal95c80702017-01-27 09:54:41 -0800112 Data data;
113 Identity subSubIdentity;
114 shared_ptr<RegexPatternListMatcher> bundleRegexMatcher;
115};
116
117template<>
118void
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400119CertificateBundleFetcherFixture<BundleWithFinalBlockId>::makeResponse(const Interest& interest)
Manika Mittal95c80702017-01-27 09:54:41 -0800120{
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400121 auto certBundle = makeBundle(interest);
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000122 certBundle->setFinalBlock(name::Component::fromSegment(1));
Manika Mittal95c80702017-01-27 09:54:41 -0800123 m_keyChain.sign(*certBundle, signingWithSha256());
Manika Mittal95c80702017-01-27 09:54:41 -0800124 face.receive(*certBundle);
125}
126
127template<>
128void
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400129CertificateBundleFetcherFixture<BundleWithoutFinalBlockId>::makeResponse(const Interest& interest)
130{
131 auto certBundle = makeBundle(interest);
132 m_keyChain.sign(*certBundle, signingWithSha256());
133 face.receive(*certBundle);
134}
135
136template<>
137void
138CertificateBundleFetcherFixture<Timeout>::makeResponse(const Interest&)
Manika Mittal95c80702017-01-27 09:54:41 -0800139{
Davide Pesavento0f830802018-01-16 23:58:58 -0500140 this->advanceClocks(200_s);
Manika Mittal95c80702017-01-27 09:54:41 -0800141}
142
143template<>
144void
145CertificateBundleFetcherFixture<Nack>::makeResponse(const Interest& interest)
146{
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400147 face.receive(makeNack(interest, lp::NackReason::NO_ROUTE));
Manika Mittal95c80702017-01-27 09:54:41 -0800148}
149
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400150using SuccessWithBundle = boost::mpl::vector<BundleWithFinalBlockId, BundleWithoutFinalBlockId>;
151
152BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithBundle, T, SuccessWithBundle,
153 CertificateBundleFetcherFixture<T>)
Manika Mittal95c80702017-01-27 09:54:41 -0800154{
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
163using SuccessWithoutBundle = boost::mpl::vector<Nack, Timeout>;
164
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400165BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithoutBundle, T, SuccessWithoutBundle,
166 CertificateBundleFetcherFixture<T>)
Manika Mittal95c80702017-01-27 09:54:41 -0800167{
168 VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the certs");
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400169 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); // since interest for bundle fails, each cert is retrieved
Manika Mittal95c80702017-01-27 09:54:41 -0800170
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
184BOOST_AUTO_TEST_SUITE_END() // TestCertificateBundleFetcher
Manika Mittal95c80702017-01-27 09:54:41 -0800185BOOST_AUTO_TEST_SUITE_END() // Security
186
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400187} // namespace ndn::tests