Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -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/validation-policy-simple-hierarchy.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 25 | #include "tests/unit/security/validator-fixture.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 26 | |
| 27 | #include <boost/mpl/vector.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 31 | inline namespace v2 { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestValidationPolicySimpleHierarchy, |
| 36 | HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy>) |
| 37 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 38 | using Packets = boost::mpl::vector<InterestV03Pkt, DataPkt>; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 39 | |
| 40 | BOOST_AUTO_TEST_CASE_TEMPLATE(Validate, Packet, Packets) |
| 41 | { |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 42 | const Name name = "/Security/ValidatorFixture/Sub1/Sub2/Packet"; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 44 | auto packet = Packet::makePacket(name); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 45 | VALIDATE_FAILURE(packet, "Unsigned"); |
Davide Pesavento | 637ea3b | 2022-09-10 00:11:34 -0400 | [diff] [blame] | 46 | BOOST_TEST((lastError.getCode() == ValidationError::MALFORMED_SIGNATURE || // Interest |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 47 | lastError.getCode() == ValidationError::INVALID_KEY_LOCATOR)); // Data |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 48 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 49 | packet = Packet::makePacket(name); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 50 | m_keyChain.sign(packet, signingWithSha256()); |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 51 | VALIDATE_FAILURE(packet, "Should not be accepted, name not prefix of /localhost/identity/digest-sha256"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 52 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 53 | |
| 54 | packet = Packet::makePacket("/localhost/identity/digest-sha256/foobar"); |
| 55 | m_keyChain.sign(packet, signingWithSha256()); |
| 56 | VALIDATE_SUCCESS(packet, "Should be accepted, as name is prefix of /localhost/identity/digest-sha256"); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 57 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 58 | packet = Packet::makePacket(name); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 59 | m_keyChain.sign(packet, signingByIdentity(identity)); |
| 60 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor"); |
| 61 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 62 | packet = Packet::makePacket(name); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 63 | m_keyChain.sign(packet, signingByIdentity(subIdentity)); |
| 64 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the policy-compliant cert"); |
| 65 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 66 | packet = Packet::makePacket(name); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 67 | m_keyChain.sign(packet, signingByIdentity(otherIdentity)); |
| 68 | VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 69 | BOOST_TEST(lastError.getCode() == ValidationError::POLICY_ERROR); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 70 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 71 | packet = Packet::makePacket(name); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 72 | m_keyChain.sign(packet, signingByIdentity(subSelfSignedIdentity)); |
| 73 | VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 74 | BOOST_TEST(lastError.getCode() == ValidationError::LOOP_DETECTED); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 75 | |
| 76 | // TODO add checks with malformed packets |
| 77 | } |
| 78 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(CertNameInKeyLocator) |
Alexander Afanasyev | 8afba42 | 2020-06-11 17:56:21 -0400 | [diff] [blame] | 80 | { |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 81 | // auto cert = identity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 82 | // std::cerr << "Certificate idCert{\"" << toHex(cert) << "\"_block};" << std::endl; |
Alexander Afanasyev | 8afba42 | 2020-06-11 17:56:21 -0400 | [diff] [blame] | 83 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 84 | // cert = subIdentity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 85 | // std::cerr << "Certificate subIdCert{\"" << toHex(cert) << "\"_block};" << std::endl; |
Alexander Afanasyev | 8afba42 | 2020-06-11 17:56:21 -0400 | [diff] [blame] | 86 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 87 | // Data pkt("/Security/ValidatorFixture/Sub1/Sub2/Packet"); |
| 88 | // m_keyChain.sign(pkt, signingByIdentity(subIdentity)); |
| 89 | // std::cerr << "Data packet{\"" << toHex(pkt.wireEncode()) << "\"_block};" << std::endl; |
Alexander Afanasyev | 8afba42 | 2020-06-11 17:56:21 -0400 | [diff] [blame] | 90 | |
| 91 | // These are hard-coded with a key locator that is the exact name of the certificate |
| 92 | Certificate idCert{"06FD014C073C08085365637572697479081056616C696461746F724669787475726508034B455" |
| 93 | "9080816971C408D162A0A080473656C660809FD000001499D598CA0140918010219040036EE80155B30593013060" |
| 94 | "72A8648CE3D020106082A8648CE3D03010703420004F795BB9A7D51ECC4491605D49ADFCB46598D8260F872F0F15" |
| 95 | "15FA4EA7259F2A27C25B5ADAFA8D548C5FA0FBB88AB7769E986057FCAB52C7AF17A7964C47DF57B165C1B01031C2" |
| 96 | "D072B08085365637572697479081056616C696461746F724669787475726508034B4559080816971C408D162A0AF" |
| 97 | "D00FD26FD00FE0F313937303031303154303030303030FD00FF0F323033343131303654303533353332174630440" |
| 98 | "2200E9C538FE7285B006476C86F1CCCEC4398C939D1F6CE2E4F3537EB145252EE1502201A1C50E647CAF90CAA76A" |
| 99 | "065452D1D560E5BB7122A7553002E7DFBCA80FCB422"_block}; |
| 100 | Certificate subIdCert{"06FD0188074408085365637572697479081056616C696461746F7246697874757265080453" |
| 101 | "75623108034B45590808D76A31B01E14092B0806706172656E740809FD000001499D598CA0140918010219040036" |
| 102 | "EE80155B3059301306072A8648CE3D020106082A8648CE3D0301070342000441783BEDE80DEF6F2910D78E1BD0AD" |
| 103 | "7EF033A62F510A7578D1FC8F7601AC2B7709B27BBF225423A865964FA523B9746169FDB03C68B2FAC8D75DA0C755" |
| 104 | "8122AF16901B01031C3E073C08085365637572697479081056616C696461746F724669787475726508034B455908" |
| 105 | "0816971C408D162A0A080473656C660809FD000001499D598CA0FD00FD26FD00FE0F323031343131313154303533" |
| 106 | "353332FD00FF0F323033343131303654303533353332FD01021FFD02001BFD02010474797065FD02020F7375622D" |
| 107 | "636572746966696361746517463044022013CEFBF0E15EA6C85B26C56B550316A8A0F6F45510CCBDA4188E7732EF" |
| 108 | "1E81B6022026BDC1B6F32BE163F20E379B86EF686F5B8B2B57B6494DE89D5D503C8E3C6125"_block}; |
| 109 | Data packet{"06CD073008085365637572697479081056616C696461746F724669787475726508045375623108045375" |
| 110 | "623208065061636B657414001500164B1B01031C46074408085365637572697479081056616C696461746F724669" |
| 111 | "787475726508045375623108034B45590808D76A31B01E14092B0806706172656E740809FD000001499D598CA017" |
| 112 | "483046022100BDD3E0EF2385658825EB73E87A02D1A16AA8ACE50840C1B91782836164AACA3B0221008007B3EBA9" |
| 113 | "B7638BD204766B08AF6E4221CDB88156CC7DA13CD916610D6D3AED"_block}; |
| 114 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 115 | BOOST_REQUIRE_EQUAL(packet.getKeyLocator().value().getName(), |
| 116 | "/Security/ValidatorFixture/Sub1/KEY/%D7j1%B0%1E%14%09%2B/parent/%FD%00%00%01I%9DY%8C%A0"); |
| 117 | |
Alexander Afanasyev | 8afba42 | 2020-06-11 17:56:21 -0400 | [diff] [blame] | 118 | this->cache.insert(idCert); |
| 119 | this->cache.insert(subIdCert); |
| 120 | this->validator.loadAnchor("", std::move(idCert)); |
| 121 | |
Alexander Afanasyev | 8afba42 | 2020-06-11 17:56:21 -0400 | [diff] [blame] | 122 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the policy-compliant cert"); |
| 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_SUITE_END() // TestValidationPolicySimpleHierarchy |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 126 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 127 | |
| 128 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 129 | } // inline namespace v2 |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 130 | } // namespace security |
| 131 | } // namespace ndn |