blob: 216aba5986f430da4ae787ce7ca735f189bce808 [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/*
Junxiao Shib032fcb2022-04-28 01:28:50 +00003 * Copyright (c) 2014-2022, 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"
23
Saurab Dulal427e0122019-11-28 11:58:02 -060024#include "tests/test-common.hpp"
25#include "nlsr.hpp"
26#include "lsdb.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050027
28#include <ndn-cxx/security/key-chain.hpp>
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
32namespace nlsr {
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050033namespace test {
34
35using std::shared_ptr;
36
Saurab Dulal427e0122019-11-28 11:58:02 -060037class CertificateStoreFixture : public UnitTestTimeFixture
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050038{
39public:
40 CertificateStoreFixture()
Saurab Dulal427e0122019-11-28 11:58:02 -060041 : 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 Lehmanc2acdcb2015-04-29 11:14:35 -050056 {
Saurab Dulal427e0122019-11-28 11:58:02 -060057 rootId = addIdentity(rootIdName);
58 siteIdentity = addSubCertificate(siteIdentityName, rootId);
59 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
60 routerId = addSubCertificate(routerIdName, opIdentity);
61
Junxiao Shib032fcb2022-04-28 01:28:50 +000062 auto instanceCert = conf.initializeKey();
63 BOOST_REQUIRE(!!instanceCert);
64 certStore.insert(*instanceCert);
65 instanceCertName = instanceCert->getName();
Saurab Dulal427e0122019-11-28 11:58:02 -060066
67 // Create certificate and load it to the validator
68 // previously this was done by in nlsr ctor
Junxiao Shib032fcb2022-04-28 01:28:50 +000069 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 Dulal427e0122019-11-28 11:58:02 -060074
75 boost::property_tree::ptree pt;
Junxiao Shib032fcb2022-04-28 01:28:50 +000076 boost::property_tree::read_info("nlsr.conf", pt);
Saurab Dulal427e0122019-11-28 11:58:02 -060077
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 Dulal427e0122019-11-28 11:58:02 -060086
Junxiao Shib032fcb2022-04-28 01:28:50 +000087 advanceClocks(20_ms);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050088 }
89
90public:
Saurab Dulal427e0122019-11-28 11:58:02 -060091 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 Shib032fcb2022-04-28 01:28:50 +0000111 ndn::Name instanceCertName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600112
113 Nlsr nlsr;
114 Lsdb& lsdb;
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400115 ndn::security::Certificate certificate;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500116 ndn::Name certificateKey;
Saurab Dulal427e0122019-11-28 11:58:02 -0600117 security::CertificateStore certStore;
118 const boost::filesystem::path ROOT_CERT_PATH;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500119};
120
121BOOST_FIXTURE_TEST_SUITE(TestSecurityCertificateStore, CertificateStoreFixture)
122
123BOOST_AUTO_TEST_CASE(Basic)
124{
Saurab Dulal427e0122019-11-28 11:58:02 -0600125 ndn::Name identityName("/TestNLSR/identity");
126 identityName.appendVersion();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500127
Saurab Dulal427e0122019-11-28 11:58:02 -0600128 auto identity = m_keyChain.createIdentity(identityName);
129 auto certificate = identity.getDefaultKey().getDefaultCertificate();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500130
Saurab Dulal427e0122019-11-28 11:58:02 -0600131 ndn::Name certKey = certificate.getKeyName();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500132
Saurab Dulal427e0122019-11-28 11:58:02 -0600133 BOOST_CHECK(certStore.find(certKey) == nullptr);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000134 BOOST_CHECK(certStore.find(certificate.getName()) == nullptr);
Saurab Dulal427e0122019-11-28 11:58:02 -0600135
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 Shib032fcb2022-04-28 01:28:50 +0000141 BOOST_CHECK(certStore.find(certificate.getName()) != nullptr);
Saurab Dulal427e0122019-11-28 11:58:02 -0600142
143 lsdb.expressInterest(certKey, 0);
144
145 advanceClocks(10_ms);
146 checkForInterest(certKey);
147}
148
Junxiao Shib032fcb2022-04-28 01:28:50 +0000149BOOST_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 Dulal427e0122019-11-28 11:58:02 -0600184BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration)
185{
186 // check if nlsrKeyPrefix is registered
187 ndn::Name nlsrKeyPrefix = conf.getRouterPrefix();
188 nlsrKeyPrefix.append("nlsr");
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700189 nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600190 checkPrefixRegistered(face, nlsrKeyPrefix);
191
192 // check if routerPrefix is registered
193 ndn::Name routerKeyPrefix = conf.getRouterPrefix();
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700194 routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600195 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
204BOOST_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 Gawande0db4d4d2020-02-05 20:30:02 -0800210 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Saurab Dulal427e0122019-11-28 11:58:02 -0600211 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 Gawande0db4d4d2020-02-05 20:30:02 -0800224 NameLsa nameLsa;
225 data.setContent(nameLsa.wireEncode());
Saurab Dulal427e0122019-11-28 11:58:02 -0600226 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 Gawande0db4d4d2020-02-05 20:30:02 -0800236 [] (const ndn::Data&) { BOOST_CHECK(true); },
Junxiao Shib032fcb2022-04-28 01:28:50 +0000237 [] (const ndn::Data&, const ndn::security::ValidationError& e) {
238 BOOST_ERROR(e);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800239 });
Saurab Dulal427e0122019-11-28 11:58:02 -0600240
241 lsdb.emitSegmentValidatedSignal(data);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000242 auto certName = data.getSignatureInfo().getKeyLocator().getName();
243 auto keyName = ndn::security::extractKeyNameFromCertName(certName);
Saurab Dulal427e0122019-11-28 11:58:02 -0600244 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 Lehmanc2acdcb2015-04-29 11:14:35 -0500251}
252
253BOOST_AUTO_TEST_SUITE_END()
254
255} // namespace test
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500256} // namespace nlsr