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-cert.hpp" |
| 21 | #include "clients/iterative-query-controller.hpp" |
| 22 | #include "clients/response.hpp" |
| 23 | #include "logger.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/tlv.hpp> |
| 26 | #include <ndn-cxx/ims/in-memory-storage-fifo.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndns { |
| 30 | |
| 31 | using security::v2::Certificate; |
| 32 | |
| 33 | NDNS_LOG_INIT("CertificateFetcherNdnsCert") |
| 34 | |
| 35 | CertificateFetcherNdnsCert::CertificateFetcherNdnsCert(Face& face, |
| 36 | size_t nsCacheSize, |
| 37 | size_t startComponentIndex) |
| 38 | : m_face(face) |
| 39 | , m_nsCache(make_unique<InMemoryStorageFifo>(nsCacheSize)) |
| 40 | , m_startComponentIndex(startComponentIndex) |
| 41 | {} |
| 42 | |
| 43 | void |
| 44 | CertificateFetcherNdnsCert::doFetch(const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 45 | const shared_ptr<security::v2::ValidationState>& state, |
| 46 | const ValidationContinuation& continueValidation) |
| 47 | { |
| 48 | using IterativeQueryTag = SimpleTag<shared_ptr<IterativeQueryController>, 1086>; |
| 49 | const Name& key = certRequest->m_interest.getName(); |
| 50 | Name domain = calculateDomain(key); |
| 51 | if (domain.size() == m_startComponentIndex) { |
| 52 | // NS record does not exist, since the domain is actually globally routable |
| 53 | nsFailCallback("[skipped] zone name " + domain.toUri() |
| 54 | + " is globally routable because startComponentIndex=" |
| 55 | + std::to_string(m_startComponentIndex), |
| 56 | certRequest, state, continueValidation); |
| 57 | return ; |
| 58 | } |
| 59 | |
| 60 | auto query = make_shared<IterativeQueryController>(domain, |
| 61 | label::NS_RR_TYPE, |
| 62 | certRequest->m_interest.getInterestLifetime(), |
| 63 | [=] (const Data& data, const Response& response) { |
| 64 | nsSuccessCallback(data, certRequest, state, continueValidation); |
| 65 | }, |
| 66 | [=] (uint32_t errCode, const std::string& errMsg) { |
| 67 | nsFailCallback(errMsg, certRequest, state, continueValidation); |
| 68 | }, |
| 69 | m_face, |
| 70 | nullptr, |
| 71 | m_nsCache.get()); |
| 72 | query->setStartComponentIndex(m_startComponentIndex); |
| 73 | query->start(); |
| 74 | auto queryTag = make_shared<IterativeQueryTag>(query); |
| 75 | state->setTag(queryTag); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | CertificateFetcherNdnsCert::nsSuccessCallback(const Data& data, |
| 80 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 81 | const shared_ptr<security::v2::ValidationState>& state, |
| 82 | const ValidationContinuation& continueValidation) |
| 83 | { |
| 84 | Name interestName(certRequest->m_interest.getName()); |
| 85 | interestName.append(label::CERT_RR_TYPE); |
| 86 | Interest interest(interestName); |
| 87 | |
| 88 | if (data.getContentType() == NDNS_LINK) { |
| 89 | Link link(data.wireEncode()); |
| 90 | if (!link.getDelegationList().empty()) { |
| 91 | interest.setForwardingHint(link.getDelegationList()); |
| 92 | NDNS_LOG_INFO(" [* -> *] sending interest with LINK:" << interestName); |
| 93 | } |
| 94 | else { |
| 95 | NDNS_LOG_INFO(" [* -> *] sending interest without LINK (empty delegation set):" << interestName); |
| 96 | } |
| 97 | } |
| 98 | else { |
| 99 | NDNS_LOG_WARN("fail to get NS rrset of " << interestName << " , returned data type:" << data.getContentType()); |
| 100 | } |
| 101 | |
| 102 | m_face.expressInterest(interest, |
| 103 | [=] (const Interest& interest, const Data& data) { |
| 104 | dataCallback(data, certRequest, state, continueValidation); |
| 105 | }, |
| 106 | [=] (const Interest& interest, const lp::Nack& nack) { |
| 107 | nackCallback(nack, certRequest, state, continueValidation); |
| 108 | }, |
| 109 | [=] (const Interest& interest) { |
| 110 | timeoutCallback(certRequest, state, continueValidation); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | CertificateFetcherNdnsCert::nsFailCallback(const std::string& errMsg, |
| 116 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 117 | const shared_ptr<security::v2::ValidationState>& state, |
| 118 | const ValidationContinuation& continueValidation) |
| 119 | { |
| 120 | NDNS_LOG_WARN("Cannot fetch link due to " + |
| 121 | errMsg + " `" + certRequest->m_interest.getName().toUri() + "`"); |
| 122 | |
| 123 | Name interestName(certRequest->m_interest.getName()); |
| 124 | interestName.append(label::CERT_RR_TYPE); |
| 125 | Interest interest(interestName); |
| 126 | m_face.expressInterest(interest, |
| 127 | [=] (const Interest& interest, const Data& data) { |
| 128 | dataCallback(data, certRequest, state, continueValidation); |
| 129 | }, |
| 130 | [=] (const Interest& interest, const lp::Nack& nack) { |
| 131 | nackCallback(nack, certRequest, state, continueValidation); |
| 132 | }, |
| 133 | [=] (const Interest& interest) { |
| 134 | timeoutCallback(certRequest, state, continueValidation); |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | Name |
| 139 | CertificateFetcherNdnsCert::calculateDomain(const Name& key) |
| 140 | { |
| 141 | for (size_t i = 0; i < key.size(); i++) { |
| 142 | if (key[i] == label::NDNS_ITERATIVE_QUERY) { |
| 143 | return key.getPrefix(i); |
| 144 | } |
| 145 | } |
| 146 | BOOST_THROW_EXCEPTION(std::runtime_error(key.toUri() + " is not a legal NDNS certificate name")); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | CertificateFetcherNdnsCert::dataCallback(const Data& data, |
| 151 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 152 | const shared_ptr<security::v2::ValidationState>& state, |
| 153 | const ValidationContinuation& continueValidation) |
| 154 | { |
| 155 | NDNS_LOG_DEBUG("Fetched certificate from network " << data.getName()); |
| 156 | |
| 157 | Certificate cert; |
| 158 | try { |
| 159 | cert = Certificate(data); |
| 160 | } |
| 161 | catch (const ndn::tlv::Error& e) { |
| 162 | return state->fail({security::v2::ValidationError::Code::MALFORMED_CERT, "Fetched a malformed certificate " |
| 163 | "`" + data.getName().toUri() + "` (" + e.what() + ")"}); |
| 164 | } |
| 165 | continueValidation(cert, state); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | CertificateFetcherNdnsCert::nackCallback(const lp::Nack& nack, |
| 170 | const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 171 | const shared_ptr<security::v2::ValidationState>& state, |
| 172 | const ValidationContinuation& continueValidation) |
| 173 | { |
| 174 | NDNS_LOG_DEBUG("NACK (" << nack.getReason() << ") while fetching certificate " |
| 175 | << certRequest->m_interest.getName()); |
| 176 | |
| 177 | --certRequest->m_nRetriesLeft; |
| 178 | if (certRequest->m_nRetriesLeft >= 0) { |
| 179 | // TODO implement delay for the the next fetch |
| 180 | fetch(certRequest, state, continueValidation); |
| 181 | } |
| 182 | else { |
| 183 | state->fail({security::v2::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all " |
| 184 | "retries `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | CertificateFetcherNdnsCert::timeoutCallback(const shared_ptr<security::v2::CertificateRequest>& certRequest, |
| 190 | const shared_ptr<security::v2::ValidationState>& state, |
| 191 | const ValidationContinuation& continueValidation) |
| 192 | { |
| 193 | NDNS_LOG_DEBUG("Timeout while fetching certificate " << certRequest->m_interest.getName() |
| 194 | << ", retrying"); |
| 195 | |
| 196 | --certRequest->m_nRetriesLeft; |
| 197 | if (certRequest->m_nRetriesLeft >= 0) { |
| 198 | fetch(certRequest, state, continueValidation); |
| 199 | } |
| 200 | else { |
| 201 | state->fail({security::v2::ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all " |
| 202 | "retries `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } // namespace ndns |
| 207 | } // namespace ndn |