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 | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/security/certificate-fetcher-offline.hpp> |
| 30 | #include <ndn-cxx/security/certificate-request.hpp> |
| 31 | #include <ndn-cxx/security/validation-policy.hpp> |
| 32 | #include <ndn-cxx/security/validation-policy-accept-all.hpp> |
| 33 | #include <ndn-cxx/security/validation-policy-command-interest.hpp> |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/tag.hpp> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/util/io.hpp> |
| 36 | |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 37 | #include <boost/filesystem/operations.hpp> |
| 38 | #include <boost/filesystem/path.hpp> |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 39 | |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 40 | namespace security = ndn::security; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 41 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 42 | namespace nfd { |
| 43 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 44 | NFD_LOG_INIT(CommandAuthenticator); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 45 | // INFO: configuration change, etc |
| 46 | // DEBUG: per authentication request result |
| 47 | |
Davide Pesavento | b83d3df | 2022-09-13 14:04:34 -0400 | [diff] [blame] | 48 | /** |
| 49 | * \brief An Interest tag to store the command signer. |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 50 | */ |
| 51 | using SignerTag = ndn::SimpleTag<Name, 20>; |
| 52 | |
Davide Pesavento | b83d3df | 2022-09-13 14:04:34 -0400 | [diff] [blame] | 53 | /** |
| 54 | * \brief Obtain signer from a SignerTag attached to \p interest, if available. |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 55 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 56 | static std::optional<std::string> |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 57 | getSignerFromTag(const Interest& interest) |
| 58 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 59 | auto signerTag = interest.getTag<SignerTag>(); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 60 | if (signerTag == nullptr) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 61 | return std::nullopt; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 62 | } |
| 63 | else { |
| 64 | return signerTag->get().toUri(); |
| 65 | } |
| 66 | } |
| 67 | |
Davide Pesavento | b83d3df | 2022-09-13 14:04:34 -0400 | [diff] [blame] | 68 | /** |
| 69 | * \brief A validation policy that only permits Interests signed by a trust anchor. |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 70 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 71 | class CommandAuthenticatorValidationPolicy final : public security::ValidationPolicy |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 72 | { |
| 73 | public: |
| 74 | void |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 75 | checkPolicy(const Interest& interest, const shared_ptr<security::ValidationState>& state, |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 76 | const ValidationContinuation& continueValidation) final |
| 77 | { |
Davide Pesavento | b83d3df | 2022-09-13 14:04:34 -0400 | [diff] [blame] | 78 | auto sigInfo = getSignatureInfo(interest, *state); |
| 79 | if (!state->getOutcome()) { // already failed |
| 80 | return; |
| 81 | } |
| 82 | Name klName = getKeyLocatorName(sigInfo, *state); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 83 | if (!state->getOutcome()) { // already failed |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // SignerTag must be placed on the 'original Interest' in ValidationState to be available for |
| 88 | // InterestValidationSuccessCallback. The 'interest' parameter refers to a different instance |
| 89 | // which is copied into 'original Interest'. |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 90 | auto state1 = std::dynamic_pointer_cast<security::InterestValidationState>(state); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 91 | state1->getOriginalInterest().setTag(make_shared<SignerTag>(klName)); |
| 92 | |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 93 | continueValidation(make_shared<security::CertificateRequest>(klName), state); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 97 | checkPolicy(const Data&, const shared_ptr<security::ValidationState>&, |
| 98 | const ValidationContinuation&) final |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 99 | { |
| 100 | // Non-certificate Data are not handled by CommandAuthenticator. |
| 101 | // Non-anchor certificates cannot be retrieved by offline fetcher. |
| 102 | BOOST_ASSERT_MSG(false, "Data should not be passed to this policy"); |
| 103 | } |
| 104 | }; |
| 105 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 106 | shared_ptr<CommandAuthenticator> |
| 107 | CommandAuthenticator::create() |
| 108 | { |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 109 | return shared_ptr<CommandAuthenticator>(new CommandAuthenticator); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 112 | CommandAuthenticator::CommandAuthenticator() = default; |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 113 | |
| 114 | void |
| 115 | CommandAuthenticator::setConfigFile(ConfigFile& configFile) |
| 116 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 117 | configFile.addSectionHandler("authorizations", [this] (auto&&... args) { |
| 118 | processConfig(std::forward<decltype(args)>(args)...); |
| 119 | }); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
| 123 | CommandAuthenticator::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename) |
| 124 | { |
| 125 | if (!isDryRun) { |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 126 | NFD_LOG_DEBUG("resetting authorizations"); |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 127 | for (auto& kv : m_validators) { |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 128 | kv.second = make_shared<security::Validator>( |
| 129 | make_unique<security::ValidationPolicyCommandInterest>(make_unique<CommandAuthenticatorValidationPolicy>()), |
| 130 | make_unique<security::CertificateFetcherOffline>()); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | if (section.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 135 | NDN_THROW(ConfigFile::Error("'authorize' is missing under 'authorizations'")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int authSectionIndex = 0; |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 139 | for (const auto& [sectionName, authSection] : section) { |
| 140 | if (sectionName != "authorize") { |
| 141 | NDN_THROW(ConfigFile::Error("'" + sectionName + "' section is not permitted under 'authorizations'")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 142 | } |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 143 | |
| 144 | std::string certfile; |
| 145 | try { |
| 146 | certfile = authSection.get<std::string>("certfile"); |
| 147 | } |
| 148 | catch (const boost::property_tree::ptree_error&) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 149 | NDN_THROW(ConfigFile::Error("'certfile' is missing under authorize[" + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 150 | std::to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool isAny = false; |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 154 | shared_ptr<security::Certificate> cert; |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 155 | if (certfile == "any") { |
| 156 | isAny = true; |
| 157 | NFD_LOG_WARN("'certfile any' is intended for demo purposes only and " |
| 158 | "SHOULD NOT be used in production environments"); |
| 159 | } |
| 160 | else { |
| 161 | using namespace boost::filesystem; |
| 162 | path certfilePath = absolute(certfile, path(filename).parent_path()); |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 163 | cert = ndn::io::load<security::Certificate>(certfilePath.string()); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 164 | if (cert == nullptr) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 165 | NDN_THROW(ConfigFile::Error("cannot load certfile " + certfilePath.string() + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 166 | " for authorize[" + std::to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | const ConfigSection* privSection = nullptr; |
| 171 | try { |
| 172 | privSection = &authSection.get_child("privileges"); |
| 173 | } |
| 174 | catch (const boost::property_tree::ptree_error&) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 175 | NDN_THROW(ConfigFile::Error("'privileges' is missing under authorize[" + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 176 | std::to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (privSection->empty()) { |
| 180 | NFD_LOG_WARN("No privileges granted to certificate " << certfile); |
| 181 | } |
| 182 | for (const auto& kv : *privSection) { |
| 183 | const std::string& module = kv.first; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 184 | auto found = m_validators.find(module); |
| 185 | if (found == m_validators.end()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 186 | NDN_THROW(ConfigFile::Error("unknown module '" + module + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 187 | "' under authorize[" + std::to_string(authSectionIndex) + "]")); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (isDryRun) { |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | if (isAny) { |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 195 | found->second = make_shared<security::Validator>(make_unique<security::ValidationPolicyAcceptAll>(), |
| 196 | make_unique<security::CertificateFetcherOffline>()); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 197 | NFD_LOG_INFO("authorize module=" << module << " signer=any"); |
| 198 | } |
| 199 | else { |
Junxiao Shi | 16a3adf | 2017-05-26 17:38:51 +0000 | [diff] [blame] | 200 | const Name& keyName = cert->getKeyName(); |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 201 | security::Certificate certCopy = *cert; |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 202 | found->second->loadAnchor(certfile, std::move(certCopy)); |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 203 | NFD_LOG_INFO("authorize module=" << module << " signer=" << keyName << " certfile=" << certfile); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | ++authSectionIndex; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | ndn::mgmt::Authorization |
| 212 | CommandAuthenticator::makeAuthorization(const std::string& module, const std::string& verb) |
| 213 | { |
Junxiao Shi | dbb6b3e | 2017-07-03 12:42:07 +0000 | [diff] [blame] | 214 | m_validators[module]; // declares module, so that privilege is recognized |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 215 | |
Davide Pesavento | b83d3df | 2022-09-13 14:04:34 -0400 | [diff] [blame] | 216 | return [module, self = shared_from_this()] (const Name&, const Interest& interest, |
| 217 | const ndn::mgmt::ControlParameters*, |
| 218 | const ndn::mgmt::AcceptContinuation& accept, |
| 219 | const ndn::mgmt::RejectContinuation& reject) { |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 220 | auto validator = self->m_validators.at(module); |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 221 | |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 222 | auto successCb = [accept, validator] (const Interest& interest1) { |
| 223 | auto signer1 = getSignerFromTag(interest1); |
| 224 | BOOST_ASSERT(signer1 || // signer must be available unless 'certfile any' |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 225 | dynamic_cast<security::ValidationPolicyAcceptAll*>(&validator->getPolicy()) != nullptr); |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 226 | std::string signer = signer1.value_or("*"); |
| 227 | NFD_LOG_DEBUG("accept " << interest1.getName() << " signer=" << signer); |
| 228 | accept(signer); |
| 229 | }; |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 230 | |
Davide Pesavento | b83d3df | 2022-09-13 14:04:34 -0400 | [diff] [blame] | 231 | using ndn::security::ValidationError; |
| 232 | auto failureCb = [reject] (const Interest& interest1, const ValidationError& err) { |
| 233 | auto reply = ndn::mgmt::RejectReply::STATUS403; |
| 234 | if (err.getCode() == ValidationError::MALFORMED_SIGNATURE || |
| 235 | err.getCode() == ValidationError::INVALID_KEY_LOCATOR) { |
| 236 | // do not waste cycles signing and sending a reply if the command is clearly malformed |
| 237 | reply = ndn::mgmt::RejectReply::SILENT; |
Davide Pesavento | d96744d | 2018-02-03 19:16:07 -0500 | [diff] [blame] | 238 | } |
| 239 | NFD_LOG_DEBUG("reject " << interest1.getName() << " signer=" << |
| 240 | getSignerFromTag(interest1).value_or("?") << " reason=" << err); |
| 241 | reject(reply); |
| 242 | }; |
| 243 | |
| 244 | if (validator) { |
| 245 | validator->validate(interest, successCb, failureCb); |
| 246 | } |
| 247 | else { |
| 248 | NFD_LOG_DEBUG("reject " << interest.getName() << " signer=" << |
| 249 | getSignerFromTag(interest).value_or("?") << " reason=Unauthorized"); |
| 250 | reject(ndn::mgmt::RejectReply::STATUS403); |
| 251 | } |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 252 | }; |
| 253 | } |
| 254 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 255 | } // namespace nfd |