blob: 00d5d623a09d53eb605d961f1526dfd78f790979 [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/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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>
29
30namespace nlsr {
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050031namespace test {
32
33using std::shared_ptr;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050034using namespace nlsr::test;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050035
Saurab Dulal427e0122019-11-28 11:58:02 -060036class CertificateStoreFixture : public UnitTestTimeFixture
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050037{
38public:
39 CertificateStoreFixture()
Saurab Dulal427e0122019-11-28 11:58:02 -060040 : face(m_ioService, m_keyChain, {true, true})
41 , conf(face, m_keyChain, "unit-test-nlsr.conf")
42 , confProcessor(conf, SYNC_PROTOCOL_PSYNC, HYPERBOLIC_STATE_OFF,
43 "/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)
53 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
54
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050055 {
Saurab Dulal427e0122019-11-28 11:58:02 -060056 rootId = addIdentity(rootIdName);
57 siteIdentity = addSubCertificate(siteIdentityName, rootId);
58 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
59 routerId = addSubCertificate(routerIdName, opIdentity);
60
61 auto certificate = conf.initializeKey();
62 if (certificate) {
63 certStore.insert(*certificate);
64 };
65
66 // Create certificate and load it to the validator
67 // previously this was done by in nlsr ctor
68 conf.loadCertToValidator(rootId.getDefaultKey().getDefaultCertificate());
69 conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
70 conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
71 conf.loadCertToValidator(routerId.getDefaultKey().getDefaultCertificate());
72
73 std::ifstream inputFile;
74 inputFile.open(std::string("nlsr.conf"));
75
76 BOOST_REQUIRE(inputFile.is_open());
77
78 boost::property_tree::ptree pt;
79
80 boost::property_tree::read_info(inputFile, pt);
81
82 // Load security section and file name
83 for (const auto& tn : pt) {
84 if (tn.first == "security") {
85 auto it = tn.second.begin();
86 conf.getValidator().load(it->second, std::string("nlsr.conf"));
87 break;
88 }
89 }
90 inputFile.close();
91
92 this->advanceClocks(ndn::time::milliseconds(20));
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050093 }
94
95public:
Saurab Dulal427e0122019-11-28 11:58:02 -060096 void
97 checkForInterest(ndn::Name& interstName)
98 {
99 std::vector<ndn::Interest>& interests = face.sentInterests;
100 BOOST_REQUIRE(interests.size() > 0);
101
102 bool didFindInterest = false;
103 for (const auto& interest : interests) {
104 didFindInterest = didFindInterest || interest.getName() == interstName;
105 }
106 BOOST_CHECK(didFindInterest);
107 }
108
109 ndn::util::DummyClientFace face;
110
111 ConfParameter conf;
112 DummyConfFileProcessor confProcessor;
113
114 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
115 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
116
117 Nlsr nlsr;
118 Lsdb& lsdb;
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400119 ndn::security::Certificate certificate;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500120 ndn::Name certificateKey;
Saurab Dulal427e0122019-11-28 11:58:02 -0600121 security::CertificateStore certStore;
122 const boost::filesystem::path ROOT_CERT_PATH;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500123};
124
125BOOST_FIXTURE_TEST_SUITE(TestSecurityCertificateStore, CertificateStoreFixture)
126
127BOOST_AUTO_TEST_CASE(Basic)
128{
Saurab Dulal427e0122019-11-28 11:58:02 -0600129 ndn::Name identityName("/TestNLSR/identity");
130 identityName.appendVersion();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500131
Saurab Dulal427e0122019-11-28 11:58:02 -0600132 auto identity = m_keyChain.createIdentity(identityName);
133 auto certificate = identity.getDefaultKey().getDefaultCertificate();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500134
Saurab Dulal427e0122019-11-28 11:58:02 -0600135 ndn::Name certKey = certificate.getKeyName();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500136
Saurab Dulal427e0122019-11-28 11:58:02 -0600137 BOOST_CHECK(certStore.find(certKey) == nullptr);
138
139 // Certificate should be retrievable from the CertificateStore
140 certStore.insert(certificate);
141 conf.loadCertToValidator(certificate);
142
143 BOOST_CHECK(certStore.find(certKey) != nullptr);
144
145 lsdb.expressInterest(certKey, 0);
146
147 advanceClocks(10_ms);
148 checkForInterest(certKey);
149}
150
151BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration)
152{
153 // check if nlsrKeyPrefix is registered
154 ndn::Name nlsrKeyPrefix = conf.getRouterPrefix();
155 nlsrKeyPrefix.append("nlsr");
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700156 nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600157 checkPrefixRegistered(face, nlsrKeyPrefix);
158
159 // check if routerPrefix is registered
160 ndn::Name routerKeyPrefix = conf.getRouterPrefix();
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700161 routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600162 checkPrefixRegistered(face, routerKeyPrefix);
163
164 // check if operatorKeyPrefix is registered
165 ndn::Name operatorKeyPrefix = conf.getNetwork();
166 operatorKeyPrefix.append(conf.getSiteName());
167 operatorKeyPrefix.append(std::string("%C1.Operator"));
168 checkPrefixRegistered(face, operatorKeyPrefix);
169}
170
171BOOST_AUTO_TEST_CASE(SegmentValidatedSignal)
172{
173 ndn::Name lsaInterestName("/localhop");
174 lsaInterestName.append(conf.getLsaPrefix().getSubName(1));
175 lsaInterestName.append(conf.getSiteName());
176 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800177 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Saurab Dulal427e0122019-11-28 11:58:02 -0600178 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq() + 1);
179
180 lsdb.expressInterest(lsaInterestName, 0);
181 advanceClocks(10_ms);
182
183 checkForInterest(lsaInterestName);
184
185 ndn::Name lsaDataName(lsaInterestName);
186 lsaDataName.appendVersion();
187 lsaDataName.appendSegment(0);
188
189 ndn::Data data(lsaDataName);
190 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800191 NameLsa nameLsa;
192 data.setContent(nameLsa.wireEncode());
Saurab Dulal427e0122019-11-28 11:58:02 -0600193 data.setFinalBlock(lsaDataName[-1]);
194
195 // Sign data with this NLSR's key (in real it would be different NLSR)
196 m_keyChain.sign(data, conf.m_signingInfo);
197 face.put(data);
198
199 this->advanceClocks(ndn::time::milliseconds(1));
200
201 // Make NLSR validate data signed by its own key
202 conf.getValidator().validate(data,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800203 [] (const ndn::Data&) { BOOST_CHECK(true); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400204 [] (const ndn::Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800205 BOOST_CHECK(false);
206 });
Saurab Dulal427e0122019-11-28 11:58:02 -0600207
208 lsdb.emitSegmentValidatedSignal(data);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700209 const auto keyName = data.getSignatureInfo().getKeyLocator().getName();
Saurab Dulal427e0122019-11-28 11:58:02 -0600210 BOOST_CHECK(certStore.find(keyName) != nullptr);
211
212 // testing a callback after segment validation signal from lsdb
213 ndn::util::signal::ScopedConnection connection = lsdb.afterSegmentValidatedSignal.connect(
214 [&] (const ndn::Data& lsaSegment) {
215 BOOST_CHECK_EQUAL(lsaSegment.getName(), data.getName());
216 });
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500217}
218
219BOOST_AUTO_TEST_SUITE_END()
220
221} // namespace test
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500222} // namespace nlsr