signature-info.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 #ifndef NDN_CXX_SIGNATURE_INFO_HPP
23 #define NDN_CXX_SIGNATURE_INFO_HPP
24 
25 #include "ndn-cxx/key-locator.hpp"
27 
28 namespace ndn {
29 
34 {
35 public:
36  class Error : public tlv::Error
37  {
38  public:
39  using tlv::Error::Error;
40  };
41 
42  enum class Type : uint32_t {
45  };
46 
47 public:
49 
52  explicit
53  SignatureInfo(tlv::SignatureTypeValue type, optional<KeyLocator> keyLocator = nullopt);
54 
60  explicit
61  SignatureInfo(const Block& wire, Type type = Type::Data);
62 
65  explicit
66  operator bool() const noexcept
67  {
68  return m_type != -1;
69  }
70 
79  template<encoding::Tag TAG>
80  size_t
81  wireEncode(EncodingImpl<TAG>& encoder, Type type = Type::Data) const;
82 
90  const Block&
91  wireEncode(Type type = Type::Data) const;
92 
98  void
99  wireDecode(const Block& wire, Type type = Type::Data);
100 
103  bool
104  hasWire() const noexcept
105  {
106  return m_wire.hasWire();
107  }
108 
109 public: // field access
113  int32_t
114  getSignatureType() const noexcept
115  {
116  return m_type;
117  }
118 
124 
127  bool
128  hasKeyLocator() const noexcept
129  {
130  return m_keyLocator.has_value();
131  }
132 
136  const KeyLocator&
137  getKeyLocator() const;
138 
146  setKeyLocator(optional<KeyLocator> keyLocator);
147 
152  getValidityPeriod() const;
153 
161  setValidityPeriod(optional<security::ValidityPeriod> validityPeriod);
162 
166  optional<std::vector<uint8_t>>
167  getNonce() const;
168 
176  setNonce(optional<span<const uint8_t>> nonce);
177 
181  optional<time::system_clock::time_point>
182  getTime() const;
183 
191  setTime(optional<time::system_clock::time_point> time = time::system_clock::now());
192 
196  optional<uint64_t>
197  getSeqNum() const;
198 
206  setSeqNum(optional<uint64_t> seqNum);
207 
212  optional<Block>
213  getCustomTlv(uint32_t type) const;
214 
219  void
220  addCustomTlv(Block block);
221 
225  void
226  removeCustomTlv(uint32_t type);
227 
228 private:
229  std::vector<Block>::const_iterator
230  findOtherTlv(uint32_t type) const;
231 
232 private:
233  int32_t m_type = -1;
234  optional<KeyLocator> m_keyLocator;
235  std::vector<Block> m_otherTlvs;
236 
237  mutable Block m_wire;
238 
239  friend bool
240  operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
241 
242  friend std::ostream&
243  operator<<(std::ostream& os, const SignatureInfo& info);
244 };
245 
246 #ifndef DOXYGEN
247 extern template size_t
248 SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingBuffer&, SignatureInfo::Type) const;
249 
250 extern template size_t
251 SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, SignatureInfo::Type) const;
252 #endif
253 
254 bool
255 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
256 
257 inline bool
258 operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs)
259 {
260  return !(lhs == rhs);
261 }
262 
263 std::ostream&
264 operator<<(std::ostream& os, const SignatureInfo& info);
265 
266 } // namespace ndn
267 
268 #endif // NDN_CXX_SIGNATURE_INFO_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
Represents a Data packet.
Definition: data.hpp:39
Represents an Interest packet.
Definition: interest.hpp:50
Represents a SignatureInfo or InterestSignatureInfo TLV element.
size_t wireEncode(EncodingImpl< TAG > &encoder, Type type=Type::Data) const
Fast encoding or block size estimation.
SignatureInfo & setSignatureType(tlv::SignatureTypeValue type)
Set the SignatureType.
SignatureInfo & setValidityPeriod(optional< security::ValidityPeriod > validityPeriod)
Append, replace, or remove the ValidityPeriod element.
SignatureInfo & setNonce(optional< span< const uint8_t >> nonce)
Append or replace SignatureNonce.
optional< std::vector< uint8_t > > getNonce() const
Get SignatureNonce.
void addCustomTlv(Block block)
Append an arbitrary TLV element to this SignatureInfo.
security::ValidityPeriod getValidityPeriod() const
Get the ValidityPeriod element.
optional< uint64_t > getSeqNum() const
Get SignatureSeqNum.
int32_t getSignatureType() const noexcept
Get the SignatureType.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
optional< Block > getCustomTlv(uint32_t type) const
Get first custom TLV element with the specified TLV-TYPE.
void removeCustomTlv(uint32_t type)
Remove all arbitrary TLV elements with the specified TLV-TYPE from this SignatureInfo.
optional< time::system_clock::time_point > getTime() const
Get SignatureTime.
void wireDecode(const Block &wire, Type type=Type::Data)
Decode from wire format.
SignatureInfo & setTime(optional< time::system_clock::time_point > time=time::system_clock::now())
Append or replace SignatureTime.
SignatureInfo & setSeqNum(optional< uint64_t > seqNum)
Append or replace SignatureSeqNum.
friend std::ostream & operator<<(std::ostream &os, const SignatureInfo &info)
friend bool operator==(const SignatureInfo &lhs, const SignatureInfo &rhs)
const KeyLocator & getKeyLocator() const
Get the KeyLocator element.
SignatureInfo & setKeyLocator(optional< KeyLocator > keyLocator)
Set or unset the KeyLocator element.
Represents a ValidityPeriod TLV element.
static time_point now() noexcept
Definition: time.cpp:46
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
@ SignatureInfo
Definition: tlv.hpp:94
@ InterestSignatureInfo
Definition: tlv.hpp:89
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:127
Definition: data.cpp:25
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:364
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:364
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:374
SignatureInfo info