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