blob: cd4a0ce7fe8de3bb20d417a7ca9888c3f82d11c9 [file] [log] [blame]
Yumin Xiafa2bce72017-04-09 16:20:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev08d18742018-03-15 16:31:28 -04002/*
Junxiao Shi81e98762022-01-11 18:17:24 +00003 * Copyright (c) 2014-2022, Regents of the University of California.
Yumin Xiafa2bce72017-04-09 16:20:25 -07004 *
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
28namespace ndn {
29namespace ndns {
30
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040031using security::Certificate;
Yumin Xiafa2bce72017-04-09 16:20:25 -070032
Alexander Afanasyev08d18742018-03-15 16:31:28 -040033NDNS_LOG_INIT(CertificateFetcherNdnsCert);
Yumin Xiafa2bce72017-04-09 16:20:25 -070034
35CertificateFetcherNdnsCert::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 Pesavento002bb422019-03-22 19:04:08 -040041{
42}
Yumin Xiafa2bce72017-04-09 16:20:25 -070043
44void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040045CertificateFetcherNdnsCert::doFetch(const shared_ptr<security::CertificateRequest>& certRequest,
46 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070047 const ValidationContinuation& continueValidation)
48{
Davide Pesavento4a315b32018-11-24 14:32:19 -050049 const Name& key = certRequest->interest.getName();
Yumin Xiafa2bce72017-04-09 16:20:25 -070050 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 Pesavento002bb422019-03-22 19:04:08 -040057 return;
Yumin Xiafa2bce72017-04-09 16:20:25 -070058 }
59
Davide Pesavento948c50c2020-12-26 21:30:45 -050060 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 Xiafa2bce72017-04-09 16:20:25 -070069 query->setStartComponentIndex(m_startComponentIndex);
70 query->start();
Davide Pesavento948c50c2020-12-26 21:30:45 -050071
72 state->setTag(std::make_shared<IterativeQueryTag>(query));
Yumin Xiafa2bce72017-04-09 16:20:25 -070073}
74
75void
76CertificateFetcherNdnsCert::nsSuccessCallback(const Data& data,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040077 const shared_ptr<security::CertificateRequest>& certRequest,
78 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070079 const ValidationContinuation& continueValidation)
80{
Davide Pesavento4a315b32018-11-24 14:32:19 -050081 Name interestName(certRequest->interest.getName());
Yumin Xiafa2bce72017-04-09 16:20:25 -070082 interestName.append(label::CERT_RR_TYPE);
83 Interest interest(interestName);
Eric Newberryc196a162020-09-13 14:05:16 -070084 interest.setCanBePrefix(true);
Yumin Xiafa2bce72017-04-09 16:20:25 -070085
86 if (data.getContentType() == NDNS_LINK) {
87 Link link(data.wireEncode());
Junxiao Shi81e98762022-01-11 18:17:24 +000088 auto delList = link.getDelegationList();
89 if (!delList.empty()) {
90 interest.setForwardingHint({delList.begin(), delList.end()});
Yumin Xiafa2bce72017-04-09 16:20:25 -070091 NDNS_LOG_INFO(" [* -> *] sending interest with LINK:" << interestName);
92 }
93 else {
94 NDNS_LOG_INFO(" [* -> *] sending interest without LINK (empty delegation set):" << interestName);
95 }
96 }
97 else {
98 NDNS_LOG_WARN("fail to get NS rrset of " << interestName << " , returned data type:" << data.getContentType());
99 }
100
101 m_face.expressInterest(interest,
Davide Pesavento948c50c2020-12-26 21:30:45 -0500102 [=] (const Interest&, const Data& data) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700103 dataCallback(data, certRequest, state, continueValidation);
104 },
Davide Pesavento948c50c2020-12-26 21:30:45 -0500105 [=] (const Interest&, const lp::Nack& nack) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700106 nackCallback(nack, certRequest, state, continueValidation);
107 },
Davide Pesavento948c50c2020-12-26 21:30:45 -0500108 [=] (const Interest&) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700109 timeoutCallback(certRequest, state, continueValidation);
110 });
111}
112
113void
114CertificateFetcherNdnsCert::nsFailCallback(const std::string& errMsg,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400115 const shared_ptr<security::CertificateRequest>& certRequest,
116 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -0700117 const ValidationContinuation& continueValidation)
118{
119 NDNS_LOG_WARN("Cannot fetch link due to " +
Davide Pesavento4a315b32018-11-24 14:32:19 -0500120 errMsg + " `" + certRequest->interest.getName().toUri() + "`");
Yumin Xiafa2bce72017-04-09 16:20:25 -0700121
Davide Pesavento4a315b32018-11-24 14:32:19 -0500122 Name interestName(certRequest->interest.getName());
Yumin Xiafa2bce72017-04-09 16:20:25 -0700123 interestName.append(label::CERT_RR_TYPE);
124 Interest interest(interestName);
Eric Newberryc196a162020-09-13 14:05:16 -0700125 interest.setCanBePrefix(true);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700126 m_face.expressInterest(interest,
Davide Pesavento002bb422019-03-22 19:04:08 -0400127 [=] (const Interest&, const Data& data) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700128 dataCallback(data, certRequest, state, continueValidation);
129 },
Davide Pesavento002bb422019-03-22 19:04:08 -0400130 [=] (const Interest&, const lp::Nack& nack) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700131 nackCallback(nack, certRequest, state, continueValidation);
132 },
Davide Pesavento002bb422019-03-22 19:04:08 -0400133 [=] (const Interest&) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700134 timeoutCallback(certRequest, state, continueValidation);
135 });
136}
137
138Name
139CertificateFetcherNdnsCert::calculateDomain(const Name& key)
140{
141 for (size_t i = 0; i < key.size(); i++) {
142 if (key[i] == label::NDNS_ITERATIVE_QUERY) {
143 return key.getPrefix(i);
144 }
145 }
Davide Pesavento948c50c2020-12-26 21:30:45 -0500146 NDN_THROW(std::runtime_error(key.toUri() + " is not a legal NDNS certificate name"));
Yumin Xiafa2bce72017-04-09 16:20:25 -0700147}
148
149void
150CertificateFetcherNdnsCert::dataCallback(const Data& data,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400151 const shared_ptr<security::CertificateRequest>& certRequest,
152 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -0700153 const ValidationContinuation& continueValidation)
154{
155 NDNS_LOG_DEBUG("Fetched certificate from network " << data.getName());
156
Davide Pesavento002bb422019-03-22 19:04:08 -0400157 state->removeTag<IterativeQueryTag>();
158
Yumin Xiafa2bce72017-04-09 16:20:25 -0700159 Certificate cert;
160 try {
161 cert = Certificate(data);
162 }
163 catch (const ndn::tlv::Error& e) {
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400164 return state->fail({security::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed "
Davide Pesavento002bb422019-03-22 19:04:08 -0400165 "certificate `" + data.getName().toUri() + "` (" + e.what() + ")"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700166 }
Davide Pesavento002bb422019-03-22 19:04:08 -0400167
Yumin Xiafa2bce72017-04-09 16:20:25 -0700168 continueValidation(cert, state);
169}
170
171void
172CertificateFetcherNdnsCert::nackCallback(const lp::Nack& nack,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400173 const shared_ptr<security::CertificateRequest>& certRequest,
174 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -0700175 const ValidationContinuation& continueValidation)
176{
177 NDNS_LOG_DEBUG("NACK (" << nack.getReason() << ") while fetching certificate "
Davide Pesavento4a315b32018-11-24 14:32:19 -0500178 << certRequest->interest.getName());
Yumin Xiafa2bce72017-04-09 16:20:25 -0700179
Davide Pesavento4a315b32018-11-24 14:32:19 -0500180 --certRequest->nRetriesLeft;
181 if (certRequest->nRetriesLeft >= 0) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700182 // TODO implement delay for the the next fetch
183 fetch(certRequest, state, continueValidation);
184 }
185 else {
Davide Pesavento002bb422019-03-22 19:04:08 -0400186 state->removeTag<IterativeQueryTag>();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400187 state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate "
Davide Pesavento002bb422019-03-22 19:04:08 -0400188 "after all retries `" + certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700189 }
190}
191
192void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400193CertificateFetcherNdnsCert::timeoutCallback(const shared_ptr<security::CertificateRequest>& certRequest,
194 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -0700195 const ValidationContinuation& continueValidation)
196{
Davide Pesavento4a315b32018-11-24 14:32:19 -0500197 NDNS_LOG_DEBUG("Timeout while fetching certificate " << certRequest->interest.getName()
Yumin Xiafa2bce72017-04-09 16:20:25 -0700198 << ", retrying");
199
Davide Pesavento4a315b32018-11-24 14:32:19 -0500200 --certRequest->nRetriesLeft;
201 if (certRequest->nRetriesLeft >= 0) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700202 fetch(certRequest, state, continueValidation);
203 }
204 else {
Davide Pesavento002bb422019-03-22 19:04:08 -0400205 state->removeTag<IterativeQueryTag>();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400206 state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate "
Davide Pesavento002bb422019-03-22 19:04:08 -0400207 "after all retries `" + certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700208 }
209}
210
211} // namespace ndns
212} // namespace ndn