blob: 1170156fdf70549ba2fbf2f6eeee72e203c6c8af [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi5dc75602021-02-19 11:33:00 -07003 * Copyright (c) 2013-2021 Regents of the University of California.
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -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/validator-config/checker.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040023#include "ndn-cxx/security/validation-policy.hpp"
24#include "ndn-cxx/security/validation-state.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040027#include "tests/unit/security/validator-fixture.hpp"
28#include "tests/unit/security/validator-config/common.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080029
30namespace ndn {
31namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040032inline namespace v2 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080033namespace validator_config {
34namespace tests {
35
36using namespace ndn::tests;
Junxiao Shi5dc75602021-02-19 11:33:00 -070037using namespace ndn::security::tests;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080038
39BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080040BOOST_AUTO_TEST_SUITE(ValidatorConfig)
Junxiao Shi5dc75602021-02-19 11:33:00 -070041BOOST_AUTO_TEST_SUITE(TestChecker)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080042
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043class CheckerFixture : public KeyChainFixture
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080044{
45public:
46 CheckerFixture()
47 {
48 names.push_back("/foo/bar");
49 names.push_back("/foo/bar/bar");
50 names.push_back("/foo");
51 names.push_back("/other/prefix");
52 }
53
Davide Pesavento5437aa22019-03-24 14:02:37 -040054 static Name
Junxiao Shi5dc75602021-02-19 11:33:00 -070055 makeKeyLocatorKeyName(const Name& name)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080056 {
Junxiao Shi5dc75602021-02-19 11:33:00 -070057 static PartialName suffix("KEY/keyid");
58 return Name(name).append(suffix);
59 }
60
61 static Name
62 makeKeyLocatorCertName(const Name& name)
63 {
64 static PartialName suffix("KEY/keyid/issuer/v=1");
65 return Name(name).append(suffix);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080066 }
67
Junxiao Shi58b9e0f2021-03-18 15:54:07 -060068 template<typename PktType, typename C>
69 static void
Alexander Afanasyev17d4b932021-03-17 17:58:40 -040070 testChecker(C& checker, tlv::SignatureTypeValue sigType, const Name& pktName, const Name& klName, bool expectedOutcome)
Junxiao Shi58b9e0f2021-03-18 15:54:07 -060071 {
72 BOOST_TEST_CONTEXT("pkt=" << pktName << " kl=" << klName) {
73 auto state = PktType::makeState();
Alexander Afanasyev17d4b932021-03-17 17:58:40 -040074 auto result = checker.check(PktType::getType(), sigType, pktName, klName, *state);
Junxiao Shi58b9e0f2021-03-18 15:54:07 -060075 BOOST_CHECK_EQUAL(bool(result), expectedOutcome);
76 BOOST_CHECK(boost::logic::indeterminate(state->getOutcome()));
77 if (!result) {
78 BOOST_CHECK_NE(result.getErrorMessage(), "");
79 }
80 }
81 }
82
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080083public:
84 std::vector<Name> names;
85};
86
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080087class NameRelationEqual : public CheckerFixture
88{
89public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -040090 NameRelationChecker checker{tlv::SignatureSha256WithRsa, "/foo/bar", NameRelation::EQUAL};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080091 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
92 {true, false, false, false},
93 {true, false, false, false},
94 {true, false, false, false}};
95};
96
97class NameRelationIsPrefixOf : public CheckerFixture
98{
99public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400100 NameRelationChecker checker{tlv::SignatureSha256WithRsa, "/foo/bar", NameRelation::IS_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800101 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
102 {true, true, false, false},
103 {true, true, false, false},
104 {true, true, false, false}};
105};
106
107class NameRelationIsStrictPrefixOf : public CheckerFixture
108{
109public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400110 NameRelationChecker checker{tlv::SignatureSha256WithRsa, "/foo/bar", NameRelation::IS_STRICT_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800111 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
112 {false, true, false, false},
113 {false, true, false, false},
114 {false, true, false, false}};
115};
116
117class RegexEqual : public CheckerFixture
118{
119public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400120 RegexChecker checker{tlv::SignatureSha256WithRsa, Regex("^<foo><bar><KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800121 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
122 {true, false, false, false},
123 {true, false, false, false},
124 {true, false, false, false}};
125};
126
127class RegexIsPrefixOf : public CheckerFixture
128{
129public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400130 RegexChecker checker{tlv::SignatureSha256WithRsa, Regex("^<foo><bar><>*<KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800131 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
132 {true, true, false, false},
133 {true, true, false, false},
134 {true, true, false, false}};
135};
136
137class RegexIsStrictPrefixOf : public CheckerFixture
138{
139public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400140 RegexChecker checker{tlv::SignatureSha256WithRsa, Regex("^<foo><bar><>+<KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800141 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
142 {false, true, false, false},
143 {false, true, false, false},
144 {false, true, false, false}};
145};
146
147class HyperRelationEqual : public CheckerFixture
148{
149public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400150 HyperRelationChecker checker{tlv::SignatureSha256WithRsa,
151 "^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::EQUAL};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800152 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
153 {false, true, false, false},
154 {false, false, true, false},
155 {false, false, false, true}};
156};
157
158class HyperRelationIsPrefixOf : public CheckerFixture
159{
160public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400161 HyperRelationChecker checker{tlv::SignatureSha256WithRsa,
162 "^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800163 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
164 {true, true, true, false},
165 {false, false, true, false},
166 {false, false, false, true}};
167};
168
169class HyperRelationIsStrictPrefixOf : public CheckerFixture
170{
171public:
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400172 HyperRelationChecker checker{tlv::SignatureSha256WithRsa,
173 "^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_STRICT_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800174 std::vector<std::vector<bool>> outcomes = {{false, false, true, false},
175 {true, false, true, false},
176 {false, false, false, false},
177 {false, false, false, false}};
178};
179
180class Hierarchical : public CheckerFixture
181{
182public:
183 Hierarchical()
184 : checkerPtr(Checker::create(makeSection(R"CONF(
185 type hierarchical
186 sig-type rsa-sha256
187 )CONF"), "test-config"))
188 , checker(*checkerPtr)
189 {
190 }
191
192public:
193 std::unique_ptr<Checker> checkerPtr;
194 Checker& checker;
195
196 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
197 {true, true, true, false},
198 {false, false, true, false},
199 {false, false, false, true}};
200};
201
202class CustomizedNameRelation : public CheckerFixture
203{
204public:
205 CustomizedNameRelation()
206 : checkerPtr(Checker::create(makeSection(R"CONF(
207 type customized
208 sig-type rsa-sha256
209 key-locator
210 {
211 type name
212 name /foo/bar
213 relation equal
214 }
215 )CONF"), "test-config"))
216 , checker(*checkerPtr)
217 {
218 }
219
220public:
221 std::unique_ptr<Checker> checkerPtr;
222 Checker& checker;
223
224 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
225 {true, false, false, false},
226 {true, false, false, false},
227 {true, false, false, false}};
228};
229
230class CustomizedRegex : public CheckerFixture
231{
232public:
233 CustomizedRegex()
234 : checkerPtr(Checker::create(makeSection(R"CONF(
235 type customized
236 sig-type rsa-sha256
237 key-locator
238 {
239 type name
Junxiao Shi5dc75602021-02-19 11:33:00 -0700240 regex ^<foo><bar><KEY><>{1,3}$
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800241 }
242 )CONF"), "test-config"))
243 , checker(*checkerPtr)
244 {
245 }
246
247public:
248 std::unique_ptr<Checker> checkerPtr;
249 Checker& checker;
250
251 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
252 {true, false, false, false},
253 {true, false, false, false},
254 {true, false, false, false}};
255};
256
257class CustomizedHyperRelation : public CheckerFixture
258{
259public:
260 CustomizedHyperRelation()
261 : checkerPtr(Checker::create(makeSection(R"CONF(
262 type customized
263 sig-type rsa-sha256
264 key-locator
265 {
266 type name
267 hyper-relation
268 {
Junxiao Shi5dc75602021-02-19 11:33:00 -0700269 k-regex ^(<>+)<KEY><>{1,3}$
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800270 k-expand \\1
271 h-relation is-prefix-of
272 p-regex ^(<>+)$
273 p-expand \\1
274 }
275 }
276 )CONF"), "test-config"))
277 , checker(*checkerPtr)
278 {
279 }
280
281public:
282 std::unique_ptr<Checker> checkerPtr;
283 Checker& checker;
284
285 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
286 {true, true, true, false},
287 {false, false, true, false},
288 {false, false, false, true}};
289};
290
Junxiao Shi5dc75602021-02-19 11:33:00 -0700291using CheckerFixtures = boost::mpl::vector<
292 NameRelationEqual,
293 NameRelationIsPrefixOf,
294 NameRelationIsStrictPrefixOf,
295 RegexEqual,
296 RegexIsPrefixOf,
297 RegexIsStrictPrefixOf,
298 HyperRelationEqual,
299 HyperRelationIsPrefixOf,
300 HyperRelationIsStrictPrefixOf,
301 Hierarchical,
302 CustomizedNameRelation,
303 CustomizedRegex,
304 CustomizedHyperRelation
305>;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800306
Junxiao Shi5dc75602021-02-19 11:33:00 -0700307// Cartesian product of [DataPkt, InterestV02Pkt, InterestV03Pkt] and CheckerFixtures.
308// Each element is a boost::mpl::pair<PktType, CheckerFixture>.
309using Tests = boost::mpl::fold<
310 CheckerFixtures,
311 boost::mpl::vector<>,
312 boost::mpl::push_back<boost::mpl::push_back<boost::mpl::push_back<boost::mpl::_1,
313 boost::mpl::pair<DataPkt, boost::mpl::_2>>,
314 boost::mpl::pair<InterestV02Pkt, boost::mpl::_2>>,
315 boost::mpl::pair<InterestV03Pkt, boost::mpl::_2>>
316>::type;
317
318BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T::second)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800319{
Junxiao Shi5dc75602021-02-19 11:33:00 -0700320 using PktType = typename T::first;
Davide Pesavento5437aa22019-03-24 14:02:37 -0400321
322 BOOST_REQUIRE_EQUAL(this->outcomes.size(), this->names.size());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800323 for (size_t i = 0; i < this->names.size(); ++i) {
Davide Pesavento5437aa22019-03-24 14:02:37 -0400324 BOOST_REQUIRE_EQUAL(this->outcomes[i].size(), this->names.size());
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600325
326 auto pktName = PktType::makeName(this->names[i], this->m_keyChain);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800327 for (size_t j = 0; j < this->names.size(); ++j) {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800328 bool expectedOutcome = this->outcomes[i][j];
329
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600330 auto klName = this->makeKeyLocatorKeyName(this->names[j]);
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400331 this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithRsa, pktName, klName, expectedOutcome);
332 this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithEcdsa, pktName, klName, false);
333
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800334
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600335 klName = this->makeKeyLocatorCertName(this->names[j]);
Alexander Afanasyev17d4b932021-03-17 17:58:40 -0400336 this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithRsa, pktName, klName, expectedOutcome);
337 this->template testChecker<PktType>(this->checker, tlv::SignatureSha256WithEcdsa, pktName, klName, false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800338 }
339 }
340}
341
342BOOST_AUTO_TEST_SUITE_END() // TestChecker
343BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800344BOOST_AUTO_TEST_SUITE_END() // Security
345
346} // namespace tests
347} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400348} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800349} // namespace security
350} // namespace ndn