Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
| 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 | |
| 30 | using security::v2::Certificate; |
| 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 |
| 43 | CertificateFetcherAppCert::doFetch(const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 44 | const shared_ptr<security::v2::ValidationState>& state, |
| 45 | const ValidationContinuation& continueValidation) |
| 46 | { |
| 47 | const Name& key = certRequest->m_interest.getName(); |
| 48 | auto query = make_shared<IterativeQueryController>(key, |
| 49 | label::APPCERT_RR_TYPE, |
| 50 | certRequest->m_interest.getInterestLifetime(), |
| 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, |
| 67 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 68 | const shared_ptr<security::v2::ValidationState>& state, |
| 69 | const ValidationContinuation& continueValidation) |
| 70 | { |
| 71 | m_validator->validate(data, |
| 72 | [=] (const Data& data) { |
| 73 | onValidationSuccessCallback(data, certRequest, state, continueValidation); |
| 74 | }, |
| 75 | [=] (const Data& data, |
| 76 | const security::v2::ValidationError& errStr) { |
| 77 | onValidationFailCallback(errStr, certRequest, state, continueValidation); |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | CertificateFetcherAppCert::onQueryFailCallback(const std::string& errMsg, |
| 83 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 84 | const shared_ptr<security::v2::ValidationState>& state, |
| 85 | const ValidationContinuation& continueValidation) |
| 86 | { |
| 87 | state->fail({security::v2::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate due to " + |
| 88 | errMsg + " `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | CertificateFetcherAppCert::onValidationSuccessCallback(const Data& data, |
| 93 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 94 | const shared_ptr<security::v2::ValidationState>& state, |
| 95 | const ValidationContinuation& continueValidation) |
| 96 | { |
| 97 | if (data.getContentType() == NDNS_NACK) { |
| 98 | state->fail({security::v2::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate: get a Nack " |
| 99 | "in query `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | Certificate cert; |
| 104 | try { |
| 105 | cert = Certificate(data.getContent().blockFromValue()); |
| 106 | } |
| 107 | catch (const ndn::tlv::Error& e) { |
| 108 | return state->fail({security::v2::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed certificate " |
| 109 | "`" + data.getName().toUri() + "` (" + e.what() + ")"}); |
| 110 | } |
| 111 | continueValidation(cert, state); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | CertificateFetcherAppCert::onValidationFailCallback(const security::v2::ValidationError& err, |
| 116 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 117 | const shared_ptr<security::v2::ValidationState>& state, |
| 118 | const ValidationContinuation& continueValidation) |
| 119 | { |
| 120 | state->fail({security::v2::ValidationError::Code::CANNOT_RETRIEVE_CERT, |
| 121 | "Cannot fetch certificate due to NDNS validation error :" |
| 122 | + err.getInfo() + " `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 123 | } |
| 124 | |
| 125 | } // namespace ndns |
| 126 | } // namespace ndn |