Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [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-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 | |
| 27 | namespace ndn { |
| 28 | namespace ndns { |
| 29 | |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 30 | using security::Certificate; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 31 | |
| 32 | CertificateFetcherAppCert::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 | |
| 42 | void |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 43 | CertificateFetcherAppCert::doFetch(const shared_ptr<security::CertificateRequest>& certRequest, |
| 44 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 45 | const ValidationContinuation& continueValidation) |
| 46 | { |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 47 | const Name& key = certRequest->interest.getName(); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 48 | auto query = make_shared<IterativeQueryController>(key, |
| 49 | label::APPCERT_RR_TYPE, |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 50 | certRequest->interest.getInterestLifetime(), |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 51 | [=] (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 | |
| 65 | void |
| 66 | CertificateFetcherAppCert::onQuerySuccessCallback(const Data& data, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 67 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 68 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 69 | const ValidationContinuation& continueValidation) |
| 70 | { |
| 71 | m_validator->validate(data, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 72 | [=] (const Data& d) { |
| 73 | onValidationSuccessCallback(d, certRequest, state, continueValidation); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 74 | }, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 75 | [=] (const Data&, const security::ValidationError& err) { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 76 | onValidationFailCallback(err, certRequest, state, continueValidation); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 77 | }); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | CertificateFetcherAppCert::onQueryFailCallback(const std::string& errMsg, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 82 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 83 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 84 | const ValidationContinuation& continueValidation) |
| 85 | { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 86 | state->removeTag<IterativeQueryTag>(); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 87 | state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate due to " + |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 88 | errMsg + " `" + certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
| 92 | CertificateFetcherAppCert::onValidationSuccessCallback(const Data& data, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 93 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 94 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 95 | const ValidationContinuation& continueValidation) |
| 96 | { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 97 | state->removeTag<IterativeQueryTag>(); |
| 98 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 99 | if (data.getContentType() == NDNS_NACK) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 100 | return state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 101 | "Cannot fetch certificate: got Nack for query `" + |
| 102 | certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Certificate cert; |
| 106 | try { |
| 107 | cert = Certificate(data.getContent().blockFromValue()); |
| 108 | } |
| 109 | catch (const ndn::tlv::Error& e) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 110 | return state->fail({security::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed " |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 111 | "certificate `" + data.getName().toUri() + "` (" + e.what() + ")"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 112 | } |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 113 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 114 | continueValidation(cert, state); |
| 115 | } |
| 116 | |
| 117 | void |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 118 | CertificateFetcherAppCert::onValidationFailCallback(const security::ValidationError& err, |
| 119 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 120 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 121 | const ValidationContinuation& continueValidation) |
| 122 | { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 123 | state->removeTag<IterativeQueryTag>(); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 124 | state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 125 | "Cannot fetch certificate due to NDNS validation error: " + |
| 126 | err.getInfo() + " `" + certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // namespace ndns |
| 130 | } // namespace ndn |