Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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. |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 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/>. |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 20 | **/ |
| 21 | |
| 22 | #include "validator.hpp" |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 23 | |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/security/certificate-cache-ttl.hpp> |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/security/key-chain.hpp> |
| 26 | #include <ndn-cxx/util/scheduler.hpp> |
| 27 | |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 28 | #include "boost-test.hpp" |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 29 | #include "common.hpp" |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 30 | #include "security/certificate-store.hpp" |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nlsr { |
| 33 | |
| 34 | namespace test { |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(TestValidator) |
| 37 | |
| 38 | struct ValidatorFixture |
| 39 | { |
| 40 | ValidatorFixture() |
| 41 | : m_face2(m_face.getIoService()) |
| 42 | , m_scheduler(m_face.getIoService()) |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 43 | , m_keyPrefix("/ndn/broadcast/KEYS") |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 44 | , m_certificateCache(new ndn::CertificateCacheTtl(m_face.getIoService())) |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 45 | , m_validator(m_face2, ndn::Name("/ndn/broadcast"), m_certificateCache, m_certStore) |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 46 | , m_identity("/TestValidator/NLSR") |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 47 | , m_wasValidated(false) |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 48 | { |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 49 | m_face.setInterestFilter(m_keyPrefix, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 50 | std::bind(&ValidatorFixture::onKeyInterest, this, _1, _2), |
| 51 | std::bind(&ValidatorFixture::onKeyPrefixRegSuccess, this, _1), |
| 52 | std::bind(&ValidatorFixture::registrationFailed, this, _1, _2)); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 53 | |
| 54 | m_keyChain.createIdentity(m_identity); |
| 55 | ndn::Name certName = m_keyChain.getDefaultCertificateNameForIdentity(m_identity); |
| 56 | m_cert = m_keyChain.getCertificate(certName); |
| 57 | ndn::io::save(*m_cert, "trust-anchor.cert"); |
| 58 | |
| 59 | const std::string CONFIG = |
| 60 | "rule\n" |
| 61 | "{\n" |
| 62 | " id \"NSLR Hello Rule\"\n" |
| 63 | " for data\n" |
| 64 | " filter\n" |
| 65 | " {\n" |
| 66 | " type name\n" |
| 67 | " regex ^[^<NLSR><INFO>]*<NLSR><INFO><><>$\n" |
| 68 | " }\n" |
| 69 | " checker\n" |
| 70 | " {\n" |
| 71 | " type customized\n" |
| 72 | " sig-type rsa-sha256\n" |
| 73 | " key-locator\n" |
| 74 | " {\n" |
| 75 | " type name\n" |
| 76 | " hyper-relation\n" |
| 77 | " {\n" |
| 78 | " k-regex ^([^<KEY><NLSR>]*)<NLSR><KEY><ksk-.*><ID-CERT>$\n" |
| 79 | " k-expand \\\\1\n" |
| 80 | " h-relation equal\n" |
| 81 | " p-regex ^([^<NLSR><INFO>]*)<NLSR><INFO><><>$\n" |
| 82 | " p-expand \\\\1\n" |
| 83 | " }\n" |
| 84 | " }\n" |
| 85 | " }\n" |
| 86 | "}\n" |
| 87 | "rule\n" |
| 88 | "{\n" |
| 89 | " id \"Single Rule\"\n" |
| 90 | " for data\n" |
| 91 | " filter\n" |
| 92 | " {\n" |
| 93 | " type name\n" |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 94 | " regex ^<TestValidator>([^<KEY><NLSR>]*)<NLSR><KEY><ksk-.*><><>$\n" |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 95 | " }\n" |
| 96 | " checker\n" |
| 97 | " {\n" |
| 98 | " type fixed-signer\n" |
| 99 | " sig-type rsa-sha256\n" |
| 100 | " signer\n" |
| 101 | " {\n" |
| 102 | " type file\n" |
| 103 | " file-name \"trust-anchor.cert\"\n" |
| 104 | " }\n" |
| 105 | " }\n" |
| 106 | "}\n"; |
| 107 | |
| 108 | const boost::filesystem::path CONFIG_PATH = |
| 109 | (boost::filesystem::current_path() / std::string("unit-test.conf")); |
| 110 | |
| 111 | m_validator.load(CONFIG, CONFIG_PATH.native()); |
| 112 | } |
| 113 | |
| 114 | ~ValidatorFixture() |
| 115 | { |
| 116 | m_keyChain.deleteIdentity(m_identity); |
| 117 | |
| 118 | const boost::filesystem::path CERT_PATH = |
| 119 | (boost::filesystem::current_path() / std::string("trust-anchor.cert")); |
| 120 | boost::filesystem::remove(CERT_PATH); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | onKeyInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 125 | { |
| 126 | const ndn::Name& interestName = interest.getName(); |
| 127 | |
| 128 | ndn::Name certName = interestName.getSubName(name.size()); |
| 129 | |
| 130 | if (certName[-2].toUri() == "ID-CERT") |
| 131 | { |
| 132 | certName = certName.getPrefix(-1); |
| 133 | } |
| 134 | else if (certName[-1].toUri() != "ID-CERT") |
| 135 | return; //Wrong key interest. |
| 136 | |
| 137 | if (certName != m_cert->getName().getPrefix(-1)) |
| 138 | return; //No such a cert |
| 139 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 140 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(interestName); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 141 | data->setContent(m_cert->wireEncode()); |
| 142 | m_keyChain.signWithSha256(*data); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 143 | |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 144 | m_face.put(*data); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | onKeyPrefixRegSuccess(const ndn::Name& name) |
| 149 | { |
| 150 | BOOST_REQUIRE(true); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | registrationFailed(const ndn::Name& name, const std::string& msg) |
| 155 | { |
| 156 | std::cerr << "Failure Info: " << msg << std::endl; |
| 157 | BOOST_REQUIRE(false); |
| 158 | } |
| 159 | |
| 160 | void |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 161 | onValidated(const std::shared_ptr<const ndn::Data>& data) |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 162 | { |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 163 | m_wasValidated = true; |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 167 | onValidationFailed(const std::shared_ptr<const ndn::Data>& data, |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 168 | const std::string& failureInfo) |
| 169 | { |
| 170 | std::cerr << "Failure Info: " << failureInfo << std::endl; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 171 | m_wasValidated = false; |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 175 | validate(const std::shared_ptr<const ndn::Data>& data) |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 176 | { |
| 177 | m_validator.validate(*data, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 178 | std::bind(&ValidatorFixture::onValidated, this, _1), |
| 179 | std::bind(&ValidatorFixture::onValidationFailed, this, _1, _2)); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
| 183 | terminate() |
| 184 | { |
| 185 | m_face.getIoService().stop(); |
| 186 | } |
| 187 | |
| 188 | protected: |
| 189 | ndn::Face m_face; |
| 190 | ndn::Face m_face2; |
| 191 | ndn::Scheduler m_scheduler; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 192 | const ndn::Name m_keyPrefix; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 193 | std::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 194 | security::CertificateStore m_certStore; |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 195 | nlsr::Validator m_validator; |
| 196 | |
| 197 | ndn::KeyChain m_keyChain; |
| 198 | ndn::Name m_identity; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 199 | std::shared_ptr<ndn::IdentityCertificate> m_cert; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 200 | |
| 201 | bool m_wasValidated; |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | BOOST_FIXTURE_TEST_CASE(InfoCertFetch, ValidatorFixture) |
| 205 | { |
| 206 | ndn::Name dataName = m_identity; |
| 207 | dataName.append("INFO").append("neighbor").append("version"); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 208 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 209 | m_keyChain.signByIdentity(*data, m_identity); |
| 210 | |
| 211 | m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 212 | std::bind(&ValidatorFixture::validate, this, data)); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 213 | m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 214 | std::bind(&ValidatorFixture::terminate, this)); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 215 | BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 216 | |
| 217 | BOOST_CHECK(m_wasValidated); |
| 218 | } |
| 219 | |
| 220 | BOOST_FIXTURE_TEST_CASE(CertificateStorage, ValidatorFixture) |
| 221 | { |
Alexander Afanasyev | 4c2bb36 | 2017-03-14 12:29:19 -0700 | [diff] [blame] | 222 | std::vector<ndn::security::v1::CertificateSubjectDescription> subjectDescription; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 223 | |
| 224 | // Create an operator identity |
| 225 | ndn::Name opIdentity("/TestValidator/operator/NLSR"); |
| 226 | m_keyChain.createIdentity(opIdentity); |
| 227 | |
| 228 | // Create an operator cert signed by the trust anchor |
| 229 | ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(opIdentity, true); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 230 | std::shared_ptr<ndn::IdentityCertificate> opCert = |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 231 | m_keyChain.prepareUnsignedIdentityCertificate(keyName, |
| 232 | m_identity, |
| 233 | ndn::time::system_clock::now(), |
| 234 | ndn::time::system_clock::now() |
| 235 | + ndn::time::days(1), |
| 236 | subjectDescription); |
| 237 | m_keyChain.signByIdentity(*opCert, m_identity); |
| 238 | m_keyChain.addCertificateAsIdentityDefault(*opCert); |
| 239 | |
| 240 | // Sign data with operator cert |
| 241 | ndn::Name dataName = opIdentity; |
| 242 | dataName.append("INFO").append("neighbor").append("version"); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 243 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 244 | m_keyChain.signByIdentity(*data, opIdentity); |
| 245 | |
| 246 | // Check without cert in CertificateStore |
| 247 | m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 248 | std::bind(&ValidatorFixture::validate, this, data)); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 249 | m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 250 | std::bind(&ValidatorFixture::terminate, this)); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 251 | |
| 252 | BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
| 253 | BOOST_CHECK_EQUAL(m_wasValidated, false); |
| 254 | |
| 255 | // Check with cert in CertificateStore |
| 256 | m_certStore.insert(opCert); |
| 257 | |
| 258 | m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 259 | std::bind(&ValidatorFixture::validate, this, data)); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 260 | m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 261 | std::bind(&ValidatorFixture::terminate, this)); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 262 | |
| 263 | BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
| 264 | BOOST_CHECK(m_wasValidated); |
| 265 | |
| 266 | // Cleanup |
| 267 | m_keyChain.deleteIdentity(opIdentity); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | BOOST_AUTO_TEST_SUITE_END() |
| 271 | |
| 272 | } // namespace test |
| 273 | } // namespace nlsr |