NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
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 
25 
26 namespace ndn {
27 
28 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
29 BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
31 BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
32 static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value,
33  "SignatureInfo::Error must inherit from tlv::Error");
34 
36  : m_type(-1)
37  , m_hasKeyLocator(false)
38 {
39 }
40 
42  : m_type(type)
43  , m_hasKeyLocator(false)
44 {
45 }
46 
48  : m_type(type)
49  , m_hasKeyLocator(true)
50  , m_keyLocator(keyLocator)
51 {
52 }
53 
55 {
56  wireDecode(block);
57 }
58 
59 template<encoding::Tag TAG>
60 size_t
62 {
63  if (m_type == -1) {
64  NDN_THROW(Error("Cannot encode invalid SignatureInfo"));
65  }
66 
67  // SignatureInfo ::= SIGNATURE-INFO-TLV TLV-LENGTH
68  // SignatureType
69  // KeyLocator?
70  // ValidityPeriod? (if present, stored as first item of m_otherTlvs)
71  // other SignatureType-specific sub-elements*
72 
73  size_t totalLength = 0;
74 
75  for (auto i = m_otherTlvs.rbegin(); i != m_otherTlvs.rend(); i++) {
76  totalLength += encoder.prependBlock(*i);
77  }
78 
79  if (m_hasKeyLocator)
80  totalLength += m_keyLocator.wireEncode(encoder);
81 
82  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType,
83  static_cast<uint64_t>(m_type));
84  totalLength += encoder.prependVarNumber(totalLength);
85  totalLength += encoder.prependVarNumber(tlv::SignatureInfo);
86 
87  return totalLength;
88 }
89 
91 
92 const Block&
94 {
95  if (m_wire.hasWire())
96  return m_wire;
97 
98  EncodingEstimator estimator;
99  size_t estimatedSize = wireEncode(estimator);
100 
101  EncodingBuffer buffer(estimatedSize, 0);
102  wireEncode(buffer);
103 
104  m_wire = buffer.block();
105  return m_wire;
106 }
107 
108 void
110 {
111  m_type = -1;
112  m_hasKeyLocator = false;
113  m_otherTlvs.clear();
114 
115  m_wire = wire;
116  m_wire.parse();
117 
118  if (m_wire.type() != tlv::SignatureInfo)
119  NDN_THROW(Error("SignatureInfo", m_wire.type()));
120 
121  auto it = m_wire.elements_begin();
122 
123  // the first sub-element must be SignatureType
124  if (it == m_wire.elements_end() || it->type() != tlv::SignatureType)
125  NDN_THROW(Error("Missing SignatureType in SignatureInfo"));
126 
127  m_type = readNonNegativeIntegerAs<tlv::SignatureTypeValue>(*it);
128  ++it;
129 
130  // the second sub-element could be KeyLocator
131  if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
132  m_keyLocator.wireDecode(*it);
133  m_hasKeyLocator = true;
134  ++it;
135  }
136 
137  // store SignatureType-specific sub-elements, if any
138  while (it != m_wire.elements_end()) {
139  m_otherTlvs.push_back(*it);
140  ++it;
141  }
142 }
143 
144 void
146 {
147  m_wire.reset();
148  m_type = type;
149 }
150 
151 const KeyLocator&
153 {
154  if (m_hasKeyLocator)
155  return m_keyLocator;
156  else
157  NDN_THROW(Error("KeyLocator does not exist in SignatureInfo"));
158 }
159 
160 void
162 {
163  m_wire.reset();
164  m_keyLocator = keyLocator;
165  m_hasKeyLocator = true;
166 }
167 
168 void
170 {
171  m_wire.reset();
172  m_keyLocator = KeyLocator();
173  m_hasKeyLocator = false;
174 }
175 
178 {
179  if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) {
180  NDN_THROW(Error("ValidityPeriod does not exist in SignatureInfo"));
181  }
182 
183  return security::ValidityPeriod(m_otherTlvs.front());
184 }
185 
186 void
188 {
190  m_otherTlvs.push_front(validityPeriod.wireEncode());
191 }
192 
193 void
195 {
196  if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) {
197  m_otherTlvs.pop_front();
198  m_wire.reset();
199  }
200 }
201 
202 const Block&
204 {
205  for (const Block& block : m_otherTlvs) {
206  if (block.type() == type)
207  return block;
208  }
209 
210  NDN_THROW(Error("TLV-TYPE " + to_string(type) + " sub-element does not exist in SignatureInfo"));
211 }
212 
213 void
215 {
216  m_wire.reset();
217  m_otherTlvs.push_back(block);
218 }
219 
220 bool
221 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs)
222 {
223  return lhs.m_type == rhs.m_type &&
224  lhs.m_hasKeyLocator == rhs.m_hasKeyLocator &&
225  lhs.m_keyLocator == rhs.m_keyLocator &&
226  lhs.m_otherTlvs == rhs.m_otherTlvs;
227 }
228 
229 std::ostream&
230 operator<<(std::ostream& os, const SignatureInfo& info)
231 {
232  if (info.getSignatureType() == -1)
233  return os << "Invalid SignatureInfo";
234 
235  os << static_cast<tlv::SignatureTypeValue>(info.getSignatureType());
236  if (info.hasKeyLocator()) {
237  os << " " << info.getKeyLocator();
238  }
239  if (!info.m_otherTlvs.empty()) {
240  os << " { ";
241  for (const auto& block : info.m_otherTlvs) {
242  os << block.type() << " ";
243  }
244  os << "}";
245  }
246 
247  return os;
248 }
249 
250 } // namespace ndn
ndn::tlv::ValidityPeriod
@ ValidityPeriod
Definition: tlv.hpp:143
ndn::SignatureInfo::setSignatureType
void setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
Definition: signature-info.cpp:145
signature-info.hpp
ndn::SignatureInfo::getTypeSpecificTlv
const Block & getTypeSpecificTlv(uint32_t type) const
Get SignatureType-specific sub-element.
Definition: signature-info.cpp:203
ndn::SignatureInfo::wireDecode
void wireDecode(const Block &wire)
Decode from wire format.
Definition: signature-info.cpp:109
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::SignatureInfo::SignatureInfo
SignatureInfo()
Create an invalid SignatureInfo.
Definition: signature-info.cpp:35
concepts.hpp
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::SignatureInfo::getValidityPeriod
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.
Definition: signature-info.cpp:177
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::KeyLocator::wireDecode
void wireDecode(const Block &wire)
Decode from wire encoding.
Definition: key-locator.cpp:90
ndn::WireEncodable
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:45
ndn::SignatureInfo::getKeyLocator
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Definition: signature-info.cpp:152
ndn::security::ValidityPeriod
Abstraction of validity period.
Definition: validity-period.hpp:38
ndn::security::ValidityPeriod::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: validity-period.cpp:63
ndn::SignatureInfo::unsetKeyLocator
void unsetKeyLocator()
Unset KeyLocator.
Definition: signature-info.cpp:169
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::SignatureInfo
Represents a SignatureInfo TLV element.
Definition: signature-info.hpp:35
ndn::SignatureInfo::getSignatureType
int32_t getSignatureType() const
Get SignatureType.
Definition: signature-info.hpp:85
ndn::tlv::SignatureTypeValue
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:129
ndn::SignatureInfo::Error
Definition: signature-info.hpp:38
ndn::SignatureInfo::wireEncode
const Block & wireEncode() const
Encode to wire format.
Definition: signature-info.cpp:93
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::encoding::prependNonNegativeIntegerBlock
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Definition: block-helpers.cpp:35
ndn::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Interest)
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
block-helpers.hpp
ndn::tlv::KeyLocator
@ KeyLocator
Definition: tlv.hpp:86
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::SignatureInfo::unsetValidityPeriod
void unsetValidityPeriod()
Unset ValidityPeriod.
Definition: signature-info.cpp:194
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::Block::elements_end
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
ndn::operator==
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:312
ndn::operator<<
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
ndn::KeyLocator::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: key-locator.cpp:52
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::SignatureInfo::appendTypeSpecificTlv
void appendTypeSpecificTlv(const Block &element)
Append SignatureType-specific sub-element.
Definition: signature-info.cpp:214
ndn::SignatureInfo::setValidityPeriod
void setValidityPeriod(const security::ValidityPeriod &validityPeriod)
Set ValidityPeriod.
Definition: signature-info.cpp:187
ndn::KeyLocator
Definition: key-locator.hpp:30
ndn::SignatureInfo::hasKeyLocator
bool hasKeyLocator() const
Check if KeyLocator exists.
Definition: signature-info.hpp:98
ndn::WireEncodableWithEncodingBuffer
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:61
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
ndn::tlv::SignatureType
@ SignatureType
Definition: tlv.hpp:85
ndn::tlv::SignatureInfo
@ SignatureInfo
Definition: tlv.hpp:80
ndn::SignatureInfo::setKeyLocator
void setKeyLocator(const KeyLocator &keyLocator)
Set KeyLocator.
Definition: signature-info.cpp:161