NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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_otherTlvs.push_back(block);
123 }
124 
125 
126 const Block&
128 {
129  for (std::list<Block>::const_iterator i = m_otherTlvs.begin();
130  i != m_otherTlvs.end(); i++) {
131  if (i->type() == type)
132  return *i;
133  }
134 
135  BOOST_THROW_EXCEPTION(Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" +
136  boost::lexical_cast<std::string>(type) + "] from SignatureInfo"));
137 }
138 
139 template<encoding::Tag TAG>
140 size_t
141 SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const
142 {
143  size_t totalLength = 0;
144 
145  for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
146  i != m_otherTlvs.rend(); i++) {
147  totalLength += encoder.appendBlock(*i);
148  }
149 
150  if (m_hasKeyLocator)
151  totalLength += m_keyLocator.wireEncode(encoder);
152 
153  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType, m_type);
154 
155  totalLength += encoder.prependVarNumber(totalLength);
156  totalLength += encoder.prependVarNumber(tlv::SignatureInfo);
157  return totalLength;
158 }
159 
160 template size_t
161 SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
162 
163 template size_t
164 SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
165 
166 
167 const Block&
169 {
170  if (m_wire.hasWire())
171  return m_wire;
172 
173  EncodingEstimator estimator;
174  size_t estimatedSize = wireEncode(estimator);
175 
176  EncodingBuffer buffer(estimatedSize, 0);
177  wireEncode(buffer);
178 
179  m_wire = buffer.block();
180  return m_wire;
181 }
182 
183 void
185 {
186  if (!wire.hasWire()) {
187  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
188  }
189 
190  m_hasKeyLocator = false;
191 
192  m_wire = wire;
193  m_wire.parse();
194 
195  if (m_wire.type() != tlv::SignatureInfo)
196  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name"));
197 
199 
200  // the first block must be SignatureType
201  if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
202  m_type = readNonNegativeInteger(*it);
203  it++;
204  }
205  else
206  BOOST_THROW_EXCEPTION(Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not "
207  "SignatureType"));
208 
209  // the second block could be KeyLocator
210  if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
211  m_keyLocator.wireDecode(*it);
212  m_hasKeyLocator = true;
213  it++;
214  }
215 
216  // Decode the rest of type-specific TLVs, if any
217  while (it != m_wire.elements_end()) {
219  it++;
220  }
221 }
222 
223 bool
225 {
226  return (m_type == rhs.m_type &&
227  m_hasKeyLocator == rhs.m_hasKeyLocator &&
228  m_keyLocator == rhs.m_keyLocator &&
229  m_otherTlvs == rhs.m_otherTlvs);
230 }
231 
232 } // namespace ndn
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:51
Copyright (c) 2011-2015 Regents of the University of California.
void setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
const Block & wireEncode() const
Encode to a wire format.
const Block & getTypeSpecificTlv(uint32_t type) const
Get signature type specific tlv block.
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
void wireDecode(const Block &wire)
decode from wire encoding
Definition: key-locator.cpp:99
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
const KeyLocator & getKeyLocator() const
Get KeyLocator.
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.
element_const_iterator elements_end() const
Definition: block.cpp:595
void setKeyLocator(const KeyLocator &keyLocator)
Set KeyLocator.
element_const_iterator elements_begin() const
Definition: block.cpp:589
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
Abstraction of validity period.
EncodingImpl< EncoderTag > EncodingBuffer
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.
void unsetKeyLocator()
Unset KeyLocator.
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
void setValidityPeriod(const security::ValidityPeriod &validityPeriod)
Set ValidityPeriod.
bool operator==(const SignatureInfo &rhs) const
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:346
void wireDecode(const Block &wire)
Decode from a wire format.
SignatureTypeValue
Definition: tlv.hpp:95
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
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
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50