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