blob: 0ec3ff53a495598c0a00ace35167b5e5a25a13bf [file] [log] [blame]
Ashlesh Gawande54e726c2017-01-30 12:48:06 -06001/* -*- 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
35using namespace ndn;
36
37namespace nlsr {
38namespace test {
39
40class LsaRuleFixture : public nlsr::test::BaseFixture
41{
42public:
43 LsaRuleFixture()
dmcoomes9f936662017-03-02 10:33:09 -060044 : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService))
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060045 , 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 }
dmcoomes9f936662017-03-02 10:33:09 -060058 catch (const std::exception& e) {
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060059 }
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
dmcoomes9f936662017-03-02 10:33:09 -0600105 createCert(ndn::Name& identity, ndn::Name& certName, std::shared_ptr<IdentityCertificate>& cert, const ndn::Name& signer)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600106 {
107 ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(identity, true);
108
dmcoomes9f936662017-03-02 10:33:09 -0600109 cert = std::make_shared<ndn::IdentityCertificate>();
110 std::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600111 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
152public:
dmcoomes9f936662017-03-02 10:33:09 -0600153 std::shared_ptr<ndn::util::DummyClientFace> face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600154 ndn::KeyChain keyChain;
155
156 ndn::Name rootId, siteIdentity, opIdentity, routerId;
157 ndn::Name rootCertName, siteCertName, opCertName, routerCertName;
dmcoomes9f936662017-03-02 10:33:09 -0600158 std::shared_ptr<IdentityCertificate> rootCert, siteCert, opCert, routerCert;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600159
160 Nlsr nlsr;
161
162 const boost::filesystem::path ROOT_CERT_PATH;
163};
164
165BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
166
167BOOST_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 Gordon727d4832017-10-13 18:04:25 -0500174 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600175
176 // This would be the sequence number of its own NameLsa
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500177 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600178
179 // Append version, segmentNo
180 lsaInterestName.appendNumber(1).appendNumber(1);
181
dmcoomes9f936662017-03-02 10:33:09 -0600182 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600183 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,
dmcoomes9f936662017-03-02 10:33:09 -0600191 [] (const std::shared_ptr<const Data>&) { BOOST_CHECK(true); },
192 [] (const std::shared_ptr<const Data>&, const std::string&) {
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600193 BOOST_CHECK(false);
194 });
195}
196
197BOOST_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 Gordon727d4832017-10-13 18:04:25 -0500205 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600206
207 // This would be the sequence number of its own NameLsa
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500208 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600209
210 // Append version, segmentNo
211 lsaInterestName.appendNumber(1).appendNumber(1);
212
dmcoomes9f936662017-03-02 10:33:09 -0600213 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600214 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,
dmcoomes9f936662017-03-02 10:33:09 -0600220 [] (const std::shared_ptr<const Data>&) { BOOST_CHECK(false); },
221 [] (const std::shared_ptr<const Data>&, const std::string&) {
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600222 BOOST_CHECK(true);
223 });
224}
225
226BOOST_AUTO_TEST_SUITE_END()
227
228} // namespace test
229} // namespace nlsr