27 #include <boost/range/adaptor/reversed.hpp>
31 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
32 BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
33 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>));
34 BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
35 static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value,
36 "SignatureInfo::Error must inherit from tlv::Error");
42 , m_keyLocator(std::move(keyLocator))
51 template<encoding::Tag TAG>
73 size_t totalLength = 0;
77 for (
const auto& block : m_otherTlvs | boost::adaptors::reversed) {
82 totalLength += m_keyLocator->wireEncode(encoder);
86 static_cast<uint64_t
>(m_type));
88 totalLength += encoder.prependVarNumber(totalLength);
107 size_t estimatedSize =
wireEncode(estimator, type);
112 m_wire = buffer.block();
120 m_keyLocator = nullopt;
130 size_t lastCriticalElement = 0;
131 for (
const auto& element : m_wire.
elements()) {
132 switch (element.type()) {
134 if (lastCriticalElement > 0) {
135 NDN_THROW(
Error(
"SignatureType element is repeated or out-of-order"));
137 m_type = readNonNegativeIntegerAs<tlv::SignatureTypeValue>(element);
138 lastCriticalElement = 1;
142 if (lastCriticalElement > 1) {
143 NDN_THROW(
Error(
"KeyLocator element is repeated or out-of-order"));
145 m_keyLocator.emplace(element);
146 lastCriticalElement = 2;
151 if (element.value_size() < 1) {
154 m_otherTlvs.push_back(element);
161 m_otherTlvs.push_back(element);
169 m_otherTlvs.push_back(element);
182 if (type != m_type) {
195 return *m_keyLocator;
201 if (keyLocator != m_keyLocator) {
202 m_keyLocator = std::move(keyLocator);
212 if (it == m_otherTlvs.end()) {
213 NDN_THROW(
Error(
"ValidityPeriod does not exist in SignatureInfo"));
221 if (!validityPeriod) {
230 optional<std::vector<uint8_t>>
234 if (it == m_otherTlvs.end()) {
237 return std::vector<uint8_t>(it->value_begin(), it->value_end());
252 optional<time::system_clock::time_point>
256 if (it == m_otherTlvs.end()) {
279 if (it == m_otherTlvs.end()) {
300 auto it = findOtherTlv(type);
301 if (it == m_otherTlvs.end()) {
310 auto existingIt = std::find_if(m_otherTlvs.begin(), m_otherTlvs.end(), [&block] (
const Block& b) {
311 return b.type() == block.type();
313 if (existingIt == m_otherTlvs.end()) {
314 m_otherTlvs.push_back(std::move(block));
317 else if (*existingIt != block) {
318 *existingIt = std::move(block);
326 auto it = std::remove_if(m_otherTlvs.begin(), m_otherTlvs.end(), [type] (
const Block& block) {
327 return block.type() == type;
330 if (it != m_otherTlvs.end()) {
331 m_otherTlvs.erase(it, m_otherTlvs.end());
336 std::vector<Block>::const_iterator
337 SignatureInfo::findOtherTlv(uint32_t type)
const
339 return std::find_if(m_otherTlvs.begin(), m_otherTlvs.end(), [type] (
const Block& block) {
340 return block.type() == type;
347 return lhs.m_type == rhs.m_type &&
348 lhs.m_keyLocator == rhs.m_keyLocator &&
349 lhs.m_otherTlvs == rhs.m_otherTlvs;
356 return os <<
"Invalid SignatureInfo";
363 if (!
info.m_otherTlvs.empty()) {
365 for (
const auto& block :
info.m_otherTlvs) {
366 switch (block.type()) {
384 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 & 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.
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.
const KeyLocator & getKeyLocator() const
Get the KeyLocator element.
SignatureInfo & setKeyLocator(optional< KeyLocator > keyLocator)
Set or unset 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)
milliseconds toUnixTimestamp(const system_clock::time_point &point)
Convert system_clock::time_point to UNIX timestamp.
boost::chrono::milliseconds milliseconds
system_clock::time_point fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::time_point.
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