blob: 43373782f3a15cdc08ae4a352edec3b9cdb2724b [file] [log] [blame]
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -05002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -08004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "certificate-fetcher-from-network.hpp"
23#include "face.hpp"
24#include "util/logger.hpp"
25
26namespace ndn {
27namespace security {
28namespace v2 {
29
30NDN_LOG_INIT(ndn.security.v2.CertificateFetcher);
31
32#define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x)
33#define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x)
34
35CertificateFetcherFromNetwork::CertificateFetcherFromNetwork(Face& face)
36 : m_face(face)
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -050037 , m_scheduler(face.getIoService())
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080038{
39}
40
41void
42CertificateFetcherFromNetwork::doFetch(const shared_ptr<CertificateRequest>& certRequest,
43 const shared_ptr<ValidationState>& state,
44 const ValidationContinuation& continueValidation)
45{
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -050046 m_face.expressInterest(certRequest->interest,
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080047 [=] (const Interest& interest, const Data& data) {
48 dataCallback(data, certRequest, state, continueValidation);
49 },
50 [=] (const Interest& interest, const lp::Nack& nack) {
51 nackCallback(nack, certRequest, state, continueValidation);
52 },
53 [=] (const Interest& interest) {
54 timeoutCallback(certRequest, state, continueValidation);
55 });
56}
57
58void
59CertificateFetcherFromNetwork::dataCallback(const Data& data,
60 const shared_ptr<CertificateRequest>& certRequest,
61 const shared_ptr<ValidationState>& state,
62 const ValidationContinuation& continueValidation)
63{
64 NDN_LOG_DEBUG_DEPTH("Fetched certificate from network " << data.getName());
65
66 Certificate cert;
67 try {
68 cert = Certificate(data);
69 }
70 catch (const tlv::Error& e) {
71 return state->fail({ValidationError::Code::MALFORMED_CERT, "Fetched a malformed certificate "
72 "`" + data.getName().toUri() + "` (" + e.what() + ")"});
73 }
74 continueValidation(cert, state);
75}
76
77void
78CertificateFetcherFromNetwork::nackCallback(const lp::Nack& nack,
79 const shared_ptr<CertificateRequest>& certRequest,
80 const shared_ptr<ValidationState>& state,
81 const ValidationContinuation& continueValidation)
82{
83 NDN_LOG_DEBUG_DEPTH("NACK (" << nack.getReason() << ") while fetching certificate "
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -050084 << certRequest->interest.getName());
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080085
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -050086 --certRequest->nRetriesLeft;
87 if (certRequest->nRetriesLeft >= 0) {
88 m_scheduler.scheduleEvent(certRequest->waitAfterNack,
89 [=] { fetch(certRequest, state, continueValidation); });
90 certRequest->waitAfterNack *= 2;
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080091 }
92 else {
93 state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all "
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -050094 "retries `" + certRequest->interest.getName().toUri() + "`"});
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080095 }
96}
97
98void
99CertificateFetcherFromNetwork::timeoutCallback(const shared_ptr<CertificateRequest>& certRequest,
100 const shared_ptr<ValidationState>& state,
101 const ValidationContinuation& continueValidation)
102{
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500103 NDN_LOG_DEBUG_DEPTH("Timeout while fetching certificate " << certRequest->interest.getName()
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800104 << ", retrying");
105
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500106 --certRequest->nRetriesLeft;
107 if (certRequest->nRetriesLeft >= 0) {
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800108 fetch(certRequest, state, continueValidation);
109 }
110 else {
111 state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all "
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500112 "retries `" + certRequest->interest.getName().toUri() + "`"});
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800113 }
114}
115
116} // namespace v2
117} // namespace security
118} // namespace ndn