Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 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, |
| 48 | const ndn::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: |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 76 | typedef std::vector<ndn::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 | |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 87 | for (const 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 |
| 92 | shared_ptr<const ndn::IdentityCertificate> cert = m_certStore.find(interest.getName()); |
| 93 | |
| 94 | if (cert != nullptr) { |
| 95 | // If the certificate is found, no reason to express interest |
| 96 | shared_ptr<ndn::Data> data = make_shared<ndn::Data>(interest.getName()); |
| 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, |
| 109 | bind(&Validator::onData, this, _1, _2, request), |
| 110 | bind(&Validator::onTimeout, |
| 111 | this, _1, request->m_nRetries, |
| 112 | onFailure, |
| 113 | request)); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 114 | } |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 115 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 118 | virtual ndn::shared_ptr<const ndn::Data> |
| 119 | preCertificateValidation(const ndn::Data& data) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 120 | { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 121 | ndn::shared_ptr<ndn::Data> internalData = ndn::make_shared<ndn::Data>(); |
| 122 | internalData->wireDecode(data.getContent().blockFromValue()); |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 123 | return internalData; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private: |
| 127 | ndn::Name m_broadcastPrefix; |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 128 | security::CertificateStore& m_certStore; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 131 | } // namespace nlsr |
| 132 | |
| 133 | #endif // NLSR_VALIDATOR_HPP |