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(); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 48 | 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 Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 57 | query->setStartComponentIndex(m_startComponentIndex); |
| 58 | query->start(); |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 59 | |
| 60 | state->setTag(std::make_shared<IterativeQueryTag>(query)); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
| 64 | CertificateFetcherAppCert::onQuerySuccessCallback(const Data& data, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 65 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 66 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 67 | const ValidationContinuation& continueValidation) |
| 68 | { |
| 69 | m_validator->validate(data, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 70 | [=] (const Data& d) { |
| 71 | onValidationSuccessCallback(d, certRequest, state, continueValidation); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 72 | }, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 73 | [=] (const Data&, const security::ValidationError& err) { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 74 | onValidationFailCallback(err, certRequest, state, continueValidation); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 75 | }); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | CertificateFetcherAppCert::onQueryFailCallback(const std::string& errMsg, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 80 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 81 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 82 | const ValidationContinuation& continueValidation) |
| 83 | { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 84 | state->removeTag<IterativeQueryTag>(); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 85 | 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] | 86 | errMsg + " `" + certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
| 90 | CertificateFetcherAppCert::onValidationSuccessCallback(const Data& data, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 91 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 92 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 93 | const ValidationContinuation& continueValidation) |
| 94 | { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 95 | state->removeTag<IterativeQueryTag>(); |
| 96 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 97 | if (data.getContentType() == NDNS_NACK) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 98 | return state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 99 | "Cannot fetch certificate: got Nack for query `" + |
| 100 | certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Certificate cert; |
| 104 | try { |
| 105 | cert = Certificate(data.getContent().blockFromValue()); |
| 106 | } |
| 107 | catch (const ndn::tlv::Error& e) { |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 108 | return state->fail({security::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed " |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 109 | "certificate `" + data.getName().toUri() + "` (" + e.what() + ")"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 110 | } |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 111 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 112 | continueValidation(cert, state); |
| 113 | } |
| 114 | |
| 115 | void |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 116 | CertificateFetcherAppCert::onValidationFailCallback(const security::ValidationError& err, |
| 117 | const shared_ptr<security::CertificateRequest>& certRequest, |
| 118 | const shared_ptr<security::ValidationState>& state, |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 119 | const ValidationContinuation& continueValidation) |
| 120 | { |
Davide Pesavento | 002bb42 | 2019-03-22 19:04:08 -0400 | [diff] [blame] | 121 | state->removeTag<IterativeQueryTag>(); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 122 | state->fail({security::ValidationError::Code::CANNOT_RETRIEVE_CERT, |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 123 | "Cannot fetch certificate due to NDNS validation error: " + |
| 124 | err.getInfo() + " `" + certRequest->interest.getName().toUri() + "`"}); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace ndns |
| 128 | } // namespace ndn |