Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | c6a8522 | 2017-01-03 16:54:34 -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 | 20e3a6e | 2014-05-26 23:16:10 -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 | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 20 | **/ |
| 21 | |
| 22 | #ifndef NLSR_VALIDATOR_HPP |
| 23 | #define NLSR_VALIDATOR_HPP |
| 24 | |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 25 | #include "common.hpp" |
| 26 | #include "security/certificate-store.hpp" |
| 27 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/security/validator-config.hpp> |
| 29 | |
| 30 | namespace nlsr { |
| 31 | |
| 32 | class Validator : public ndn::ValidatorConfig |
| 33 | { |
| 34 | public: |
| 35 | class Error : public ndn::ValidatorConfig::Error |
| 36 | { |
| 37 | public: |
| 38 | explicit |
| 39 | Error(const std::string& what) |
| 40 | : ndn::ValidatorConfig::Error(what) |
| 41 | { |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | explicit |
| 46 | Validator(ndn::Face& face, |
| 47 | const ndn::Name broadcastPrefix, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 48 | const std::shared_ptr<ndn::CertificateCache>& cache, |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 49 | security::CertificateStore& certStore, |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 50 | const int stepLimit = 10) |
Yingdi Yu | e024803 | 2014-06-19 14:06:13 -0700 | [diff] [blame] | 51 | : ndn::ValidatorConfig(face, cache, ndn::ValidatorConfig::DEFAULT_GRACE_INTERVAL, stepLimit) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 52 | , m_broadcastPrefix(broadcastPrefix) |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 53 | , m_certStore(certStore) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 54 | { |
| 55 | m_broadcastPrefix.append("KEYS"); |
| 56 | } |
| 57 | |
| 58 | virtual |
| 59 | ~Validator() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | const ndn::Name& |
| 64 | getBroadcastPrefix() |
| 65 | { |
| 66 | return m_broadcastPrefix; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | setBroadcastPrefix(const ndn::Name& broadcastPrefix) |
| 71 | { |
| 72 | m_broadcastPrefix = broadcastPrefix; |
| 73 | } |
| 74 | |
| 75 | protected: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 76 | typedef std::vector<std::shared_ptr<ndn::ValidationRequest>> NextSteps; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 77 | |
| 78 | virtual void |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 79 | afterCheckPolicy(const NextSteps& nextSteps, |
| 80 | const OnFailure& onFailure) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 81 | { |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 82 | if (m_face == nullptr) { |
| 83 | onFailure("Require more information to validate the packet!"); |
| 84 | return; |
| 85 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 86 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 87 | for (const std::shared_ptr<ndn::ValidationRequest>& request : nextSteps) { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 88 | |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 89 | ndn::Interest& interest = request->m_interest; |
| 90 | |
| 91 | // Look for certificate in permanent storage |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 92 | std::shared_ptr<const ndn::IdentityCertificate> cert = m_certStore.find(interest.getName()); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 93 | |
| 94 | if (cert != nullptr) { |
| 95 | // If the certificate is found, no reason to express interest |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 96 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(interest.getName()); |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 97 | data->setContent(cert->wireEncode()); |
| 98 | |
| 99 | Validator::onData(interest, *data, request); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 100 | } |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 101 | else { |
| 102 | // Prepend broadcast prefix to interest name |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 103 | ndn::Name broadcastName = m_broadcastPrefix; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 104 | broadcastName.append(interest.getName()); |
| 105 | interest.setName(broadcastName); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 106 | |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 107 | // Attempt to fetch the certificate |
| 108 | m_face->expressInterest(interest, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 109 | std::bind(&Validator::onData, this, _1, _2, request), |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 110 | std::bind(&Validator::onTimeout, // Nack |
| 111 | this, _1, request->m_nRetries, |
| 112 | onFailure, |
| 113 | request), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 114 | std::bind(&Validator::onTimeout, |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame] | 115 | this, _1, request->m_nRetries, |
| 116 | onFailure, |
| 117 | request)); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 118 | } |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 119 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 122 | virtual std::shared_ptr<const ndn::Data> |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 123 | preCertificateValidation(const ndn::Data& data) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 124 | { |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 125 | std::shared_ptr<ndn::Data> internalData = std::make_shared<ndn::Data>(); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 126 | internalData->wireDecode(data.getContent().blockFromValue()); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 127 | return internalData; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private: |
| 131 | ndn::Name m_broadcastPrefix; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 132 | security::CertificateStore& m_certStore; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 135 | } // namespace nlsr |
| 136 | |
| 137 | #endif // NLSR_VALIDATOR_HPP |