27 #include <boost/range/adaptor/reversed.hpp>
35 , m_keyLocator(std::move(keyLocator))
44 template<encoding::Tag TAG>
66 size_t totalLength = 0;
70 for (
const auto& block : m_otherTlvs | boost::adaptors::reversed) {
75 totalLength += m_keyLocator->wireEncode(encoder);
79 static_cast<uint64_t
>(m_type));
81 totalLength += encoder.prependVarNumber(totalLength);
100 size_t estimatedSize =
wireEncode(estimator, type);
105 m_wire = buffer.block();
113 m_keyLocator = std::nullopt;
123 size_t lastCriticalElement = 0;
124 for (
const auto& element : m_wire.
elements()) {
125 switch (element.type()) {
127 if (lastCriticalElement > 0) {
128 NDN_THROW(
Error(
"SignatureType element is repeated or out-of-order"));
130 m_type = readNonNegativeIntegerAs<tlv::SignatureTypeValue>(element);
131 lastCriticalElement = 1;
135 if (lastCriticalElement > 1) {
136 NDN_THROW(
Error(
"KeyLocator element is repeated or out-of-order"));
138 m_keyLocator.emplace(element);
139 lastCriticalElement = 2;
144 if (element.value_size() < 1) {
147 m_otherTlvs.push_back(element);
154 m_otherTlvs.push_back(element);
162 m_otherTlvs.push_back(element);
175 if (type != m_type) {
188 return *m_keyLocator;
194 if (keyLocator != m_keyLocator) {
195 m_keyLocator = std::move(keyLocator);
205 if (it == m_otherTlvs.end()) {
206 NDN_THROW(
Error(
"ValidityPeriod does not exist in SignatureInfo"));
214 if (!validityPeriod) {
223 std::optional<std::vector<uint8_t>>
227 if (it == m_otherTlvs.end()) {
230 return std::vector<uint8_t>(it->value_begin(), it->value_end());
245 std::optional<time::system_clock::time_point>
249 if (it == m_otherTlvs.end()) {
268 std::optional<uint64_t>
272 if (it == m_otherTlvs.end()) {
293 auto it = findOtherTlv(type);
294 if (it == m_otherTlvs.end()) {
303 auto existingIt = std::find_if(m_otherTlvs.begin(), m_otherTlvs.end(), [&block] (
const Block& b) {
304 return b.type() == block.type();
306 if (existingIt == m_otherTlvs.end()) {
307 m_otherTlvs.push_back(std::move(block));
310 else if (*existingIt != block) {
311 *existingIt = std::move(block);
319 auto it = std::remove_if(m_otherTlvs.begin(), m_otherTlvs.end(), [type] (
const Block& block) {
320 return block.type() == type;
323 if (it != m_otherTlvs.end()) {
324 m_otherTlvs.erase(it, m_otherTlvs.end());
329 std::vector<Block>::const_iterator
330 SignatureInfo::findOtherTlv(uint32_t type)
const
332 return std::find_if(m_otherTlvs.begin(), m_otherTlvs.end(), [type] (
const Block& block) {
333 return block.type() == type;
340 return lhs.m_type == rhs.m_type &&
341 lhs.m_keyLocator == rhs.m_keyLocator &&
342 lhs.m_otherTlvs == rhs.m_otherTlvs;
349 return os <<
"Invalid SignatureInfo";
356 if (!
info.m_otherTlvs.empty()) {
358 for (
const auto& block :
info.m_otherTlvs) {
359 switch (block.type()) {
377 os << block.type() <<
" ";
Represents a TLV element of the NDN packet format.
const element_container & elements() const noexcept
Get container of sub-elements.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
void reset() noexcept
Reset the Block to a default-constructed state.
void parse() const
Parse TLV-VALUE into sub-elements.
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 & setSeqNum(std::optional< uint64_t > seqNum)
Append or replace SignatureSeqNum.
std::optional< uint64_t > getSeqNum() const
Get SignatureSeqNum.
std::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.
int32_t getSignatureType() const noexcept
Get the SignatureType.
SignatureInfo & setTime(std::optional< time::system_clock::time_point > time=time::system_clock::now())
Append or replace SignatureTime.
SignatureInfo & setValidityPeriod(std::optional< security::ValidityPeriod > validityPeriod)
Append, replace, or remove the ValidityPeriod element.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
void removeCustomTlv(uint32_t type)
Remove all arbitrary TLV elements with the specified TLV-TYPE from this SignatureInfo.
void wireDecode(const Block &wire, Type type=Type::Data)
Decode from wire format.
std::optional< Block > getCustomTlv(uint32_t type) const
Get first custom TLV element with the specified TLV-TYPE.
std::optional< time::system_clock::time_point > getTime() const
Get SignatureTime.
SignatureInfo & setNonce(std::optional< span< const uint8_t >> nonce)
Append or replace SignatureNonce.
SignatureInfo & setKeyLocator(std::optional< KeyLocator > keyLocator)
Set or unset the KeyLocator element.
const KeyLocator & getKeyLocator() const
Get the KeyLocator element.
Represents a ValidityPeriod TLV element.
EncodingImpl< EstimatorTag > EncodingEstimator
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
EncodingImpl< EncoderTag > EncodingBuffer
size_t prependBlock(EncodingImpl< TAG > &encoder, const Block &block)
Prepend a TLV element.
std::string to_string(const errinfo_stacktrace &x)
constexpr system_clock::time_point fromUnixTimestamp(system_clock::duration d)
Convert UNIX timestamp to system_clock::time_point.
constexpr Duration toUnixTimestamp(const system_clock::time_point &tp)
Convert system_clock::time_point to UNIX timestamp.
::boost::chrono::milliseconds milliseconds
constexpr bool isCriticalType(uint32_t type) noexcept
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
SignatureTypeValue
SignatureType values.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
bool operator==(const Data &lhs, const Data &rhs)
std::ostream & operator<<(std::ostream &os, const Data &data)
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept