blob: 81f31c69aeae21c13f1949e470ef34931880eec8 [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();
Yumin Xiafa2bce72017-04-09 16:20:25 -070048 auto query = make_shared<IterativeQueryController>(key,
49 label::APPCERT_RR_TYPE,
Davide Pesavento4a315b32018-11-24 14:32:19 -050050 certRequest->interest.getInterestLifetime(),
Yumin Xiafa2bce72017-04-09 16:20:25 -070051 [=] (const Data& data, const Response& response) {
52 onQuerySuccessCallback(data, certRequest, state, continueValidation);
53 },
54 [=] (uint32_t errCode, const std::string& errMsg) {
55 onQueryFailCallback(errMsg, certRequest, state, continueValidation);
56 },
57 m_face,
58 nullptr,
59 m_nsCache);
60 query->setStartComponentIndex(m_startComponentIndex);
61 query->start();
62 state->setTag(make_shared<IterativeQueryTag>(query));
63}
64
65void
66CertificateFetcherAppCert::onQuerySuccessCallback(const Data& data,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040067 const shared_ptr<security::CertificateRequest>& certRequest,
68 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070069 const ValidationContinuation& continueValidation)
70{
71 m_validator->validate(data,
Davide Pesavento002bb422019-03-22 19:04:08 -040072 [=] (const Data& d) {
73 onValidationSuccessCallback(d, certRequest, state, continueValidation);
Yumin Xiafa2bce72017-04-09 16:20:25 -070074 },
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040075 [=] (const Data&, const security::ValidationError& err) {
Davide Pesavento002bb422019-03-22 19:04:08 -040076 onValidationFailCallback(err, certRequest, state, continueValidation);
Yumin Xiafa2bce72017-04-09 16:20:25 -070077 });
78}
79
80void
81CertificateFetcherAppCert::onQueryFailCallback(const std::string& errMsg,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040082 const shared_ptr<security::CertificateRequest>& certRequest,
83 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070084 const ValidationContinuation& continueValidation)
85{
Davide Pesavento002bb422019-03-22 19:04:08 -040086 state->removeTag<IterativeQueryTag>();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040087 state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate due to " +
Davide Pesavento4a315b32018-11-24 14:32:19 -050088 errMsg + " `" + certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -070089}
90
91void
92CertificateFetcherAppCert::onValidationSuccessCallback(const Data& data,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040093 const shared_ptr<security::CertificateRequest>& certRequest,
94 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -070095 const ValidationContinuation& continueValidation)
96{
Davide Pesavento002bb422019-03-22 19:04:08 -040097 state->removeTag<IterativeQueryTag>();
98
Yumin Xiafa2bce72017-04-09 16:20:25 -070099 if (data.getContentType() == NDNS_NACK) {
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400100 return state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT,
Davide Pesavento002bb422019-03-22 19:04:08 -0400101 "Cannot fetch certificate: got Nack for query `" +
102 certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700103 }
104
105 Certificate cert;
106 try {
107 cert = Certificate(data.getContent().blockFromValue());
108 }
109 catch (const ndn::tlv::Error& e) {
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400110 return state->fail({security::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed "
Davide Pesavento002bb422019-03-22 19:04:08 -0400111 "certificate `" + data.getName().toUri() + "` (" + e.what() + ")"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700112 }
Davide Pesavento002bb422019-03-22 19:04:08 -0400113
Yumin Xiafa2bce72017-04-09 16:20:25 -0700114 continueValidation(cert, state);
115}
116
117void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400118CertificateFetcherAppCert::onValidationFailCallback(const security::ValidationError& err,
119 const shared_ptr<security::CertificateRequest>& certRequest,
120 const shared_ptr<security::ValidationState>& state,
Yumin Xiafa2bce72017-04-09 16:20:25 -0700121 const ValidationContinuation& continueValidation)
122{
Davide Pesavento002bb422019-03-22 19:04:08 -0400123 state->removeTag<IterativeQueryTag>();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400124 state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT,
Davide Pesavento4a315b32018-11-24 14:32:19 -0500125 "Cannot fetch certificate due to NDNS validation error: " +
126 err.getInfo() + " `" + certRequest->interest.getName().toUri() + "`"});
Yumin Xiafa2bce72017-04-09 16:20:25 -0700127}
128
129} // namespace ndns
130} // namespace ndn