NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
safe-bag.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  * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
22  */
23 
28 
29 namespace ndn {
30 namespace security {
31 
32 BOOST_CONCEPT_ASSERT((WireEncodable<SafeBag>));
33 BOOST_CONCEPT_ASSERT((WireDecodable<SafeBag>));
34 
35 SafeBag::SafeBag() = default;
36 
37 SafeBag::SafeBag(const Block& wire)
38 {
39  this->wireDecode(wire);
40 }
41 
42 SafeBag::SafeBag(const Data& certificate, span<const uint8_t> encryptedKey)
43  : m_certificate(certificate)
44  , m_encryptedKey(encryptedKey.begin(), encryptedKey.end())
45 {
46 }
47 
48 template<encoding::Tag TAG>
49 size_t
51 {
52  size_t totalLength = 0;
53 
54  // EncryptedKey
55  totalLength += prependBinaryBlock(encoder, tlv::security::EncryptedKey, m_encryptedKey);
56 
57  // Certificate
58  totalLength += m_certificate.wireEncode(encoder);
59 
60  totalLength += encoder.prependVarNumber(totalLength);
61  totalLength += encoder.prependVarNumber(tlv::security::SafeBag);
62  return totalLength;
63 }
64 
66 
67 const Block&
69 {
70  EncodingEstimator estimator;
71  size_t estimatedSize = wireEncode(estimator);
72 
73  EncodingBuffer buffer(estimatedSize, 0);
74  wireEncode(buffer);
75 
76  m_wire = buffer.block();
77  return m_wire;
78 }
79 
80 void
82 {
83  if (wire.type() != tlv::security::SafeBag) {
84  NDN_THROW(tlv::Error("SafeBag", wire.type()));
85  }
86 
87  m_wire = wire;
88  m_wire.parse();
89  auto it = m_wire.elements_begin();
90 
91  // Certificate must be the first part
92  if (it != m_wire.elements_end()) {
93  m_certificate.wireDecode(*it);
94  it++;
95  }
96  else {
97  NDN_THROW(tlv::Error("Unexpected TLV structure when decoding Certificate"));
98  }
99 
100  // EncryptedKey
101  if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKey) {
102  m_encryptedKey = Buffer(it->value(), it->value_size());
103  it++;
104  }
105  else {
106  NDN_THROW(tlv::Error("Unexpected TLV structure when decoding EncryptedKey"));
107  }
108 
109  // Check if end
110  if (it != m_wire.elements_end()) {
111  NDN_THROW(tlv::Error("Unexpected TLV element at the end of SafeBag"));
112  }
113 }
114 
115 } // namespace security
116 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:125
SafeBag()
Create a new empty SafeBag object.
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:433
#define NDN_THROW(e)
Definition: exception.hpp:61
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:441
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: safe-bag.cpp:81
size_t prependBinaryBlock(EncodingImpl< TAG > &encoder, uint32_t type, span< const uint8_t > value)
Prepend a TLV element containing a sequence of raw bytes.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SafeBag)
A secured container for sensitive information (certificate, private key)
Definition: safe-bag.hpp:38
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder.
Definition: data.cpp:46
const Block & wireEncode() const
Encode to wire format.
Definition: safe-bag.cpp:68
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
Represents a Data packet.
Definition: data.hpp:37
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
EncodingImpl< EncoderTag > EncodingBuffer
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator