Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "command-authenticator.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/logger.hpp" |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 28 | |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/tag.hpp> |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 30 | #include <ndn-cxx/security/certificate-fetcher-offline.hpp> |
| 31 | #include <ndn-cxx/security/certificate-request.hpp> |
| 32 | #include <ndn-cxx/security/validation-policy.hpp> |
| 33 | #include <ndn-cxx/security/validation-policy-accept-all.hpp> |
| 34 | #include <ndn-cxx/security/validation-policy-command-interest.hpp> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/util/io.hpp> |
| 36 | |
| 37 | #include <boost/filesystem.hpp> |
| 38 | |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 39 | namespace security = ndn::security; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 40 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 41 | namespace nfd { |
| 42 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 43 | NFD_LOG_INIT(CommandAuthenticator); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 44 | // INFO: configuration change, etc |
| 45 | // DEBUG: per authentication request result |
| 46 | |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 47 | /** \brief an Interest tag to indicate command signer |
| 48 | */ |
| 49 | using SignerTag = ndn::SimpleTag<Name, 20>; |
| 50 | |
| 51 | /** \brief obtain signer from SignerTag attached to Interest, if available |
| 52 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 53 | static std::optional<std::string> |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 54 | getSignerFromTag(const Interest& interest) |
| 55 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 56 | auto signerTag = interest.getTag<SignerTag>(); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 57 | if (signerTag == nullptr) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 58 | return std::nullopt; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 59 | } |
| 60 | else { |
| 61 | return signerTag->get().toUri(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** \brief a validation policy that only permits Interest signed by a trust anchor |
| 66 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 67 | class CommandAuthenticatorValidationPolicy final : public security::ValidationPolicy |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 68 | { |
| 69 | public: |
| 70 | void |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 71 | checkPolicy(const Interest& interest, const shared_ptr<security::ValidationState>& state, |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 72 | const ValidationContinuation& continueValidation) final |
| 73 | { |
| 74 | Name klName = getKeyLocatorName(interest, *state); |
| 75 | if (!state->getOutcome()) { // already failed |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // SignerTag must be placed on the 'original Interest' in ValidationState to be available for |
| 80 | // InterestValidationSuccessCallback. The 'interest' parameter refers to a different instance |
| 81 | // which is copied into 'original Interest'. |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 82 | auto state1 = dynamic_pointer_cast<security::InterestValidationState>(state); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 83 | state1->getOriginalInterest().setTag(make_shared<SignerTag>(klName)); |
| 84 | |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 85 | continueValidation(make_shared<security::CertificateRequest>(klName), state); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 89 | checkPolicy(const Data&, const shared_ptr<security::ValidationState>&, |
| 90 | const ValidationContinuation&) final |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 91 | { |
| 92 | // Non-certificate Data are not handled by CommandAuthenticator. |
| 93 | // Non-anchor certificates cannot be retrieved by offline fetcher. |
| 94 | BOOST_ASSERT_MSG(false, "Data should not be passed to this policy"); |
| 95 | } |
| 96 | }; |
| 97 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 98 | shared_ptr<CommandAuthenticator> |
| 99 | CommandAuthenticator::create() |
| 100 | { |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 101 | return shared_ptr<CommandAuthenticator>(new CommandAuthenticator); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 104 | CommandAuthenticator::CommandAuthenticator() = default; |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 105 | |
| 106 | void |
| 107 | CommandAuthenticator::setConfigFile(ConfigFile& configFile) |
| 108 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 109 | configFile.addSectionHandler("authorizations", [this] (auto&&... args) { |
| 110 | processConfig(std::forward<decltype(args)>(args)...); |
| 111 | }); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void |
| 115 | CommandAuthenticator::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename) |
| 116 | { |
| 117 | if (!isDryRun) { |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 118 | NFD_LOG_DEBUG("resetting authorizations"); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 119 | for (auto& kv : m_validators) { |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 120 | kv.second = make_shared<security::Validator>( |
| 121 | make_unique<security::ValidationPolicyCommandInterest>(make_unique<CommandAuthenticatorValidationPolicy>()), |
| 122 | make_unique<security::CertificateFetcherOffline>()); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | if (section.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 127 | NDN_THROW(ConfigFile::Error("'authorize' is missing under 'authorizations'")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int authSectionIndex = 0; |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 131 | for (const auto& [sectionName, authSection] : section) { |
| 132 | if (sectionName != "authorize") { |
| 133 | NDN_THROW(ConfigFile::Error("'" + sectionName + "' section is not permitted under 'authorizations'")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 134 | } |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 135 | |
| 136 | std::string certfile; |
| 137 | try { |
| 138 | certfile = authSection.get<std::string>("certfile"); |
| 139 | } |
| 140 | catch (const boost::property_tree::ptree_error&) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 141 | NDN_THROW(ConfigFile::Error("'certfile' is missing under authorize[" + |
| 142 | to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool isAny = false; |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 146 | shared_ptr<security::Certificate> cert; |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 147 | if (certfile == "any") { |
| 148 | isAny = true; |
| 149 | NFD_LOG_WARN("'certfile any' is intended for demo purposes only and " |
| 150 | "SHOULD NOT be used in production environments"); |
| 151 | } |
| 152 | else { |
| 153 | using namespace boost::filesystem; |
| 154 | path certfilePath = absolute(certfile, path(filename).parent_path()); |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 155 | cert = ndn::io::load<security::Certificate>(certfilePath.string()); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 156 | if (cert == nullptr) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 157 | NDN_THROW(ConfigFile::Error("cannot load certfile " + certfilePath.string() + |
| 158 | " for authorize[" + to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | const ConfigSection* privSection = nullptr; |
| 163 | try { |
| 164 | privSection = &authSection.get_child("privileges"); |
| 165 | } |
| 166 | catch (const boost::property_tree::ptree_error&) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 167 | NDN_THROW(ConfigFile::Error("'privileges' is missing under authorize[" + |
| 168 | to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (privSection->empty()) { |
| 172 | NFD_LOG_WARN("No privileges granted to certificate " << certfile); |
| 173 | } |
| 174 | for (const auto& kv : *privSection) { |
| 175 | const std::string& module = kv.first; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 176 | auto found = m_validators.find(module); |
| 177 | if (found == m_validators.end()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 178 | NDN_THROW(ConfigFile::Error("unknown module '" + module + |
| 179 | "' under authorize[" + to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (isDryRun) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | if (isAny) { |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 187 | found->second = make_shared<security::Validator>(make_unique<security::ValidationPolicyAcceptAll>(), |
| 188 | make_unique<security::CertificateFetcherOffline>()); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 189 | NFD_LOG_INFO("authorize module=" << module << " signer=any"); |
| 190 | } |
| 191 | else { |
Junxiao Shi | 16a3adf | 2017-05-26 17:38:51 +0000 | [diff] [blame] | 192 | const Name& keyName = cert->getKeyName(); |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 193 | security::Certificate certCopy = *cert; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 194 | found->second->loadAnchor(certfile, std::move(certCopy)); |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 195 | NFD_LOG_INFO("authorize module=" << module << " signer=" << keyName << " certfile=" << certfile); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | ++authSectionIndex; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | ndn::mgmt::Authorization |
| 204 | CommandAuthenticator::makeAuthorization(const std::string& module, const std::string& verb) |
| 205 | { |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 206 | m_validators[module]; // declares module, so that privilege is recognized |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 207 | |
| 208 | auto self = this->shared_from_this(); |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 209 | return [=] (const Name&, const Interest& interest, |
| 210 | const ndn::mgmt::ControlParameters*, |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 211 | const ndn::mgmt::AcceptContinuation& accept, |
| 212 | const ndn::mgmt::RejectContinuation& reject) { |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 213 | auto validator = self->m_validators.at(module); |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 214 | |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 215 | auto successCb = [accept, validator] (const Interest& interest1) { |
| 216 | auto signer1 = getSignerFromTag(interest1); |
| 217 | BOOST_ASSERT(signer1 || // signer must be available unless 'certfile any' |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 218 | dynamic_cast<security::ValidationPolicyAcceptAll*>(&validator->getPolicy()) != nullptr); |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 219 | std::string signer = signer1.value_or("*"); |
| 220 | NFD_LOG_DEBUG("accept " << interest1.getName() << " signer=" << signer); |
| 221 | accept(signer); |
| 222 | }; |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 223 | |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 224 | auto failureCb = [reject] (const Interest& interest1, const security::ValidationError& err) { |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 225 | using ndn::mgmt::RejectReply; |
| 226 | RejectReply reply = RejectReply::STATUS403; |
| 227 | switch (err.getCode()) { |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 228 | case security::ValidationError::NO_SIGNATURE: |
| 229 | case security::ValidationError::INVALID_KEY_LOCATOR: |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 230 | reply = RejectReply::SILENT; |
| 231 | break; |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 232 | case security::ValidationError::POLICY_ERROR: |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 233 | if (interest1.getName().size() < ndn::command_interest::MIN_SIZE) { // "name too short" |
| 234 | reply = RejectReply::SILENT; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 235 | } |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | NFD_LOG_DEBUG("reject " << interest1.getName() << " signer=" << |
| 239 | getSignerFromTag(interest1).value_or("?") << " reason=" << err); |
| 240 | reject(reply); |
| 241 | }; |
| 242 | |
| 243 | if (validator) { |
| 244 | validator->validate(interest, successCb, failureCb); |
| 245 | } |
| 246 | else { |
| 247 | NFD_LOG_DEBUG("reject " << interest.getName() << " signer=" << |
| 248 | getSignerFromTag(interest).value_or("?") << " reason=Unauthorized"); |
| 249 | reject(ndn::mgmt::RejectReply::STATUS403); |
| 250 | } |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 251 | }; |
| 252 | } |
| 253 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 254 | } // namespace nfd |