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 | /* |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/security/signing-helpers.hpp" |
| 24 | #include "ndn-cxx/security/verification-helpers.hpp" |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/transform/private-key.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "tests/boost-test.hpp" |
| 28 | #include "tests/identity-management-fixture.hpp" |
| 29 | #include "tests/unit/test-home-env-saver.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 33 | inline namespace v2 { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 34 | namespace tests { |
| 35 | |
| 36 | using namespace ndn::tests; |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(TestKeyChain, TestHomeEnvSaver) |
| 40 | |
| 41 | template<class Path> |
| 42 | class TestHomeAndPibFixture : public TestHomeFixture<Path> |
| 43 | { |
| 44 | public: |
| 45 | TestHomeAndPibFixture() |
| 46 | { |
| 47 | unsetenv("NDN_CLIENT_PIB"); |
| 48 | unsetenv("NDN_CLIENT_TPM"); |
| 49 | } |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 50 | |
| 51 | ~TestHomeAndPibFixture() |
| 52 | { |
Alexander Afanasyev | 57d02b6 | 2018-06-15 18:19:50 -0400 | [diff] [blame] | 53 | try { |
| 54 | const_cast<std::string&>(KeyChain::getDefaultPibLocator()).clear(); |
| 55 | } |
| 56 | catch (const KeyChain::Error&) { |
| 57 | // ignore |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | const_cast<std::string&>(KeyChain::getDefaultTpmLocator()).clear(); |
| 62 | } |
| 63 | catch (const KeyChain::Error&) { |
| 64 | // ignore |
| 65 | } |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 66 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct PibPathConfigFileHome |
| 70 | { |
| 71 | const std::string PATH = "build/config-file-home/"; |
| 72 | }; |
| 73 | |
| 74 | BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathConfigFileHome>) |
| 75 | { |
| 76 | createClientConf({"pib=pib-memory:", "tpm=tpm-memory:"}); |
| 77 | |
| 78 | BOOST_REQUIRE_NO_THROW(KeyChain()); |
| 79 | |
| 80 | KeyChain keyChain; |
| 81 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:"); |
| 82 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:"); |
| 83 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:"); |
| 84 | } |
| 85 | |
| 86 | struct PibPathConfigFileEmptyHome |
| 87 | { |
| 88 | const std::string PATH = "build/config-file-empty-home/"; |
| 89 | }; |
| 90 | |
| 91 | BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathConfigFileEmptyHome>) |
| 92 | { |
| 93 | createClientConf({"pib=pib-memory:"}); |
| 94 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 95 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 96 | std::string oldHOME; |
| 97 | if (std::getenv("OLD_HOME")) |
| 98 | oldHOME = std::getenv("OLD_HOME"); |
| 99 | |
| 100 | std::string HOME; |
| 101 | if (std::getenv("HOME")) |
| 102 | HOME = std::getenv("HOME"); |
| 103 | |
| 104 | if (!oldHOME.empty()) |
| 105 | setenv("HOME", oldHOME.c_str(), 1); |
| 106 | else |
| 107 | unsetenv("HOME"); |
| 108 | #endif |
| 109 | |
| 110 | BOOST_REQUIRE_NO_THROW(KeyChain()); |
| 111 | KeyChain keyChain; |
| 112 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:"); |
| 113 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 114 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:"); |
| 116 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:"); |
| 117 | #else |
| 118 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:"); |
| 119 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:"); |
| 120 | #endif |
| 121 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 122 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 123 | if (!HOME.empty()) |
| 124 | setenv("HOME", HOME.c_str(), 1); |
| 125 | else |
| 126 | unsetenv("HOME"); |
| 127 | |
| 128 | if (!oldHOME.empty()) |
| 129 | setenv("OLD_HOME", oldHOME.c_str(), 1); |
| 130 | else |
| 131 | unsetenv("OLD_HOME"); |
| 132 | #endif |
| 133 | } |
| 134 | |
| 135 | struct PibPathConfigFileEmpty2Home |
| 136 | { |
| 137 | const std::string PATH = "build/config-file-empty2-home/"; |
| 138 | }; |
| 139 | |
| 140 | BOOST_FIXTURE_TEST_CASE(ConstructorEmpty2Config, TestHomeAndPibFixture<PibPathConfigFileEmpty2Home>) |
| 141 | { |
| 142 | createClientConf({"tpm=tpm-memory:"}); |
| 143 | |
| 144 | BOOST_REQUIRE_NO_THROW(KeyChain()); |
| 145 | |
| 146 | KeyChain keyChain; |
| 147 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:"); |
| 148 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:"); |
| 149 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:"); |
| 150 | } |
| 151 | |
| 152 | struct PibPathConfigFileMalformedHome |
| 153 | { |
| 154 | const std::string PATH = "build/config-file-malformed-home/"; |
| 155 | }; |
| 156 | |
| 157 | BOOST_FIXTURE_TEST_CASE(ConstructorMalConfig, TestHomeAndPibFixture<PibPathConfigFileMalformedHome>) |
| 158 | { |
| 159 | createClientConf({"pib=lord", "tpm=ring"}); |
| 160 | |
| 161 | BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected. |
| 162 | } |
| 163 | |
| 164 | struct PibPathConfigFileMalformed2Home |
| 165 | { |
| 166 | const std::string PATH = "build/config-file-malformed2-home/"; |
| 167 | }; |
| 168 | |
| 169 | BOOST_FIXTURE_TEST_CASE(ConstructorMal2Config, TestHomeAndPibFixture<PibPathConfigFileMalformed2Home>) |
| 170 | { |
| 171 | createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=just-wrong"}); |
| 172 | |
| 173 | BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected. |
| 174 | } |
| 175 | |
Alexander Afanasyev | 57d02b6 | 2018-06-15 18:19:50 -0400 | [diff] [blame] | 176 | struct PibPathConfigFileNonCanonicalTpm |
| 177 | { |
| 178 | const std::string PATH = "build/config-file-non-canonical-tpm/"; |
| 179 | }; |
| 180 | |
| 181 | BOOST_FIXTURE_TEST_CASE(ConstructorNonCanonicalTpm, TestHomeAndPibFixture<PibPathConfigFileNonCanonicalTpm>) // Bug 4297 |
| 182 | { |
| 183 | createClientConf({"pib=pib-sqlite3:", "tpm=tpm-file"}); |
| 184 | |
| 185 | { |
| 186 | KeyChain keyChain; |
| 187 | keyChain.createIdentity("/test"); |
| 188 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:"); |
| 189 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:"); |
| 190 | } |
| 191 | |
| 192 | { |
| 193 | KeyChain keyChain; |
| 194 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:"); |
| 195 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:"); |
| 196 | BOOST_CHECK(keyChain.getPib().getIdentities().find("/test") != keyChain.getPib().getIdentities().end()); |
| 197 | } |
| 198 | } |
| 199 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 200 | BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib) |
| 201 | { |
| 202 | BOOST_REQUIRE_NO_THROW((KeyChain("pib-memory", "tpm-memory"))); |
| 203 | BOOST_REQUIRE_NO_THROW((KeyChain("pib-memory:", "tpm-memory:"))); |
| 204 | BOOST_REQUIRE_NO_THROW((KeyChain("pib-memory:/something", "tpm-memory:/something"))); |
| 205 | |
| 206 | KeyChain keyChain("pib-memory", "tpm-memory"); |
| 207 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:"); |
| 208 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:"); |
| 209 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:"); |
| 210 | } |
| 211 | |
Alexander Afanasyev | f601e19 | 2020-06-02 16:41:07 -0400 | [diff] [blame] | 212 | BOOST_FIXTURE_TEST_CASE(SigningWithCorruptedPibTpm, IdentityManagementFixture) |
| 213 | { |
| 214 | Identity id = m_keyChain.createIdentity("/test"); |
| 215 | |
| 216 | Data data("/foobar"); |
| 217 | BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id))); |
| 218 | |
| 219 | // now, "corrupting TPM" |
| 220 | const_cast<Tpm&>(m_keyChain.getTpm()).deleteKey(id.getDefaultKey().getName()); |
| 221 | |
| 222 | BOOST_CHECK_NO_THROW(id.getDefaultKey()); |
| 223 | BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity(id)), KeyChain::InvalidSigningInfoError); |
| 224 | } |
| 225 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 226 | BOOST_FIXTURE_TEST_CASE(Management, IdentityManagementFixture) |
| 227 | { |
| 228 | Name identityName("/test/id"); |
| 229 | Name identity2Name("/test/id2"); |
| 230 | |
| 231 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 232 | BOOST_REQUIRE_THROW(m_keyChain.getPib().getDefaultIdentity(), Pib::Error); |
| 233 | |
| 234 | // Create identity |
| 235 | Identity id = m_keyChain.createIdentity(identityName); |
| 236 | BOOST_CHECK(id); |
| 237 | BOOST_CHECK(m_keyChain.getPib().getIdentities().find(identityName) != m_keyChain.getPib().getIdentities().end()); |
| 238 | // The first added identity becomes the default identity |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 239 | BOOST_CHECK_NO_THROW(m_keyChain.getPib().getDefaultIdentity()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 240 | // The default key of the added identity must exist |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 241 | Key key = id.getDefaultKey(); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 242 | // The default certificate of the default key must exist |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 243 | BOOST_CHECK_NO_THROW(key.getDefaultCertificate()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 244 | |
| 245 | // Delete key |
| 246 | Name key1Name = key.getName(); |
| 247 | BOOST_CHECK_NO_THROW(id.getKey(key1Name)); |
| 248 | BOOST_CHECK_EQUAL(id.getKeys().size(), 1); |
| 249 | m_keyChain.deleteKey(id, key); |
| 250 | // The key instance should not be valid any more |
| 251 | BOOST_CHECK(!key); |
| 252 | BOOST_CHECK_THROW(id.getKey(key1Name), Pib::Error); |
| 253 | BOOST_CHECK_EQUAL(id.getKeys().size(), 0); |
| 254 | |
| 255 | // Create another key |
| 256 | m_keyChain.createKey(id); |
| 257 | // The added key becomes the default key. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 258 | Key key2 = id.getDefaultKey(); |
| 259 | BOOST_REQUIRE(key2); |
| 260 | BOOST_CHECK_NE(key2.getName(), key1Name); |
| 261 | BOOST_CHECK_EQUAL(id.getKeys().size(), 1); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 262 | BOOST_CHECK_NO_THROW(key2.getDefaultCertificate()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 263 | |
| 264 | // Create the third key |
| 265 | Key key3 = m_keyChain.createKey(id); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 266 | BOOST_CHECK_NE(key3.getName(), key2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 267 | // The added key will not be the default key, because the default key already exists |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 268 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(id.getKeys().size(), 2); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 270 | BOOST_CHECK_NO_THROW(key3.getDefaultCertificate()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 271 | |
| 272 | // Delete cert |
| 273 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
| 274 | Certificate key3Cert1 = *key3.getCertificates().begin(); |
| 275 | Name key3CertName = key3Cert1.getName(); |
| 276 | m_keyChain.deleteCertificate(key3, key3CertName); |
| 277 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 0); |
| 278 | BOOST_REQUIRE_THROW(key3.getDefaultCertificate(), Pib::Error); |
| 279 | |
| 280 | // Add cert |
| 281 | m_keyChain.addCertificate(key3, key3Cert1); |
| 282 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 283 | BOOST_CHECK_NO_THROW(key3.getDefaultCertificate()); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 284 | m_keyChain.addCertificate(key3, key3Cert1); // overwriting the cert should work |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 285 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
| 286 | // Add another cert |
| 287 | Certificate key3Cert2 = key3Cert1; |
| 288 | Name key3Cert2Name = key3.getName(); |
| 289 | key3Cert2Name.append("Self"); |
| 290 | key3Cert2Name.appendVersion(); |
| 291 | key3Cert2.setName(key3Cert2Name); |
| 292 | m_keyChain.addCertificate(key3, key3Cert2); |
| 293 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 2); |
| 294 | |
| 295 | // Default certificate setting |
| 296 | BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3CertName); |
| 297 | m_keyChain.setDefaultCertificate(key3, key3Cert2); |
| 298 | BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3Cert2Name); |
| 299 | |
| 300 | // Default key setting |
| 301 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName()); |
| 302 | m_keyChain.setDefaultKey(id, key3); |
| 303 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key3.getName()); |
| 304 | |
| 305 | // Default identity setting |
| 306 | Identity id2 = m_keyChain.createIdentity(identity2Name); |
| 307 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id.getName()); |
| 308 | m_keyChain.setDefaultIdentity(id2); |
| 309 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id2.getName()); |
| 310 | |
| 311 | // Delete identity |
| 312 | m_keyChain.deleteIdentity(id); |
| 313 | // The identity instance should not be valid any more |
| 314 | BOOST_CHECK(!id); |
| 315 | BOOST_REQUIRE_THROW(m_keyChain.getPib().getIdentity(identityName), Pib::Error); |
| 316 | BOOST_CHECK(m_keyChain.getPib().getIdentities().find(identityName) == m_keyChain.getPib().getIdentities().end()); |
| 317 | } |
| 318 | |
| 319 | BOOST_FIXTURE_TEST_CASE(GeneralSigningInterface, IdentityManagementFixture) |
| 320 | { |
| 321 | Identity id = addIdentity("/id"); |
| 322 | Key key = id.getDefaultKey(); |
| 323 | Certificate cert = key.getDefaultCertificate(); |
| 324 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 325 | Name hmacKeyName = m_keyChain.createHmacKey(); |
| 326 | const Tpm& tpm = m_keyChain.getTpm(); |
| 327 | |
| 328 | std::vector<SigningInfo> signingInfos = { |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 329 | // New signed Interest format |
| 330 | SigningInfo().setSignedInterestFormat(SignedInterestFormat::V03), |
| 331 | |
| 332 | SigningInfo(SigningInfo::SIGNER_TYPE_ID, id.getName()) |
| 333 | .setSignedInterestFormat(SignedInterestFormat::V03), |
| 334 | signingByIdentity(id.getName()).setSignedInterestFormat(SignedInterestFormat::V03), |
| 335 | |
| 336 | SigningInfo(id).setSignedInterestFormat(SignedInterestFormat::V03), |
| 337 | signingByIdentity(id).setSignedInterestFormat(SignedInterestFormat::V03), |
| 338 | |
| 339 | SigningInfo(SigningInfo::SIGNER_TYPE_KEY, key.getName()) |
| 340 | .setSignedInterestFormat(SignedInterestFormat::V03), |
| 341 | signingByKey(key.getName()).setSignedInterestFormat(SignedInterestFormat::V03), |
| 342 | |
| 343 | SigningInfo(key).setSignedInterestFormat(SignedInterestFormat::V03), |
| 344 | signingByKey(key).setSignedInterestFormat(SignedInterestFormat::V03), |
| 345 | |
| 346 | SigningInfo(SigningInfo::SIGNER_TYPE_CERT, cert.getName()) |
| 347 | .setSignedInterestFormat(SignedInterestFormat::V03), |
| 348 | signingByCertificate(cert.getName()).setSignedInterestFormat(SignedInterestFormat::V03), |
| 349 | signingByCertificate(cert).setSignedInterestFormat(SignedInterestFormat::V03), |
| 350 | |
| 351 | SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, hmacKeyName) |
| 352 | .setSignedInterestFormat(SignedInterestFormat::V03), |
| 353 | SigningInfo("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg==") |
| 354 | .setSignedInterestFormat(SignedInterestFormat::V03), |
| 355 | |
| 356 | SigningInfo(SigningInfo::SIGNER_TYPE_SHA256) |
| 357 | .setSignedInterestFormat(SignedInterestFormat::V03), |
| 358 | signingWithSha256().setSignedInterestFormat(SignedInterestFormat::V03), |
| 359 | |
| 360 | // Deprecated signed Interest format |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 361 | SigningInfo(), |
| 362 | |
| 363 | SigningInfo(SigningInfo::SIGNER_TYPE_ID, id.getName()), |
| 364 | signingByIdentity(id.getName()), |
| 365 | |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 366 | SigningInfo(id), |
| 367 | signingByIdentity(id), |
| 368 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 369 | SigningInfo(SigningInfo::SIGNER_TYPE_KEY, key.getName()), |
| 370 | signingByKey(key.getName()), |
| 371 | |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 372 | SigningInfo(key), |
| 373 | signingByKey(key), |
| 374 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 375 | SigningInfo(SigningInfo::SIGNER_TYPE_CERT, cert.getName()), |
| 376 | signingByCertificate(cert.getName()), |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 377 | signingByCertificate(cert), |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 378 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 379 | SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, hmacKeyName), |
| 380 | SigningInfo("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg=="), |
| 381 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 382 | SigningInfo(SigningInfo::SIGNER_TYPE_SHA256), |
| 383 | signingWithSha256() |
| 384 | }; |
| 385 | |
| 386 | for (const auto& signingInfo : signingInfos) { |
| 387 | BOOST_TEST_MESSAGE("SigningInfo: " << signingInfo); |
| 388 | Data data("/data"); |
| 389 | Interest interest("/interest"); |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 390 | interest.setCanBePrefix(false); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 391 | |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 392 | m_keyChain.sign(data, signingInfo); |
| 393 | m_keyChain.sign(interest, signingInfo); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 394 | |
| 395 | if (signingInfo.getSignerType() == SigningInfo::SIGNER_TYPE_SHA256) { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 396 | BOOST_CHECK_EQUAL(data.getSignatureType(), tlv::DigestSha256); |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 397 | |
| 398 | if (signingInfo.getSignedInterestFormat() == SignedInterestFormat::V03) { |
| 399 | BOOST_REQUIRE(interest.getSignatureInfo() != nullopt); |
| 400 | BOOST_CHECK_EQUAL(interest.getSignatureInfo()->getSignatureType(), tlv::DigestSha256); |
| 401 | } |
| 402 | else { |
| 403 | SignatureInfo sigInfo(interest.getName()[signed_interest::POS_SIG_INFO].blockFromValue()); |
| 404 | BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), tlv::DigestSha256); |
| 405 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 406 | |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 407 | BOOST_CHECK(verifyDigest(data, DigestAlgorithm::SHA256)); |
| 408 | BOOST_CHECK(verifyDigest(interest, DigestAlgorithm::SHA256)); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 409 | } |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 410 | else if (signingInfo.getSignerType() == SigningInfo::SIGNER_TYPE_HMAC) { |
| 411 | Name keyName = signingInfo.getSignerName(); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 412 | BOOST_CHECK_EQUAL(data.getSignatureType(), tlv::SignatureHmacWithSha256); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 413 | |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 414 | if (signingInfo.getSignedInterestFormat() == SignedInterestFormat::V03) { |
| 415 | BOOST_REQUIRE(interest.getSignatureInfo() != nullopt); |
| 416 | BOOST_CHECK_EQUAL(interest.getSignatureInfo()->getSignatureType(), |
| 417 | tlv::SignatureHmacWithSha256); |
| 418 | } |
| 419 | else { |
| 420 | SignatureInfo sigInfo(interest.getName()[signed_interest::POS_SIG_INFO].blockFromValue()); |
| 421 | BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), tlv::SignatureHmacWithSha256); |
| 422 | } |
| 423 | |
| 424 | BOOST_CHECK(verifySignature(data, tpm, keyName, DigestAlgorithm::SHA256)); |
| 425 | BOOST_CHECK(verifySignature(interest, tpm, keyName, DigestAlgorithm::SHA256)); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 426 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 427 | else { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 428 | BOOST_CHECK_EQUAL(data.getSignatureType(), tlv::SignatureSha256WithEcdsa); |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 429 | |
| 430 | if (signingInfo.getSignedInterestFormat() == SignedInterestFormat::V03) { |
| 431 | BOOST_REQUIRE(interest.getSignatureInfo() != nullopt); |
| 432 | BOOST_CHECK_EQUAL(interest.getSignatureInfo()->getSignatureType(), |
| 433 | tlv::SignatureSha256WithEcdsa); |
| 434 | } |
| 435 | else { |
| 436 | SignatureInfo sigInfo(interest.getName()[signed_interest::POS_SIG_INFO].blockFromValue()); |
| 437 | BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), tlv::SignatureSha256WithEcdsa); |
| 438 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 439 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 440 | BOOST_CHECK_EQUAL(data.getKeyLocator()->getName(), cert.getName().getPrefix(-2)); |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 441 | |
| 442 | if (signingInfo.getSignedInterestFormat() == SignedInterestFormat::V03) { |
| 443 | BOOST_CHECK_EQUAL(interest.getSignatureInfo()->getKeyLocator().getName(), |
| 444 | cert.getName().getPrefix(-2)); |
| 445 | } |
| 446 | else { |
| 447 | SignatureInfo sigInfo(interest.getName()[signed_interest::POS_SIG_INFO].blockFromValue()); |
| 448 | BOOST_CHECK_EQUAL(sigInfo.getKeyLocator().getName(), cert.getName().getPrefix(-2)); |
| 449 | } |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 450 | |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 451 | BOOST_CHECK(verifySignature(data, key)); |
| 452 | BOOST_CHECK(verifySignature(interest, key)); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | BOOST_FIXTURE_TEST_CASE(PublicKeySigningDefaults, IdentityManagementFixture) |
| 458 | { |
| 459 | Data data("/test/data"); |
| 460 | |
| 461 | // Identity will be created with generated key and self-signed cert with default parameters |
| 462 | BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity("/non-existing/identity")), KeyChain::InvalidSigningInfoError); |
| 463 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 464 | // Create identity with EC key and the corresponding self-signed certificate |
| 465 | Identity id = addIdentity("/ndn/test/ec", EcKeyParams()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 466 | BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id.getName()))); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 467 | BOOST_CHECK_EQUAL(data.getSignatureType(), |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 468 | KeyChain::getSignatureType(EcKeyParams().getKeyType(), DigestAlgorithm::SHA256)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 469 | BOOST_REQUIRE(data.getKeyLocator().has_value()); |
| 470 | BOOST_CHECK(id.getName().isPrefixOf(data.getKeyLocator()->getName())); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 471 | |
| 472 | // Create identity with RSA key and the corresponding self-signed certificate |
| 473 | id = addIdentity("/ndn/test/rsa", RsaKeyParams()); |
| 474 | BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id.getName()))); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 475 | BOOST_CHECK_EQUAL(data.getSignatureType(), |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 476 | KeyChain::getSignatureType(RsaKeyParams().getKeyType(), DigestAlgorithm::SHA256)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 477 | BOOST_REQUIRE(data.getKeyLocator().has_value()); |
| 478 | BOOST_CHECK(id.getName().isPrefixOf(data.getKeyLocator()->getName())); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 479 | } |
| 480 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 481 | BOOST_FIXTURE_TEST_CASE(ImportPrivateKey, IdentityManagementFixture) |
| 482 | { |
| 483 | Name keyName("/test/device2"); |
| 484 | std::string rawKey("nPSNOHyZKsg2WLqHAs7MXGb0sjQb4zCT"); |
| 485 | auto key = make_shared<transform::PrivateKey>(); |
| 486 | key->loadRaw(KeyType::HMAC, reinterpret_cast<const uint8_t*>(rawKey.data()), rawKey.size()); |
| 487 | |
| 488 | m_keyChain.importPrivateKey(keyName, key); |
| 489 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(keyName), true); |
| 490 | BOOST_CHECK_THROW(m_keyChain.importPrivateKey(keyName, key), KeyChain::Error); |
| 491 | } |
| 492 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 493 | BOOST_FIXTURE_TEST_CASE(ExportImport, IdentityManagementFixture) |
| 494 | { |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 495 | Identity id = addIdentity("/TestKeyChain/ExportIdentity"); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 496 | Certificate cert = id.getDefaultKey().getDefaultCertificate(); |
| 497 | |
| 498 | shared_ptr<SafeBag> exported = m_keyChain.exportSafeBag(cert, "1234", 4); |
| 499 | Block block = exported->wireEncode(); |
| 500 | |
| 501 | m_keyChain.deleteIdentity(id); |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 502 | BOOST_CHECK_THROW(m_keyChain.exportSafeBag(cert, "1234", 4), KeyChain::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 503 | |
| 504 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false); |
| 505 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 506 | |
| 507 | SafeBag imported; |
| 508 | imported.wireDecode(block); |
| 509 | m_keyChain.importSafeBag(imported, "1234", 4); |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 510 | BOOST_CHECK_THROW(m_keyChain.importSafeBag(imported, "1234", 4), KeyChain::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 511 | |
| 512 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), true); |
| 513 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 514 | Identity newId = m_keyChain.getPib().getIdentity(cert.getIdentity()); |
| 515 | BOOST_CHECK_EQUAL(newId.getKeys().size(), 1); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 516 | Key newKey = newId.getKey(cert.getKeyName()); |
| 517 | BOOST_CHECK_EQUAL(newKey.getCertificates().size(), 1); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 518 | BOOST_CHECK_NO_THROW(newKey.getCertificate(cert.getName())); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 519 | |
| 520 | m_keyChain.deleteIdentity(newId); |
| 521 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 522 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false); |
| 523 | } |
| 524 | |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 525 | BOOST_FIXTURE_TEST_CASE(SelfSignedCertValidity, IdentityManagementFixture) |
| 526 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 527 | Certificate cert = addIdentity("/Security/TestKeyChain/SelfSignedCertValidity") |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 528 | .getDefaultKey() |
| 529 | .getDefaultCertificate(); |
| 530 | BOOST_CHECK(cert.isValid()); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 531 | BOOST_CHECK(cert.isValid(time::system_clock::now() + 10 * 365_days)); |
| 532 | 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] | 533 | } |
| 534 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 535 | BOOST_AUTO_TEST_SUITE_END() // TestKeyChain |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 536 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 537 | |
| 538 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 539 | } // inline namespace v2 |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 540 | } // namespace security |
| 541 | } // namespace ndn |