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-2020 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_SIGNATURE_INFO_HPP
23 #define NDN_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:
47  SignatureInfo();
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 
149  [[deprecated("use setKeyLocator(nullopt)")]]
150  void
151  unsetKeyLocator();
152 
157  getValidityPeriod() const;
158 
165  setValidityPeriod(optional<security::ValidityPeriod> validityPeriod);
166 
170  [[deprecated("use setValidityPeriod(nullopt)")]]
171  void
173 
177  optional<std::vector<uint8_t>>
178  getNonce() const;
179 
186  setNonce(optional<std::vector<uint8_t>> nonce);
187 
191  optional<time::system_clock::time_point>
192  getTime() const;
193 
200  setTime(optional<time::system_clock::time_point> time = time::system_clock::now());
201 
205  optional<uint64_t>
206  getSeqNum() const;
207 
214  setSeqNum(optional<uint64_t> seqNum);
215 
220  optional<Block>
221  getCustomTlv(uint32_t type) const;
222 
227  void
228  addCustomTlv(Block block);
229 
233  void
234  removeCustomTlv(uint32_t type);
235 
241  [[deprecated("use getCustomTlv")]]
242  const Block&
243  getTypeSpecificTlv(uint32_t type) const;
244 
248  [[deprecated("use addCustomTlv")]]
249  void
250  appendTypeSpecificTlv(const Block& block);
251 
252 private:
253  std::vector<Block>::const_iterator
254  findOtherTlv(uint32_t type) const;
255 
256 private:
257  int32_t m_type = -1;
258  optional<KeyLocator> m_keyLocator;
259  std::vector<Block> m_otherTlvs;
260 
261  mutable Block m_wire;
262 
263  friend bool
264  operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
265 
266  friend std::ostream&
267  operator<<(std::ostream& os, const SignatureInfo& info);
268 };
269 
270 extern template size_t
271 SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingBuffer&, SignatureInfo::Type) const;
272 
273 extern template size_t
274 SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, SignatureInfo::Type) const;
275 
276 bool
277 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
278 
279 inline bool
280 operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs)
281 {
282  return !(lhs == rhs);
283 }
284 
285 std::ostream&
286 operator<<(std::ostream& os, const SignatureInfo& info);
287 
288 } // namespace ndn
289 
290 #endif // NDN_SIGNATURE_INFO_HPP
SignatureInfo & setSeqNum(optional< uint64_t > seqNum)
Append or replace SignatureSeqNum.
friend std::ostream & operator<<(std::ostream &os, const SignatureInfo &info)
Definition: data.cpp:26
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
optional< uint64_t > getSeqNum() const
Get SignatureSeqNum.
const Block & getTypeSpecificTlv(uint32_t type) const
Get SignatureType-specific sub-element.
SignatureInfo & setValidityPeriod(optional< security::ValidityPeriod > validityPeriod)
Append or replace ValidityPeriod.
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
int32_t getSignatureType() const noexcept
Get SignatureType.
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:50
static time_point now() noexcept
Definition: time.cpp:46
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:354
optional< Block > getCustomTlv(uint32_t type) const
Get first custom TLV element with the specified TLV-TYPE.
void unsetValidityPeriod()
Remove ValidityPeriod.
optional< time::system_clock::time_point > getTime() const
Get SignatureTime.
void appendTypeSpecificTlv(const Block &block)
Append SignatureType-specific sub-element.
friend bool operator==(const SignatureInfo &lhs, const SignatureInfo &rhs)
Abstraction of validity period.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
optional< std::vector< uint8_t > > getNonce() const
Get SignatureNonce.
SignatureInfo & setTime(optional< time::system_clock::time_point > time=time::system_clock::now())
Append or replace SignatureTime.
void unsetKeyLocator()
Remove KeyLocator.
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:131
SignatureInfo & setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
size_t wireEncode(EncodingImpl< TAG > &encoder, Type type=Type::Data) const
Fast encoding or block size estimation.
void removeCustomTlv(uint32_t type)
Remove all arbitrary TLV elements with the specified TLV-TYPE from this SignatureInfo.
SignatureInfo & setKeyLocator(optional< KeyLocator > keyLocator)
Set KeyLocator.
void addCustomTlv(Block block)
Append an arbitrary TLV element to this SignatureInfo.
Represents a Data packet.
Definition: data.hpp:39
void wireDecode(const Block &wire, Type type=Type::Data)
Decode from wire format.
EncodingImpl< EncoderTag > EncodingBuffer
SignatureInfo & setNonce(optional< std::vector< uint8_t >> nonce)
Append or replace SignatureNonce.
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
EncodingImpl< EstimatorTag > EncodingEstimator
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.