blob: b00744ff2e3edc7869b4b30dcbc4477000ec1f73 [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
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070029#include <boost/lexical_cast.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040030#include <boost/property_tree/info_parser.hpp>
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050031
Davide Pesaventob0716542024-12-16 19:12:11 -050032#include <filesystem>
33
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 Pesaventob0716542024-12-16 19:12:11 -050053 , ROOT_CERT_PATH(std::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
Saurab Dulal427e0122019-11-28 11:58:02 -060088 void
89 checkForInterest(ndn::Name& interstName)
90 {
91 std::vector<ndn::Interest>& interests = face.sentInterests;
92 BOOST_REQUIRE(interests.size() > 0);
93
94 bool didFindInterest = false;
95 for (const auto& interest : interests) {
96 didFindInterest = didFindInterest || interest.getName() == interstName;
97 }
98 BOOST_CHECK(didFindInterest);
99 }
100
Davide Pesaventob6adfe12024-07-05 13:03:15 -0400101protected:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000102 ndn::DummyClientFace face;
Saurab Dulal427e0122019-11-28 11:58:02 -0600103 ConfParameter conf;
104 DummyConfFileProcessor confProcessor;
105
106 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
107 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Junxiao Shib032fcb2022-04-28 01:28:50 +0000108 ndn::Name instanceCertName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600109
110 Nlsr nlsr;
111 Lsdb& lsdb;
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400112 ndn::security::Certificate certificate;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500113 ndn::Name certificateKey;
Saurab Dulal427e0122019-11-28 11:58:02 -0600114 security::CertificateStore certStore;
Davide Pesaventob0716542024-12-16 19:12:11 -0500115 const std::filesystem::path ROOT_CERT_PATH;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500116};
117
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400118BOOST_FIXTURE_TEST_SUITE(TestCertificateStore, CertificateStoreFixture)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500119
120BOOST_AUTO_TEST_CASE(Basic)
121{
Saurab Dulal427e0122019-11-28 11:58:02 -0600122 ndn::Name identityName("/TestNLSR/identity");
123 identityName.appendVersion();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500124
Saurab Dulal427e0122019-11-28 11:58:02 -0600125 auto identity = m_keyChain.createIdentity(identityName);
126 auto certificate = identity.getDefaultKey().getDefaultCertificate();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500127
Saurab Dulal427e0122019-11-28 11:58:02 -0600128 ndn::Name certKey = certificate.getKeyName();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500129
Saurab Dulal427e0122019-11-28 11:58:02 -0600130 BOOST_CHECK(certStore.find(certKey) == nullptr);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000131 BOOST_CHECK(certStore.find(certificate.getName()) == nullptr);
Saurab Dulal427e0122019-11-28 11:58:02 -0600132
133 // Certificate should be retrievable from the CertificateStore
134 certStore.insert(certificate);
135 conf.loadCertToValidator(certificate);
136
137 BOOST_CHECK(certStore.find(certKey) != nullptr);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000138 BOOST_CHECK(certStore.find(certificate.getName()) != nullptr);
Saurab Dulal427e0122019-11-28 11:58:02 -0600139
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400140 lsdb.expressInterest(certKey, 0, 0);
Saurab Dulal427e0122019-11-28 11:58:02 -0600141
142 advanceClocks(10_ms);
143 checkForInterest(certKey);
144}
145
Junxiao Shib032fcb2022-04-28 01:28:50 +0000146BOOST_AUTO_TEST_CASE(RetrieveCert)
147{
Junxiao Shi43f37a02023-08-09 00:09:00 +0000148 ndn::DummyClientFace consumer(m_io);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000149 consumer.linkTo(face);
150
151 auto checkRetrieve = [&] (const ndn::Name& interestName, bool canBePrefix, const ndn::Name& dataName) {
152 ndn::Interest interest(interestName);
153 interest.setCanBePrefix(canBePrefix);
154 BOOST_TEST_CONTEXT(interest) {
155 bool hasData = false;
156 consumer.expressInterest(interest,
157 [&] (const auto&, const auto& data) {
158 BOOST_CHECK(!hasData);
159 hasData = true;
160 BOOST_CHECK_EQUAL(data.getName(), dataName);
161 },
162 [&] (const auto&, const auto&) { BOOST_ERROR("unexpected Nack"); },
163 [&] (const auto&) { BOOST_ERROR("unexpected timeout"); }
164 );
165 advanceClocks(10_ms, 2);
166 BOOST_CHECK(hasData);
167 }
168 };
169
170 for (const auto& id : {siteIdentity, opIdentity, routerId}) {
171 auto key = id.getDefaultKey();
172 auto cert = key.getDefaultCertificate();
173 checkRetrieve(key.getName(), true, cert.getName());
174 checkRetrieve(cert.getName(), false, cert.getName());
175 }
176
177 checkRetrieve(ndn::security::extractKeyNameFromCertName(instanceCertName), true, instanceCertName);
178 checkRetrieve(instanceCertName, false, instanceCertName);
179}
180
Saurab Dulal427e0122019-11-28 11:58:02 -0600181BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration)
182{
183 // check if nlsrKeyPrefix is registered
184 ndn::Name nlsrKeyPrefix = conf.getRouterPrefix();
185 nlsrKeyPrefix.append("nlsr");
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700186 nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600187 checkPrefixRegistered(face, nlsrKeyPrefix);
188
189 // check if routerPrefix is registered
190 ndn::Name routerKeyPrefix = conf.getRouterPrefix();
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700191 routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600192 checkPrefixRegistered(face, routerKeyPrefix);
193
194 // check if operatorKeyPrefix is registered
195 ndn::Name operatorKeyPrefix = conf.getNetwork();
196 operatorKeyPrefix.append(conf.getSiteName());
197 operatorKeyPrefix.append(std::string("%C1.Operator"));
198 checkPrefixRegistered(face, operatorKeyPrefix);
199}
200
201BOOST_AUTO_TEST_CASE(SegmentValidatedSignal)
202{
203 ndn::Name lsaInterestName("/localhop");
204 lsaInterestName.append(conf.getLsaPrefix().getSubName(1));
205 lsaInterestName.append(conf.getSiteName());
206 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800207 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Saurab Dulal427e0122019-11-28 11:58:02 -0600208 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq() + 1);
209
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400210 lsdb.expressInterest(lsaInterestName, 0, 0);
Saurab Dulal427e0122019-11-28 11:58:02 -0600211 advanceClocks(10_ms);
212
213 checkForInterest(lsaInterestName);
214
215 ndn::Name lsaDataName(lsaInterestName);
216 lsaDataName.appendVersion();
217 lsaDataName.appendSegment(0);
218
219 ndn::Data data(lsaDataName);
Davide Pesavento288141a2024-02-13 17:30:35 -0500220 data.setFreshnessPeriod(10_s);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800221 NameLsa nameLsa;
222 data.setContent(nameLsa.wireEncode());
Saurab Dulal427e0122019-11-28 11:58:02 -0600223 data.setFinalBlock(lsaDataName[-1]);
224
Davide Pesaventob6adfe12024-07-05 13:03:15 -0400225 // Test with unsigned data first (lacks KeyLocator).
226 // This should not happen during normal operations, but CertificateStore
227 // should still be able to handle invalid packets without crashing
228 lsdb.emitSegmentValidatedSignal(data);
Saurab Dulal427e0122019-11-28 11:58:02 -0600229
Davide Pesaventob6adfe12024-07-05 13:03:15 -0400230 // Sign data with this NLSR's key (in reality it would be a different NLSR)
231 m_keyChain.sign(data, conf.m_signingInfo);
Saurab Dulal427e0122019-11-28 11:58:02 -0600232
233 // Make NLSR validate data signed by its own key
234 conf.getValidator().validate(data,
Davide Pesaventob6adfe12024-07-05 13:03:15 -0400235 [] (const auto&) { BOOST_CHECK(true); },
236 [] (const auto&, const auto& err) { BOOST_ERROR(err); });
Saurab Dulal427e0122019-11-28 11:58:02 -0600237
238 lsdb.emitSegmentValidatedSignal(data);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000239 auto certName = data.getSignatureInfo().getKeyLocator().getName();
240 auto keyName = ndn::security::extractKeyNameFromCertName(certName);
Saurab Dulal427e0122019-11-28 11:58:02 -0600241 BOOST_CHECK(certStore.find(keyName) != nullptr);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500242}
243
244BOOST_AUTO_TEST_SUITE_END()
245
Davide Pesavento288141a2024-02-13 17:30:35 -0500246} // namespace nlsr::tests