Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, The University of Memphis, |
| 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "test-common.hpp" |
| 23 | #include "nlsr.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/interest.hpp> |
| 26 | #include <ndn-cxx/security/key-chain.hpp> |
| 27 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 28 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 29 | #include <ndn-cxx/security/signing-info.hpp> |
| 30 | |
| 31 | #include <boost/filesystem.hpp> |
| 32 | #include <boost/property_tree/ptree.hpp> |
| 33 | #include <boost/property_tree/info_parser.hpp> |
| 34 | |
| 35 | using namespace ndn; |
| 36 | |
| 37 | namespace nlsr { |
| 38 | namespace test { |
| 39 | |
| 40 | class LsaRuleFixture : public nlsr::test::BaseFixture |
| 41 | { |
| 42 | public: |
| 43 | LsaRuleFixture() |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 44 | : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService)) |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 45 | , rootId(ndn::Name("ndn")) |
| 46 | , siteIdentity(ndn::Name("/ndn/edu/test-site")) |
| 47 | , opIdentity(ndn::Name(siteIdentity).append(ndn::Name("%C1.Operator/op1"))) |
| 48 | , routerId(ndn::Name("/ndn/edu/test-site/%C1.Router/router1")) |
| 49 | , nlsr(g_ioService, g_scheduler, *face, g_keyChain) |
| 50 | , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert")) |
| 51 | { |
| 52 | try { |
| 53 | keyChain.deleteIdentity(rootId); |
| 54 | keyChain.deleteIdentity(siteIdentity); |
| 55 | keyChain.deleteIdentity(opIdentity); |
| 56 | keyChain.deleteIdentity(routerId); |
| 57 | } |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 58 | catch (const std::exception& e) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | createCert(rootId, rootCertName, rootCert, rootId); |
| 62 | BOOST_REQUIRE(rootCert != nullptr); |
| 63 | |
| 64 | createCert(siteIdentity, siteCertName, siteCert, rootId); |
| 65 | BOOST_REQUIRE(siteCert != nullptr); |
| 66 | |
| 67 | createCert(opIdentity, opCertName, opCert, siteIdentity); |
| 68 | BOOST_REQUIRE(opCert != nullptr); |
| 69 | |
| 70 | createCert(routerId, routerCertName, routerCert, opIdentity); |
| 71 | BOOST_REQUIRE(routerCert != nullptr); |
| 72 | |
| 73 | // Loading the security section's validator part into the validator |
| 74 | // See conf file processor for more details |
| 75 | std::ifstream inputFile; |
| 76 | inputFile.open(std::string("nlsr.conf")); |
| 77 | |
| 78 | BOOST_REQUIRE(inputFile.is_open()); |
| 79 | |
| 80 | boost::property_tree::ptree pt; |
| 81 | |
| 82 | boost::property_tree::read_info(inputFile, pt); |
| 83 | |
| 84 | //Loads section and file name |
| 85 | for (auto tn = pt.begin(); tn != pt.end(); ++tn) { |
| 86 | if (tn->first == "security") { |
| 87 | auto it = tn->second.begin(); |
| 88 | nlsr.loadValidator(it->second, std::string("nlsr.conf")); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | inputFile.close(); |
| 93 | |
| 94 | // Set the network so the LSA prefix is constructed |
| 95 | // Set all so that buildRouterPrefix is set |
| 96 | nlsr.getConfParameter().setNetwork("/ndn"); |
| 97 | nlsr.getConfParameter().setSiteName("/edu/test-site"); |
| 98 | nlsr.getConfParameter().setRouterName("/%C1.Router/router1"); |
| 99 | |
| 100 | // Initialize NLSR to initialize the keyChain |
| 101 | nlsr.initialize(); |
| 102 | } |
| 103 | |
| 104 | void |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 105 | createCert(ndn::Name& identity, ndn::Name& certName, std::shared_ptr<IdentityCertificate>& cert, const ndn::Name& signer) |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 106 | { |
| 107 | ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(identity, true); |
| 108 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 109 | cert = std::make_shared<ndn::IdentityCertificate>(); |
| 110 | std::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 111 | certName = keyName.getPrefix(-1); |
| 112 | certName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 113 | cert->setName(certName); |
| 114 | cert->setNotBefore(time::system_clock::now() - time::days(1)); |
| 115 | cert->setNotAfter(time::system_clock::now() + time::days(1)); |
| 116 | cert->setPublicKeyInfo(*pubKey); |
| 117 | cert->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME, |
| 118 | keyName.toUri())); |
| 119 | cert->encode(); |
| 120 | |
| 121 | // root is self signed and root.cert is saved |
| 122 | if (signer == identity) { |
| 123 | keyChain.selfSign(*cert); |
| 124 | |
| 125 | keyChain.addCertificateAsIdentityDefault(*cert); |
| 126 | |
| 127 | nlsr.loadCertToPublish(cert); |
| 128 | |
| 129 | ndn::io::save(*cert, ROOT_CERT_PATH.string()); |
| 130 | } |
| 131 | else { |
| 132 | ndn::security::SigningInfo signingInfo; |
| 133 | signingInfo.setSigningIdentity(signer); |
| 134 | keyChain.sign(*cert, signingInfo); |
| 135 | |
| 136 | keyChain.addCertificateAsIdentityDefault(*cert); |
| 137 | |
| 138 | nlsr.loadCertToPublish(cert); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | ~LsaRuleFixture() |
| 143 | { |
| 144 | keyChain.deleteIdentity(rootId); |
| 145 | keyChain.deleteIdentity(siteIdentity); |
| 146 | keyChain.deleteIdentity(opIdentity); |
| 147 | keyChain.deleteIdentity(routerId); |
| 148 | |
| 149 | boost::filesystem::remove(ROOT_CERT_PATH); |
| 150 | } |
| 151 | |
| 152 | public: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 153 | std::shared_ptr<ndn::util::DummyClientFace> face; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 154 | ndn::KeyChain keyChain; |
| 155 | |
| 156 | ndn::Name rootId, siteIdentity, opIdentity, routerId; |
| 157 | ndn::Name rootCertName, siteCertName, opCertName, routerCertName; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 158 | std::shared_ptr<IdentityCertificate> rootCert, siteCert, opCert, routerCert; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 159 | |
| 160 | Nlsr nlsr; |
| 161 | |
| 162 | const boost::filesystem::path ROOT_CERT_PATH; |
| 163 | }; |
| 164 | |
| 165 | BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture) |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(ValidateCorrectLSA) |
| 168 | { |
| 169 | ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix(); |
| 170 | lsaInterestName.append(nlsr.getConfParameter().getSiteName()); |
| 171 | lsaInterestName.append(nlsr.getConfParameter().getRouterName()); |
| 172 | |
| 173 | // Append LSA type |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 174 | lsaInterestName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 175 | |
| 176 | // This would be the sequence number of its own NameLsa |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 177 | lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 178 | |
| 179 | // Append version, segmentNo |
| 180 | lsaInterestName.appendNumber(1).appendNumber(1); |
| 181 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 182 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 183 | data->setName(lsaInterestName); |
| 184 | data->setFreshnessPeriod(ndn::time::seconds(10)); |
| 185 | |
| 186 | // Sign data with NLSR's key |
| 187 | nlsr.getKeyChain().sign(*data, ndn::security::signingByCertificate(nlsr.getDefaultCertName())); |
| 188 | |
| 189 | // Make NLSR validate data signed by its own key |
| 190 | nlsr.getValidator().validate(*data, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 191 | [] (const std::shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
| 192 | [] (const std::shared_ptr<const Data>&, const std::string&) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 193 | BOOST_CHECK(false); |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA) |
| 198 | { |
| 199 | // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA |
| 200 | ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix().getSubName(1); |
| 201 | lsaInterestName.append(nlsr.getConfParameter().getSiteName()); |
| 202 | lsaInterestName.append(nlsr.getConfParameter().getRouterName()); |
| 203 | |
| 204 | // Append LSA type |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 205 | lsaInterestName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 206 | |
| 207 | // This would be the sequence number of its own NameLsa |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 208 | lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 209 | |
| 210 | // Append version, segmentNo |
| 211 | lsaInterestName.appendNumber(1).appendNumber(1); |
| 212 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 213 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 214 | data->setName(lsaInterestName); |
| 215 | data->setFreshnessPeriod(ndn::time::seconds(10)); |
| 216 | nlsr.getKeyChain().sign(*data, ndn::security::signingByCertificate(nlsr.getDefaultCertName())); |
| 217 | |
| 218 | // Make NLSR validate data signed by its own key |
| 219 | nlsr.getValidator().validate(*data, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 220 | [] (const std::shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
| 221 | [] (const std::shared_ptr<const Data>&, const std::string&) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 222 | BOOST_CHECK(true); |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | BOOST_AUTO_TEST_SUITE_END() |
| 227 | |
| 228 | } // namespace test |
| 229 | } // namespace nlsr |