Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9062a50 | 2020-01-04 17:14:04 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, Regents of the University of California |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include "encryptor.hpp" |
| 21 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 23 | #include "tests/io-key-chain-fixture.hpp" |
| 24 | #include "tests/unit/static-data.hpp" |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 25 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 26 | #include <ndn-cxx/security/signing-helpers.hpp> |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 27 | #include <ndn-cxx/security/validator-null.hpp> |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/util/string-helper.hpp> |
| 30 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 31 | #include <iostream> |
| 32 | |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 33 | namespace ndn::nac::tests { |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 34 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 35 | class EncryptorStaticDataEnvironment : public IoKeyChainFixture |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 36 | { |
| 37 | public: |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 38 | EncryptorStaticDataEnvironment(bool shouldPublishData) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 39 | { |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 40 | if (shouldPublishData) { |
| 41 | publishData(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 44 | auto serveFromIms = [this] (const Name&, const Interest& interest) { |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 45 | auto data = m_ims.find(interest); |
| 46 | if (data != nullptr) { |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 47 | m_imsFace.put(*data); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 48 | } |
| 49 | }; |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 50 | m_imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {}); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 51 | advanceClocks(1_ms, 10); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 52 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 53 | m_imsFace.sentData.clear(); |
| 54 | m_imsFace.sentInterests.clear(); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
| 58 | publishData() |
| 59 | { |
| 60 | StaticData data; |
| 61 | for (const auto& block : data.managerPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 62 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 63 | } |
| 64 | advanceClocks(1_ms, 10); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 67 | protected: |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 68 | DummyClientFace m_imsFace{m_io, m_keyChain, {true, true}}; |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 69 | |
| 70 | private: |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 71 | InMemoryStoragePersistent m_ims; |
| 72 | }; |
| 73 | |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 74 | template<bool shouldPublishData = true> |
| 75 | class EncryptorFixture : public EncryptorStaticDataEnvironment |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 76 | { |
| 77 | public: |
| 78 | EncryptorFixture() |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 79 | : EncryptorStaticDataEnvironment(shouldPublishData) |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 80 | , face(m_io, m_keyChain, {true, true}) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 81 | , encryptor("/access/policy/identity/NAC/dataset", "/some/ck/prefix", signingWithSha256(), |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 82 | [=] (const ErrorCode& code, const std::string& error) { |
| 83 | onFailure(code, error); |
| 84 | }, |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 85 | validator, m_keyChain, face) |
| 86 | { |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 87 | face.linkTo(m_imsFace); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 88 | advanceClocks(1_ms, 10); |
| 89 | } |
| 90 | |
| 91 | public: |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 92 | DummyClientFace face; |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 93 | security::ValidatorNull validator; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 94 | Encryptor encryptor; |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 95 | signal::Signal<EncryptorFixture, ErrorCode, std::string> onFailure; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 98 | BOOST_FIXTURE_TEST_SUITE(TestEncryptor, EncryptorFixture<>) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 99 | |
| 100 | BOOST_AUTO_TEST_CASE(EncryptAndPublishedCk) |
| 101 | { |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 102 | encryptor.m_kek.reset(); |
| 103 | BOOST_CHECK_EQUAL(encryptor.m_isKekRetrievalInProgress, false); |
| 104 | encryptor.regenerateCk(); |
| 105 | BOOST_CHECK_EQUAL(encryptor.m_isKekRetrievalInProgress, true); |
| 106 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 107 | const std::string plaintext = "Data to encrypt"; |
| 108 | auto block = encryptor.encrypt({reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size()}); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 109 | |
| 110 | EncryptedContent content(block); |
| 111 | auto ckPrefix = content.getKeyLocator(); |
| 112 | BOOST_CHECK_EQUAL(ckPrefix.getPrefix(-1), "/some/ck/prefix/CK"); |
| 113 | |
| 114 | BOOST_CHECK(content.hasIv()); |
| 115 | BOOST_CHECK_NE(std::string(reinterpret_cast<const char*>(content.getPayload().value()), |
| 116 | content.getPayload().value_size()), |
| 117 | plaintext); |
| 118 | |
| 119 | advanceClocks(1_ms, 10); |
| 120 | |
| 121 | // check that KEK interests has been sent |
| 122 | BOOST_CHECK_EQUAL(face.sentInterests.at(0).getName().getPrefix(6), |
| 123 | Name("/access/policy/identity/NAC/dataset/KEK")); |
| 124 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 125 | auto kek = m_imsFace.sentData.at(0); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(kek.getName().getPrefix(6), Name("/access/policy/identity/NAC/dataset/KEK")); |
| 127 | BOOST_CHECK_EQUAL(kek.getName().size(), 7); |
| 128 | |
| 129 | face.sentData.clear(); |
| 130 | face.sentInterests.clear(); |
| 131 | |
| 132 | face.receive(Interest(ckPrefix) |
| 133 | .setCanBePrefix(true).setMustBeFresh(true)); |
| 134 | advanceClocks(1_ms, 10); |
| 135 | |
| 136 | auto ckName = face.sentData.at(0).getName(); |
| 137 | BOOST_CHECK_EQUAL(ckName.getPrefix(4), "/some/ck/prefix/CK"); |
| 138 | BOOST_CHECK_EQUAL(ckName.get(5), name::Component("ENCRYPTED-BY")); |
| 139 | |
| 140 | auto extractedKek = ckName.getSubName(6); |
| 141 | BOOST_CHECK_EQUAL(extractedKek, kek.getName()); |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 142 | |
| 143 | BOOST_CHECK_EQUAL(encryptor.m_isKekRetrievalInProgress, false); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 146 | BOOST_FIXTURE_TEST_CASE(KekRetrievalFailure, EncryptorFixture<false>) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 147 | { |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 148 | size_t nErrors = 0; |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 149 | onFailure.connect([&] (auto&&...) { ++nErrors; }); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 150 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 151 | const std::string plaintext = "Data to encrypt"; |
| 152 | auto block = encryptor.encrypt({reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size()}); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 153 | advanceClocks(1_ms, 10); |
| 154 | |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 155 | // check that KEK interests has been sent |
| 156 | BOOST_CHECK_EQUAL(face.sentInterests.at(0).getName().getPrefix(6), Name("/access/policy/identity/NAC/dataset/KEK")); |
| 157 | |
| 158 | // and failed |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(m_imsFace.sentData.size(), 0); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 160 | |
| 161 | advanceClocks(1_s, 13); // 4_s default interest lifetime x 3 |
| 162 | BOOST_CHECK_EQUAL(nErrors, 1); |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(m_imsFace.sentData.size(), 0); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 164 | |
| 165 | advanceClocks(1_s, 730); // 60 seconds between attempts + ~12 seconds for each attempt |
| 166 | BOOST_CHECK_EQUAL(nErrors, 11); |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(m_imsFace.sentData.size(), 0); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 168 | |
| 169 | // check recovery |
| 170 | |
| 171 | publishData(); |
| 172 | |
| 173 | advanceClocks(1_s, 73); |
| 174 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 175 | auto kek = m_imsFace.sentData.at(0); |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(kek.getName().getPrefix(6), Name("/access/policy/identity/NAC/dataset/KEK")); |
| 177 | BOOST_CHECK_EQUAL(kek.getName().size(), 7); |
| 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_CASE(EnumerateDataFromIms) |
| 181 | { |
| 182 | encryptor.regenerateCk(); |
| 183 | advanceClocks(1_ms, 10); |
| 184 | |
| 185 | encryptor.regenerateCk(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 186 | advanceClocks(1_ms, 10); |
| 187 | |
| 188 | BOOST_CHECK_EQUAL(encryptor.size(), 3); |
| 189 | size_t nCk = 0; |
| 190 | for (const auto& data : encryptor) { |
| 191 | BOOST_TEST_MESSAGE(data.getName()); |
| 192 | if (data.getName().getPrefix(4) == Name("/some/ck/prefix/CK")) { |
| 193 | ++nCk; |
| 194 | } |
| 195 | } |
| 196 | BOOST_CHECK_EQUAL(nCk, 3); |
| 197 | } |
| 198 | |
Davide Pesavento | 32d1dc2 | 2020-12-09 18:01:47 -0500 | [diff] [blame] | 199 | BOOST_AUTO_TEST_CASE(GenerateTestData, |
| 200 | * ut::description("regenerates the static test data used by other test cases") |
| 201 | * ut::disabled()) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 202 | { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 203 | const auto plaintext = "Data to encrypt"s; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 204 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 205 | std::cerr << "const std::vector<Block> encryptedBlobs = {\n"; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 206 | for (size_t i = 0; i < 3; ++i) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 207 | std::cerr << " \""; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 208 | auto block = encryptor.encrypt({reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size()}); |
| 209 | printHex(std::cerr, block.wireEncode(), true); |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 210 | std::cerr << "\"_block,\n"; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 211 | |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 212 | encryptor.regenerateCk(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 213 | advanceClocks(1_ms, 10); |
| 214 | } |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 215 | std::cerr << "};\n\n"; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 216 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 217 | std::cerr << "const std::vector<Block> encryptorPackets = {\n"; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 218 | for (const auto& data : encryptor) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 219 | std::cerr << " \""; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 220 | printHex(std::cerr, data.wireEncode(), true); |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 221 | std::cerr << "\"_block,\n"; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 222 | } |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 223 | std::cerr << "};\n\n"; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | BOOST_AUTO_TEST_SUITE_END() |
| 227 | |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 228 | } // namespace ndn::nac::tests |