blob: f7a45063412aa71f2249a0626d5a88240631ea29 [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento0a6456c2019-11-14 00:33:11 -05003 * Copyright (c) 2013-2019 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/v2/validation-policy-config.hpp"
23#include "ndn-cxx/security/transform/base64-encode.hpp"
24#include "ndn-cxx/security/transform/buffer-source.hpp"
25#include "ndn-cxx/security/transform/stream-sink.hpp"
26#include "ndn-cxx/util/logger.hpp"
27#include "ndn-cxx/util/io.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080028
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
30#include "tests/unit/security/v2/validator-config/common.hpp"
31#include "tests/unit/security/v2/validator-fixture.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080032
33namespace ndn {
34namespace security {
35namespace v2 {
36namespace validator_config {
37namespace tests {
38
39using namespace ndn::tests;
40using namespace ndn::security::v2::tests;
41
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080042BOOST_AUTO_TEST_SUITE(Security)
43BOOST_AUTO_TEST_SUITE(V2)
44BOOST_AUTO_TEST_SUITE(TestValidationPolicyConfig)
45
Davide Pesavento0a6456c2019-11-14 00:33:11 -050046BOOST_FIXTURE_TEST_CASE(EmptyConfig, HierarchicalValidatorFixture<ValidationPolicyConfig>)
47{
48 this->policy.load(ConfigSection{}, "<empty>");
49
50 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
51 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
52 BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0);
53 BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0);
54
55 Data d("/Security/V2/ValidationPolicyConfig/D");
56 this->m_keyChain.sign(d, signingByIdentity(this->identity));
57 VALIDATE_FAILURE(d, "Empty policy should reject everything");
58
59 Interest i("/Security/V2/ValidationPolicyConfig/I");
60 this->m_keyChain.sign(i, signingByIdentity(this->identity));
61 VALIDATE_FAILURE(i, "Empty policy should reject everything");
62}
63
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080064template<typename Packet>
65class PacketName;
66
67template<>
68class PacketName<Interest>
69{
70public:
71 static std::string
72 getName()
73 {
74 return "interest";
75 }
76};
77
78template<>
79class PacketName<Data>
80{
81public:
82 static std::string
83 getName()
84 {
85 return "data";
86 }
87};
88
89template<typename PacketType>
90class ValidationPolicyConfigFixture : public HierarchicalValidatorFixture<ValidationPolicyConfig>
91{
92public:
93 ValidationPolicyConfigFixture()
94 : path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "security" / "v2" / "validation-policy-config")
95 {
96 boost::filesystem::create_directories(path);
97 baseConfig = R"CONF(
98 rule
99 {
100 id test-rule-id
101 for )CONF" + PacketName<Packet>::getName() + R"CONF(
102 filter
103 {
104 type name
105 name )CONF" + identity.getName().toUri() + R"CONF(
106 relation is-prefix-of
107 }
108 checker
109 {
110 type hierarchical
111 sig-type rsa-sha256
112 }
113 }
114 )CONF";
115 }
116
117 ~ValidationPolicyConfigFixture()
118 {
Davide Pesavento0a6456c2019-11-14 00:33:11 -0500119 boost::system::error_code ec;
120 boost::filesystem::remove_all(path, ec);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800121 }
122
Davide Pesavento0a6456c2019-11-14 00:33:11 -0500123protected:
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800124 using Packet = PacketType;
125
126 const boost::filesystem::path path;
127 std::string baseConfig;
128};
129
130template<typename PacketType>
131class LoadStringWithFileAnchor : public ValidationPolicyConfigFixture<PacketType>
132{
133public:
134 LoadStringWithFileAnchor()
135 {
136 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false);
137
138 this->saveCertificate(this->identity, (this->path / "identity.ndncert").string());
139 this->policy.load(this->baseConfig + R"CONF(
140 trust-anchor
141 {
142 type file
143 file-name "trust-anchor.ndncert"
144 }
145 )CONF", (this->path / "test-config").string());
146
147 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
148 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
149 }
150};
151
152template<typename PacketType>
153class LoadFileWithFileAnchor : public ValidationPolicyConfigFixture<PacketType>
154{
155public:
156 LoadFileWithFileAnchor()
157 {
158 std::string configFile = (this->path / "config.conf").string();
159 {
Davide Pesavento0a6456c2019-11-14 00:33:11 -0500160 std::ofstream config(configFile);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800161 config << this->baseConfig << R"CONF(
Davide Pesavento0a6456c2019-11-14 00:33:11 -0500162 trust-anchor
163 {
164 type file
165 file-name "trust-anchor.ndncert"
166 }
167 )CONF";
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800168 }
Davide Pesavento0a6456c2019-11-14 00:33:11 -0500169
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800170 this->saveCertificate(this->identity, (this->path / "identity.ndncert").string());
171
172 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false);
173
174 this->policy.load(configFile);
175
176 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
177 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
178 }
179};
180
181template<typename PacketType>
182class LoadSectionWithFileAnchor : public ValidationPolicyConfigFixture<PacketType>
183{
184public:
185 LoadSectionWithFileAnchor()
186 {
187 auto section = makeSection(this->baseConfig + R"CONF(
188 trust-anchor
189 {
190 type file
191 file-name "trust-anchor.ndncert"
192 }
193 )CONF");
194
195 this->saveCertificate(this->identity, (this->path / "identity.ndncert").string());
196
197 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false);
198
199 this->policy.load(section, (this->path / "test-config").string());
200
201 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
202 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
203 }
204};
205
206template<typename PacketType>
207class LoadStringWithBase64Anchor : public ValidationPolicyConfigFixture<PacketType>
208{
209public:
210 LoadStringWithBase64Anchor()
211 {
212 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false);
213
214 std::ostringstream os;
Davide Pesavento0a6456c2019-11-14 00:33:11 -0500215 {
216 using namespace ndn::security::transform;
217 const auto& cert = this->identity.getDefaultKey().getDefaultCertificate().wireEncode();
218 bufferSource(cert.wire(), cert.size()) >> base64Encode(false) >> streamSink(os);
219 }
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800220
221 this->policy.load(this->baseConfig + R"CONF(
222 trust-anchor
223 {
224 type base64
225 base64-string ")CONF" + os.str() + R"CONF("
226 }
227 )CONF", (this->path / "test-config").string());
228
229 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
230 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
231 }
232};
233
234class NoRefresh
235{
236public:
237 static std::string
238 getRefreshString()
239 {
240 return "";
241 }
242};
243
244class Refresh1h
245{
246public:
247 static std::string
248 getRefreshString()
249 {
250 return "refresh 1h";
251 }
252
253 static time::milliseconds
254 getRefreshTime()
255 {
Davide Pesavento0f830802018-01-16 23:58:58 -0500256 return 1_h;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800257 }
258};
259
260class Refresh1m
261{
262public:
263 static std::string
264 getRefreshString()
265 {
266 return "refresh 1m";
267 }
268
269 static time::milliseconds
270 getRefreshTime()
271 {
Davide Pesavento0f830802018-01-16 23:58:58 -0500272 return 1_min;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800273 }
274};
275
276class Refresh1s
277{
278public:
279 static std::string
280 getRefreshString()
281 {
282 return "refresh 1s";
283 }
284
285 static time::milliseconds
286 getRefreshTime()
287 {
Davide Pesavento0f830802018-01-16 23:58:58 -0500288 return 1_s;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800289 }
290};
291
292template<typename PacketType, typename Refresh = NoRefresh>
293class LoadStringWithDirAnchor : public ValidationPolicyConfigFixture<PacketType>
294{
295public:
296 LoadStringWithDirAnchor()
297 {
298 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false);
299
300 boost::filesystem::create_directories(this->path / "keys");
301 this->saveCertificate(this->identity, (this->path / "keys" / "identity.ndncert").string());
302
303 this->policy.load(this->baseConfig + R"CONF(
304 trust-anchor
305 {
306 type dir
307 dir keys
308 )CONF" + Refresh::getRefreshString() + R"CONF(
309 }
310 )CONF", (this->path / "test-config").string());
311
312 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
313 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
314 }
315};
316
317using DataPolicies = boost::mpl::vector<LoadStringWithFileAnchor<Data>,
318 LoadFileWithFileAnchor<Data>,
319 LoadSectionWithFileAnchor<Data>,
320 LoadStringWithBase64Anchor<Data>,
321 LoadStringWithDirAnchor<Data>,
322 LoadStringWithDirAnchor<Data, Refresh1h>,
323 LoadStringWithDirAnchor<Data, Refresh1m>,
324 LoadStringWithDirAnchor<Data, Refresh1s>
325 >;
326
327using InterestPolicies = boost::mpl::vector<LoadStringWithFileAnchor<Interest>,
328 LoadFileWithFileAnchor<Interest>,
329 LoadSectionWithFileAnchor<Interest>,
330 LoadStringWithBase64Anchor<Interest>,
331 LoadStringWithDirAnchor<Interest>,
332 LoadStringWithDirAnchor<Interest, Refresh1h>,
333 LoadStringWithDirAnchor<Interest, Refresh1m>,
334 LoadStringWithDirAnchor<Interest, Refresh1s>
335 >;
336
337BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateData, Policy, DataPolicies, Policy)
338{
339 BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 1);
340 BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0);
341
342 using Packet = typename Policy::Packet;
343 Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet");
344
345 Packet packet = unsignedPacket;
346 VALIDATE_FAILURE(packet, "Unsigned");
347
348 packet = unsignedPacket;
349 this->m_keyChain.sign(packet, signingWithSha256());
350 VALIDATE_FAILURE(packet, "Policy doesn't accept Sha256Digest signature");
351
352 packet = unsignedPacket;
353 this->m_keyChain.sign(packet, signingByIdentity(this->identity));
354 VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor");
355
356 packet = unsignedPacket;
357 this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity));
358 VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the policy-compliant cert");
359
360 packet = unsignedPacket;
361 this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity));
362 VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert");
363
364 packet = unsignedPacket;
365 this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity));
366 VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor");
367}
368
369BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateInterest, Policy, InterestPolicies, Policy)
370{
371 BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0);
372 BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 1);
373
374 using Packet = typename Policy::Packet;
375 Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet");
376
377 Packet packet = unsignedPacket;
378 VALIDATE_FAILURE(packet, "Unsigned");
379
380 packet = unsignedPacket;
381 this->m_keyChain.sign(packet, signingWithSha256());
382 VALIDATE_FAILURE(packet, "Policy doesn't accept Sha256Digest signature");
383
384 packet = unsignedPacket;
385 this->m_keyChain.sign(packet, signingByIdentity(this->identity));
386 VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor");
387
388 packet = unsignedPacket;
389 this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity));
390 VALIDATE_FAILURE(packet, "Should fail, as there is no matching rule for data");
391
392 packet = unsignedPacket;
393 this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity));
394 VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert");
395
396 packet = unsignedPacket;
397 this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity));
398 VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor");
399}
400
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400401BOOST_FIXTURE_TEST_CASE(Reload, HierarchicalValidatorFixture<ValidationPolicyConfig>)
402{
403 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false);
404 this->policy.load(R"CONF(
405 rule
406 {
407 id test-rule-data-id
408 for data
409 filter
410 {
411 type name
412 name /foo/bar
413 relation is-prefix-of
414 }
415 checker
416 {
417 type hierarchical
418 sig-type rsa-sha256
419 }
420 }
421 rule
422 {
423 id test-rule-interest-id
424 for interest
425 filter
426 {
427 type name
428 name /foo/bar
429 relation is-prefix-of
430 }
431 checker
432 {
433 type hierarchical
434 sig-type rsa-sha256
435 }
436 }
437 trust-anchor
438 {
439 type dir
440 dir keys
441 refresh 1h
442 }
443 )CONF", "test-config");
444 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
445 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false);
446 BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 1);
447 BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 1);
448
449 this->policy.load(R"CONF(
450 trust-anchor
451 {
452 type any
453 }
454 )CONF", "test-config");
455 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
456 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, true);
457 BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0);
458 BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0);
459}
460
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800461using Packets = boost::mpl::vector<Interest, Data>;
462
463BOOST_FIXTURE_TEST_CASE_TEMPLATE(TrustAnchorWildcard, Packet, Packets, ValidationPolicyConfigFixture<Packet>)
464{
465 this->policy.load(R"CONF(
466 trust-anchor
467 {
468 type any
469 }
470 )CONF", "test-config");
471
472 BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true);
473 BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, true);
474 BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0);
475 BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0);
476
477 Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet");
478
479 Packet packet = unsignedPacket;
480 VALIDATE_SUCCESS(packet, "Policy should accept everything");
481
482 packet = unsignedPacket;
483 this->m_keyChain.sign(packet, signingWithSha256());
484 VALIDATE_SUCCESS(packet, "Policy should accept everything");
485
486 packet = unsignedPacket;
487 this->m_keyChain.sign(packet, signingByIdentity(this->identity));
488 VALIDATE_SUCCESS(packet, "Policy should accept everything");
489
490 packet = unsignedPacket;
491 this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity));
492 VALIDATE_SUCCESS(packet, "Policy should accept everything");
493
494 packet = unsignedPacket;
495 this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity));
496 VALIDATE_SUCCESS(packet, "Policy should accept everything");
497
498 packet = unsignedPacket;
499 this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity));
500 VALIDATE_SUCCESS(packet, "Policy should accept everything");
501}
502
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400503using RefreshPolicies = boost::mpl::vector<Refresh1h, Refresh1m, Refresh1s>;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800504
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400505template<typename RefreshPolicy>
506class RefreshPolicyFixture : public LoadStringWithDirAnchor<Data, RefreshPolicy>
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800507{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800508};
509
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400510BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateRefresh, Refresh, RefreshPolicies, RefreshPolicyFixture<Refresh>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800511{
512 using Packet = Data;
513 Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet");
514
515 boost::filesystem::remove(this->path / "keys" / "identity.ndncert");
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400516 this->advanceClocks(Refresh::getRefreshTime(), 3);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800517
518 Packet packet = unsignedPacket;
519 this->m_keyChain.sign(packet, signingByIdentity(this->identity));
520 VALIDATE_FAILURE(packet, "Should fail, as the trust anchor should no longer exist");
521
522 packet = unsignedPacket;
523 this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity));
524 VALIDATE_FAILURE(packet, "Should fail, as the trust anchor should no longer exist");
525}
526
Alexander Afanasyev7b112462018-10-17 11:51:52 -0400527BOOST_FIXTURE_TEST_CASE(OrphanedPolicyLoad, HierarchicalValidatorFixture<ValidationPolicyConfig>) // Bug #4758
528{
529 ValidationPolicyConfig policy1;
530 BOOST_CHECK_THROW(policy1.load("trust-anchor { type any }", "test-config"), Error);
531
532 // Reloading would have triggered a segfault
533 BOOST_CHECK_THROW(policy1.load("trust-anchor { type any }", "test-config"), Error);
534
535 ValidationPolicyConfig policy2;
536
537 std::string config = R"CONF(
538 trust-anchor
539 {
540 type dir
541 dir keys
542 refresh 1h
543 }
544 )CONF";
545
546 // Inserting trust anchor would have triggered a segfault
547 BOOST_CHECK_THROW(policy2.load(config, "test-config"), Error);
548}
549
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800550BOOST_AUTO_TEST_SUITE_END() // TestValidationPolicyConfig
551BOOST_AUTO_TEST_SUITE_END() // V2
552BOOST_AUTO_TEST_SUITE_END() // Security
553
554} // namespace tests
555} // namespace validator_config
556} // namespace v2
557} // namespace security
558} // namespace ndn