blob: d9dfba0a50beea1cbcd060690cb47e08430355be [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>
Junxiao Shid7631272016-08-17 04:16:31 +000035#include <ndn-cxx/util/io.hpp>
36
37#include <boost/filesystem.hpp>
38
Alexander Afanasyeva1583702020-06-03 13:55:45 -040039namespace security = ndn::security;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000040
Junxiao Shid7631272016-08-17 04:16:31 +000041namespace nfd {
42
Davide Pesaventoa3148082018-04-12 18:21:54 -040043NFD_LOG_INIT(CommandAuthenticator);
Junxiao Shid7631272016-08-17 04:16:31 +000044// INFO: configuration change, etc
45// DEBUG: per authentication request result
46
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000047/** \brief an Interest tag to indicate command signer
48 */
49using SignerTag = ndn::SimpleTag<Name, 20>;
50
51/** \brief obtain signer from SignerTag attached to Interest, if available
52 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040053static std::optional<std::string>
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000054getSignerFromTag(const Interest& interest)
55{
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040056 auto signerTag = interest.getTag<SignerTag>();
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000057 if (signerTag == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040058 return std::nullopt;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000059 }
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 Pesavento3db98072021-03-09 23:03:27 -050067class CommandAuthenticatorValidationPolicy final : public security::ValidationPolicy
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000068{
69public:
70 void
Alexander Afanasyeva1583702020-06-03 13:55:45 -040071 checkPolicy(const Interest& interest, const shared_ptr<security::ValidationState>& state,
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000072 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 Afanasyeva1583702020-06-03 13:55:45 -040082 auto state1 = dynamic_pointer_cast<security::InterestValidationState>(state);
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000083 state1->getOriginalInterest().setTag(make_shared<SignerTag>(klName));
84
Davide Pesavento22085362021-03-18 22:08:03 -040085 continueValidation(make_shared<security::CertificateRequest>(klName), state);
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000086 }
87
88 void
Davide Pesavento3db98072021-03-09 23:03:27 -050089 checkPolicy(const Data&, const shared_ptr<security::ValidationState>&,
90 const ValidationContinuation&) final
Junxiao Shidbb6b3e2017-07-03 12:42:07 +000091 {
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 Shid7631272016-08-17 04:16:31 +000098shared_ptr<CommandAuthenticator>
99CommandAuthenticator::create()
100{
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000101 return shared_ptr<CommandAuthenticator>(new CommandAuthenticator);
Junxiao Shid7631272016-08-17 04:16:31 +0000102}
103
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000104CommandAuthenticator::CommandAuthenticator() = default;
Junxiao Shid7631272016-08-17 04:16:31 +0000105
106void
107CommandAuthenticator::setConfigFile(ConfigFile& configFile)
108{
Davide Pesavento412c9822021-07-02 00:21:05 -0400109 configFile.addSectionHandler("authorizations", [this] (auto&&... args) {
110 processConfig(std::forward<decltype(args)>(args)...);
111 });
Junxiao Shid7631272016-08-17 04:16:31 +0000112}
113
114void
115CommandAuthenticator::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
116{
117 if (!isDryRun) {
Davide Pesavento22085362021-03-18 22:08:03 -0400118 NFD_LOG_DEBUG("resetting authorizations");
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000119 for (auto& kv : m_validators) {
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400120 kv.second = make_shared<security::Validator>(
121 make_unique<security::ValidationPolicyCommandInterest>(make_unique<CommandAuthenticatorValidationPolicy>()),
122 make_unique<security::CertificateFetcherOffline>());
Junxiao Shid7631272016-08-17 04:16:31 +0000123 }
124 }
125
126 if (section.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500127 NDN_THROW(ConfigFile::Error("'authorize' is missing under 'authorizations'"));
Junxiao Shid7631272016-08-17 04:16:31 +0000128 }
129
130 int authSectionIndex = 0;
131 for (const auto& kv : section) {
132 if (kv.first != "authorize") {
Davide Pesavento19779d82019-02-14 13:40:04 -0500133 NDN_THROW(ConfigFile::Error("'" + kv.first + "' section is not permitted under 'authorizations'"));
Junxiao Shid7631272016-08-17 04:16:31 +0000134 }
135 const ConfigSection& authSection = kv.second;
136
137 std::string certfile;
138 try {
139 certfile = authSection.get<std::string>("certfile");
140 }
141 catch (const boost::property_tree::ptree_error&) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500142 NDN_THROW(ConfigFile::Error("'certfile' is missing under authorize[" +
143 to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000144 }
145
146 bool isAny = false;
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400147 shared_ptr<security::Certificate> cert;
Junxiao Shid7631272016-08-17 04:16:31 +0000148 if (certfile == "any") {
149 isAny = true;
150 NFD_LOG_WARN("'certfile any' is intended for demo purposes only and "
151 "SHOULD NOT be used in production environments");
152 }
153 else {
154 using namespace boost::filesystem;
155 path certfilePath = absolute(certfile, path(filename).parent_path());
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400156 cert = ndn::io::load<security::Certificate>(certfilePath.string());
Junxiao Shid7631272016-08-17 04:16:31 +0000157 if (cert == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500158 NDN_THROW(ConfigFile::Error("cannot load certfile " + certfilePath.string() +
159 " for authorize[" + to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000160 }
161 }
162
163 const ConfigSection* privSection = nullptr;
164 try {
165 privSection = &authSection.get_child("privileges");
166 }
167 catch (const boost::property_tree::ptree_error&) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500168 NDN_THROW(ConfigFile::Error("'privileges' is missing under authorize[" +
169 to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000170 }
171
172 if (privSection->empty()) {
173 NFD_LOG_WARN("No privileges granted to certificate " << certfile);
174 }
175 for (const auto& kv : *privSection) {
176 const std::string& module = kv.first;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000177 auto found = m_validators.find(module);
178 if (found == m_validators.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500179 NDN_THROW(ConfigFile::Error("unknown module '" + module +
180 "' under authorize[" + to_string(authSectionIndex) + "]"));
Junxiao Shid7631272016-08-17 04:16:31 +0000181 }
182
183 if (isDryRun) {
184 continue;
185 }
186
187 if (isAny) {
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400188 found->second = make_shared<security::Validator>(make_unique<security::ValidationPolicyAcceptAll>(),
189 make_unique<security::CertificateFetcherOffline>());
Junxiao Shid7631272016-08-17 04:16:31 +0000190 NFD_LOG_INFO("authorize module=" << module << " signer=any");
191 }
192 else {
Junxiao Shi16a3adf2017-05-26 17:38:51 +0000193 const Name& keyName = cert->getKeyName();
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400194 security::Certificate certCopy = *cert;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000195 found->second->loadAnchor(certfile, std::move(certCopy));
Davide Pesavento19779d82019-02-14 13:40:04 -0500196 NFD_LOG_INFO("authorize module=" << module << " signer=" << keyName << " certfile=" << certfile);
Junxiao Shid7631272016-08-17 04:16:31 +0000197 }
198 }
199
200 ++authSectionIndex;
201 }
202}
203
204ndn::mgmt::Authorization
205CommandAuthenticator::makeAuthorization(const std::string& module, const std::string& verb)
206{
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000207 m_validators[module]; // declares module, so that privilege is recognized
Junxiao Shid7631272016-08-17 04:16:31 +0000208
209 auto self = this->shared_from_this();
Davide Pesaventod96744d2018-02-03 19:16:07 -0500210 return [=] (const Name&, const Interest& interest,
211 const ndn::mgmt::ControlParameters*,
Junxiao Shid7631272016-08-17 04:16:31 +0000212 const ndn::mgmt::AcceptContinuation& accept,
213 const ndn::mgmt::RejectContinuation& reject) {
Davide Pesaventod96744d2018-02-03 19:16:07 -0500214 auto validator = self->m_validators.at(module);
Davide Pesavento22085362021-03-18 22:08:03 -0400215
Davide Pesaventod96744d2018-02-03 19:16:07 -0500216 auto successCb = [accept, validator] (const Interest& interest1) {
217 auto signer1 = getSignerFromTag(interest1);
218 BOOST_ASSERT(signer1 || // signer must be available unless 'certfile any'
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400219 dynamic_cast<security::ValidationPolicyAcceptAll*>(&validator->getPolicy()) != nullptr);
Davide Pesaventod96744d2018-02-03 19:16:07 -0500220 std::string signer = signer1.value_or("*");
221 NFD_LOG_DEBUG("accept " << interest1.getName() << " signer=" << signer);
222 accept(signer);
223 };
Davide Pesavento22085362021-03-18 22:08:03 -0400224
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400225 auto failureCb = [reject] (const Interest& interest1, const security::ValidationError& err) {
Davide Pesaventod96744d2018-02-03 19:16:07 -0500226 using ndn::mgmt::RejectReply;
227 RejectReply reply = RejectReply::STATUS403;
228 switch (err.getCode()) {
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400229 case security::ValidationError::NO_SIGNATURE:
230 case security::ValidationError::INVALID_KEY_LOCATOR:
Davide Pesaventod96744d2018-02-03 19:16:07 -0500231 reply = RejectReply::SILENT;
232 break;
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400233 case security::ValidationError::POLICY_ERROR:
Davide Pesaventod96744d2018-02-03 19:16:07 -0500234 if (interest1.getName().size() < ndn::command_interest::MIN_SIZE) { // "name too short"
235 reply = RejectReply::SILENT;
Junxiao Shidbb6b3e2017-07-03 12:42:07 +0000236 }
Davide Pesaventod96744d2018-02-03 19:16:07 -0500237 break;
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 Shid7631272016-08-17 04:16:31 +0000252 };
253}
254
Junxiao Shid7631272016-08-17 04:16:31 +0000255} // namespace nfd