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 | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "security/v2/key-chain.hpp" |
| 23 | #include "security/signing-helpers.hpp" |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 24 | #include "security/verification-helpers.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 25 | |
| 26 | #include "boost-test.hpp" |
| 27 | #include "unit-tests/test-home-env-saver.hpp" |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 28 | #include "identity-management-fixture.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace v2 { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace ndn::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
| 38 | BOOST_AUTO_TEST_SUITE(V2) |
| 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 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 212 | BOOST_FIXTURE_TEST_CASE(Management, IdentityManagementFixture) |
| 213 | { |
| 214 | Name identityName("/test/id"); |
| 215 | Name identity2Name("/test/id2"); |
| 216 | |
| 217 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 218 | BOOST_REQUIRE_THROW(m_keyChain.getPib().getDefaultIdentity(), Pib::Error); |
| 219 | |
| 220 | // Create identity |
| 221 | Identity id = m_keyChain.createIdentity(identityName); |
| 222 | BOOST_CHECK(id); |
| 223 | BOOST_CHECK(m_keyChain.getPib().getIdentities().find(identityName) != m_keyChain.getPib().getIdentities().end()); |
| 224 | // The first added identity becomes the default identity |
| 225 | BOOST_REQUIRE_NO_THROW(m_keyChain.getPib().getDefaultIdentity()); |
| 226 | // The default key of the added identity must exist |
| 227 | Key key; |
| 228 | BOOST_REQUIRE_NO_THROW(key = id.getDefaultKey()); |
| 229 | // The default certificate of the default key must exist |
| 230 | BOOST_REQUIRE_NO_THROW(key.getDefaultCertificate()); |
| 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. |
| 245 | BOOST_REQUIRE_NO_THROW(id.getDefaultKey()); |
| 246 | Key key2 = id.getDefaultKey(); |
| 247 | BOOST_REQUIRE(key2); |
| 248 | BOOST_CHECK_NE(key2.getName(), key1Name); |
| 249 | BOOST_CHECK_EQUAL(id.getKeys().size(), 1); |
| 250 | BOOST_REQUIRE_NO_THROW(key2.getDefaultCertificate()); |
| 251 | |
| 252 | // Create the third key |
| 253 | Key key3 = m_keyChain.createKey(id); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 254 | BOOST_CHECK_NE(key3.getName(), key2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 255 | // 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] | 256 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(id.getKeys().size(), 2); |
| 258 | BOOST_REQUIRE_NO_THROW(key3.getDefaultCertificate()); |
| 259 | |
| 260 | // Delete cert |
| 261 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
| 262 | Certificate key3Cert1 = *key3.getCertificates().begin(); |
| 263 | Name key3CertName = key3Cert1.getName(); |
| 264 | m_keyChain.deleteCertificate(key3, key3CertName); |
| 265 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 0); |
| 266 | BOOST_REQUIRE_THROW(key3.getDefaultCertificate(), Pib::Error); |
| 267 | |
| 268 | // Add cert |
| 269 | m_keyChain.addCertificate(key3, key3Cert1); |
| 270 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
| 271 | BOOST_REQUIRE_NO_THROW(key3.getDefaultCertificate()); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 272 | m_keyChain.addCertificate(key3, key3Cert1); // overwriting the cert should work |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 273 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1); |
| 274 | // Add another cert |
| 275 | Certificate key3Cert2 = key3Cert1; |
| 276 | Name key3Cert2Name = key3.getName(); |
| 277 | key3Cert2Name.append("Self"); |
| 278 | key3Cert2Name.appendVersion(); |
| 279 | key3Cert2.setName(key3Cert2Name); |
| 280 | m_keyChain.addCertificate(key3, key3Cert2); |
| 281 | BOOST_CHECK_EQUAL(key3.getCertificates().size(), 2); |
| 282 | |
| 283 | // Default certificate setting |
| 284 | BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3CertName); |
| 285 | m_keyChain.setDefaultCertificate(key3, key3Cert2); |
| 286 | BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3Cert2Name); |
| 287 | |
| 288 | // Default key setting |
| 289 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName()); |
| 290 | m_keyChain.setDefaultKey(id, key3); |
| 291 | BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key3.getName()); |
| 292 | |
| 293 | // Default identity setting |
| 294 | Identity id2 = m_keyChain.createIdentity(identity2Name); |
| 295 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id.getName()); |
| 296 | m_keyChain.setDefaultIdentity(id2); |
| 297 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id2.getName()); |
| 298 | |
| 299 | // Delete identity |
| 300 | m_keyChain.deleteIdentity(id); |
| 301 | // The identity instance should not be valid any more |
| 302 | BOOST_CHECK(!id); |
| 303 | BOOST_REQUIRE_THROW(m_keyChain.getPib().getIdentity(identityName), Pib::Error); |
| 304 | BOOST_CHECK(m_keyChain.getPib().getIdentities().find(identityName) == m_keyChain.getPib().getIdentities().end()); |
| 305 | } |
| 306 | |
| 307 | BOOST_FIXTURE_TEST_CASE(GeneralSigningInterface, IdentityManagementFixture) |
| 308 | { |
| 309 | Identity id = addIdentity("/id"); |
| 310 | Key key = id.getDefaultKey(); |
| 311 | Certificate cert = key.getDefaultCertificate(); |
| 312 | |
| 313 | std::list<SigningInfo> signingInfos = { |
| 314 | SigningInfo(), |
| 315 | |
| 316 | SigningInfo(SigningInfo::SIGNER_TYPE_ID, id.getName()), |
| 317 | signingByIdentity(id.getName()), |
| 318 | |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 319 | SigningInfo(id), |
| 320 | signingByIdentity(id), |
| 321 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 322 | SigningInfo(SigningInfo::SIGNER_TYPE_KEY, key.getName()), |
| 323 | signingByKey(key.getName()), |
| 324 | |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 325 | SigningInfo(key), |
| 326 | signingByKey(key), |
| 327 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 328 | SigningInfo(SigningInfo::SIGNER_TYPE_CERT, cert.getName()), |
| 329 | signingByCertificate(cert.getName()), |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 330 | signingByCertificate(cert), |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 331 | |
| 332 | SigningInfo(SigningInfo::SIGNER_TYPE_SHA256), |
| 333 | signingWithSha256() |
| 334 | }; |
| 335 | |
| 336 | for (const auto& signingInfo : signingInfos) { |
| 337 | BOOST_TEST_MESSAGE("SigningInfo: " << signingInfo); |
| 338 | Data data("/data"); |
| 339 | Interest interest("/interest"); |
| 340 | |
| 341 | if (signingInfo.getSignerType() == SigningInfo::SIGNER_TYPE_NULL) { |
| 342 | m_keyChain.sign(data); |
| 343 | m_keyChain.sign(interest); |
| 344 | } |
| 345 | else { |
| 346 | m_keyChain.sign(data, signingInfo); |
| 347 | m_keyChain.sign(interest, signingInfo); |
| 348 | } |
| 349 | |
| 350 | Signature interestSignature(interest.getName()[-2].blockFromValue(), interest.getName()[-1].blockFromValue()); |
| 351 | |
| 352 | if (signingInfo.getSignerType() == SigningInfo::SIGNER_TYPE_SHA256) { |
| 353 | BOOST_CHECK_EQUAL(data.getSignature().getType(), tlv::DigestSha256); |
| 354 | BOOST_CHECK_EQUAL(interestSignature.getType(), tlv::DigestSha256); |
| 355 | |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 356 | BOOST_CHECK(verifyDigest(data, DigestAlgorithm::SHA256)); |
| 357 | BOOST_CHECK(verifyDigest(interest, DigestAlgorithm::SHA256)); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 358 | } |
| 359 | else { |
| 360 | BOOST_CHECK_EQUAL(data.getSignature().getType(), tlv::SignatureSha256WithEcdsa); |
| 361 | BOOST_CHECK_EQUAL(interestSignature.getType(), tlv::SignatureSha256WithEcdsa); |
| 362 | |
| 363 | BOOST_CHECK_EQUAL(data.getSignature().getKeyLocator().getName(), cert.getName().getPrefix(-2)); |
| 364 | BOOST_CHECK_EQUAL(interestSignature.getKeyLocator().getName(), cert.getName().getPrefix(-2)); |
| 365 | |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 366 | BOOST_CHECK(verifySignature(data, key)); |
| 367 | BOOST_CHECK(verifySignature(interest, key)); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | BOOST_FIXTURE_TEST_CASE(PublicKeySigningDefaults, IdentityManagementFixture) |
| 373 | { |
| 374 | Data data("/test/data"); |
| 375 | |
| 376 | // Identity will be created with generated key and self-signed cert with default parameters |
| 377 | BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity("/non-existing/identity")), KeyChain::InvalidSigningInfoError); |
| 378 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 379 | // Create identity with EC key and the corresponding self-signed certificate |
| 380 | Identity id = addIdentity("/ndn/test/ec", EcKeyParams()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 381 | BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id.getName()))); |
| 382 | BOOST_CHECK_EQUAL(data.getSignature().getType(), |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 383 | KeyChain::getSignatureType(EcKeyParams().getKeyType(), DigestAlgorithm::SHA256)); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 384 | BOOST_CHECK(id.getName().isPrefixOf(data.getSignature().getKeyLocator().getName())); |
| 385 | |
| 386 | // Create identity with RSA key and the corresponding self-signed certificate |
| 387 | id = addIdentity("/ndn/test/rsa", RsaKeyParams()); |
| 388 | BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id.getName()))); |
| 389 | BOOST_CHECK_EQUAL(data.getSignature().getType(), |
| 390 | KeyChain::getSignatureType(RsaKeyParams().getKeyType(), DigestAlgorithm::SHA256)); |
| 391 | BOOST_CHECK(id.getName().isPrefixOf(data.getSignature().getKeyLocator().getName())); |
| 392 | } |
| 393 | |
| 394 | BOOST_FIXTURE_TEST_CASE(ExportImport, IdentityManagementFixture) |
| 395 | { |
| 396 | Identity id = addIdentity("/TestKeyChain/ExportIdentity/"); |
| 397 | Certificate cert = id.getDefaultKey().getDefaultCertificate(); |
| 398 | |
| 399 | shared_ptr<SafeBag> exported = m_keyChain.exportSafeBag(cert, "1234", 4); |
| 400 | Block block = exported->wireEncode(); |
| 401 | |
| 402 | m_keyChain.deleteIdentity(id); |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 403 | BOOST_CHECK_THROW(m_keyChain.exportSafeBag(cert, "1234", 4), KeyChain::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 404 | |
| 405 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false); |
| 406 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 407 | |
| 408 | SafeBag imported; |
| 409 | imported.wireDecode(block); |
| 410 | m_keyChain.importSafeBag(imported, "1234", 4); |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 411 | BOOST_CHECK_THROW(m_keyChain.importSafeBag(imported, "1234", 4), KeyChain::Error); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 412 | |
| 413 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), true); |
| 414 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1); |
| 415 | BOOST_REQUIRE_NO_THROW(m_keyChain.getPib().getIdentity(cert.getIdentity())); |
| 416 | Identity newId = m_keyChain.getPib().getIdentity(cert.getIdentity()); |
| 417 | BOOST_CHECK_EQUAL(newId.getKeys().size(), 1); |
| 418 | BOOST_REQUIRE_NO_THROW(newId.getKey(cert.getKeyName())); |
| 419 | Key newKey = newId.getKey(cert.getKeyName()); |
| 420 | BOOST_CHECK_EQUAL(newKey.getCertificates().size(), 1); |
| 421 | BOOST_REQUIRE_NO_THROW(newKey.getCertificate(cert.getName())); |
| 422 | |
| 423 | m_keyChain.deleteIdentity(newId); |
| 424 | BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0); |
| 425 | BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false); |
| 426 | } |
| 427 | |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 428 | BOOST_FIXTURE_TEST_CASE(SelfSignedCertValidity, IdentityManagementFixture) |
| 429 | { |
| 430 | Certificate cert = addIdentity("/Security/V2/TestKeyChain/SelfSignedCertValidity") |
| 431 | .getDefaultKey() |
| 432 | .getDefaultCertificate(); |
| 433 | BOOST_CHECK(cert.isValid()); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 434 | BOOST_CHECK(cert.isValid(time::system_clock::now() + 10 * 365_days)); |
| 435 | 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] | 436 | } |
| 437 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 438 | BOOST_AUTO_TEST_SUITE_END() // TestKeyChain |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 439 | BOOST_AUTO_TEST_SUITE_END() // V2 |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 440 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 441 | |
| 442 | } // namespace tests |
| 443 | } // namespace v2 |
| 444 | } // namespace security |
| 445 | } // namespace ndn |