certificate-bundle-decoder.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 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 #include "ndn-cxx/util/scope.hpp"
24 
26 
27 void
29 {
30  if (m_hasError) {
31  NDN_THROW(tlv::Error("Unrecoverable decoding error"));
32  }
33 
34  m_bufferedData.insert(m_bufferedData.end(), segment.value_begin(), segment.value_end());
35  decode();
36 }
37 
38 void
39 CertificateBundleDecoder::decode()
40 {
41  auto onThrow = make_scope_fail([this] { m_hasError = true; });
42 
43  while (!m_bufferedData.empty()) {
44  auto [isOk, element] = Block::fromBuffer(m_bufferedData);
45  if (!isOk) {
46  return;
47  }
48 
49  m_bufferedData.erase(m_bufferedData.begin(), m_bufferedData.begin() + element.size());
50 
51  if (element.type() == tlv::Data) {
52  onCertDecoded(Certificate(element));
53  }
54  else if (tlv::isCriticalType(element.type())) {
55  NDN_THROW(tlv::Error("Unrecognized element of critical type " + to_string(element.type())));
56  }
57  // unrecognized non-critical elements are silently skipped
58  }
59 }
60 
61 } // namespace ndn::security::detail
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
const_iterator value_end() const noexcept
Get end iterator of TLV-VALUE.
Definition: block.hpp:338
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset=0)
Try to parse Block from a wire buffer.
Definition: block.cpp:165
const_iterator value_begin() const noexcept
Get begin iterator of TLV-VALUE.
Definition: block.hpp:328
signal::Signal< CertificateBundleDecoder, Certificate > onCertDecoded
Emitted every time a certificate is successfully decoded.
void append(const Block &block)
Append a bundle segment to the internal decoding buffer and trigger decoding.
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
#define NDN_THROW(e)
Definition: exception.hpp:56
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
@ Data
Definition: tlv.hpp:69
constexpr bool isCriticalType(uint32_t type) noexcept
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Definition: tlv.hpp:162