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-2022 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 
25 namespace ndn {
26 namespace security {
27 namespace detail {
28 
29 void
31 {
32  if (m_hasError) {
33  NDN_THROW(tlv::Error("Unrecoverable decoding error"));
34  }
35 
36  m_bufferedData.insert(m_bufferedData.end(), segment.value_begin(), segment.value_end());
37  decode();
38 }
39 
40 void
41 CertificateBundleDecoder::decode()
42 {
43  auto onThrow = make_scope_fail([this] { m_hasError = true; });
44 
45  while (!m_bufferedData.empty()) {
46  bool isOk;
47  Block element;
48  std::tie(isOk, element) = Block::fromBuffer(m_bufferedData);
49  if (!isOk) {
50  return;
51  }
52 
53  m_bufferedData.erase(m_bufferedData.begin(), m_bufferedData.begin() + element.size());
54 
55  if (element.type() == tlv::Data) {
56  onCertDecoded(Certificate(element));
57  }
58  else if (tlv::isCriticalType(element.type())) {
59  NDN_THROW(tlv::Error("Unrecognized element of critical type " + to_string(element.type())));
60  }
61  // unrecognized non-critical elements are silently skipped
62  }
63 }
64 
65 } // namespace detail
66 } // namespace security
67 } // namespace ndn
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:311
size_t size() const
Return the size of the encoded wire, i.e., of the whole TLV.
Definition: block.cpp:294
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset=0)
Try to parse Block from a wire buffer.
Definition: block.cpp:162
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
const_iterator value_begin() const noexcept
Get begin iterator of TLV-VALUE.
Definition: block.hpp:301
util::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:61
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:31
@ 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
Definition: data.cpp:25