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; -*- */
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>));
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
61 SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const
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, m_type);
83 
84  totalLength += encoder.prependVarNumber(totalLength);
85  totalLength += encoder.prependVarNumber(tlv::SignatureInfo);
86  return totalLength;
87 }
88 
90 
91 const Block&
93 {
94  if (m_wire.hasWire())
95  return m_wire;
96 
97  EncodingEstimator estimator;
98  size_t estimatedSize = wireEncode(estimator);
99 
100  EncodingBuffer buffer(estimatedSize, 0);
101  wireEncode(buffer);
102 
103  m_wire = buffer.block();
104  return m_wire;
105 }
106 
107 void
109 {
110  m_hasKeyLocator = false;
111  m_otherTlvs.clear();
112 
113  m_wire = wire;
114  m_wire.parse();
115 
116  if (m_wire.type() != tlv::SignatureInfo)
117  BOOST_THROW_EXCEPTION(Error("Decoding SignatureInfo, but TLV-TYPE is " + to_string(m_wire.type())));
118 
120 
121  // the first sub-element must be SignatureType
122  if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
123  m_type = readNonNegativeIntegerAs<int32_t>(*it);
124  ++it;
125  }
126  else
127  BOOST_THROW_EXCEPTION(Error("Missing SignatureType in SignatureInfo"));
128 
129  // the second sub-element could be KeyLocator
130  if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
131  m_keyLocator.wireDecode(*it);
132  m_hasKeyLocator = true;
133  ++it;
134  }
135 
136  // store SignatureType-specific sub-elements, if any
137  while (it != m_wire.elements_end()) {
138  m_otherTlvs.push_back(*it);
139  ++it;
140  }
141 }
142 
143 void
145 {
146  m_wire.reset();
147  m_type = type;
148 }
149 
150 const KeyLocator&
152 {
153  if (m_hasKeyLocator)
154  return m_keyLocator;
155  else
156  BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist in SignatureInfo"));
157 }
158 
159 void
161 {
162  m_wire.reset();
163  m_keyLocator = keyLocator;
164  m_hasKeyLocator = true;
165 }
166 
167 void
169 {
170  m_wire.reset();
171  m_keyLocator = KeyLocator();
172  m_hasKeyLocator = false;
173 }
174 
177 {
178  if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) {
179  BOOST_THROW_EXCEPTION(Error("ValidityPeriod does not exist in SignatureInfo"));
180  }
181 
182  return security::ValidityPeriod(m_otherTlvs.front());
183 }
184 
185 void
187 {
189  m_otherTlvs.push_front(validityPeriod.wireEncode());
190 }
191 
192 void
194 {
195  if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) {
196  m_otherTlvs.pop_front();
197  m_wire.reset();
198  }
199 }
200 
201 const Block&
203 {
204  for (const Block& block : m_otherTlvs) {
205  if (block.type() == type)
206  return block;
207  }
208 
209  BOOST_THROW_EXCEPTION(Error("TLV-TYPE " + to_string(type) + " sub-element does not exist in SignatureInfo"));
210 }
211 
212 void
214 {
215  m_wire.reset();
216  m_otherTlvs.push_back(block);
217 }
218 
219 bool
220 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs)
221 {
222  return lhs.m_type == rhs.m_type &&
223  lhs.m_hasKeyLocator == rhs.m_hasKeyLocator &&
224  lhs.m_keyLocator == rhs.m_keyLocator &&
225  lhs.m_otherTlvs == rhs.m_otherTlvs;
226 }
227 
228 std::ostream&
229 operator<<(std::ostream& os, const SignatureInfo& info)
230 {
231  os << static_cast<tlv::SignatureTypeValue>(info.getSignatureType());
232  if (info.hasKeyLocator()) {
233  os << " " << info.getKeyLocator();
234  }
235  if (!info.m_otherTlvs.empty()) {
236  os << " { ";
237  for (const auto& block : info.m_otherTlvs) {
238  os << block.type() << " ";
239  }
240  os << "}";
241  }
242  return os;
243 }
244 
245 } // 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:52
friend std::ostream & operator<<(std::ostream &os, const SignatureInfo &info)
Copyright (c) 2011-2015 Regents of the University of California.
void setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
const Block & wireEncode() const
Encode to wire format.
Represents a SignatureInfo TLV element.
const Block & getTypeSpecificTlv(uint32_t type) const
Get SignatureType-specific sub-element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
void wireDecode(const Block &wire)
decode from wire encoding
Definition: key-locator.cpp:96
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
void appendTypeSpecificTlv(const Block &element)
Append SignatureType-specific sub-element.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
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
friend bool operator==(const SignatureInfo &lhs, const SignatureInfo &rhs)
Abstraction of validity period.
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.
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:256
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:334
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
void wireDecode(const Block &wire)
Decode from wire format.
SignatureTypeValue
Definition: tlv.hpp:99
bool hasKeyLocator() const
Check if KeyLocator exists.
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:250
std::string to_string(const V &v)
Definition: backports.hpp:84
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
int32_t getSignatureType() const
Get SignatureType.
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
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
EncodingImpl< EstimatorTag > EncodingEstimator