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 SafeBag::SafeBag(const Data& certificate,
49  const uint8_t* encryptedKey,
50  size_t encryptedKeyLen)
51  : m_certificate(certificate)
52  , m_encryptedKey(encryptedKey, encryptedKeyLen)
53 {
54 }
55 
56 template<encoding::Tag TAG>
57 size_t
59 {
60  size_t totalLength = 0;
61 
62  // EncryptedKey
63  totalLength += prependBinaryBlock(encoder, tlv::security::EncryptedKey, m_encryptedKey);
64 
65  // Certificate
66  totalLength += m_certificate.wireEncode(encoder);
67 
68  totalLength += encoder.prependVarNumber(totalLength);
69  totalLength += encoder.prependVarNumber(tlv::security::SafeBag);
70  return totalLength;
71 }
72 
74 
75 const Block&
77 {
78  EncodingEstimator estimator;
79  size_t estimatedSize = wireEncode(estimator);
80 
81  EncodingBuffer buffer(estimatedSize, 0);
82  wireEncode(buffer);
83 
84  m_wire = buffer.block();
85  return m_wire;
86 }
87 
88 void
90 {
91  if (wire.type() != tlv::security::SafeBag) {
92  NDN_THROW(tlv::Error("SafeBag", wire.type()));
93  }
94 
95  m_wire = wire;
96  m_wire.parse();
97  auto it = m_wire.elements_begin();
98 
99  // Certificate must be the first part
100  if (it != m_wire.elements_end()) {
101  m_certificate.wireDecode(*it);
102  it++;
103  }
104  else {
105  NDN_THROW(tlv::Error("Unexpected TLV structure when decoding Certificate"));
106  }
107 
108  // EncryptedKey
109  if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKey) {
110  m_encryptedKey = Buffer(it->value(), it->value_size());
111  it++;
112  }
113  else {
114  NDN_THROW(tlv::Error("Unexpected TLV structure when decoding EncryptedKey"));
115  }
116 
117  // Check if end
118  if (it != m_wire.elements_end()) {
119  NDN_THROW(tlv::Error("Unexpected TLV element at the end of SafeBag"));
120  }
121 }
122 
123 } // namespace security
124 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:418
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:410
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:42
Represents a Data packet.
Definition: data.hpp:38
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:125
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder.
Definition: data.cpp:46
A secured container for sensitive information (certificate, private key)
Definition: safe-bag.hpp:39
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: safe-bag.cpp:89
SafeBag()
Create a new empty SafeBag object.
const Block & wireEncode() const
Encode to a wire format.
Definition: safe-bag.cpp:76
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependBinaryBlock(EncodingImpl< TAG > &encoder, uint32_t type, span< const uint8_t > value)
Prepend a TLV element containing a sequence of raw bytes.
EncodingImpl< EncoderTag > EncodingBuffer
Definition: data.cpp:25