blob: ffb94d1bcbd21bacf5350805ca16d8ac69030d10 [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/*
Ashlesh Gawande30d96e42021-03-21 19:15:33 -07003 * Copyright (c) 2014-2021, 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
62 auto certificate = conf.initializeKey();
63 if (certificate) {
64 certStore.insert(*certificate);
65 };
66
67 // Create certificate and load it to the validator
68 // previously this was done by in nlsr ctor
69 conf.loadCertToValidator(rootId.getDefaultKey().getDefaultCertificate());
70 conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
71 conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
72 conf.loadCertToValidator(routerId.getDefaultKey().getDefaultCertificate());
73
74 std::ifstream inputFile;
75 inputFile.open(std::string("nlsr.conf"));
76
77 BOOST_REQUIRE(inputFile.is_open());
78
79 boost::property_tree::ptree pt;
80
81 boost::property_tree::read_info(inputFile, pt);
82
83 // Load security section and file name
84 for (const auto& tn : pt) {
85 if (tn.first == "security") {
86 auto it = tn.second.begin();
87 conf.getValidator().load(it->second, std::string("nlsr.conf"));
88 break;
89 }
90 }
91 inputFile.close();
92
93 this->advanceClocks(ndn::time::milliseconds(20));
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050094 }
95
96public:
Saurab Dulal427e0122019-11-28 11:58:02 -060097 void
98 checkForInterest(ndn::Name& interstName)
99 {
100 std::vector<ndn::Interest>& interests = face.sentInterests;
101 BOOST_REQUIRE(interests.size() > 0);
102
103 bool didFindInterest = false;
104 for (const auto& interest : interests) {
105 didFindInterest = didFindInterest || interest.getName() == interstName;
106 }
107 BOOST_CHECK(didFindInterest);
108 }
109
110 ndn::util::DummyClientFace face;
111
112 ConfParameter conf;
113 DummyConfFileProcessor confProcessor;
114
115 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
116 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
117
118 Nlsr nlsr;
119 Lsdb& lsdb;
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400120 ndn::security::Certificate certificate;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500121 ndn::Name certificateKey;
Saurab Dulal427e0122019-11-28 11:58:02 -0600122 security::CertificateStore certStore;
123 const boost::filesystem::path ROOT_CERT_PATH;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500124};
125
126BOOST_FIXTURE_TEST_SUITE(TestSecurityCertificateStore, CertificateStoreFixture)
127
128BOOST_AUTO_TEST_CASE(Basic)
129{
Saurab Dulal427e0122019-11-28 11:58:02 -0600130 ndn::Name identityName("/TestNLSR/identity");
131 identityName.appendVersion();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500132
Saurab Dulal427e0122019-11-28 11:58:02 -0600133 auto identity = m_keyChain.createIdentity(identityName);
134 auto certificate = identity.getDefaultKey().getDefaultCertificate();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500135
Saurab Dulal427e0122019-11-28 11:58:02 -0600136 ndn::Name certKey = certificate.getKeyName();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500137
Saurab Dulal427e0122019-11-28 11:58:02 -0600138 BOOST_CHECK(certStore.find(certKey) == nullptr);
139
140 // Certificate should be retrievable from the CertificateStore
141 certStore.insert(certificate);
142 conf.loadCertToValidator(certificate);
143
144 BOOST_CHECK(certStore.find(certKey) != nullptr);
145
146 lsdb.expressInterest(certKey, 0);
147
148 advanceClocks(10_ms);
149 checkForInterest(certKey);
150}
151
152BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration)
153{
154 // check if nlsrKeyPrefix is registered
155 ndn::Name nlsrKeyPrefix = conf.getRouterPrefix();
156 nlsrKeyPrefix.append("nlsr");
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700157 nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600158 checkPrefixRegistered(face, nlsrKeyPrefix);
159
160 // check if routerPrefix is registered
161 ndn::Name routerKeyPrefix = conf.getRouterPrefix();
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700162 routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600163 checkPrefixRegistered(face, routerKeyPrefix);
164
165 // check if operatorKeyPrefix is registered
166 ndn::Name operatorKeyPrefix = conf.getNetwork();
167 operatorKeyPrefix.append(conf.getSiteName());
168 operatorKeyPrefix.append(std::string("%C1.Operator"));
169 checkPrefixRegistered(face, operatorKeyPrefix);
170}
171
172BOOST_AUTO_TEST_CASE(SegmentValidatedSignal)
173{
174 ndn::Name lsaInterestName("/localhop");
175 lsaInterestName.append(conf.getLsaPrefix().getSubName(1));
176 lsaInterestName.append(conf.getSiteName());
177 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800178 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Saurab Dulal427e0122019-11-28 11:58:02 -0600179 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq() + 1);
180
181 lsdb.expressInterest(lsaInterestName, 0);
182 advanceClocks(10_ms);
183
184 checkForInterest(lsaInterestName);
185
186 ndn::Name lsaDataName(lsaInterestName);
187 lsaDataName.appendVersion();
188 lsaDataName.appendSegment(0);
189
190 ndn::Data data(lsaDataName);
191 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800192 NameLsa nameLsa;
193 data.setContent(nameLsa.wireEncode());
Saurab Dulal427e0122019-11-28 11:58:02 -0600194 data.setFinalBlock(lsaDataName[-1]);
195
196 // Sign data with this NLSR's key (in real it would be different NLSR)
197 m_keyChain.sign(data, conf.m_signingInfo);
198 face.put(data);
199
200 this->advanceClocks(ndn::time::milliseconds(1));
201
202 // Make NLSR validate data signed by its own key
203 conf.getValidator().validate(data,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800204 [] (const ndn::Data&) { BOOST_CHECK(true); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400205 [] (const ndn::Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800206 BOOST_CHECK(false);
207 });
Saurab Dulal427e0122019-11-28 11:58:02 -0600208
209 lsdb.emitSegmentValidatedSignal(data);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700210 const auto keyName = data.getSignatureInfo().getKeyLocator().getName();
Saurab Dulal427e0122019-11-28 11:58:02 -0600211 BOOST_CHECK(certStore.find(keyName) != nullptr);
212
213 // testing a callback after segment validation signal from lsdb
214 ndn::util::signal::ScopedConnection connection = lsdb.afterSegmentValidatedSignal.connect(
215 [&] (const ndn::Data& lsaSegment) {
216 BOOST_CHECK_EQUAL(lsaSegment.getName(), data.getName());
217 });
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500218}
219
220BOOST_AUTO_TEST_SUITE_END()
221
222} // namespace test
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500223} // namespace nlsr