Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 2 | /* |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 4 | * 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 Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 20 | */ |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 21 | |
| 22 | #include "security/certificate-store.hpp" |
| 23 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 24 | #include "tests/test-common.hpp" |
| 25 | #include "nlsr.hpp" |
| 26 | #include "lsdb.hpp" |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 27 | |
| 28 | #include <ndn-cxx/security/key-chain.hpp> |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame^] | 30 | #include <boost/property_tree/info_parser.hpp> |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 31 | |
| 32 | namespace nlsr { |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 33 | namespace test { |
| 34 | |
| 35 | using std::shared_ptr; |
| 36 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 37 | class CertificateStoreFixture : public UnitTestTimeFixture |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | CertificateStoreFixture() |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 41 | : 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 Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 56 | { |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 57 | 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 Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public: |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 97 | 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 Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 120 | ndn::security::Certificate certificate; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 121 | ndn::Name certificateKey; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 122 | security::CertificateStore certStore; |
| 123 | const boost::filesystem::path ROOT_CERT_PATH; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | BOOST_FIXTURE_TEST_SUITE(TestSecurityCertificateStore, CertificateStoreFixture) |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(Basic) |
| 129 | { |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 130 | ndn::Name identityName("/TestNLSR/identity"); |
| 131 | identityName.appendVersion(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 132 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 133 | auto identity = m_keyChain.createIdentity(identityName); |
| 134 | auto certificate = identity.getDefaultKey().getDefaultCertificate(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 135 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 136 | ndn::Name certKey = certificate.getKeyName(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 137 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 138 | 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 | |
| 152 | BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration) |
| 153 | { |
| 154 | // check if nlsrKeyPrefix is registered |
| 155 | ndn::Name nlsrKeyPrefix = conf.getRouterPrefix(); |
| 156 | nlsrKeyPrefix.append("nlsr"); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 157 | nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 158 | checkPrefixRegistered(face, nlsrKeyPrefix); |
| 159 | |
| 160 | // check if routerPrefix is registered |
| 161 | ndn::Name routerKeyPrefix = conf.getRouterPrefix(); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 162 | routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 163 | 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 | |
| 172 | BOOST_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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 178 | lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME)); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 179 | 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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 192 | NameLsa nameLsa; |
| 193 | data.setContent(nameLsa.wireEncode()); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 194 | 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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 204 | [] (const ndn::Data&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 205 | [] (const ndn::Data&, const ndn::security::ValidationError&) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 206 | BOOST_CHECK(false); |
| 207 | }); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 208 | |
| 209 | lsdb.emitSegmentValidatedSignal(data); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 210 | const auto keyName = data.getSignatureInfo().getKeyLocator().getName(); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 211 | 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 Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | BOOST_AUTO_TEST_SUITE_END() |
| 221 | |
| 222 | } // namespace test |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 223 | } // namespace nlsr |