NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
certificate-bundle-fetcher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 Regents of the University of California.
4  *
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 
23 
24 #include "ndn-cxx/face.hpp"
28 #include "ndn-cxx/util/logger.hpp"
29 
30 namespace ndn {
31 namespace security {
32 inline namespace v2 {
33 
34 NDN_LOG_INIT(ndn.security.CertificateBundleFetcher);
35 
36 #define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x)
37 #define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x)
38 
39 CertificateBundleFetcher::CertificateBundleFetcher(unique_ptr<CertificateFetcher> inner,
40  Face& face)
41  : m_inner(std::move(inner))
42  , m_face(face)
43  , m_bundleInterestLifetime(1000)
44 {
45  BOOST_ASSERT(m_inner != nullptr);
46 }
47 
48 void
50 {
51  m_bundleInterestLifetime = time;
52 }
53 
56 {
57  return m_bundleInterestLifetime;
58 }
59 
60 void
62 {
63  m_certStorage = &certStorage;
64  m_inner->setCertificateStorage(certStorage);
65 }
66 
67 void
68 CertificateBundleFetcher::doFetch(const shared_ptr<CertificateRequest>& certRequest,
69  const shared_ptr<ValidationState>& state,
70  const ValidationContinuation& continueValidation)
71 {
72  auto dataValidationState = dynamic_pointer_cast<DataValidationState>(state);
73  if (dataValidationState == nullptr) {
74  return m_inner->fetch(certRequest, state, continueValidation);
75  }
76 
77  // check if a bundle segment was fetched before
78  shared_ptr<BundleNameTag> bundleNameTag = state->getTag<BundleNameTag>();
79  if (bundleNameTag == nullptr) {
80  const Name& originalDataName = dataValidationState->getOriginalData().getName();
81  if (originalDataName.empty()) {
82  return m_inner->fetch(certRequest, state, continueValidation);
83  }
84  // derive certificate bundle name from original data name
85  Name bundleNamePrefix = deriveBundleName(originalDataName);
86  fetchFirstBundleSegment(bundleNamePrefix, certRequest, state, continueValidation);
87  }
88  else {
89  Name fullBundleName = bundleNameTag->get();
90  fetchNextBundleSegment(fullBundleName, fullBundleName.get(-1).getSuccessor(),
91  certRequest, state, continueValidation);
92  }
93 }
94 
95 void
96 CertificateBundleFetcher::fetchFirstBundleSegment(const Name& bundleNamePrefix,
97  const shared_ptr<CertificateRequest>& certRequest,
98  const shared_ptr<ValidationState>& state,
99  const ValidationContinuation& continueValidation)
100 {
101  Interest bundleInterest = Interest(bundleNamePrefix);
102  bundleInterest.setCanBePrefix(true);
103  bundleInterest.setMustBeFresh(true);
104  bundleInterest.setInterestLifetime(m_bundleInterestLifetime);
105 
106  m_face.expressInterest(bundleInterest,
107  [=] (const Interest&, const Data& data) {
108  dataCallback(data, true, certRequest, state, continueValidation);
109  },
110  [=] (const Interest&, const lp::Nack& nack) {
111  nackCallback(nack, certRequest, state, continueValidation, bundleNamePrefix);
112  },
113  [=] (const Interest&) {
114  timeoutCallback(certRequest, state, continueValidation, bundleNamePrefix);
115  });
116 }
117 
118 void
119 CertificateBundleFetcher::fetchNextBundleSegment(const Name& fullBundleName, const name::Component& segmentNo,
120  const shared_ptr<CertificateRequest>& certRequest,
121  const shared_ptr<ValidationState>& state,
122  const ValidationContinuation& continueValidation)
123 {
124  shared_ptr<FinalBlockIdTag> finalBlockId = state->getTag<FinalBlockIdTag>();
125  if (finalBlockId != nullptr && segmentNo > finalBlockId->get()) {
126  return m_inner->fetch(certRequest, state, continueValidation);
127  }
128 
129  Interest bundleInterest(fullBundleName.getPrefix(-1).append(segmentNo));
130  bundleInterest.setCanBePrefix(false);
131  bundleInterest.setMustBeFresh(false);
132  bundleInterest.setInterestLifetime(m_bundleInterestLifetime);
133 
134  m_face.expressInterest(bundleInterest,
135  [=] (const Interest&, const Data& data) {
136  dataCallback(data, false, certRequest, state, continueValidation);
137  },
138  [=] (const Interest&, const lp::Nack& nack) {
139  nackCallback(nack, certRequest, state, continueValidation, fullBundleName);
140  },
141  [=] (const Interest&) {
142  timeoutCallback(certRequest, state, continueValidation, fullBundleName);
143  });
144 }
145 
146 void
147 CertificateBundleFetcher::dataCallback(const Data& bundleData,
148  bool isSegmentZeroExpected,
149  const shared_ptr<CertificateRequest>& certRequest,
150  const shared_ptr<ValidationState>& state,
151  const ValidationContinuation& continueValidation)
152 {
153  NDN_LOG_DEBUG_DEPTH("Fetched certificate bundle from network " << bundleData.getName());
154 
155  name::Component currentSegment = bundleData.getName().get(-1);
156  if (!currentSegment.isSegment()) {
157  return m_inner->fetch(certRequest, state, continueValidation);
158  }
159 
160  if (isSegmentZeroExpected && currentSegment.toSegment() != 0) {
161  // fetch segment zero
162  fetchNextBundleSegment(bundleData.getName(), name::Component::fromSegment(0),
163  certRequest, state, continueValidation);
164  }
165  else {
166  state->setTag(make_shared<BundleNameTag>(bundleData.getName()));
167 
168  const auto& finalBlockId = bundleData.getFinalBlock();
169  if (finalBlockId) {
170  state->setTag(make_shared<FinalBlockIdTag>(*finalBlockId));
171  }
172 
173  Block bundleContent = bundleData.getContent();
174  bundleContent.parse();
175 
176  // store all the certificates in unverified cache
177  for (const auto& block : bundleContent.elements()) {
179  }
180 
181  auto cert = m_certStorage->getUnverifiedCertCache().find(certRequest->interest);
182  continueValidation(*cert, state);
183  }
184 }
185 
186 void
187 CertificateBundleFetcher::nackCallback(const lp::Nack& nack,
188  const shared_ptr<CertificateRequest>& certRequest,
189  const shared_ptr<ValidationState>& state,
190  const ValidationContinuation& continueValidation,
191  const Name& bundleName)
192 {
193  NDN_LOG_DEBUG_DEPTH("NACK (" << nack.getReason() << ") while fetching certificate bundle"
194  << bundleName);
195 
196  m_inner->fetch(certRequest, state, continueValidation);
197 }
198 
199 void
200 CertificateBundleFetcher::timeoutCallback(const shared_ptr<CertificateRequest>& certRequest,
201  const shared_ptr<ValidationState>& state,
202  const ValidationContinuation& continueValidation,
203  const Name& bundleName)
204 {
205  NDN_LOG_DEBUG_DEPTH("Timeout while fetching certificate bundle" << bundleName);
206 
207  m_inner->fetch(certRequest, state, continueValidation);
208 }
209 
210 Name
211 CertificateBundleFetcher::deriveBundleName(const Name& name)
212 {
213  name::Component lastComponent = name.at(-1);
214 
215  Name bundleName = name;
216  if (lastComponent.isImplicitSha256Digest()) {
217  if (name.size() >= 2 && name.get(-2).isSegment()) {
218  bundleName = name.getPrefix(-2);
219  }
220  else {
221  bundleName = name.getPrefix(-1);
222  }
223  }
224  else if (lastComponent.isSegment()) {
225  bundleName = name.getPrefix(-1);
226  }
227  bundleName.append("_BUNDLE");
228  bundleName.appendNumber(0);
229 
230  return bundleName;
231 }
232 
233 } // inline namespace v2
234 } // namespace security
235 } // namespace ndn
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:209
Copyright (c) 2011-2015 Regents of the University of California.
Represents an NDN certificate following the version 2.0 format.
Definition: certificate.hpp:60
#define NDN_LOG_DEBUG_DEPTH(x)
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:214
const Certificate * find(const Name &certPrefix) const
Get certificate given key name.
const Component & get(ssize_t i) const
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:162
void setBundleInterestLifetime(time::milliseconds time)
Set the lifetime of certificate bundle interest.
STL namespace.
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
void cacheUnverifiedCert(Certificate &&cert)
Cache unverified certificate for a period of time (5 minutes)
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
Represents an Interest packet.
Definition: interest.hpp:48
std::function< void(const Certificate &cert, const shared_ptr< ValidationState > &state)> ValidationContinuation
CertificateBundleFetcher(unique_ptr< CertificateFetcher > inner, Face &face)
Name & appendNumber(uint64_t number)
Append a component with a NonNegativeInteger.
Definition: name.hpp:387
Name & append(const Component &component)
Append a component.
Definition: name.hpp:275
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:425
represents a Network Nack
Definition: nack.hpp:38
Validation state for a data packet.
NackReason getReason() const
Definition: nack.hpp:90
provides a tag type for simple types
Definition: tag.hpp:58
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking...
Definition: name.cpp:171
static Component fromSegment(uint64_t segmentNo)
Create a segment number component using NDN naming conventions.
Component getSuccessor() const
Get the successor of this name component.
const CertificateCache & getUnverifiedCertCache() const
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:412
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:143
Storage for trusted anchors, verified certificate cache, and unverified certificate cache...
bool isSegment() const
Check if the component is a segment number per NDN naming conventions.
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
void doFetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Asynchronous certificate fetching implementation.
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
bool isImplicitSha256Digest() const
Check if the component is an ImplicitSha256DigestComponent.
Represents an absolute name.
Definition: name.hpp:41
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:123
size_t size() const
Returns the number of components.
Definition: name.hpp:151
Represents a name component.
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:175
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:81
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest&#39;s lifetime.
Definition: interest.cpp:435
Represents a Data packet.
Definition: data.hpp:37
const optional< name::Component > & getFinalBlock() const
Definition: data.hpp:293
void setCertificateStorage(CertificateStorage &certStorage) override
Set the storage for this and inner certificate fetcher.
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:195
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48