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 
33 {
34 public:
35  class Error : public tlv::Error
36  {
37  public:
38  using tlv::Error::Error;
39  };
40 
41  enum class Type : uint32_t {
44  };
45 
46 public:
48 
51  explicit
52  SignatureInfo(tlv::SignatureTypeValue type, optional<KeyLocator> keyLocator = nullopt);
53 
59  explicit
60  SignatureInfo(const Block& wire, Type type = Type::Data);
61 
64  explicit
65  operator bool() const noexcept
66  {
67  return m_type != -1;
68  }
69 
78  template<encoding::Tag TAG>
79  size_t
80  wireEncode(EncodingImpl<TAG>& encoder, Type type = Type::Data) const;
81 
89  const Block&
90  wireEncode(Type type = Type::Data) const;
91 
97  void
98  wireDecode(const Block& wire, Type type = Type::Data);
99 
102  bool
103  hasWire() const noexcept
104  {
105  return m_wire.hasWire();
106  }
107 
108 public: // field access
112  int32_t
113  getSignatureType() const noexcept
114  {
115  return m_type;
116  }
117 
123 
126  bool
127  hasKeyLocator() const noexcept
128  {
129  return m_keyLocator.has_value();
130  }
131 
135  const KeyLocator&
136  getKeyLocator() const;
137 
144  setKeyLocator(optional<KeyLocator> keyLocator);
145 
150  getValidityPeriod() const;
151 
158  setValidityPeriod(optional<security::ValidityPeriod> validityPeriod);
159 
163  optional<std::vector<uint8_t>>
164  getNonce() const;
165 
172  setNonce(optional<span<const uint8_t>> nonce);
173 
177  optional<time::system_clock::time_point>
178  getTime() const;
179 
186  setTime(optional<time::system_clock::time_point> time = time::system_clock::now());
187 
191  optional<uint64_t>
192  getSeqNum() const;
193 
200  setSeqNum(optional<uint64_t> seqNum);
201 
206  optional<Block>
207  getCustomTlv(uint32_t type) const;
208 
213  void
214  addCustomTlv(Block block);
215 
219  void
220  removeCustomTlv(uint32_t type);
221 
222 private:
223  std::vector<Block>::const_iterator
224  findOtherTlv(uint32_t type) const;
225 
226 private:
227  int32_t m_type = -1;
228  optional<KeyLocator> m_keyLocator;
229  std::vector<Block> m_otherTlvs;
230 
231  mutable Block m_wire;
232 
233  friend bool
234  operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
235 
236  friend std::ostream&
237  operator<<(std::ostream& os, const SignatureInfo& info);
238 };
239 
240 #ifndef DOXYGEN
241 extern template size_t
242 SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingBuffer&, SignatureInfo::Type) const;
243 
244 extern template size_t
245 SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, SignatureInfo::Type) const;
246 #endif
247 
248 bool
249 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
250 
251 inline bool
252 operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs)
253 {
254  return !(lhs == rhs);
255 }
256 
257 std::ostream&
258 operator<<(std::ostream& os, const SignatureInfo& info);
259 
260 } // namespace ndn
261 
262 #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:241
Represents a Data packet.
Definition: data.hpp:38
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 SignatureType.
SignatureInfo & setValidityPeriod(optional< security::ValidityPeriod > validityPeriod)
Append or replace ValidityPeriod.
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 ValidityPeriod.
optional< uint64_t > getSeqNum() const
Get SignatureSeqNum.
int32_t getSignatureType() const noexcept
Get 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 KeyLocator.
SignatureInfo & setKeyLocator(optional< KeyLocator > keyLocator)
Set KeyLocator.
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:53
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
@ SignatureInfo
Definition: tlv.hpp:82
@ InterestSignatureInfo
Definition: tlv.hpp:78
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:132
Definition: data.cpp:25
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:350
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:366
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
SignatureInfo info