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-2017 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 
22 #include "signature-info.hpp"
24 #include "util/concepts.hpp"
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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(Error("Decoding SignatureInfo, but TLV-TYPE is " + to_string(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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(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
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:251
Copyright (c) 2011-2015 Regents of the University of California.
void setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Represents a SignatureInfo TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:274
const Block & getTypeSpecificTlv(uint32_t type) const
Get SignatureType-specific sub-element.
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:96
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:335
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
const Block & wireEncode() const
Encode to wire format.
void appendTypeSpecificTlv(const Block &element)
Append SignatureType-specific sub-element.
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
void unsetValidityPeriod()
Unset ValidityPeriod.
void setKeyLocator(const KeyLocator &keyLocator)
Set KeyLocator.
SignatureInfo()
Create an invalid SignatureInfo.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
Abstraction of validity period.
bool hasKeyLocator() const
Check if KeyLocator exists.
int32_t getSignatureType() const
Get SignatureType.
void unsetKeyLocator()
Unset KeyLocator.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
void setValidityPeriod(const security::ValidityPeriod &validityPeriod)
Set ValidityPeriod.
void reset()
Reset wire buffer of the element.
Definition: block.cpp:257
SignatureTypeValue
Definition: tlv.hpp:97
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:52
void wireDecode(const Block &wire)
Decode from wire format.
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:265
std::string to_string(const V &v)
Definition: backports.hpp:84
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
EncodingImpl< EstimatorTag > EncodingEstimator
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.