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-2019 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,
43  const Buffer& encryptedKeyBag)
44  : m_certificate(certificate)
45  , m_encryptedKeyBag(encryptedKeyBag)
46 {
47 }
48 
49 SafeBag::SafeBag(const Data& certificate,
50  const uint8_t* encryptedKey,
51  size_t encryptedKeyLen)
52  : m_certificate(certificate)
53  , m_encryptedKeyBag(encryptedKey, encryptedKeyLen)
54 {
55 }
56 
58 
59 template<encoding::Tag TAG>
60 size_t
62 {
63  size_t totalLength = 0;
64 
65  // EncryptedKeyBag
66  totalLength += encoder.prependByteArrayBlock(tlv::security::EncryptedKeyBag,
67  m_encryptedKeyBag.data(),
68  m_encryptedKeyBag.size());
69 
70  // Certificate
71  totalLength += this->m_certificate.wireEncode(encoder);
72 
73  totalLength += encoder.prependVarNumber(totalLength);
74  totalLength += encoder.prependVarNumber(tlv::security::SafeBag);
75 
76  return totalLength;
77 }
78 
80 
81 const Block&
83 {
84  EncodingEstimator estimator;
85  size_t estimatedSize = wireEncode(estimator);
86 
87  EncodingBuffer buffer(estimatedSize, 0);
88  wireEncode(buffer);
89 
90  m_wire = buffer.block();
91  return m_wire;
92 }
93 
94 void
96 {
97  if (wire.type() != tlv::security::SafeBag) {
98  NDN_THROW(tlv::Error("SafeBag", wire.type()));
99  }
100 
101  m_wire = wire;
102  m_wire.parse();
103  auto it = m_wire.elements_begin();
104 
105  // Certificate must be the first part
106  if (it != m_wire.elements_end()) {
107  m_certificate.wireDecode(*it);
108  it++;
109  }
110  else {
111  NDN_THROW(tlv::Error("Unexpected TLV structure when decoding Certificate"));
112  }
113 
114  // EncryptedKeyBag
115  if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKeyBag) {
116  m_encryptedKeyBag = Buffer(it->value(), it->value_size());
117  it++;
118  }
119  else {
120  NDN_THROW(tlv::Error("Unexpected TLV structure when decoding EncryptedKeyBag"));
121  }
122 
123  // Check if end
124  if (it != m_wire.elements_end()) {
125  NDN_THROW(tlv::Error("Unexpected TLV element at the end of SafeBag"));
126  }
127 }
128 
129 } // namespace security
130 } // namespace ndn
ndn::security::SafeBag
a secured container for sensitive information(certificate, private key)
Definition: safe-bag.hpp:38
ndn::security::SafeBag::wireDecode
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: safe-bag.cpp:95
ndn::Buffer
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::Data::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
Definition: data.cpp:48
concepts.hpp
ndn::WireDecodable
a concept check for TLV abstraction with .wireDecode method and constructible from Block
Definition: concepts.hpp:81
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::WireEncodable
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:45
ndn::tlv::security::EncryptedKeyBag
@ EncryptedKeyBag
Definition: tlv-security.hpp:33
ndn::Data::wireDecode
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: data.cpp:122
tlv-security.hpp
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::security::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SafeBag)
ndn::security::SafeBag::SafeBag
SafeBag()
Create a new empty SafeBag object.
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_end
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
encoding-buffer.hpp
ndn::security::SafeBag::wireEncode
const Block & wireEncode() const
Encode to a wire format.
Definition: safe-bag.cpp:82
safe-bag.hpp
ndn::tlv::security::SafeBag
@ SafeBag
Definition: tlv-security.hpp:32
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38