NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
23 #include "safe-bag.hpp"
25 #include "util/concepts.hpp"
26 
27 namespace ndn {
28 namespace security {
29 
30 BOOST_CONCEPT_ASSERT((WireEncodable<SafeBag>));
31 BOOST_CONCEPT_ASSERT((WireDecodable<SafeBag>));
32 
33 SafeBag::SafeBag() = default;
34 
35 SafeBag::SafeBag(const Block& wire)
36 {
37  this->wireDecode(wire);
38 }
39 
40 SafeBag::SafeBag(const Data& certificate,
41  const Buffer& encryptedKeyBag)
42  : m_certificate(certificate)
43  , m_encryptedKeyBag(encryptedKeyBag)
44 {
45 }
46 
47 SafeBag::SafeBag(const Data& certificate,
48  const uint8_t* encryptedKey,
49  size_t encryptedKeyLen)
50  : m_certificate(certificate)
51  , m_encryptedKeyBag(encryptedKey, encryptedKeyLen)
52 {
53 }
54 
56 
57 template<encoding::Tag TAG>
58 size_t
60 {
61  size_t totalLength = 0;
62 
63  // EncryptedKeyBag
64  totalLength += encoder.prependByteArrayBlock(tlv::security::EncryptedKeyBag,
65  m_encryptedKeyBag.get(),
66  m_encryptedKeyBag.size());
67 
68  // Certificate
69  totalLength += this->m_certificate.wireEncode(encoder);
70 
71  totalLength += encoder.prependVarNumber(totalLength);
72  totalLength += encoder.prependVarNumber(tlv::security::SafeBag);
73 
74  return totalLength;
75 }
76 
77 template size_t
78 SafeBag::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
79 
80 template size_t
81 SafeBag::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
82 
83 const Block&
85 {
86  EncodingEstimator estimator;
87  size_t estimatedSize = wireEncode(estimator);
88 
89  EncodingBuffer buffer(estimatedSize, 0);
90  wireEncode(buffer);
91 
92  this->m_wire = buffer.block();
93  return m_wire;
94 }
95 
96 void
98 {
99  if (wire.type() != tlv::security::SafeBag)
100  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding safebag"));
101 
102  this->m_wire = wire;
103  m_wire.parse();
104 
106 
107  // Certificate must be the first part
108  if (it != m_wire.elements_end()) {
109  this->m_certificate.wireDecode(*it);
110  it++;
111  }
112  else
113  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure when decoding certificate"));
114 
115  // EncryptedKeyBag
116  if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKeyBag) {
117  this->m_encryptedKeyBag = Buffer(it->value(), it->value_size());
118  it++;
119  }
120  else
121  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure when decoding encryptedkeybag"));
122 
123  // Check if end
124  if (it != m_wire.elements_end())
125  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure after decoding the block"));
126 }
127 
128 } // namespace security
129 } // namespace ndn
element_const_iterator elements_begin() const
Definition: block.cpp:589
Copyright (c) 2011-2015 Regents of the University of California.
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: data.cpp:135
EncodingImpl< EstimatorTag > EncodingEstimator
SafeBag()
Create a new empty SafeBag object.
element_const_iterator elements_end() const
Definition: block.cpp:595
uint8_t * get()
Definition: buffer.hpp:77
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: safe-bag.cpp:97
EncodingImpl< EncoderTag > EncodingBuffer
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:52
const Block & wireEncode() const
Encode to a wire format.
Definition: safe-bag.cpp:84
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
represents a Data packet
Definition: data.hpp:37
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
Class representing a general-use automatically managed/resized buffer.
Definition: buffer.hpp:44
uint32_t type() const
Definition: block.hpp:324
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50