Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/validator.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/util/logger.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 27 | namespace ndn { |
| 28 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 29 | inline namespace v2 { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 31 | NDN_LOG_INIT(ndn.security.Validator); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 32 | |
| 33 | #define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x) |
| 34 | #define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x) |
| 35 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 36 | Validator::Validator(unique_ptr<ValidationPolicy> policy, unique_ptr<CertificateFetcher> certFetcher) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 37 | : m_policy(std::move(policy)) |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 38 | , m_certFetcher(std::move(certFetcher)) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 40 | BOOST_ASSERT(m_policy != nullptr); |
| 41 | BOOST_ASSERT(m_certFetcher != nullptr); |
Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 42 | m_policy->setValidator(*this); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 43 | m_certFetcher->setCertificateStorage(*this); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 46 | Validator::~Validator() noexcept = default; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 47 | |
| 48 | void |
| 49 | Validator::validate(const Data& data, |
| 50 | const DataValidationSuccessCallback& successCb, |
| 51 | const DataValidationFailureCallback& failureCb) |
| 52 | { |
| 53 | auto state = make_shared<DataValidationState>(data, successCb, failureCb); |
| 54 | NDN_LOG_DEBUG_DEPTH("Start validating data " << data.getName()); |
| 55 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 56 | m_policy->checkPolicy(data, state, [this] (auto&&... args) { |
| 57 | continueValidation(std::forward<decltype(args)>(args)...); |
| 58 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | Validator::validate(const Interest& interest, |
| 63 | const InterestValidationSuccessCallback& successCb, |
| 64 | const InterestValidationFailureCallback& failureCb) |
| 65 | { |
| 66 | auto state = make_shared<InterestValidationState>(interest, successCb, failureCb); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 67 | NDN_LOG_DEBUG_DEPTH("Start validating interest " << interest.getName()); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 68 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 69 | try { |
| 70 | auto fmt = interest.getSignatureInfo() ? SignedInterestFormat::V03 : SignedInterestFormat::V02; |
| 71 | state->setTag(make_shared<SignedInterestFormatTag>(fmt)); |
| 72 | } |
| 73 | catch (const tlv::Error& e) { |
Davide Pesavento | 637ea3b | 2022-09-10 00:11:34 -0400 | [diff] [blame] | 74 | return state->fail({ValidationError::MALFORMED_SIGNATURE, "Malformed InterestSignatureInfo in `" + |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 75 | interest.getName().toUri() + "`: " + e.what()}); |
| 76 | } |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 77 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 78 | m_policy->checkPolicy(interest, state, [this] (auto&&... args) { |
| 79 | continueValidation(std::forward<decltype(args)>(args)...); |
| 80 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | Validator::validate(const Certificate& cert, const shared_ptr<ValidationState>& state) |
| 85 | { |
| 86 | NDN_LOG_DEBUG_DEPTH("Start validating certificate " << cert.getName()); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 87 | |
| 88 | if (!cert.isValid()) { |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 89 | return state->fail({ValidationError::EXPIRED_CERT, "`" + cert.getName().toUri() + "` is valid " |
| 90 | "between " + boost::lexical_cast<std::string>(cert.getValidityPeriod())}); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 93 | m_policy->checkPolicy(cert, state, |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 94 | [this, cert] (const shared_ptr<CertificateRequest>& certRequest, const shared_ptr<ValidationState>& state) { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 95 | if (certRequest == nullptr) { |
| 96 | state->fail({ValidationError::POLICY_ERROR, "Validation policy is not allowed to designate `" + |
| 97 | cert.getName().toUri() + "` as a trust anchor"}); |
| 98 | } |
| 99 | else { |
| 100 | // need to fetch key and validate it |
| 101 | state->addCertificate(cert); |
| 102 | requestCertificate(certRequest, state); |
| 103 | } |
| 104 | }); |
| 105 | } |
| 106 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 107 | void |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 108 | Validator::continueValidation(const shared_ptr<CertificateRequest>& certRequest, |
| 109 | const shared_ptr<ValidationState>& state) |
| 110 | { |
| 111 | BOOST_ASSERT(state); |
| 112 | |
| 113 | if (certRequest == nullptr) { |
| 114 | state->bypassValidation(); |
| 115 | } |
| 116 | else { |
| 117 | // need to fetch key and validate it |
| 118 | requestCertificate(certRequest, state); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 123 | Validator::requestCertificate(const shared_ptr<CertificateRequest>& certRequest, |
| 124 | const shared_ptr<ValidationState>& state) |
| 125 | { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 126 | if (state->getDepth() >= m_maxDepth) { |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 127 | state->fail({ValidationError::EXCEEDED_DEPTH_LIMIT, to_string(m_maxDepth)}); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 131 | if (certRequest->interest.getName() == SigningInfo::getDigestSha256Identity()) { |
| 132 | state->verifyOriginalPacket(nullopt); |
| 133 | return; |
| 134 | } |
| 135 | |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 136 | if (state->hasSeenCertificateName(certRequest->interest.getName())) { |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 137 | state->fail({ValidationError::LOOP_DETECTED, certRequest->interest.getName().toUri()}); |
Alexander Afanasyev | 5af04a7 | 2017-01-30 22:41:23 -0800 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 141 | NDN_LOG_DEBUG_DEPTH("Retrieving " << certRequest->interest.getName()); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 142 | |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 143 | auto cert = findTrustedCert(certRequest->interest); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 144 | if (cert != nullptr) { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 145 | NDN_LOG_TRACE_DEPTH("Found trusted certificate " << cert->getName()); |
| 146 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 147 | cert = state->verifyCertificateChain(*cert); |
| 148 | if (cert != nullptr) { |
| 149 | state->verifyOriginalPacket(*cert); |
| 150 | } |
| 151 | for (auto trustedCert = std::make_move_iterator(state->m_certificateChain.begin()); |
| 152 | trustedCert != std::make_move_iterator(state->m_certificateChain.end()); |
| 153 | ++trustedCert) { |
| 154 | cacheVerifiedCertificate(*trustedCert); |
| 155 | } |
| 156 | return; |
| 157 | } |
| 158 | |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 159 | m_certFetcher->fetch(certRequest, state, [this] (auto&&... args) { |
| 160 | validate(std::forward<decltype(args)>(args)...); |
| 161 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | //////////////////////////////////////////////////////////////////////// |
| 165 | // Trust anchor management |
| 166 | //////////////////////////////////////////////////////////////////////// |
| 167 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 168 | // to change visibility from protected to public |
| 169 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 170 | void |
| 171 | Validator::loadAnchor(const std::string& groupId, Certificate&& cert) |
| 172 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 173 | CertificateStorage::loadAnchor(groupId, std::move(cert)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void |
| 177 | Validator::loadAnchor(const std::string& groupId, const std::string& certfilePath, |
| 178 | time::nanoseconds refreshPeriod, bool isDir) |
| 179 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 180 | CertificateStorage::loadAnchor(groupId, certfilePath, refreshPeriod, isDir); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 184 | Validator::resetAnchors() |
| 185 | { |
| 186 | CertificateStorage::resetAnchors(); |
| 187 | } |
| 188 | |
| 189 | void |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 190 | Validator::cacheVerifiedCertificate(Certificate&& cert) |
| 191 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 192 | CertificateStorage::cacheVerifiedCert(std::move(cert)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 195 | void |
| 196 | Validator::resetVerifiedCertificates() |
| 197 | { |
| 198 | CertificateStorage::resetVerifiedCerts(); |
| 199 | } |
| 200 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 201 | } // inline namespace v2 |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 202 | } // namespace security |
| 203 | } // namespace ndn |