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 | /* |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/face.hpp" |
| 25 | #include "ndn-cxx/security/transform/public-key.hpp" |
| 26 | #include "ndn-cxx/util/logger.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 30 | inline namespace v2 { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 32 | NDN_LOG_INIT(ndn.security.Validator); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 33 | |
| 34 | #define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x) |
| 35 | #define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x) |
| 36 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 37 | Validator::Validator(unique_ptr<ValidationPolicy> policy, unique_ptr<CertificateFetcher> certFetcher) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 38 | : m_policy(std::move(policy)) |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 39 | , m_certFetcher(std::move(certFetcher)) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 40 | , m_maxDepth(25) |
| 41 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 42 | BOOST_ASSERT(m_policy != nullptr); |
| 43 | BOOST_ASSERT(m_certFetcher != nullptr); |
Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 44 | m_policy->setValidator(*this); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 45 | m_certFetcher->setCertificateStorage(*this); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Validator::~Validator() = default; |
| 49 | |
Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 50 | ValidationPolicy& |
| 51 | Validator::getPolicy() |
| 52 | { |
| 53 | return *m_policy; |
| 54 | } |
| 55 | |
| 56 | CertificateFetcher& |
| 57 | Validator::getFetcher() |
| 58 | { |
| 59 | return *m_certFetcher; |
| 60 | } |
| 61 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 62 | void |
| 63 | Validator::setMaxDepth(size_t depth) |
| 64 | { |
| 65 | m_maxDepth = depth; |
| 66 | } |
| 67 | |
| 68 | size_t |
| 69 | Validator::getMaxDepth() const |
| 70 | { |
| 71 | return m_maxDepth; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | Validator::validate(const Data& data, |
| 76 | const DataValidationSuccessCallback& successCb, |
| 77 | const DataValidationFailureCallback& failureCb) |
| 78 | { |
| 79 | auto state = make_shared<DataValidationState>(data, successCb, failureCb); |
| 80 | NDN_LOG_DEBUG_DEPTH("Start validating data " << data.getName()); |
| 81 | |
| 82 | m_policy->checkPolicy(data, state, |
| 83 | [this] (const shared_ptr<CertificateRequest>& certRequest, const shared_ptr<ValidationState>& state) { |
| 84 | if (certRequest == nullptr) { |
| 85 | state->bypassValidation(); |
| 86 | } |
| 87 | else { |
| 88 | // need to fetch key and validate it |
| 89 | requestCertificate(certRequest, state); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | Validator::validate(const Interest& interest, |
| 96 | const InterestValidationSuccessCallback& successCb, |
| 97 | const InterestValidationFailureCallback& failureCb) |
| 98 | { |
| 99 | auto state = make_shared<InterestValidationState>(interest, successCb, failureCb); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 100 | |
| 101 | auto fmt = interest.getSignatureInfo() ? SignedInterestFormat::V03 : SignedInterestFormat::V02; |
| 102 | state->setTag(make_shared<SignedInterestFormatTag>(fmt)); |
| 103 | |
| 104 | NDN_LOG_DEBUG_DEPTH("Start validating interest (" << fmt << ") " << interest.getName()); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 105 | |
| 106 | m_policy->checkPolicy(interest, state, |
| 107 | [this] (const shared_ptr<CertificateRequest>& certRequest, const shared_ptr<ValidationState>& state) { |
| 108 | if (certRequest == nullptr) { |
| 109 | state->bypassValidation(); |
| 110 | } |
| 111 | else { |
| 112 | // need to fetch key and validate it |
| 113 | requestCertificate(certRequest, state); |
| 114 | } |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | Validator::validate(const Certificate& cert, const shared_ptr<ValidationState>& state) |
| 120 | { |
| 121 | NDN_LOG_DEBUG_DEPTH("Start validating certificate " << cert.getName()); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 122 | |
| 123 | if (!cert.isValid()) { |
| 124 | return state->fail({ValidationError::Code::EXPIRED_CERT, "Retrieved certificate is not yet valid or expired " |
| 125 | "`" + cert.getName().toUri() + "`"}); |
| 126 | } |
| 127 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 128 | m_policy->checkPolicy(cert, state, |
| 129 | [this, cert] (const shared_ptr<CertificateRequest>& certRequest, const shared_ptr<ValidationState>& state) { |
| 130 | if (certRequest == nullptr) { |
| 131 | state->fail({ValidationError::POLICY_ERROR, "Validation policy is not allowed to designate `" + |
| 132 | cert.getName().toUri() + "` as a trust anchor"}); |
| 133 | } |
| 134 | else { |
| 135 | // need to fetch key and validate it |
| 136 | state->addCertificate(cert); |
| 137 | requestCertificate(certRequest, state); |
| 138 | } |
| 139 | }); |
| 140 | } |
| 141 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 142 | void |
| 143 | Validator::requestCertificate(const shared_ptr<CertificateRequest>& certRequest, |
| 144 | const shared_ptr<ValidationState>& state) |
| 145 | { |
| 146 | // TODO configurable check for the maximum number of steps |
| 147 | if (state->getDepth() >= m_maxDepth) { |
| 148 | state->fail({ValidationError::Code::EXCEEDED_DEPTH_LIMIT, |
| 149 | "Exceeded validation depth limit (" + to_string(m_maxDepth) + ")"}); |
| 150 | return; |
| 151 | } |
| 152 | |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 153 | if (state->hasSeenCertificateName(certRequest->interest.getName())) { |
Alexander Afanasyev | 5af04a7 | 2017-01-30 22:41:23 -0800 | [diff] [blame] | 154 | state->fail({ValidationError::Code::LOOP_DETECTED, |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 155 | "Validation loop detected for certificate `" + certRequest->interest.getName().toUri() + "`"}); |
Alexander Afanasyev | 5af04a7 | 2017-01-30 22:41:23 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 159 | NDN_LOG_DEBUG_DEPTH("Retrieving " << certRequest->interest.getName()); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 160 | |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 161 | auto cert = findTrustedCert(certRequest->interest); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 162 | if (cert != nullptr) { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 163 | NDN_LOG_TRACE_DEPTH("Found trusted certificate " << cert->getName()); |
| 164 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 165 | cert = state->verifyCertificateChain(*cert); |
| 166 | if (cert != nullptr) { |
| 167 | state->verifyOriginalPacket(*cert); |
| 168 | } |
| 169 | for (auto trustedCert = std::make_move_iterator(state->m_certificateChain.begin()); |
| 170 | trustedCert != std::make_move_iterator(state->m_certificateChain.end()); |
| 171 | ++trustedCert) { |
| 172 | cacheVerifiedCertificate(*trustedCert); |
| 173 | } |
| 174 | return; |
| 175 | } |
| 176 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 177 | m_certFetcher->fetch(certRequest, state, [this] (const Certificate& cert, const shared_ptr<ValidationState>& state) { |
| 178 | validate(cert, state); |
| 179 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | //////////////////////////////////////////////////////////////////////// |
| 183 | // Trust anchor management |
| 184 | //////////////////////////////////////////////////////////////////////// |
| 185 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 186 | // to change visibility from protected to public |
| 187 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 188 | void |
| 189 | Validator::loadAnchor(const std::string& groupId, Certificate&& cert) |
| 190 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 191 | CertificateStorage::loadAnchor(groupId, std::move(cert)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
| 195 | Validator::loadAnchor(const std::string& groupId, const std::string& certfilePath, |
| 196 | time::nanoseconds refreshPeriod, bool isDir) |
| 197 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 198 | CertificateStorage::loadAnchor(groupId, certfilePath, refreshPeriod, isDir); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 202 | Validator::resetAnchors() |
| 203 | { |
| 204 | CertificateStorage::resetAnchors(); |
| 205 | } |
| 206 | |
| 207 | void |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 208 | Validator::cacheVerifiedCertificate(Certificate&& cert) |
| 209 | { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 210 | CertificateStorage::cacheVerifiedCert(std::move(cert)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 213 | void |
| 214 | Validator::resetVerifiedCertificates() |
| 215 | { |
| 216 | CertificateStorage::resetVerifiedCerts(); |
| 217 | } |
| 218 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 219 | } // inline namespace v2 |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 220 | } // namespace security |
| 221 | } // namespace ndn |