blob: 5600257cf901e3e0149bc064fbde477bc92d4735 [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 Pesavento49e1e872023-11-11 00:45:23 -050029#include <boost/mp11/list.hpp>
30
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031namespace ndn::tests {
Manika Mittal95c80702017-01-27 09:54:41 -080032
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040033using namespace ndn::security;
Manika Mittal95c80702017-01-27 09:54:41 -080034
35BOOST_AUTO_TEST_SUITE(Security)
Manika Mittal95c80702017-01-27 09:54:41 -080036BOOST_AUTO_TEST_SUITE(TestCertificateBundleFetcher)
37
38class CertificateBundleFetcherWrapper : public CertificateBundleFetcher
39{
40public:
41 CertificateBundleFetcherWrapper(Face& face)
42 : CertificateBundleFetcher(make_unique<CertificateFetcherFromNetwork>(face), face)
43 {
44 }
45};
46
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040047struct BundleWithFinalBlockId {};
48struct BundleWithoutFinalBlockId {};
49struct Timeout {};
50struct Nack {};
Manika Mittal95c80702017-01-27 09:54:41 -080051
52template<class Response>
53class CertificateBundleFetcherFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
54 CertificateBundleFetcherWrapper>
55{
56public:
57 CertificateBundleFetcherFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040058 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
Davide Pesavento2ca5e792020-06-16 22:52:23 -040059 , bundleRegexMatcher(std::make_shared<RegexPatternListMatcher>("<>*<_BUNDLE><>*", nullptr))
Manika Mittal95c80702017-01-27 09:54:41 -080060 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040061 subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Manika Mittal95c80702017-01-27 09:54:41 -080062 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
Manika Mittal95c80702017-01-27 09:54:41 -080063 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
Davide Pesavento2ca5e792020-06-16 22:52:23 -040064
Manika Mittal95c80702017-01-27 09:54:41 -080065 processInterest = [this] (const Interest& interest) {
Davide Pesavento2ca5e792020-06-16 22:52:23 -040066 // check if the interest is for bundle or individual certificates
Manika Mittal95c80702017-01-27 09:54:41 -080067 if (bundleRegexMatcher->match(interest.getName(), 0, interest.getName().size())) {
68 makeResponse(interest);
69 }
70 else {
71 auto cert = cache.find(interest);
Davide Pesavento2ca5e792020-06-16 22:52:23 -040072 if (cert) {
73 face.receive(*cert);
Manika Mittal95c80702017-01-27 09:54:41 -080074 }
Manika Mittal95c80702017-01-27 09:54:41 -080075 }
76 };
77 }
78
Davide Pesavento2ca5e792020-06-16 22:52:23 -040079private:
Manika Mittal95c80702017-01-27 09:54:41 -080080 void
81 makeResponse(const Interest& interest);
82
Davide Pesavento2ca5e792020-06-16 22:52:23 -040083 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
113protected:
Manika Mittal95c80702017-01-27 09:54:41 -0800114 Data data;
115 Identity subSubIdentity;
116 shared_ptr<RegexPatternListMatcher> bundleRegexMatcher;
117};
118
119template<>
120void
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400121CertificateBundleFetcherFixture<BundleWithFinalBlockId>::makeResponse(const Interest& interest)
Manika Mittal95c80702017-01-27 09:54:41 -0800122{
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400123 auto certBundle = makeBundle(interest);
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000124 certBundle->setFinalBlock(name::Component::fromSegment(1));
Manika Mittal95c80702017-01-27 09:54:41 -0800125 m_keyChain.sign(*certBundle, signingWithSha256());
Manika Mittal95c80702017-01-27 09:54:41 -0800126 face.receive(*certBundle);
127}
128
129template<>
130void
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400131CertificateBundleFetcherFixture<BundleWithoutFinalBlockId>::makeResponse(const Interest& interest)
132{
133 auto certBundle = makeBundle(interest);
134 m_keyChain.sign(*certBundle, signingWithSha256());
135 face.receive(*certBundle);
136}
137
138template<>
139void
140CertificateBundleFetcherFixture<Timeout>::makeResponse(const Interest&)
Manika Mittal95c80702017-01-27 09:54:41 -0800141{
Davide Pesavento0f830802018-01-16 23:58:58 -0500142 this->advanceClocks(200_s);
Manika Mittal95c80702017-01-27 09:54:41 -0800143}
144
145template<>
146void
147CertificateBundleFetcherFixture<Nack>::makeResponse(const Interest& interest)
148{
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400149 face.receive(makeNack(interest, lp::NackReason::NO_ROUTE));
Manika Mittal95c80702017-01-27 09:54:41 -0800150}
151
Davide Pesavento49e1e872023-11-11 00:45:23 -0500152using SuccessWithBundle = boost::mp11::mp_list<BundleWithFinalBlockId, BundleWithoutFinalBlockId>;
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400153
154BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithBundle, T, SuccessWithBundle,
155 CertificateBundleFetcherFixture<T>)
Manika Mittal95c80702017-01-27 09:54:41 -0800156{
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 Pesavento49e1e872023-11-11 00:45:23 -0500165using SuccessWithoutBundle = boost::mp11::mp_list<Nack, Timeout>;
Manika Mittal95c80702017-01-27 09:54:41 -0800166
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400167BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithoutBundle, T, SuccessWithoutBundle,
168 CertificateBundleFetcherFixture<T>)
Manika Mittal95c80702017-01-27 09:54:41 -0800169{
170 VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the certs");
Davide Pesavento2ca5e792020-06-16 22:52:23 -0400171 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); // since interest for bundle fails, each cert is retrieved
Manika Mittal95c80702017-01-27 09:54:41 -0800172
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
186BOOST_AUTO_TEST_SUITE_END() // TestCertificateBundleFetcher
Manika Mittal95c80702017-01-27 09:54:41 -0800187BOOST_AUTO_TEST_SUITE_END() // Security
188
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400189} // namespace ndn::tests