blob: 03e086a1f20a41b793c63a6d4a3797b65d76dec9 [file] [log] [blame]
Vince Lehmanc2acdcb2015-04-29 11:14:35 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -04002/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, The University of Memphis,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -05004 * 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 Afanasyev0ad01f32020-06-03 14:12:58 -040020 */
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050021
22#include "security/certificate-store.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060023#include "nlsr.hpp"
24#include "lsdb.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050025
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040026#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 Gawande30d96e42021-03-21 19:15:33 -070031#include <boost/lexical_cast.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040032#include <boost/property_tree/info_parser.hpp>
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050033
Davide Pesavento288141a2024-02-13 17:30:35 -050034namespace nlsr::tests {
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050035
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040036class CertificateStoreFixture : public IoKeyChainFixture
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050037{
38public:
39 CertificateStoreFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040040 : face(m_io, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060041 , conf(face, m_keyChain, "unit-test-nlsr.conf")
Davide Pesavento1954a0c2022-09-30 15:56:04 -040042 , confProcessor(conf, SyncProtocol::PSYNC, HYPERBOLIC_STATE_OFF,
Saurab Dulal427e0122019-11-28 11:58:02 -060043 "/ndn/", "/site", "/%C1.Router/router1")
44 , rootIdName(conf.getNetwork())
45 , siteIdentityName(ndn::Name(conf.getNetwork()).append(conf.getSiteName()))
46 , opIdentityName(ndn::Name(conf.getNetwork())
47 .append(ndn::Name(conf.getSiteName()))
48 .append(ndn::Name("%C1.Operator")))
49 , routerIdName(conf.getRouterPrefix())
50 , nlsr(face, m_keyChain, conf)
51 , lsdb(nlsr.getLsdb())
52 , certStore(face, conf, lsdb)
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040053 , ROOT_CERT_PATH(boost::filesystem::current_path() / "root.cert")
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050054 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040055 rootId = m_keyChain.createIdentity(rootIdName);
Saurab Dulal427e0122019-11-28 11:58:02 -060056 siteIdentity = addSubCertificate(siteIdentityName, rootId);
57 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
58 routerId = addSubCertificate(routerIdName, opIdentity);
59
Junxiao Shib032fcb2022-04-28 01:28:50 +000060 auto instanceCert = conf.initializeKey();
61 BOOST_REQUIRE(!!instanceCert);
62 certStore.insert(*instanceCert);
63 instanceCertName = instanceCert->getName();
Saurab Dulal427e0122019-11-28 11:58:02 -060064
65 // Create certificate and load it to the validator
66 // previously this was done by in nlsr ctor
Junxiao Shib032fcb2022-04-28 01:28:50 +000067 for (const auto& id : {rootId, siteIdentity, opIdentity, routerId}) {
Davide Pesaventoe0ad5802023-02-20 19:42:52 -050068 auto cert = id.getDefaultKey().getDefaultCertificate();
Junxiao Shib032fcb2022-04-28 01:28:50 +000069 conf.loadCertToValidator(cert);
70 certStore.insert(cert);
71 }
Saurab Dulal427e0122019-11-28 11:58:02 -060072
73 boost::property_tree::ptree pt;
Junxiao Shib032fcb2022-04-28 01:28:50 +000074 boost::property_tree::read_info("nlsr.conf", pt);
Saurab Dulal427e0122019-11-28 11:58:02 -060075
76 // Load security section and file name
77 for (const auto& tn : pt) {
78 if (tn.first == "security") {
79 auto it = tn.second.begin();
80 conf.getValidator().load(it->second, std::string("nlsr.conf"));
81 break;
82 }
83 }
Saurab Dulal427e0122019-11-28 11:58:02 -060084
Junxiao Shib032fcb2022-04-28 01:28:50 +000085 advanceClocks(20_ms);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050086 }
87
88public:
Saurab Dulal427e0122019-11-28 11:58:02 -060089 void
90 checkForInterest(ndn::Name& interstName)
91 {
92 std::vector<ndn::Interest>& interests = face.sentInterests;
93 BOOST_REQUIRE(interests.size() > 0);
94
95 bool didFindInterest = false;
96 for (const auto& interest : interests) {
97 didFindInterest = didFindInterest || interest.getName() == interstName;
98 }
99 BOOST_CHECK(didFindInterest);
100 }
101
Junxiao Shi43f37a02023-08-09 00:09:00 +0000102 ndn::DummyClientFace face;
Saurab Dulal427e0122019-11-28 11:58:02 -0600103
104 ConfParameter conf;
105 DummyConfFileProcessor confProcessor;
106
107 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
108 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Junxiao Shib032fcb2022-04-28 01:28:50 +0000109 ndn::Name instanceCertName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600110
111 Nlsr nlsr;
112 Lsdb& lsdb;
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400113 ndn::security::Certificate certificate;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500114 ndn::Name certificateKey;
Saurab Dulal427e0122019-11-28 11:58:02 -0600115 security::CertificateStore certStore;
116 const boost::filesystem::path ROOT_CERT_PATH;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500117};
118
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400119BOOST_FIXTURE_TEST_SUITE(TestCertificateStore, CertificateStoreFixture)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500120
121BOOST_AUTO_TEST_CASE(Basic)
122{
Saurab Dulal427e0122019-11-28 11:58:02 -0600123 ndn::Name identityName("/TestNLSR/identity");
124 identityName.appendVersion();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500125
Saurab Dulal427e0122019-11-28 11:58:02 -0600126 auto identity = m_keyChain.createIdentity(identityName);
127 auto certificate = identity.getDefaultKey().getDefaultCertificate();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500128
Saurab Dulal427e0122019-11-28 11:58:02 -0600129 ndn::Name certKey = certificate.getKeyName();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500130
Saurab Dulal427e0122019-11-28 11:58:02 -0600131 BOOST_CHECK(certStore.find(certKey) == nullptr);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000132 BOOST_CHECK(certStore.find(certificate.getName()) == nullptr);
Saurab Dulal427e0122019-11-28 11:58:02 -0600133
134 // Certificate should be retrievable from the CertificateStore
135 certStore.insert(certificate);
136 conf.loadCertToValidator(certificate);
137
138 BOOST_CHECK(certStore.find(certKey) != nullptr);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000139 BOOST_CHECK(certStore.find(certificate.getName()) != nullptr);
Saurab Dulal427e0122019-11-28 11:58:02 -0600140
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400141 lsdb.expressInterest(certKey, 0, 0);
Saurab Dulal427e0122019-11-28 11:58:02 -0600142
143 advanceClocks(10_ms);
144 checkForInterest(certKey);
145}
146
Junxiao Shib032fcb2022-04-28 01:28:50 +0000147BOOST_AUTO_TEST_CASE(RetrieveCert)
148{
Junxiao Shi43f37a02023-08-09 00:09:00 +0000149 ndn::DummyClientFace consumer(m_io);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000150 consumer.linkTo(face);
151
152 auto checkRetrieve = [&] (const ndn::Name& interestName, bool canBePrefix, const ndn::Name& dataName) {
153 ndn::Interest interest(interestName);
154 interest.setCanBePrefix(canBePrefix);
155 BOOST_TEST_CONTEXT(interest) {
156 bool hasData = false;
157 consumer.expressInterest(interest,
158 [&] (const auto&, const auto& data) {
159 BOOST_CHECK(!hasData);
160 hasData = true;
161 BOOST_CHECK_EQUAL(data.getName(), dataName);
162 },
163 [&] (const auto&, const auto&) { BOOST_ERROR("unexpected Nack"); },
164 [&] (const auto&) { BOOST_ERROR("unexpected timeout"); }
165 );
166 advanceClocks(10_ms, 2);
167 BOOST_CHECK(hasData);
168 }
169 };
170
171 for (const auto& id : {siteIdentity, opIdentity, routerId}) {
172 auto key = id.getDefaultKey();
173 auto cert = key.getDefaultCertificate();
174 checkRetrieve(key.getName(), true, cert.getName());
175 checkRetrieve(cert.getName(), false, cert.getName());
176 }
177
178 checkRetrieve(ndn::security::extractKeyNameFromCertName(instanceCertName), true, instanceCertName);
179 checkRetrieve(instanceCertName, false, instanceCertName);
180}
181
Saurab Dulal427e0122019-11-28 11:58:02 -0600182BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration)
183{
184 // check if nlsrKeyPrefix is registered
185 ndn::Name nlsrKeyPrefix = conf.getRouterPrefix();
186 nlsrKeyPrefix.append("nlsr");
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700187 nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600188 checkPrefixRegistered(face, nlsrKeyPrefix);
189
190 // check if routerPrefix is registered
191 ndn::Name routerKeyPrefix = conf.getRouterPrefix();
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700192 routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600193 checkPrefixRegistered(face, routerKeyPrefix);
194
195 // check if operatorKeyPrefix is registered
196 ndn::Name operatorKeyPrefix = conf.getNetwork();
197 operatorKeyPrefix.append(conf.getSiteName());
198 operatorKeyPrefix.append(std::string("%C1.Operator"));
199 checkPrefixRegistered(face, operatorKeyPrefix);
200}
201
202BOOST_AUTO_TEST_CASE(SegmentValidatedSignal)
203{
204 ndn::Name lsaInterestName("/localhop");
205 lsaInterestName.append(conf.getLsaPrefix().getSubName(1));
206 lsaInterestName.append(conf.getSiteName());
207 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800208 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Saurab Dulal427e0122019-11-28 11:58:02 -0600209 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq() + 1);
210
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400211 lsdb.expressInterest(lsaInterestName, 0, 0);
Saurab Dulal427e0122019-11-28 11:58:02 -0600212 advanceClocks(10_ms);
213
214 checkForInterest(lsaInterestName);
215
216 ndn::Name lsaDataName(lsaInterestName);
217 lsaDataName.appendVersion();
218 lsaDataName.appendSegment(0);
219
220 ndn::Data data(lsaDataName);
Davide Pesavento288141a2024-02-13 17:30:35 -0500221 data.setFreshnessPeriod(10_s);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800222 NameLsa nameLsa;
223 data.setContent(nameLsa.wireEncode());
Saurab Dulal427e0122019-11-28 11:58:02 -0600224 data.setFinalBlock(lsaDataName[-1]);
225
226 // Sign data with this NLSR's key (in real it would be different NLSR)
227 m_keyChain.sign(data, conf.m_signingInfo);
228 face.put(data);
229
Davide Pesavento288141a2024-02-13 17:30:35 -0500230 this->advanceClocks(1_ms);
Saurab Dulal427e0122019-11-28 11:58:02 -0600231
232 // Make NLSR validate data signed by its own key
233 conf.getValidator().validate(data,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800234 [] (const ndn::Data&) { BOOST_CHECK(true); },
Junxiao Shib032fcb2022-04-28 01:28:50 +0000235 [] (const ndn::Data&, const ndn::security::ValidationError& e) {
236 BOOST_ERROR(e);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800237 });
Saurab Dulal427e0122019-11-28 11:58:02 -0600238
239 lsdb.emitSegmentValidatedSignal(data);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000240 auto certName = data.getSignatureInfo().getKeyLocator().getName();
241 auto keyName = ndn::security::extractKeyNameFromCertName(certName);
Saurab Dulal427e0122019-11-28 11:58:02 -0600242 BOOST_CHECK(certStore.find(keyName) != nullptr);
243
244 // testing a callback after segment validation signal from lsdb
Junxiao Shi43f37a02023-08-09 00:09:00 +0000245 ndn::signal::ScopedConnection connection = lsdb.afterSegmentValidatedSignal.connect(
Saurab Dulal427e0122019-11-28 11:58:02 -0600246 [&] (const ndn::Data& lsaSegment) {
247 BOOST_CHECK_EQUAL(lsaSegment.getName(), data.getName());
248 });
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500249}
250
251BOOST_AUTO_TEST_SUITE_END()
252
Davide Pesavento288141a2024-02-13 17:30:35 -0500253} // namespace nlsr::tests