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 | /* |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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> |
| 29 | |
| 30 | namespace nlsr { |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 31 | namespace test { |
| 32 | |
| 33 | using std::shared_ptr; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 34 | using namespace nlsr::test; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 35 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 36 | class CertificateStoreFixture : public UnitTestTimeFixture |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 37 | { |
| 38 | public: |
| 39 | CertificateStoreFixture() |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 40 | : 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 Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 55 | { |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 56 | 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 Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | public: |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 96 | 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 Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 119 | ndn::security::Certificate certificate; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 120 | ndn::Name certificateKey; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 121 | security::CertificateStore certStore; |
| 122 | const boost::filesystem::path ROOT_CERT_PATH; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | BOOST_FIXTURE_TEST_SUITE(TestSecurityCertificateStore, CertificateStoreFixture) |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Basic) |
| 128 | { |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 129 | ndn::Name identityName("/TestNLSR/identity"); |
| 130 | identityName.appendVersion(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 131 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 132 | auto identity = m_keyChain.createIdentity(identityName); |
| 133 | auto certificate = identity.getDefaultKey().getDefaultCertificate(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 134 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 135 | ndn::Name certKey = certificate.getKeyName(); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 136 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 137 | 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 | |
| 151 | BOOST_AUTO_TEST_CASE(TestKeyPrefixRegistration) |
| 152 | { |
| 153 | // check if nlsrKeyPrefix is registered |
| 154 | ndn::Name nlsrKeyPrefix = conf.getRouterPrefix(); |
| 155 | nlsrKeyPrefix.append("nlsr"); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 156 | nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 157 | checkPrefixRegistered(face, nlsrKeyPrefix); |
| 158 | |
| 159 | // check if routerPrefix is registered |
| 160 | ndn::Name routerKeyPrefix = conf.getRouterPrefix(); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 161 | routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 162 | 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 | |
| 171 | BOOST_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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 177 | lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME)); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 178 | 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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 191 | NameLsa nameLsa; |
| 192 | data.setContent(nameLsa.wireEncode()); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 193 | 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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 203 | [] (const ndn::Data&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 204 | [] (const ndn::Data&, const ndn::security::ValidationError&) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 205 | BOOST_CHECK(false); |
| 206 | }); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 207 | |
| 208 | lsdb.emitSegmentValidatedSignal(data); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 209 | const auto keyName = data.getSignatureInfo().getKeyLocator().getName(); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 210 | 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 Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_SUITE_END() |
| 220 | |
| 221 | } // namespace test |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 222 | } // namespace nlsr |