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