Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e0ad580 | 2023-02-20 19:42:52 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, The University of Memphis, |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 20 | */ |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 21 | |
| 22 | #include "security/certificate-store.hpp" |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 23 | #include "nlsr.hpp" |
| 24 | #include "lsdb.hpp" |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 25 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 26 | #include "tests/io-key-chain-fixture.hpp" |
| 27 | #include "tests/test-common.hpp" |
| 28 | |
| 29 | #include <boost/filesystem/operations.hpp> |
| 30 | #include <boost/filesystem/path.hpp> |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 31 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 32 | #include <boost/property_tree/info_parser.hpp> |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 33 | |
| 34 | namespace nlsr { |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 35 | namespace test { |
| 36 | |
| 37 | using std::shared_ptr; |
| 38 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 39 | class CertificateStoreFixture : public IoKeyChainFixture |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | CertificateStoreFixture() |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 43 | : face(m_io, m_keyChain, {true, true}) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 44 | , conf(face, m_keyChain, "unit-test-nlsr.conf") |
Davide Pesavento | 1954a0c | 2022-09-30 15:56:04 -0400 | [diff] [blame] | 45 | , confProcessor(conf, SyncProtocol::PSYNC, HYPERBOLIC_STATE_OFF, |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 46 | "/ndn/", "/site", "/%C1.Router/router1") |
| 47 | , rootIdName(conf.getNetwork()) |
| 48 | , siteIdentityName(ndn::Name(conf.getNetwork()).append(conf.getSiteName())) |
| 49 | , opIdentityName(ndn::Name(conf.getNetwork()) |
| 50 | .append(ndn::Name(conf.getSiteName())) |
| 51 | .append(ndn::Name("%C1.Operator"))) |
| 52 | , routerIdName(conf.getRouterPrefix()) |
| 53 | , nlsr(face, m_keyChain, conf) |
| 54 | , lsdb(nlsr.getLsdb()) |
| 55 | , certStore(face, conf, lsdb) |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 56 | , ROOT_CERT_PATH(boost::filesystem::current_path() / "root.cert") |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 57 | { |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 58 | rootId = m_keyChain.createIdentity(rootIdName); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 59 | siteIdentity = addSubCertificate(siteIdentityName, rootId); |
| 60 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
| 61 | routerId = addSubCertificate(routerIdName, opIdentity); |
| 62 | |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 63 | auto instanceCert = conf.initializeKey(); |
| 64 | BOOST_REQUIRE(!!instanceCert); |
| 65 | certStore.insert(*instanceCert); |
| 66 | instanceCertName = instanceCert->getName(); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 67 | |
| 68 | // Create certificate and load it to the validator |
| 69 | // previously this was done by in nlsr ctor |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 70 | for (const auto& id : {rootId, siteIdentity, opIdentity, routerId}) { |
Davide Pesavento | e0ad580 | 2023-02-20 19:42:52 -0500 | [diff] [blame] | 71 | auto cert = id.getDefaultKey().getDefaultCertificate(); |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 72 | conf.loadCertToValidator(cert); |
| 73 | certStore.insert(cert); |
| 74 | } |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 75 | |
| 76 | boost::property_tree::ptree pt; |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 77 | boost::property_tree::read_info("nlsr.conf", pt); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 78 | |
| 79 | // Load security section and file name |
| 80 | for (const auto& tn : pt) { |
| 81 | if (tn.first == "security") { |
| 82 | auto it = tn.second.begin(); |
| 83 | conf.getValidator().load(it->second, std::string("nlsr.conf")); |
| 84 | break; |
| 85 | } |
| 86 | } |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 87 | |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 88 | advanceClocks(20_ms); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public: |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 92 | void |
| 93 | checkForInterest(ndn::Name& interstName) |
| 94 | { |
| 95 | std::vector<ndn::Interest>& interests = face.sentInterests; |
| 96 | BOOST_REQUIRE(interests.size() > 0); |
| 97 | |
| 98 | bool didFindInterest = false; |
| 99 | for (const auto& interest : interests) { |
| 100 | didFindInterest = didFindInterest || interest.getName() == interstName; |
| 101 | } |
| 102 | BOOST_CHECK(didFindInterest); |
| 103 | } |
| 104 | |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 105 | ndn::DummyClientFace face; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 106 | |
| 107 | ConfParameter conf; |
| 108 | DummyConfFileProcessor confProcessor; |
| 109 | |
| 110 | ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName; |
| 111 | ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId; |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 112 | ndn::Name instanceCertName; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 113 | |
| 114 | Nlsr nlsr; |
| 115 | Lsdb& lsdb; |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 116 | ndn::security::Certificate certificate; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 117 | ndn::Name certificateKey; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 118 | security::CertificateStore certStore; |
| 119 | const boost::filesystem::path ROOT_CERT_PATH; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 120 | }; |
| 121 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 122 | BOOST_FIXTURE_TEST_SUITE(TestCertificateStore, CertificateStoreFixture) |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 123 | |
| 124 | BOOST_AUTO_TEST_CASE(Basic) |
| 125 | { |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 126 | ndn::Name identityName("/TestNLSR/identity"); |
| 127 | identityName.appendVersion(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 128 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 129 | auto identity = m_keyChain.createIdentity(identityName); |
| 130 | auto certificate = identity.getDefaultKey().getDefaultCertificate(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 131 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 132 | ndn::Name certKey = certificate.getKeyName(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 133 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 134 | BOOST_CHECK(certStore.find(certKey) == nullptr); |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 135 | BOOST_CHECK(certStore.find(certificate.getName()) == nullptr); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 136 | |
| 137 | // Certificate should be retrievable from the CertificateStore |
| 138 | certStore.insert(certificate); |
| 139 | conf.loadCertToValidator(certificate); |
| 140 | |
| 141 | BOOST_CHECK(certStore.find(certKey) != nullptr); |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 142 | BOOST_CHECK(certStore.find(certificate.getName()) != nullptr); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 143 | |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 144 | lsdb.expressInterest(certKey, 0, 0); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 145 | |
| 146 | advanceClocks(10_ms); |
| 147 | checkForInterest(certKey); |
| 148 | } |
| 149 | |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(RetrieveCert) |
| 151 | { |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 152 | ndn::DummyClientFace consumer(m_io); |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 153 | consumer.linkTo(face); |
| 154 | |
| 155 | auto checkRetrieve = [&] (const ndn::Name& interestName, bool canBePrefix, const ndn::Name& dataName) { |
| 156 | ndn::Interest interest(interestName); |
| 157 | interest.setCanBePrefix(canBePrefix); |
| 158 | BOOST_TEST_CONTEXT(interest) { |
| 159 | bool hasData = false; |
| 160 | consumer.expressInterest(interest, |
| 161 | [&] (const auto&, const auto& data) { |
| 162 | BOOST_CHECK(!hasData); |
| 163 | hasData = true; |
| 164 | BOOST_CHECK_EQUAL(data.getName(), dataName); |
| 165 | }, |
| 166 | [&] (const auto&, const auto&) { BOOST_ERROR("unexpected Nack"); }, |
| 167 | [&] (const auto&) { BOOST_ERROR("unexpected timeout"); } |
| 168 | ); |
| 169 | advanceClocks(10_ms, 2); |
| 170 | BOOST_CHECK(hasData); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | for (const auto& id : {siteIdentity, opIdentity, routerId}) { |
| 175 | auto key = id.getDefaultKey(); |
| 176 | auto cert = key.getDefaultCertificate(); |
| 177 | checkRetrieve(key.getName(), true, cert.getName()); |
| 178 | checkRetrieve(cert.getName(), false, cert.getName()); |
| 179 | } |
| 180 | |
| 181 | checkRetrieve(ndn::security::extractKeyNameFromCertName(instanceCertName), true, instanceCertName); |
| 182 | checkRetrieve(instanceCertName, false, instanceCertName); |
| 183 | } |
| 184 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 185 | BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration) |
| 186 | { |
| 187 | // check if nlsrKeyPrefix is registered |
| 188 | ndn::Name nlsrKeyPrefix = conf.getRouterPrefix(); |
| 189 | nlsrKeyPrefix.append("nlsr"); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 190 | nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 191 | checkPrefixRegistered(face, nlsrKeyPrefix); |
| 192 | |
| 193 | // check if routerPrefix is registered |
| 194 | ndn::Name routerKeyPrefix = conf.getRouterPrefix(); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 195 | routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 196 | checkPrefixRegistered(face, routerKeyPrefix); |
| 197 | |
| 198 | // check if operatorKeyPrefix is registered |
| 199 | ndn::Name operatorKeyPrefix = conf.getNetwork(); |
| 200 | operatorKeyPrefix.append(conf.getSiteName()); |
| 201 | operatorKeyPrefix.append(std::string("%C1.Operator")); |
| 202 | checkPrefixRegistered(face, operatorKeyPrefix); |
| 203 | } |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE(SegmentValidatedSignal) |
| 206 | { |
| 207 | ndn::Name lsaInterestName("/localhop"); |
| 208 | lsaInterestName.append(conf.getLsaPrefix().getSubName(1)); |
| 209 | lsaInterestName.append(conf.getSiteName()); |
| 210 | lsaInterestName.append(conf.getRouterName()); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 211 | lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME)); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 212 | lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq() + 1); |
| 213 | |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 214 | lsdb.expressInterest(lsaInterestName, 0, 0); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 215 | advanceClocks(10_ms); |
| 216 | |
| 217 | checkForInterest(lsaInterestName); |
| 218 | |
| 219 | ndn::Name lsaDataName(lsaInterestName); |
| 220 | lsaDataName.appendVersion(); |
| 221 | lsaDataName.appendSegment(0); |
| 222 | |
| 223 | ndn::Data data(lsaDataName); |
| 224 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 225 | NameLsa nameLsa; |
| 226 | data.setContent(nameLsa.wireEncode()); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 227 | data.setFinalBlock(lsaDataName[-1]); |
| 228 | |
| 229 | // Sign data with this NLSR's key (in real it would be different NLSR) |
| 230 | m_keyChain.sign(data, conf.m_signingInfo); |
| 231 | face.put(data); |
| 232 | |
| 233 | this->advanceClocks(ndn::time::milliseconds(1)); |
| 234 | |
| 235 | // Make NLSR validate data signed by its own key |
| 236 | conf.getValidator().validate(data, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 237 | [] (const ndn::Data&) { BOOST_CHECK(true); }, |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 238 | [] (const ndn::Data&, const ndn::security::ValidationError& e) { |
| 239 | BOOST_ERROR(e); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 240 | }); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 241 | |
| 242 | lsdb.emitSegmentValidatedSignal(data); |
Junxiao Shi | b032fcb | 2022-04-28 01:28:50 +0000 | [diff] [blame] | 243 | auto certName = data.getSignatureInfo().getKeyLocator().getName(); |
| 244 | auto keyName = ndn::security::extractKeyNameFromCertName(certName); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 245 | BOOST_CHECK(certStore.find(keyName) != nullptr); |
| 246 | |
| 247 | // testing a callback after segment validation signal from lsdb |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 248 | ndn::signal::ScopedConnection connection = lsdb.afterSegmentValidatedSignal.connect( |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 249 | [&] (const ndn::Data& lsaSegment) { |
| 250 | BOOST_CHECK_EQUAL(lsaSegment.getName(), data.getName()); |
| 251 | }); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | BOOST_AUTO_TEST_SUITE_END() |
| 255 | |
| 256 | } // namespace test |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 257 | } // namespace nlsr |