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) {
78 totalLength += encoder.prependBlock(block);
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);
218 if (it == m_otherTlvs.end()) {
219 NDN_THROW(
Error(
"ValidityPeriod does not exist in SignatureInfo"));
227 if (!validityPeriod) {
242 optional<std::vector<uint8_t>>
246 if (it == m_otherTlvs.end()) {
249 return std::vector<uint8_t>(it->value_begin(), it->value_end());
264 optional<time::system_clock::time_point>
268 if (it == m_otherTlvs.end()) {
290 if (it == m_otherTlvs.end()) {
311 auto it = findOtherTlv(type);
312 if (it == m_otherTlvs.end()) {
321 auto existingIt = std::find_if(m_otherTlvs.begin(), m_otherTlvs.end(), [&block] (
const Block& b) {
322 return b.type() == block.
type();
324 if (existingIt == m_otherTlvs.end()) {
325 m_otherTlvs.push_back(std::move(block));
328 else if (*existingIt != block) {
329 *existingIt = std::move(block);
337 auto it = std::remove_if(m_otherTlvs.begin(), m_otherTlvs.end(), [type] (
const Block& block) {
338 return block.type() == type;
341 if (it != m_otherTlvs.end()) {
342 m_otherTlvs.erase(it, m_otherTlvs.end());
350 auto it = findOtherTlv(type);
351 if (it == m_otherTlvs.end()) {
363 std::vector<Block>::const_iterator
364 SignatureInfo::findOtherTlv(uint32_t type)
const 366 return std::find_if(m_otherTlvs.begin(), m_otherTlvs.end(), [type] (
const Block& block) {
367 return block.type() == type;
374 return lhs.m_type == rhs.m_type &&
375 lhs.m_keyLocator == rhs.m_keyLocator &&
376 lhs.m_otherTlvs == rhs.m_otherTlvs;
383 return os <<
"Invalid SignatureInfo";
390 if (!info.m_otherTlvs.empty()) {
392 for (
const auto& block : info.m_otherTlvs) {
393 switch (block.type()) {
397 printHex(os, nonce.data(), nonce.size(),
false);
405 os <<
"SeqNum=" << *info.
getSeqNum() <<
" ";
411 os << block.type() <<
" ";
SignatureInfo & setSeqNum(optional< uint64_t > seqNum)
Append or replace SignatureSeqNum.
friend std::ostream & operator<<(std::ostream &os, const SignatureInfo &info)
const KeyLocator & getKeyLocator() const
Get KeyLocator.
system_clock::TimePoint fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::TimePoint.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
EncodingImpl specialization for actual TLV encoding.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
std::string to_string(const T &val)
constexpr bool isCriticalType(uint32_t type)
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
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.
int32_t getSignatureType() const noexcept
Get SignatureType.
void parse() const
Parse TLV-VALUE into sub-elements.
Represents a TLV element of the NDN packet format.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
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.
EncodingImpl specialization for TLV size estimation.
const element_container & elements() const
Get container of sub-elements.
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.
void reset() noexcept
Reset the Block to a default-constructed state.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
SignatureInfo & setTime(optional< time::system_clock::time_point > time=time::system_clock::now())
Append or replace SignatureTime.
void unsetKeyLocator()
Remove KeyLocator.
SignatureTypeValue
SignatureType values.
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.
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
SignatureInfo & setKeyLocator(optional< KeyLocator > keyLocator)
Set KeyLocator.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
void addCustomTlv(Block block)
Append an arbitrary TLV element to this SignatureInfo.
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
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.
uint32_t type() const
Return the TLV-TYPE of the Block.
EncodingImpl< EstimatorTag > EncodingEstimator
security::ValidityPeriod getValidityPeriod() const
Get ValidityPeriod.