NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
signature-info.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "signature-info.hpp"
24 #include "util/concepts.hpp"
25 
26 #include <boost/lexical_cast.hpp>
27 
28 namespace ndn {
29 
30 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
33 BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
34 static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value,
35  "SignatureInfo::Error must inherit from tlv::Error");
36 
38  : m_type(-1)
39  , m_hasKeyLocator(false)
40 {
41 }
42 
44  : m_type(type)
45  , m_hasKeyLocator(false)
46 {
47 }
48 
50  : m_type(type)
51  , m_hasKeyLocator(true)
52  , m_keyLocator(keyLocator)
53 {
54 }
55 
57 {
58  wireDecode(block);
59 }
60 
61 void
63 {
64  m_wire.reset();
65  m_type = type;
66 }
67 
68 void
70 {
71  m_wire.reset();
72  m_keyLocator = keyLocator;
73  m_hasKeyLocator = true;
74 }
75 
76 void
78 {
79  m_wire.reset();
80  m_keyLocator = KeyLocator();
81  m_hasKeyLocator = false;
82 }
83 
84 const KeyLocator&
86 {
87  if (m_hasKeyLocator)
88  return m_keyLocator;
89  else
90  BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist"));
91 }
92 
93 void
95 {
97  m_otherTlvs.push_front(validityPeriod.wireEncode());
98 }
99 
100 void
102 {
103  m_wire.reset();
104  if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) {
105  m_otherTlvs.erase(m_otherTlvs.begin());
106  }
107 }
108 
111 {
112  if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) {
113  BOOST_THROW_EXCEPTION(Error("SignatureInfo does not contain the requested ValidityPeriod field"));
114  }
115 
116  return security::ValidityPeriod(m_otherTlvs.front());
117 }
118 
119 void
121 {
122  m_wire.reset();
123  m_otherTlvs.push_back(block);
124 }
125 
126 
127 const Block&
129 {
130  for (std::list<Block>::const_iterator i = m_otherTlvs.begin();
131  i != m_otherTlvs.end(); i++) {
132  if (i->type() == type)
133  return *i;
134  }
135 
136  BOOST_THROW_EXCEPTION(Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" +
137  boost::lexical_cast<std::string>(type) + "] from SignatureInfo"));
138 }
139 
140 template<encoding::Tag TAG>
141 size_t
143 {
144  size_t totalLength = 0;
145 
146  for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
147  i != m_otherTlvs.rend(); i++) {
148  totalLength += encoder.appendBlock(*i);
149  }
150 
151  if (m_hasKeyLocator)
152  totalLength += m_keyLocator.wireEncode(encoder);
153 
154  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType, m_type);
155 
156  totalLength += encoder.prependVarNumber(totalLength);
157  totalLength += encoder.prependVarNumber(tlv::SignatureInfo);
158  return totalLength;
159 }
160 
161 template size_t
162 SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
163 
164 template size_t
165 SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
166 
167 
168 const Block&
170 {
171  if (m_wire.hasWire())
172  return m_wire;
173 
174  EncodingEstimator estimator;
175  size_t estimatedSize = wireEncode(estimator);
176 
177  EncodingBuffer buffer(estimatedSize, 0);
178  wireEncode(buffer);
179 
180  m_wire = buffer.block();
181  return m_wire;
182 }
183 
184 void
186 {
187  if (!wire.hasWire()) {
188  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
189  }
190 
191  m_hasKeyLocator = false;
192 
193  m_wire = wire;
194  m_wire.parse();
195 
196  if (m_wire.type() != tlv::SignatureInfo)
197  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name"));
198 
200 
201  // the first block must be SignatureType
202  if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
203  m_type = readNonNegativeInteger(*it);
204  it++;
205  }
206  else
207  BOOST_THROW_EXCEPTION(Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not "
208  "SignatureType"));
209 
210  // the second block could be KeyLocator
211  if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
212  m_keyLocator.wireDecode(*it);
213  m_hasKeyLocator = true;
214  it++;
215  }
216 
217  // Decode the rest of type-specific TLVs, if any
218  while (it != m_wire.elements_end()) {
219  m_otherTlvs.push_back(*it);
220  it++;
221  }
222 }
223 
224 bool
226 {
227  return (m_type == rhs.m_type &&
228  m_hasKeyLocator == rhs.m_hasKeyLocator &&
229  m_keyLocator == rhs.m_keyLocator &&
230  m_otherTlvs == rhs.m_otherTlvs);
231 }
232 
233 } // namespace ndn
element_const_iterator elements_begin() const
Definition: block.cpp:589
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
void setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
EncodingImpl< EstimatorTag > EncodingEstimator
const Block & getTypeSpecificTlv(uint32_t type) const
Get signature type specific tlv block.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
void wireDecode(const Block &wire)
decode from wire encoding
Definition: key-locator.cpp:99
element_const_iterator elements_end() const
Definition: block.cpp:595
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
const Block & wireEncode() const
Encode to a wire format.
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
void unsetValidityPeriod()
Unset ValidityPeriod.
void appendTypeSpecificTlv(const Block &block)
Append signature type specific tlv block.
void setKeyLocator(const KeyLocator &keyLocator)
Set KeyLocator.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
Abstraction of validity period.
bool operator==(const SignatureInfo &rhs) const
EncodingImpl< EncoderTag > EncodingBuffer
void unsetKeyLocator()
Unset KeyLocator.
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
void setValidityPeriod(const security::ValidityPeriod &validityPeriod)
Set ValidityPeriod.
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:51
void wireDecode(const Block &wire)
Decode from a wire format.
SignatureTypeValue
Definition: tlv.hpp:95
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
uint32_t type() const
Definition: block.hpp:324
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.