Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "certificate-fetcher-ndns-cert.hpp" |
| 21 | #include "clients/iterative-query-controller.hpp" |
| 22 | #include "clients/response.hpp" |
| 23 | #include "logger.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/tlv.hpp> |
| 26 | #include <ndn-cxx/ims/in-memory-storage-fifo.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndns { |
| 30 | |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 31 | using security::Certificate; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 33 | NDNS_LOG_INIT(CertificateFetcherNdnsCert); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 34 | |
| 35 | CertificateFetcherNdnsCert::CertificateFetcherNdnsCert(Face& face, |
| 36 | size_t nsCacheSize, |
| 37 | size_t startComponentIndex) |
| 38 | : m_face(face) |
| 39 | , m_nsCache(make_unique<InMemoryStorageFifo>(nsCacheSize)) |
| 40 | , m_startComponentIndex(startComponentIndex) |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 41 | { |
| 42 | } |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 43 | |
| 44 | void |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 45 | CertificateFetcherNdnsCert::doFetch(const shared_ptr<security::CertificateRequest>& certRequest, |
| 46 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 47 | const ValidationContinuation& continueValidation) |
| 48 | { |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 49 | const Name& key = certRequest->interest.getName(); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 50 | Name domain = calculateDomain(key); |
| 51 | if (domain.size() == m_startComponentIndex) { |
| 52 | // NS record does not exist, since the domain is actually globally routable |
| 53 | nsFailCallback("[skipped] zone name " + domain.toUri() |
| 54 | + " is globally routable because startComponentIndex=" |
| 55 | + std::to_string(m_startComponentIndex), |
| 56 | certRequest, state, continueValidation); |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 57 | return; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 60 | auto query = std::make_shared<IterativeQueryController>(domain, label::NS_RR_TYPE, |
| 61 | certRequest->interest.getInterestLifetime(), |
| 62 | [=] (const Data& data, const Response&) { |
| 63 | nsSuccessCallback(data, certRequest, state, continueValidation); |
| 64 | }, |
| 65 | [=] (uint32_t errCode, const std::string& errMsg) { |
| 66 | nsFailCallback(errMsg, certRequest, state, continueValidation); |
| 67 | }, |
| 68 | m_face, nullptr, m_nsCache.get()); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 69 | query->setStartComponentIndex(m_startComponentIndex); |
| 70 | query->start(); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 71 | |
| 72 | state->setTag(std::make_shared<IterativeQueryTag>(query)); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | CertificateFetcherNdnsCert::nsSuccessCallback(const Data& data, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 77 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 78 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 79 | const ValidationContinuation& continueValidation) |
| 80 | { |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 81 | Name interestName(certRequest->interest.getName()); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 82 | interestName.append(label::CERT_RR_TYPE); |
| 83 | Interest interest(interestName); |
| 84 | |
| 85 | if (data.getContentType() == NDNS_LINK) { |
| 86 | Link link(data.wireEncode()); |
| 87 | if (!link.getDelegationList().empty()) { |
| 88 | interest.setForwardingHint(link.getDelegationList()); |
| 89 | NDNS_LOG_INFO(" [* -> *] sending interest with LINK:" << interestName); |
| 90 | } |
| 91 | else { |
| 92 | NDNS_LOG_INFO(" [* -> *] sending interest without LINK (empty delegation set):" << interestName); |
| 93 | } |
| 94 | } |
| 95 | else { |
| 96 | NDNS_LOG_WARN("fail to get NS rrset of " << interestName << " , returned data type:" << data.getContentType()); |
| 97 | } |
| 98 | |
| 99 | m_face.expressInterest(interest, |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 100 | [=] (const Interest&, const Data& data) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 101 | dataCallback(data, certRequest, state, continueValidation); |
| 102 | }, |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 103 | [=] (const Interest&, const lp::Nack& nack) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 104 | nackCallback(nack, certRequest, state, continueValidation); |
| 105 | }, |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 106 | [=] (const Interest&) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 107 | timeoutCallback(certRequest, state, continueValidation); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | CertificateFetcherNdnsCert::nsFailCallback(const std::string& errMsg, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 113 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 114 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 115 | const ValidationContinuation& continueValidation) |
| 116 | { |
| 117 | NDNS_LOG_WARN("Cannot fetch link due to " + |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 118 | errMsg + " `" + certRequest->interest.getName().toUri() + "`"); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 119 | |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 120 | Name interestName(certRequest->interest.getName()); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 121 | interestName.append(label::CERT_RR_TYPE); |
| 122 | Interest interest(interestName); |
| 123 | m_face.expressInterest(interest, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 124 | [=] (const Interest&, const Data& data) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 125 | dataCallback(data, certRequest, state, continueValidation); |
| 126 | }, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 127 | [=] (const Interest&, const lp::Nack& nack) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 128 | nackCallback(nack, certRequest, state, continueValidation); |
| 129 | }, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 130 | [=] (const Interest&) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 131 | timeoutCallback(certRequest, state, continueValidation); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | Name |
| 136 | CertificateFetcherNdnsCert::calculateDomain(const Name& key) |
| 137 | { |
| 138 | for (size_t i = 0; i < key.size(); i++) { |
| 139 | if (key[i] == label::NDNS_ITERATIVE_QUERY) { |
| 140 | return key.getPrefix(i); |
| 141 | } |
| 142 | } |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 143 | NDN_THROW(std::runtime_error(key.toUri() + " is not a legal NDNS certificate name")); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void |
| 147 | CertificateFetcherNdnsCert::dataCallback(const Data& data, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 148 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 149 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 150 | const ValidationContinuation& continueValidation) |
| 151 | { |
| 152 | NDNS_LOG_DEBUG("Fetched certificate from network " << data.getName()); |
| 153 | |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 154 | state->removeTag<IterativeQueryTag>(); |
| 155 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 156 | Certificate cert; |
| 157 | try { |
| 158 | cert = Certificate(data); |
| 159 | } |
| 160 | catch (const ndn::tlv::Error& e) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 161 | return state->fail({security::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed " |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 162 | "certificate `" + data.getName().toUri() + "` (" + e.what() + ")"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 163 | } |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 164 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 165 | continueValidation(cert, state); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | CertificateFetcherNdnsCert::nackCallback(const lp::Nack& nack, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 170 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 171 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 172 | const ValidationContinuation& continueValidation) |
| 173 | { |
| 174 | NDNS_LOG_DEBUG("NACK (" << nack.getReason() << ") while fetching certificate " |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 175 | << certRequest->interest.getName()); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 176 | |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 177 | --certRequest->nRetriesLeft; |
| 178 | if (certRequest->nRetriesLeft >= 0) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 179 | // TODO implement delay for the the next fetch |
| 180 | fetch(certRequest, state, continueValidation); |
| 181 | } |
| 182 | else { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 183 | state->removeTag<IterativeQueryTag>(); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 184 | state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate " |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 185 | "after all retries `" + certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 190 | CertificateFetcherNdnsCert::timeoutCallback(const shared_ptr<security::CertificateRequest>& certRequest, |
| 191 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 192 | const ValidationContinuation& continueValidation) |
| 193 | { |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 194 | NDNS_LOG_DEBUG("Timeout while fetching certificate " << certRequest->interest.getName() |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 195 | << ", retrying"); |
| 196 | |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 197 | --certRequest->nRetriesLeft; |
| 198 | if (certRequest->nRetriesLeft >= 0) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 199 | fetch(certRequest, state, continueValidation); |
| 200 | } |
| 201 | else { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 202 | state->removeTag<IterativeQueryTag>(); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 203 | state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate " |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 204 | "after all retries `" + certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | } // namespace ndns |
| 209 | } // namespace ndn |