blob: ebf79919fd519afe6ae16bc0d94ff96886eca08d [file] [log] [blame]
Junxiao Shid7631272016-08-17 04:16:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod96744d2018-02-03 19:16:07 -05002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shid7631272016-08-17 04:16:31 +00004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/logger.hpp"
Junxiao Shid7631272016-08-17 04:16:31 +000028
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000029#include <ndn-cxx/tag.hpp>
Alexander Afanasyeva1583702020-06-03 13:55:45 -040030#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>
35#include <ndn-cxx/security/validator.hpp>
Junxiao Shid7631272016-08-17 04:16:31 +000036#include <ndn-cxx/util/io.hpp>
37
38#include <boost/filesystem.hpp>
39
Alexander Afanasyeva1583702020-06-03 13:55:45 -040040namespace security = ndn::security;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000041
Junxiao Shid7631272016-08-17 04:16:31 +000042namespace nfd {
43
Davide Pesaventoa3148082018-04-12 18:21:54 -040044NFD_LOG_INIT(CommandAuthenticator);
Junxiao Shid7631272016-08-17 04:16:31 +000045// INFO: configuration change, etc
46// DEBUG: per authentication request result
47
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000048/** \brief an Interest tag to indicate command signer
49 */
50using SignerTag = ndn::SimpleTag<Name, 20>;
51
52/** \brief obtain signer from SignerTag attached to Interest, if available
53 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040054static std::optional<std::string>
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000055getSignerFromTag(const Interest& interest)
56{
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040057 auto signerTag = interest.getTag<SignerTag>();
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000058 if (signerTag == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040059 return std::nullopt;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000060 }
61 else {
62 return signerTag->get().toUri();
63 }
64}
65
66/** \brief a validation policy that only permits Interest signed by a trust anchor
67 */
Davide Pesavento3db98072021-03-09 23:03:27 -050068class CommandAuthenticatorValidationPolicy final : public security::ValidationPolicy
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000069{
70public:
71 void
Alexander Afanasyeva1583702020-06-03 13:55:45 -040072 checkPolicy(const Interest& interest, const shared_ptr<security::ValidationState>& state,
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000073 const ValidationContinuation& continueValidation) final
74 {
75 Name klName = getKeyLocatorName(interest, *state);
76 if (!state->getOutcome()) { // already failed
77 return;
78 }
79
80 // SignerTag must be placed on the 'original Interest' in ValidationState to be available for
81 // InterestValidationSuccessCallback. The 'interest' parameter refers to a different instance
82 // which is copied into 'original Interest'.
Alexander Afanasyeva1583702020-06-03 13:55:45 -040083 auto state1 = dynamic_pointer_cast<security::InterestValidationState>(state);
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000084 state1->getOriginalInterest().setTag(make_shared<SignerTag>(klName));
85
Davide Pesavento22085362021-03-18 22:08:03 -040086 continueValidation(make_shared<security::CertificateRequest>(klName), state);
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000087 }
88
89 void
Davide Pesavento3db98072021-03-09 23:03:27 -050090 checkPolicy(const Data&, const shared_ptr<security::ValidationState>&,
91 const ValidationContinuation&) final
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000092 {
93 // Non-certificate Data are not handled by CommandAuthenticator.
94 // Non-anchor certificates cannot be retrieved by offline fetcher.
95 BOOST_ASSERT_MSG(false, "Data should not be passed to this policy");
96 }
97};
98
Junxiao Shid7631272016-08-17 04:16:31 +000099shared_ptr<CommandAuthenticator>
100CommandAuthenticator::create()
101{
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000102 return shared_ptr<CommandAuthenticator>(new CommandAuthenticator);
Junxiao Shid7631272016-08-17 04:16:31 +0000103}
104
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000105CommandAuthenticator::CommandAuthenticator() = default;
Junxiao Shid7631272016-08-17 04:16:31 +0000106
107void
108CommandAuthenticator::setConfigFile(ConfigFile& configFile)
109{
Davide Pesavento412c9822021-07-02 00:21:05 -0400110 configFile.addSectionHandler("authorizations", [this] (auto&&... args) {
111 processConfig(std::forward<decltype(args)>(args)...);
112 });
Junxiao Shid7631272016-08-17 04:16:31 +0000113}
114
115void
116CommandAuthenticator::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
117{
118 if (!isDryRun) {
Davide Pesavento22085362021-03-18 22:08:03 -0400119 NFD_LOG_DEBUG("resetting authorizations");
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000120 for (auto& kv : m_validators) {
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400121 kv.second = make_shared<security::Validator>(
122 make_unique<security::ValidationPolicyCommandInterest>(make_unique<CommandAuthenticatorValidationPolicy>()),
123 make_unique<security::CertificateFetcherOffline>());
Junxiao Shid7631272016-08-17 04:16:31 +0000124 }
125 }
126
127 if (section.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500128 NDN_THROW(ConfigFile::Error("'authorize' is missing under 'authorizations'"));
Junxiao Shid7631272016-08-17 04:16:31 +0000129 }
130
131 int authSectionIndex = 0;
132 for (const auto& kv : section) {
133 if (kv.first != "authorize") {
Davide Pesavento19779d82019-02-14 13:40:04 -0500134 NDN_THROW(ConfigFile::Error("'" + kv.first + "' section is not permitted under 'authorizations'"));
Junxiao Shid7631272016-08-17 04:16:31 +0000135 }
136 const ConfigSection& authSection = kv.second;
137
138 std::string certfile;
139 try {
140 certfile = authSection.get<std::string>("certfile");
141 }
142 catch (const boost::property_tree::ptree_error&) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500143 NDN_THROW(ConfigFile::Error("'certfile' is missing under authorize[" +
144 to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000145 }
146
147 bool isAny = false;
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400148 shared_ptr<security::Certificate> cert;
Junxiao Shid7631272016-08-17 04:16:31 +0000149 if (certfile == "any") {
150 isAny = true;
151 NFD_LOG_WARN("'certfile any' is intended for demo purposes only and "
152 "SHOULD NOT be used in production environments");
153 }
154 else {
155 using namespace boost::filesystem;
156 path certfilePath = absolute(certfile, path(filename).parent_path());
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400157 cert = ndn::io::load<security::Certificate>(certfilePath.string());
Junxiao Shid7631272016-08-17 04:16:31 +0000158 if (cert == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500159 NDN_THROW(ConfigFile::Error("cannot load certfile " + certfilePath.string() +
160 " for authorize[" + to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000161 }
162 }
163
164 const ConfigSection* privSection = nullptr;
165 try {
166 privSection = &authSection.get_child("privileges");
167 }
168 catch (const boost::property_tree::ptree_error&) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500169 NDN_THROW(ConfigFile::Error("'privileges' is missing under authorize[" +
170 to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000171 }
172
173 if (privSection->empty()) {
174 NFD_LOG_WARN("No privileges granted to certificate " << certfile);
175 }
176 for (const auto& kv : *privSection) {
177 const std::string& module = kv.first;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000178 auto found = m_validators.find(module);
179 if (found == m_validators.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500180 NDN_THROW(ConfigFile::Error("unknown module '" + module +
181 "' under authorize[" + to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000182 }
183
184 if (isDryRun) {
185 continue;
186 }
187
188 if (isAny) {
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400189 found->second = make_shared<security::Validator>(make_unique<security::ValidationPolicyAcceptAll>(),
190 make_unique<security::CertificateFetcherOffline>());
Junxiao Shid7631272016-08-17 04:16:31 +0000191 NFD_LOG_INFO("authorize module=" << module << " signer=any");
192 }
193 else {
Junxiao Shi16a3adf2017-05-26 17:38:51 +0000194 const Name& keyName = cert->getKeyName();
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400195 security::Certificate certCopy = *cert;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000196 found->second->loadAnchor(certfile, std::move(certCopy));
Davide Pesavento19779d82019-02-14 13:40:04 -0500197 NFD_LOG_INFO("authorize module=" << module << " signer=" << keyName << " certfile=" << certfile);
Junxiao Shid7631272016-08-17 04:16:31 +0000198 }
199 }
200
201 ++authSectionIndex;
202 }
203}
204
205ndn::mgmt::Authorization
206CommandAuthenticator::makeAuthorization(const std::string& module, const std::string& verb)
207{
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000208 m_validators[module]; // declares module, so that privilege is recognized
Junxiao Shid7631272016-08-17 04:16:31 +0000209
210 auto self = this->shared_from_this();
Davide Pesaventod96744d2018-02-03 19:16:07 -0500211 return [=] (const Name&, const Interest& interest,
212 const ndn::mgmt::ControlParameters*,
Junxiao Shid7631272016-08-17 04:16:31 +0000213 const ndn::mgmt::AcceptContinuation& accept,
214 const ndn::mgmt::RejectContinuation& reject) {
Davide Pesaventod96744d2018-02-03 19:16:07 -0500215 auto validator = self->m_validators.at(module);
Davide Pesavento22085362021-03-18 22:08:03 -0400216
Davide Pesaventod96744d2018-02-03 19:16:07 -0500217 auto successCb = [accept, validator] (const Interest& interest1) {
218 auto signer1 = getSignerFromTag(interest1);
219 BOOST_ASSERT(signer1 || // signer must be available unless 'certfile any'
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400220 dynamic_cast<security::ValidationPolicyAcceptAll*>(&validator->getPolicy()) != nullptr);
Davide Pesaventod96744d2018-02-03 19:16:07 -0500221 std::string signer = signer1.value_or("*");
222 NFD_LOG_DEBUG("accept " << interest1.getName() << " signer=" << signer);
223 accept(signer);
224 };
Davide Pesavento22085362021-03-18 22:08:03 -0400225
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400226 auto failureCb = [reject] (const Interest& interest1, const security::ValidationError& err) {
Davide Pesaventod96744d2018-02-03 19:16:07 -0500227 using ndn::mgmt::RejectReply;
228 RejectReply reply = RejectReply::STATUS403;
229 switch (err.getCode()) {
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400230 case security::ValidationError::NO_SIGNATURE:
231 case security::ValidationError::INVALID_KEY_LOCATOR:
Davide Pesaventod96744d2018-02-03 19:16:07 -0500232 reply = RejectReply::SILENT;
233 break;
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400234 case security::ValidationError::POLICY_ERROR:
Davide Pesaventod96744d2018-02-03 19:16:07 -0500235 if (interest1.getName().size() < ndn::command_interest::MIN_SIZE) { // "name too short"
236 reply = RejectReply::SILENT;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000237 }
Davide Pesaventod96744d2018-02-03 19:16:07 -0500238 break;
239 }
240 NFD_LOG_DEBUG("reject " << interest1.getName() << " signer=" <<
241 getSignerFromTag(interest1).value_or("?") << " reason=" << err);
242 reject(reply);
243 };
244
245 if (validator) {
246 validator->validate(interest, successCb, failureCb);
247 }
248 else {
249 NFD_LOG_DEBUG("reject " << interest.getName() << " signer=" <<
250 getSignerFromTag(interest).value_or("?") << " reason=Unauthorized");
251 reject(ndn::mgmt::RejectReply::STATUS403);
252 }
Junxiao Shid7631272016-08-17 04:16:31 +0000253 };
254}
255
Junxiao Shid7631272016-08-17 04:16:31 +0000256} // namespace nfd