Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [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/key-chain.hpp" |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/security/transform/private-key.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/security/verification-helpers.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 27 | #include "tests/key-chain-fixture.hpp" |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 28 | #include "tests/unit/clock-fixture.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/unit/test-home-env-saver.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 31 | #include <boost/mp11/list.hpp> |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 33 | namespace ndn::tests { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 35 | using namespace ndn::security; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestKeyChain, TestHomeEnvSaver) |
| 39 | |
| 40 | template<class Path> |
| 41 | class TestHomeAndPibFixture : public TestHomeFixture<Path> |
| 42 | { |
| 43 | public: |
| 44 | TestHomeAndPibFixture() |
| 45 | { |
| 46 | unsetenv("NDN_CLIENT_PIB"); |
| 47 | unsetenv("NDN_CLIENT_TPM"); |
| 48 | } |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 49 | |
| 50 | ~TestHomeAndPibFixture() |
| 51 | { |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 52 | KeyChain::resetDefaultLocators(); |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 53 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct PibPathConfigFileHome |
| 57 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 58 | static constexpr std::string_view PATH = "build/config-file-home/"; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathConfigFileHome>) |
| 62 | { |
| 63 | createClientConf({"pib=pib-memory:", "tpm=tpm-memory:"}); |
| 64 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 65 | KeyChain keyChain; |
| 66 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:"); |
| 67 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:"); |
| 68 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:"); |
| 69 | } |
| 70 | |
| 71 | struct PibPathConfigFileEmptyHome |
| 72 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 73 | static constexpr std::string_view PATH = "build/config-file-empty-home/"; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathConfigFileEmptyHome>) |
| 77 | { |
| 78 | createClientConf({"pib=pib-memory:"}); |
| 79 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 80 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 81 | std::string oldHOME; |
| 82 | if (std::getenv("OLD_HOME")) |
| 83 | oldHOME = std::getenv("OLD_HOME"); |
| 84 | |
| 85 | std::string HOME; |
| 86 | if (std::getenv("HOME")) |
| 87 | HOME = std::getenv("HOME"); |
| 88 | |
| 89 | if (!oldHOME.empty()) |
| 90 | setenv("HOME", oldHOME.c_str(), 1); |
| 91 | else |
| 92 | unsetenv("HOME"); |
| 93 | #endif |
| 94 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 95 | KeyChain keyChain; |
| 96 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:"); |
| 97 | |
Davide Pesavento | dc3575f | 2022-07-30 21:10:34 -0400 | [diff] [blame] | 98 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:"); |
| 100 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:"); |
| 101 | #else |
| 102 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:"); |
| 103 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:"); |
| 104 | #endif |
| 105 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 106 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 107 | if (!HOME.empty()) |
| 108 | setenv("HOME", HOME.c_str(), 1); |
| 109 | else |
| 110 | unsetenv("HOME"); |
| 111 | |
| 112 | if (!oldHOME.empty()) |
| 113 | setenv("OLD_HOME", oldHOME.c_str(), 1); |
| 114 | else |
| 115 | unsetenv("OLD_HOME"); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | struct PibPathConfigFileEmpty2Home |
| 120 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 121 | static constexpr std::string_view PATH = "build/config-file-empty2-home/"; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 124 | BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig2, TestHomeAndPibFixture<PibPathConfigFileEmpty2Home>) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 125 | { |
| 126 | createClientConf({"tpm=tpm-memory:"}); |
| 127 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 128 | KeyChain keyChain; |
| 129 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:"); |
| 130 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:"); |
| 131 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:"); |
| 132 | } |
| 133 | |
| 134 | struct PibPathConfigFileMalformedHome |
| 135 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 136 | static constexpr std::string_view PATH = "build/config-file-malformed-home/"; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 139 | BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig, TestHomeAndPibFixture<PibPathConfigFileMalformedHome>) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 140 | { |
| 141 | createClientConf({"pib=lord", "tpm=ring"}); |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 142 | BOOST_CHECK_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | struct PibPathConfigFileMalformed2Home |
| 146 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 147 | static constexpr std::string_view PATH = "build/config-file-malformed2-home/"; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 150 | BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig2, TestHomeAndPibFixture<PibPathConfigFileMalformed2Home>) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 151 | { |
| 152 | createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=just-wrong"}); |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 153 | BOOST_CHECK_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Alexander Afanasyev | 57d02b6 | 2018-06-15 18:19:50 -0400 | [diff] [blame] | 156 | struct PibPathConfigFileNonCanonicalTpm |
| 157 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 158 | static constexpr std::string_view PATH = "build/config-file-non-canonical-tpm/"; |
Alexander Afanasyev | 57d02b6 | 2018-06-15 18:19:50 -0400 | [diff] [blame] | 159 | }; |
| 160 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 161 | BOOST_FIXTURE_TEST_CASE(ConstructorNonCanonicalTpm, TestHomeAndPibFixture<PibPathConfigFileNonCanonicalTpm>, |
| 162 | * ut::description("test for bug #4297")) |
Alexander Afanasyev | 57d02b6 | 2018-06-15 18:19:50 -0400 | [diff] [blame] | 163 | { |
| 164 | createClientConf({"pib=pib-sqlite3:", "tpm=tpm-file"}); |
| 165 | |
| 166 | { |
| 167 | KeyChain keyChain; |
| 168 | keyChain.createIdentity("/test"); |
| 169 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:"); |
| 170 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:"); |
| 171 | } |
| 172 | |
| 173 | { |
| 174 | KeyChain keyChain; |
| 175 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:"); |
| 176 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:"); |
| 177 | BOOST_CHECK(keyChain.getPib().getIdentities().find("/test") != keyChain.getPib().getIdentities().end()); |
| 178 | } |
| 179 | } |
| 180 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 181 | BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib) |
| 182 | { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 183 | KeyChain keyChain("pib-memory", "tpm-memory"); |
| 184 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:"); |
| 185 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:"); |
| 186 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:"); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 187 | |
| 188 | BOOST_CHECK_NO_THROW(KeyChain("pib-memory:", "tpm-memory:")); |
| 189 | BOOST_CHECK_NO_THROW(KeyChain("pib-memory:/something", "tpm-memory:/something")); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 192 | BOOST_FIXTURE_TEST_CASE(SigningWithCorruptedPibTpm, KeyChainFixture) |
Alexander Afanasyev | f601e19 | 2020-06-02 16:41:07 -0400 | [diff] [blame] | 193 | { |
| 194 | Identity id = m_keyChain.createIdentity("/test"); |
| 195 | |
| 196 | Data data("/foobar"); |
| 197 | BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id))); |
| 198 | |
| 199 | // now, "corrupting TPM" |
| 200 | const_cast<Tpm&>(m_keyChain.getTpm()).deleteKey(id.getDefaultKey().getName()); |
| 201 | |
| 202 | BOOST_CHECK_NO_THROW(id.getDefaultKey()); |
| 203 | BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity(id)), KeyChain::InvalidSigningInfoError); |
| 204 | } |
| 205 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 206 | BOOST_FIXTURE_TEST_CASE(SigningWithNonExistingIdentity, KeyChainFixture) |
| 207 | { |
| 208 | Data data("/test/data"); |
| 209 | BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity("/non-existing/identity")), |
| 210 | KeyChain::InvalidSigningInfoError); |
| 211 | } |
| 212 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 213 | BOOST_FIXTURE_TEST_CASE(Management, KeyChainFixture) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 214 | { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 215 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 216 | BOOST_CHECK_THROW(m_keyChain.getPib().getDefaultIdentity(), Pib::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 217 | |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 218 | // Create an identity |
| 219 | Name idName("/test/id"); |
| 220 | Identity id = m_keyChain.createIdentity(idName); |
| 221 | BOOST_REQUIRE(id); |
| 222 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1); |
| 223 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentity(idName), id); |
| 224 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 225 | // The first added identity becomes the default identity |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity(), id); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 227 | // The default key of the added identity must exist |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 228 | Key key = id.getDefaultKey(); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 229 | BOOST_REQUIRE(key); |
| 230 | BOOST_CHECK_EQUAL(id.getKeys().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 231 | // The default certificate of the default key must exist |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 232 | BOOST_CHECK_NO_THROW(key.getDefaultCertificate()); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 233 | BOOST_CHECK_EQUAL(key.getCertificates().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 234 | |
| 235 | // Delete key |
| 236 | Name key1Name = key.getName(); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 237 | m_keyChain.deleteKey(id, key); |
| 238 | // The key instance should not be valid any more |
| 239 | BOOST_CHECK(!key); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 240 | BOOST_CHECK_THROW(id.getDefaultKey(), Pib::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 241 | BOOST_CHECK_THROW(id.getKey(key1Name), Pib::Error); |
| 242 | BOOST_CHECK_EQUAL(id.getKeys().size(), 0); |
| 243 | |
| 244 | // Create another key |
| 245 | m_keyChain.createKey(id); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 246 | // The added key becomes the default key |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 247 | Key key2 = id.getDefaultKey(); |
| 248 | BOOST_REQUIRE(key2); |
| 249 | BOOST_CHECK_NE(key2.getName(), key1Name); |
| 250 | BOOST_CHECK_EQUAL(id.getKeys().size(), 1); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 251 | BOOST_CHECK_NO_THROW(key2.getDefaultCertificate()); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(key2.getCertificates().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 253 | |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 254 | // Create a third key |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 255 | Key key3 = m_keyChain.createKey(id); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 256 | BOOST_CHECK_NE(key3.getName(), key2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(id.getKeys().size(), 2); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 258 | // The added key will not be the default key, because the default was already set |
| 259 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 260 | |
| 261 | // Delete cert |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 262 | BOOST_REQUIRE_EQUAL(key3.getCertificates().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 263 | Certificate key3Cert1 = *key3.getCertificates().begin(); |
| 264 | Name key3CertName = key3Cert1.getName(); |
| 265 | m_keyChain.deleteCertificate(key3, key3CertName); |
| 266 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 0); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 267 | BOOST_CHECK_THROW(key3.getDefaultCertificate(), Pib::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 268 | |
| 269 | // Add cert |
| 270 | m_keyChain.addCertificate(key3, key3Cert1); |
| 271 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 272 | BOOST_CHECK_NO_THROW(key3.getDefaultCertificate()); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 273 | m_keyChain.addCertificate(key3, key3Cert1); // overwriting the cert should work |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 274 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 275 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 276 | // Add another cert |
| 277 | Certificate key3Cert2 = key3Cert1; |
| 278 | Name key3Cert2Name = key3.getName(); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 279 | key3Cert2Name.append("Self").appendVersion(); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 280 | key3Cert2.setName(key3Cert2Name); |
| 281 | m_keyChain.addCertificate(key3, key3Cert2); |
| 282 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 2); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 283 | |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 284 | // Add empty cert |
| 285 | Certificate key3Cert3 = key3Cert1; |
| 286 | key3Cert3.unsetContent(); |
| 287 | BOOST_CHECK_THROW(m_keyChain.addCertificate(key3, key3Cert3), std::invalid_argument); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 288 | |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 289 | // Create another identity |
| 290 | Name id2Name("/test/id2"); |
| 291 | Identity id2 = m_keyChain.createIdentity(id2Name); |
| 292 | BOOST_REQUIRE(id2); |
| 293 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 2); |
| 294 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentity(id2Name), id2); |
| 295 | |
| 296 | // Default identity setting |
| 297 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id.getName()); |
| 298 | m_keyChain.setDefaultIdentity(id2); |
| 299 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 300 | |
| 301 | // Default key setting |
| 302 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName()); |
| 303 | m_keyChain.setDefaultKey(id, key3); |
| 304 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key3.getName()); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 305 | // Set key of a different identity as default |
| 306 | BOOST_CHECK_THROW(m_keyChain.setDefaultKey(id, id2.getDefaultKey()), std::invalid_argument); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 307 | |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 308 | // Default certificate setting |
| 309 | BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3CertName); |
| 310 | m_keyChain.setDefaultCertificate(key3, key3Cert2); |
| 311 | BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3Cert2Name); |
| 312 | // Set certificate of a different key as default |
| 313 | BOOST_CHECK_THROW(m_keyChain.setDefaultCertificate(key3, key2.getDefaultCertificate()), |
| 314 | std::invalid_argument); |
| 315 | |
| 316 | // Delete certificate name mismatch |
| 317 | BOOST_CHECK_THROW(m_keyChain.deleteCertificate(key2, key3CertName), std::invalid_argument); |
| 318 | // Delete key name mismatch |
| 319 | BOOST_CHECK_THROW(m_keyChain.deleteKey(id, id2.getDefaultKey()), std::invalid_argument); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 320 | |
| 321 | // Delete identity |
| 322 | m_keyChain.deleteIdentity(id); |
| 323 | // The identity instance should not be valid any more |
| 324 | BOOST_CHECK(!id); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 325 | BOOST_CHECK_THROW(m_keyChain.getPib().getIdentity(idName), Pib::Error); |
| 326 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 329 | struct DataPkt |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 330 | { |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 331 | Data packet{"/data"}; |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 332 | static constexpr SignedInterestFormat sigFormat = SignedInterestFormat::V02; // irrelevant for Data |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 333 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 334 | SignatureInfo |
| 335 | getSignatureInfo() const |
| 336 | { |
| 337 | return packet.getSignatureInfo(); |
| 338 | } |
| 339 | }; |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 340 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 341 | struct InterestV02Pkt |
| 342 | { |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 343 | Interest packet{"/interest02"}; |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 344 | static constexpr SignedInterestFormat sigFormat = SignedInterestFormat::V02; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 345 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 346 | SignatureInfo |
| 347 | getSignatureInfo() const |
| 348 | { |
| 349 | return SignatureInfo(packet.getName()[signed_interest::POS_SIG_INFO].blockFromValue()); |
| 350 | } |
| 351 | }; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 352 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 353 | struct InterestV03Pkt |
| 354 | { |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 355 | Interest packet{"/interest03"}; |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 356 | static constexpr SignedInterestFormat sigFormat = SignedInterestFormat::V03; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 357 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 358 | SignatureInfo |
| 359 | getSignatureInfo() const |
| 360 | { |
| 361 | return packet.getSignatureInfo().value(); // use .value() for checked access |
| 362 | } |
| 363 | }; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 364 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 365 | template<typename KeyParams> |
| 366 | struct DefaultIdentity |
| 367 | { |
| 368 | Identity |
| 369 | operator()(KeyChain& keyChain) const |
| 370 | { |
| 371 | auto id = keyChain.createIdentity("/id", KeyParams()); |
| 372 | BOOST_ASSERT(keyChain.getPib().getDefaultIdentity() == id); |
| 373 | return id; |
| 374 | } |
| 375 | }; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 376 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 377 | template<typename KeyParams> |
| 378 | struct NonDefaultIdentity |
| 379 | { |
| 380 | Identity |
| 381 | operator()(KeyChain& keyChain) const |
| 382 | { |
| 383 | auto id = keyChain.createIdentity("/id"); |
| 384 | auto id2 = keyChain.createIdentity("/id2", KeyParams()); |
| 385 | BOOST_ASSERT(keyChain.getPib().getDefaultIdentity() == id); |
| 386 | return id2; |
| 387 | } |
| 388 | }; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 389 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 390 | template<typename KeyParams> |
| 391 | struct DefaultKey |
| 392 | { |
| 393 | Key |
| 394 | operator()(KeyChain&, const Identity& id) const |
| 395 | { |
| 396 | auto key = id.getDefaultKey(); |
| 397 | BOOST_ASSERT(key.getKeyType() == KeyParams().getKeyType()); |
| 398 | return key; |
| 399 | } |
| 400 | }; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 401 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 402 | template<typename KeyParams> |
| 403 | struct NonDefaultKey |
| 404 | { |
| 405 | Key |
| 406 | operator()(KeyChain& keyChain, const Identity& id) const |
| 407 | { |
| 408 | auto key2 = keyChain.createKey(id, KeyParams()); |
| 409 | BOOST_ASSERT(id.getDefaultKey() != key2); |
| 410 | return key2; |
| 411 | } |
| 412 | }; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 413 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 414 | template<typename PacketType, |
| 415 | template<typename> class IdentityMaker = DefaultIdentity, |
| 416 | template<typename> class KeyMaker = DefaultKey, |
| 417 | typename AsymmetricKeyParams = EcKeyParams, |
| 418 | uint32_t SignatureTypeTlvValue = tlv::SignatureSha256WithEcdsa> |
| 419 | struct AsymmetricSigningBase : protected KeyChainFixture, protected PacketType |
| 420 | { |
| 421 | const Identity id = IdentityMaker<AsymmetricKeyParams>()(m_keyChain); |
| 422 | const Key key = KeyMaker<AsymmetricKeyParams>()(m_keyChain, id); |
| 423 | const Certificate cert = key.getDefaultCertificate(); |
| 424 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 425 | static constexpr uint32_t expectedSigType = SignatureTypeTlvValue; |
| 426 | static constexpr bool shouldHaveKeyLocator = true; |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 427 | const std::optional<KeyLocator> expectedKeyLocator = cert.getName(); |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 428 | |
| 429 | bool |
| 430 | verify(const SigningInfo&) const |
| 431 | { |
| 432 | return verifySignature(this->packet, key); |
| 433 | } |
| 434 | }; |
| 435 | |
| 436 | template<typename PacketType, |
| 437 | typename AsymmetricKeyParams, |
| 438 | uint32_t SignatureTypeTlvValue> |
| 439 | struct AsymmetricSigning : protected AsymmetricSigningBase<PacketType, DefaultIdentity, DefaultKey, |
| 440 | AsymmetricKeyParams, SignatureTypeTlvValue> |
| 441 | { |
| 442 | const std::vector<SigningInfo> signingInfos = { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 443 | SigningInfo(), |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 444 | SigningInfo(""), |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 445 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 446 | SigningInfo(this->id), |
| 447 | SigningInfo(SigningInfo::SIGNER_TYPE_ID, this->id.getName()), |
| 448 | SigningInfo("id:" + this->id.getName().toUri()), |
| 449 | signingByIdentity(this->id), |
| 450 | signingByIdentity(this->id.getName()), |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 451 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 452 | SigningInfo(this->key), |
| 453 | SigningInfo(SigningInfo::SIGNER_TYPE_KEY, this->key.getName()), |
| 454 | SigningInfo("key:" + this->key.getName().toUri()), |
| 455 | signingByKey(this->key), |
| 456 | signingByKey(this->key.getName()), |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 457 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 458 | SigningInfo(SigningInfo::SIGNER_TYPE_CERT, this->cert.getName()), |
| 459 | SigningInfo("cert:" + this->cert.getName().toUri()), |
| 460 | signingByCertificate(this->cert), |
| 461 | signingByCertificate(this->cert.getName()), |
| 462 | }; |
| 463 | }; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 464 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 465 | template<typename PacketType> |
| 466 | using RsaSigning = AsymmetricSigning<PacketType, RsaKeyParams, tlv::SignatureSha256WithRsa>; |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 467 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 468 | template<typename PacketType> |
| 469 | using EcdsaSigning = AsymmetricSigning<PacketType, EcKeyParams, tlv::SignatureSha256WithEcdsa>; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 470 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 471 | template<typename PacketType> |
| 472 | struct SigningWithNonDefaultIdentity : protected AsymmetricSigningBase<PacketType, NonDefaultIdentity> |
| 473 | { |
| 474 | const std::vector<SigningInfo> signingInfos = { |
| 475 | signingByIdentity(this->id), |
| 476 | signingByIdentity(this->id.getName()), |
| 477 | signingByKey(this->key), |
| 478 | signingByCertificate(this->cert), |
| 479 | }; |
| 480 | }; |
| 481 | |
| 482 | template<typename PacketType> |
| 483 | struct SigningWithNonDefaultKey : protected AsymmetricSigningBase<PacketType, NonDefaultIdentity, NonDefaultKey> |
| 484 | { |
| 485 | const std::vector<SigningInfo> signingInfos = { |
| 486 | signingByKey(this->key), |
| 487 | signingByKey(this->key.getName()), |
| 488 | signingByCertificate(this->cert), |
| 489 | }; |
| 490 | }; |
| 491 | |
| 492 | template<typename PacketType, |
| 493 | DigestAlgorithm DigestAlgo = DigestAlgorithm::SHA256, |
| 494 | uint32_t SignatureTypeTlvValue = tlv::SignatureHmacWithSha256> |
| 495 | struct HmacSigning : protected KeyChainFixture, protected PacketType |
| 496 | { |
| 497 | const std::vector<SigningInfo> signingInfos = { |
| 498 | SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, m_keyChain.createHmacKey()), |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 499 | SigningInfo("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg=="), |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 500 | }; |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 501 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 502 | static constexpr uint32_t expectedSigType = SignatureTypeTlvValue; |
| 503 | static constexpr bool shouldHaveKeyLocator = true; |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 504 | const std::optional<KeyLocator> expectedKeyLocator = std::nullopt; // don't check KeyLocator value |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 505 | |
| 506 | bool |
| 507 | verify(const SigningInfo& si) const |
| 508 | { |
| 509 | return verifySignature(this->packet, m_keyChain.getTpm(), si.getSignerName(), DigestAlgo); |
| 510 | } |
| 511 | }; |
| 512 | |
| 513 | template<typename PacketType> |
| 514 | struct Sha256Signing : protected KeyChainFixture, protected PacketType |
| 515 | { |
| 516 | const std::vector<SigningInfo> signingInfos = { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 517 | SigningInfo(SigningInfo::SIGNER_TYPE_SHA256), |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 518 | SigningInfo("id:" + SigningInfo::getDigestSha256Identity().toUri()), |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 519 | signingWithSha256() |
| 520 | }; |
| 521 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 522 | static constexpr uint32_t expectedSigType = tlv::DigestSha256; |
| 523 | static constexpr bool shouldHaveKeyLocator = false; |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 524 | const std::optional<KeyLocator> expectedKeyLocator = std::nullopt; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 525 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 526 | bool |
| 527 | verify(const SigningInfo&) const |
| 528 | { |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 529 | return verifySignature(this->packet, std::nullopt); |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 530 | } |
| 531 | }; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 532 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 533 | using SigningTests = boost::mp11::mp_list< |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 534 | RsaSigning<DataPkt>, |
| 535 | RsaSigning<InterestV02Pkt>, |
| 536 | RsaSigning<InterestV03Pkt>, |
| 537 | EcdsaSigning<DataPkt>, |
| 538 | EcdsaSigning<InterestV02Pkt>, |
| 539 | EcdsaSigning<InterestV03Pkt>, |
| 540 | HmacSigning<DataPkt>, |
| 541 | HmacSigning<InterestV02Pkt>, |
| 542 | HmacSigning<InterestV03Pkt>, |
| 543 | Sha256Signing<DataPkt>, |
| 544 | Sha256Signing<InterestV02Pkt>, |
| 545 | Sha256Signing<InterestV03Pkt>, |
| 546 | SigningWithNonDefaultIdentity<DataPkt>, |
Davide Pesavento | 809f754 | 2021-03-24 18:53:05 -0400 | [diff] [blame] | 547 | SigningWithNonDefaultIdentity<InterestV03Pkt>, |
| 548 | SigningWithNonDefaultKey<DataPkt>, |
| 549 | SigningWithNonDefaultKey<InterestV03Pkt> |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 550 | >; |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 551 | |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 552 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(SigningInterface, T, SigningTests, T) |
| 553 | { |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 554 | BOOST_TEST_INFO_SCOPE("Packet = " << this->packet.getName()); |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 555 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 556 | for (auto signingInfo : this->signingInfos) { |
| 557 | signingInfo.setSignedInterestFormat(this->sigFormat); |
| 558 | BOOST_TEST_INFO_SCOPE("SigningInfo = " << signingInfo); |
Davide Pesavento | 7371036 | 2020-12-04 16:10:03 -0500 | [diff] [blame] | 559 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 560 | this->m_keyChain.sign(this->packet, signingInfo); |
| 561 | |
| 562 | auto sigInfo = this->getSignatureInfo(); |
| 563 | BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), this->expectedSigType); |
| 564 | BOOST_CHECK_EQUAL(sigInfo.hasKeyLocator(), this->shouldHaveKeyLocator); |
| 565 | if (this->expectedKeyLocator) { |
| 566 | BOOST_CHECK_EQUAL(sigInfo.getKeyLocator(), *this->expectedKeyLocator); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 567 | } |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 568 | BOOST_CHECK(this->verify(signingInfo)); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 572 | class MakeCertificateFixture : public ClockFixture |
| 573 | { |
| 574 | public: |
| 575 | MakeCertificateFixture() |
| 576 | : requesterKeyChain("pib-memory:", "tpm-memory:") |
| 577 | , signerKeyChain("pib-memory:", "tpm-memory:") |
| 578 | { |
| 579 | m_systemClock->setNow(time::fromIsoString("20091117T203458,651387237").time_since_epoch()); |
| 580 | |
| 581 | requester = requesterKeyChain.createIdentity("/requester").getDefaultKey(); |
| 582 | Name signerIdentityName("/signer"); |
| 583 | signerKey = signerKeyChain.createIdentity(signerIdentityName).getDefaultKey(); |
| 584 | signerParams = signingByIdentity(signerIdentityName); |
| 585 | } |
| 586 | |
| 587 | void |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 588 | checkKeyLocatorName(const Certificate& cert, |
| 589 | const std::optional<Name>& klName = std::nullopt) const |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 590 | { |
| 591 | auto kl = cert.getKeyLocator(); |
| 592 | if (!kl.has_value()) { |
| 593 | BOOST_ERROR("KeyLocator is missing"); |
| 594 | return; |
| 595 | } |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 596 | BOOST_CHECK_EQUAL(kl->getName(), klName.value_or(signerKey.getDefaultCertificate().getName())); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void |
| 600 | checkCertFromDefaults(const Certificate& cert) const |
| 601 | { |
| 602 | BOOST_CHECK(Certificate::isValidName(cert.getName())); |
| 603 | BOOST_CHECK_EQUAL(cert.getKeyName(), requester.getName()); |
| 604 | BOOST_CHECK_EQUAL(cert.getName()[-2], name::Component("NA")); |
| 605 | BOOST_CHECK(cert.getName()[-1].isVersion()); |
| 606 | |
| 607 | BOOST_CHECK_EQUAL(cert.getContentType(), tlv::ContentType_Key); |
| 608 | BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), 1_h); |
| 609 | |
Davide Pesavento | 8e2a61d | 2022-05-13 18:44:03 -0400 | [diff] [blame] | 610 | BOOST_TEST(cert.getPublicKey() == requester.getPublicKey(), boost::test_tools::per_element()); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 611 | |
| 612 | checkKeyLocatorName(cert); |
| 613 | |
| 614 | BOOST_CHECK(cert.isValid()); |
| 615 | auto vp = cert.getValidityPeriod().getPeriod(); |
| 616 | BOOST_CHECK_EQUAL(vp.first, time::fromIsoString("20091117T203458")); |
| 617 | BOOST_CHECK_EQUAL(vp.second, time::fromIsoString("20101117T203458")); |
| 618 | |
| 619 | auto adBlock = cert.getSignatureInfo().getCustomTlv(tlv::AdditionalDescription); |
| 620 | BOOST_CHECK(!adBlock.has_value()); |
| 621 | } |
| 622 | |
| 623 | public: |
| 624 | KeyChain requesterKeyChain; |
| 625 | pib::Key requester; |
| 626 | |
| 627 | KeyChain signerKeyChain; |
| 628 | pib::Key signerKey; |
| 629 | Name signerCertificateName; |
| 630 | SigningInfo signerParams; |
| 631 | }; |
| 632 | |
| 633 | BOOST_FIXTURE_TEST_SUITE(MakeCertificate, MakeCertificateFixture) |
| 634 | |
| 635 | BOOST_AUTO_TEST_CASE(DefaultsFromKey) |
| 636 | { |
| 637 | auto cert = signerKeyChain.makeCertificate(requester, signerParams); |
| 638 | checkCertFromDefaults(cert); |
| 639 | } |
| 640 | |
| 641 | BOOST_AUTO_TEST_CASE(DefaultsFromCert) |
| 642 | { |
| 643 | auto cert = signerKeyChain.makeCertificate(requester.getDefaultCertificate(), signerParams); |
| 644 | checkCertFromDefaults(cert); |
| 645 | } |
| 646 | |
| 647 | BOOST_AUTO_TEST_CASE(Options) |
| 648 | { |
| 649 | MakeCertificateOptions opts; |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 650 | opts.issuerId = name::Component::fromUri("ISSUER"); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 651 | opts.version = 41218268; |
| 652 | opts.freshnessPeriod = 321_s; |
| 653 | opts.validity.emplace(time::fromIsoString("20060702T150405"), |
| 654 | time::fromIsoString("20160702T150405")); |
| 655 | |
| 656 | SignatureInfo sigInfo; |
| 657 | sigInfo.setKeyLocator(signerKey.getName()); |
| 658 | sigInfo.setValidityPeriod(ValidityPeriod(time::fromIsoString("20060102T150405"), |
| 659 | time::fromIsoString("20160102T150405"))); |
| 660 | sigInfo.addCustomTlv(Block(0xF0)); |
| 661 | signerParams.setSignatureInfo(sigInfo); |
| 662 | |
| 663 | auto cert = signerKeyChain.makeCertificate(requester, signerParams, opts); |
| 664 | |
| 665 | BOOST_CHECK_EQUAL(cert.getName(), |
| 666 | Name(requester.getName()).append(PartialName("ISSUER/v=41218268"))); |
| 667 | BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), 321_s); |
| 668 | checkKeyLocatorName(cert, signerKey.getName()); |
| 669 | |
| 670 | auto vp = cert.getValidityPeriod().getPeriod(); |
| 671 | BOOST_CHECK_EQUAL(vp.first, time::fromIsoString("20060702T150405")); |
| 672 | BOOST_CHECK_EQUAL(vp.second, time::fromIsoString("20160702T150405")); |
| 673 | |
| 674 | BOOST_CHECK(cert.getSignatureInfo().getCustomTlv(0xF0).has_value()); |
| 675 | } |
| 676 | |
| 677 | BOOST_AUTO_TEST_CASE(ErrSigner) |
| 678 | { |
| 679 | signerParams = signingByIdentity("/nonexistent"); |
| 680 | BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams), KeyChain::Error); |
| 681 | } |
| 682 | |
| 683 | BOOST_AUTO_TEST_CASE(ErrZeroFreshness) |
| 684 | { |
| 685 | MakeCertificateOptions opts; |
| 686 | opts.freshnessPeriod = 0_ms; |
| 687 | BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams, opts), |
| 688 | std::invalid_argument); |
| 689 | } |
| 690 | |
| 691 | BOOST_AUTO_TEST_CASE(ErrNegativeFreshness) |
| 692 | { |
| 693 | MakeCertificateOptions opts; |
| 694 | opts.freshnessPeriod = -1_ms; |
| 695 | BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams, opts), |
| 696 | std::invalid_argument); |
| 697 | } |
| 698 | |
| 699 | BOOST_AUTO_TEST_CASE(ErrContent) |
| 700 | { |
| 701 | Certificate request(requester.getDefaultCertificate()); |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 702 | |
| 703 | // malformed public key |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 704 | const auto& oldContent = request.getContent(); |
| 705 | std::vector<uint8_t> content(oldContent.value_begin(), oldContent.value_end()); |
| 706 | content[0] ^= 0x80; |
| 707 | request.setContent(content); |
| 708 | BOOST_CHECK_THROW(signerKeyChain.makeCertificate(request, signerParams), std::invalid_argument); |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 709 | |
| 710 | // empty content |
Davide Pesavento | 296c3a1 | 2023-05-04 21:40:40 -0400 | [diff] [blame] | 711 | request.setContent(""); |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 712 | BOOST_CHECK_THROW(signerKeyChain.makeCertificate(request, signerParams), std::invalid_argument); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | BOOST_AUTO_TEST_SUITE_END() // MakeCertificate |
| 716 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 717 | BOOST_FIXTURE_TEST_CASE(ImportPrivateKey, KeyChainFixture) |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 718 | { |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 719 | const Name keyName("/test/device2"); |
| 720 | const uint8_t rawKey[] = "nPSNOHyZKsg2WLqHAs7MXGb0sjQb4zCT"; |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 721 | auto key = make_shared<transform::PrivateKey>(); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 722 | key->loadRaw(KeyType::HMAC, rawKey); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 723 | |
| 724 | m_keyChain.importPrivateKey(keyName, key); |
| 725 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(keyName), true); |
| 726 | BOOST_CHECK_THROW(m_keyChain.importPrivateKey(keyName, key), KeyChain::Error); |
| 727 | } |
| 728 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 729 | BOOST_FIXTURE_TEST_CASE(ExportImport, KeyChainFixture) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 730 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 731 | Identity id = m_keyChain.createIdentity("/TestKeyChain/ExportIdentity"); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 732 | Certificate cert = id.getDefaultKey().getDefaultCertificate(); |
| 733 | |
| 734 | shared_ptr<SafeBag> exported = m_keyChain.exportSafeBag(cert, "1234", 4); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 735 | const auto& block = exported->wireEncode(); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 736 | |
| 737 | m_keyChain.deleteIdentity(id); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 738 | BOOST_CHECK_EXCEPTION(m_keyChain.exportSafeBag(cert, "1234", 4), KeyChain::Error, [] (const auto& e) { |
| 739 | return std::string(e.what()).find("Failed to export private key") == 0; |
| 740 | }); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 741 | |
| 742 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false); |
| 743 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 744 | |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 745 | SafeBag imported(block); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 746 | m_keyChain.importSafeBag(imported, "1234", 4); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 747 | BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(imported, "1234", 4), KeyChain::Error, [] (const auto& e) { |
| 748 | return std::string(e.what()).find("already exists") != std::string::npos; |
| 749 | }); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 750 | |
| 751 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), true); |
| 752 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 753 | Identity newId = m_keyChain.getPib().getIdentity(cert.getIdentity()); |
| 754 | BOOST_CHECK_EQUAL(newId.getKeys().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 755 | Key newKey = newId.getKey(cert.getKeyName()); |
| 756 | BOOST_CHECK_EQUAL(newKey.getCertificates().size(), 1); |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 757 | Certificate newCert = newKey.getCertificate(cert.getName()); |
| 758 | BOOST_CHECK_EQUAL(newCert, cert); |
| 759 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 760 | |
Davide Pesavento | e30389c | 2022-05-31 17:23:33 -0400 | [diff] [blame] | 761 | BOOST_FIXTURE_TEST_CASE(ImportInvalid, KeyChainFixture) |
| 762 | { |
| 763 | Identity id = m_keyChain.createIdentity("/TestKeyChain/ExportIdentity"); |
| 764 | Certificate cert = id.getDefaultKey().getDefaultCertificate(); |
| 765 | |
| 766 | auto exported = m_keyChain.exportSafeBag(cert, "1234", 4); |
| 767 | m_keyChain.deleteIdentity(id); |
| 768 | |
| 769 | Identity id2 = m_keyChain.createIdentity("/TestKeyChain/AnotherIdentity"); |
| 770 | Certificate cert2 = id2.getDefaultKey().getDefaultCertificate(); |
| 771 | m_keyChain.deleteIdentity(id2); |
| 772 | |
| 773 | SafeBag mismatch(cert2, exported->getEncryptedKey()); |
| 774 | BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(mismatch, "1234", 4), KeyChain::Error, [] (const auto& e) { |
| 775 | return std::string(e.what()).find("do not match") != std::string::npos; |
| 776 | }); |
| 777 | |
| 778 | SafeBag invalidPriv(cert2, {0xCA, 0xFE}); |
| 779 | BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(invalidPriv, "1234", 4), KeyChain::Error, [] (const auto& e) { |
| 780 | return std::string(e.what()).find("Failed to import private key") == 0; |
| 781 | }); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 784 | BOOST_FIXTURE_TEST_CASE(SelfSignedCertValidity, KeyChainFixture) |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 785 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 786 | Certificate cert = m_keyChain.createIdentity("/Security/TestKeyChain/SelfSignedCertValidity") |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 787 | .getDefaultKey() |
| 788 | .getDefaultCertificate(); |
| 789 | BOOST_CHECK(cert.isValid()); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 790 | BOOST_CHECK(cert.isValid(time::system_clock::now() + 10 * 365_days)); |
| 791 | BOOST_CHECK_GT(cert.getValidityPeriod().getPeriod().second, time::system_clock::now() + 10 * 365_days); |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 794 | BOOST_AUTO_TEST_SUITE_END() // TestKeyChain |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 795 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 796 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 797 | } // namespace ndn::tests |