blob: 966f7de92e0f3f07d9dc81b05c666a18113b1132 [file] [log] [blame]
Yumin Xiafa2bce72017-04-09 16:20:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4a315b32018-11-24 14:32:19 -05002/*
Alexander Afanasyev60514ec2020-06-03 14:18:53 -04003 * Copyright (c) 2014-2020, 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-appcert.hpp"
21#include "certificate-fetcher-ndns-cert.hpp"
22#include "clients/iterative-query-controller.hpp"
23
24#include "validator.hpp"
25#include "clients/response.hpp"
26
27namespace ndn {
28namespace ndns {
29
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040030using security::Certificate;
Yumin Xiafa2bce72017-04-09 16:20:25 -070031
32CertificateFetcherAppCert::CertificateFetcherAppCert(Face& face,
33 size_t nsCacheSize,
34 size_t startComponentIndex)
35 : m_face(face)
36 , m_validator(NdnsValidatorBuilder::create(face, nsCacheSize, startComponentIndex))
37 , m_startComponentIndex(startComponentIndex)
38{
39 m_nsCache = dynamic_cast<CertificateFetcherNdnsCert&>(m_validator->getFetcher()).getNsCache();
40}
41
42void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040043CertificateFetcherAppCert::doFetch(const shared_ptr<security::CertificateRequest>& certRequest,
44 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070045 const ValidationContinuation& continueValidation)
46{
Davide Pesavento4a315b32018-11-24 14:32:19 -050047 const Name& key = certRequest->interest.getName();
Davide Pesavento948c50c2020-12-26 21:30:45 -050048 auto query = std::make_shared<IterativeQueryController>(key, label::APPCERT_RR_TYPE,
49 certRequest->interest.getInterestLifetime(),
50 [=] (const Data& data, const Response&) {
51 onQuerySuccessCallback(data, certRequest, state, continueValidation);
52 },
53 [=] (uint32_t errCode, const std::string& errMsg) {
54 onQueryFailCallback(errMsg, certRequest, state, continueValidation);
55 },
56 m_face, nullptr, m_nsCache);
Yumin Xiafa2bce72017-04-09 16:20:25 -070057 query->setStartComponentIndex(m_startComponentIndex);
58 query->start();
Davide Pesavento948c50c2020-12-26 21:30:45 -050059
60 state->setTag(std::make_shared<IterativeQueryTag>(query));
Yumin Xiafa2bce72017-04-09 16:20:25 -070061}
62
63void
64CertificateFetcherAppCert::onQuerySuccessCallback(const Data& data,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040065 const shared_ptr<security::CertificateRequest>& certRequest,
66 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070067 const ValidationContinuation& continueValidation)
68{
69 m_validator->validate(data,
Davide Pesavento002bb422019-03-22 19:04:08 -040070 [=] (const Data& d) {
71 onValidationSuccessCallback(d, certRequest, state, continueValidation);
Yumin Xiafa2bce72017-04-09 16:20:25 -070072 },
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040073 [=] (const Data&, const security::ValidationError& err) {
Davide Pesavento002bb422019-03-22 19:04:08 -040074 onValidationFailCallback(err, certRequest, state, continueValidation);
Yumin Xiafa2bce72017-04-09 16:20:25 -070075 });
76}
77
78void
79CertificateFetcherAppCert::onQueryFailCallback(const std::string& errMsg,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040080 const shared_ptr<security::CertificateRequest>& certRequest,
81 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070082 const ValidationContinuation& continueValidation)
83{
Davide Pesavento002bb422019-03-22 19:04:08 -040084 state->removeTag<IterativeQueryTag>();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040085 state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate due to " +
Davide Pesavento4a315b32018-11-24 14:32:19 -050086 errMsg + " `" + certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -070087}
88
89void
90CertificateFetcherAppCert::onValidationSuccessCallback(const Data& data,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040091 const shared_ptr<security::CertificateRequest>& certRequest,
92 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070093 const ValidationContinuation& continueValidation)
94{
Davide Pesavento002bb422019-03-22 19:04:08 -040095 state->removeTag<IterativeQueryTag>();
96
Yumin Xiafa2bce72017-04-09 16:20:25 -070097 if (data.getContentType() == NDNS_NACK) {
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040098 return state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT,
Davide Pesavento002bb422019-03-22 19:04:08 -040099 "Cannot fetch certificate: got Nack for query `" +
100 certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700101 }
102
103 Certificate cert;
104 try {
105 cert = Certificate(data.getContent().blockFromValue());
106 }
107 catch (const ndn::tlv::Error& e) {
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400108 return state->fail({security::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed "
Davide Pesavento002bb422019-03-22 19:04:08 -0400109 "certificate `" + data.getName().toUri() + "` (" + e.what() + ")"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700110 }
Davide Pesavento002bb422019-03-22 19:04:08 -0400111
Yumin Xiafa2bce72017-04-09 16:20:25 -0700112 continueValidation(cert, state);
113}
114
115void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400116CertificateFetcherAppCert::onValidationFailCallback(const security::ValidationError& err,
117 const shared_ptr<security::CertificateRequest>& certRequest,
118 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -0700119 const ValidationContinuation& continueValidation)
120{
Davide Pesavento002bb422019-03-22 19:04:08 -0400121 state->removeTag<IterativeQueryTag>();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400122 state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT,
Davide Pesavento4a315b32018-11-24 14:32:19 -0500123 "Cannot fetch certificate due to NDNS validation error: " +
124 err.getInfo() + " `" + certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700125}
126
127} // namespace ndns
128} // namespace ndn