esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sig-info.hpp
Go to the documentation of this file.
1#ifndef NDNPH_PACKET_SIG_INFO_HPP
2#define NDNPH_PACKET_SIG_INFO_HPP
3
4#include "../port/random/port.hpp"
5#include "../port/unixtime/port.hpp"
6#include "../tlv/ev-decoder.hpp"
7#include "name.hpp"
8
9namespace ndnph {
10
12class SigInfo {
13public:
14 bool decodeFrom(const Decoder::Tlv& input) {
16 input, {TT::ISigInfo, TT::DSigInfo},
17 [this, &input](const Decoder::Tlv& d, int& currentOrder) {
18 if (currentOrder < 1000) {
19 extensions = tlv::Value(d.tlv, input.value + input.length);
20 currentOrder = 1000;
21 }
22 return true;
23 },
24 EvDecoder::DefaultIsCritical(), EvDecoder::defNni<TT::SigType, tlv::NNI, 1>(&sigType),
25 EvDecoder::def<TT::KeyLocator, false, 2>([this](const Decoder::Tlv& d) {
26 return EvDecoder::decode(d, {}, EvDecoder::def<TT::Name>(&name));
27 }));
28 }
29
30protected:
31 ~SigInfo() = default;
32
33 void encodeImpl(uint32_t type, Encoder& encoder) const {
34 encoder.prependTlv(
36 [this](Encoder& encoder) {
37 if (name.size() > 0) {
38 encoder.prependTlv(TT::KeyLocator, name);
39 }
40 },
42 }
43
44public:
47 uint8_t sigType = 0;
48};
49
51class ISigInfo : public SigInfo {
52public:
53 void encodeTo(Encoder& encoder) const {
54 return encodeImpl(TT::ISigInfo, encoder);
55 }
56};
57
59class DSigInfo : public SigInfo {
60public:
61 void encodeTo(Encoder& encoder) const {
62 return encodeImpl(TT::DSigInfo, encoder);
63 }
64};
65
66namespace isig {
67
69class Fields {
70public:
71 bool decode(const ISigInfo& si) {
72 return EvDecoder::decodeValue(si.extensions.makeDecoder(), EvDecoder::def<TT::SigNonce>(&nonce),
73 EvDecoder::defNni<TT::SigTime>(&timestamp),
74 EvDecoder::defNni<TT::SigSeqNum>(&seqNum));
75 }
76
77public:
79 uint64_t timestamp = 0;
80 uint64_t seqNum = 0;
81};
82
83namespace detail {
84
85template<int order>
86class Skip {
87public:
88 using Order = std::integral_constant<int, order>;
89
91 public:
92 void encodeTo(Encoder&) const {}
93 };
94
96 return EncodeValue();
97 }
98
99 bool check(const Fields&) const {
100 return true;
101 }
102
103 void save(const Fields&) {}
104};
105
106} // namespace detail
107
113template<int nonceLength = 8, int nTrackedNonces = 16>
114class Nonce {
115public:
116 using Order = std::integral_constant<int, 1>;
117 static_assert(nonceLength > 0, "");
118 static_assert(nTrackedNonces > 0, "");
119
120 class Value : public std::array<uint8_t, nonceLength> {
121 public:
122 Value() = default;
123
124 explicit Value(tlv::Value input)
125 : valid(input.size() == nonceLength) {
126 if (valid) {
127 std::copy(input.begin(), input.end(), this->begin());
128 }
129 }
130
131 void encodeTo(Encoder& encoder) const {
132 if (!valid) {
133 encoder.setError();
134 return;
135 }
136 encoder.prependTlv(TT::SigNonce, tlv::Value(this->data(), this->size()));
137 }
138
139 bool operator==(const Value& other) const {
140 return valid && other.valid && std::equal(this->begin(), this->end(), other.begin());
141 }
142
143 public:
144 bool valid = false;
145 };
146
148 Value value;
149 do {
150 value.valid = port::RandomSource::generate(value.data(), value.size());
151 if (!value.valid) {
152 return value;
153 }
154 } while (exists(value));
155 append(value);
156 return value;
157 }
158
159 bool check(const Fields& f) const {
160 Value value(f.nonce);
161 return !exists(value);
162 }
163
164 void save(const Fields& f) {
165 Value value(f.nonce);
166 append(value);
167 }
168
169private:
170 bool exists(const Value& value) const {
171 return std::find(m_nonces.begin(), m_nonces.end(), value) != m_nonces.end();
172 }
173
174 void append(const Value& value) {
175 NDNPH_ASSERT(value.valid);
176 m_nonces[m_pos] = value;
177 if (++m_pos == m_nonces.size()) {
178 m_pos = 0;
179 }
180 }
181
182private:
183 std::array<Value, nTrackedNonces> m_nonces = {};
184 size_t m_pos = 0;
185};
186
191template<int maxClockOffset = 60000>
192class Time {
193public:
194 using Order = std::integral_constant<int, 2>;
195
197 uint64_t timestamp = std::max(now(), m_last + 1);
198 m_last = timestamp;
199 return tlv::NniElement<>(TT::SigTime, timestamp);
200 }
201
202 bool check(const Fields& f) const {
203 static_assert(maxClockOffset >= 0, "");
204 return f.timestamp > m_last &&
205 std::abs(static_cast<int64_t>(now() - f.timestamp)) <= maxClockOffset;
206 }
207
208 void save(const Fields& f) {
209 m_last = std::max(m_last, f.timestamp);
210 }
211
212private:
213 uint64_t now() const {
214 // SigTime field uses milliseconds
215 return port::UnixTime::now() / 1000;
216 }
217
218private:
219 uint64_t m_last = 0;
220};
221
223class SeqNum {
224public:
225 using Order = std::integral_constant<int, 3>;
226
227 explicit SeqNum(uint64_t next = 0)
228 : m_next(next) {}
229
231 return tlv::NniElement<>(TT::SigSeqNum, m_next++);
232 }
233
234 bool check(const Fields& f) const {
235 return f.seqNum >= m_next;
236 }
237
238 void save(const Fields& f) {
239 m_next = std::max(m_next, f.seqNum) + 1;
240 }
241
242private:
243 uint64_t m_next;
244};
245
254template<typename R0, typename R1 = detail::Skip<11>, typename R2 = detail::Skip<12>>
255class Policy {
256public:
257 explicit Policy(const R0& r0 = R0(), const R1& r1 = R1(), const R2& r2 = R2())
258 : m_r0(r0)
259 , m_r1(r1)
260 , m_r2(r2) {}
261
269 bool create(Region& region, ISigInfo& si) {
270 Encoder encoder(region);
271 if (!encoder.prepend(m_r0.create(), m_r1.create(), m_r2.create())) {
272 encoder.discard();
273 return false;
274 }
275 encoder.trim();
276 si.extensions = tlv::Value(encoder);
277 return true;
278 }
279
285 bool check(const ISigInfo& si) {
286 Fields f;
287 if (f.decode(si) && m_r0.check(f) && m_r1.check(f) && m_r2.check(f)) {
288 m_r0.save(f);
289 m_r1.save(f);
290 m_r2.save(f);
291 return true;
292 }
293 return false;
294 }
295
296private:
297 R0 m_r0;
298 R1 m_r1;
299 R2 m_r2;
300
301 static_assert(R0::Order::value < R1::Order::value, "");
302 static_assert(R1::Order::value < R2::Order::value, "");
303};
304
310template<typename... R>
311Policy<R...>
312makePolicy(R&&... rule) {
313 return Policy<R...>(std::forward<R>(rule)...);
314}
315
316} // namespace isig
317} // namespace ndnph
318
319#endif // NDNPH_PACKET_SIG_INFO_HPP
static bool generate(uint8_t *output, size_t count)
SignatureInfo on Data.
Definition sig-info.hpp:59
void encodeTo(Encoder &encoder) const
Definition sig-info.hpp:61
Decoded TLV.
Definition decoder.hpp:13
const uint8_t * tlv
Definition decoder.hpp:42
TLV encoder that accepts items in reverse order.
Definition encoder.hpp:10
bool prepend(const First &first, const Arg &... arg)
Prepend a sequence of values.
Definition encoder.hpp:123
void trim() const
Release unused space to the Region.
Definition encoder.hpp:58
void discard()
Release all space to the Region.
Definition encoder.hpp:72
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition encoder.hpp:143
void setError()
Indicate an error has occurred.
Definition encoder.hpp:166
Definition ev-decoder.hpp:102
static bool decodeEx(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const UnknownCallback &unknownCb, const IsCritical &isCritical, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition ev-decoder.hpp:145
static bool decodeValue(const Decoder &input, const E &... defs)
Decode input TLV-VALUE with a sequence of element definitions.
Definition ev-decoder.hpp:126
static bool decode(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition ev-decoder.hpp:115
SignatureInfo on Interest.
Definition sig-info.hpp:51
void encodeTo(Encoder &encoder) const
Definition sig-info.hpp:53
Name.
Definition name.hpp:14
size_t size() const
Get number of components.
Definition name.hpp:86
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
SignatureInfo.
Definition sig-info.hpp:12
void encodeImpl(uint32_t type, Encoder &encoder) const
Definition sig-info.hpp:33
Name name
Definition sig-info.hpp:45
tlv::Value extensions
Definition sig-info.hpp:46
~SigInfo()=default
bool decodeFrom(const Decoder::Tlv &input)
Definition sig-info.hpp:14
uint8_t sigType
Definition sig-info.hpp:47
Parsed extension fields from Interest SigInfo.
Definition sig-info.hpp:69
tlv::Value nonce
Definition sig-info.hpp:78
uint64_t seqNum
Definition sig-info.hpp:80
uint64_t timestamp
Definition sig-info.hpp:79
bool decode(const ISigInfo &si)
Definition sig-info.hpp:71
Definition sig-info.hpp:120
Value(tlv::Value input)
Definition sig-info.hpp:124
void encodeTo(Encoder &encoder) const
Definition sig-info.hpp:131
bool valid
Definition sig-info.hpp:144
bool operator==(const Value &other) const
Definition sig-info.hpp:139
Require SigNonce field in Interest SigInfo.
Definition sig-info.hpp:114
void save(const Fields &f)
Definition sig-info.hpp:164
Value create()
Definition sig-info.hpp:147
std::integral_constant< int, 1 > Order
Definition sig-info.hpp:116
bool check(const Fields &f) const
Definition sig-info.hpp:159
Validation policy for SigInfo fields in signed Interest.
Definition sig-info.hpp:255
bool check(const ISigInfo &si)
Check that SigInfo fields fulfill current policy.
Definition sig-info.hpp:285
Policy(const R0 &r0=R0(), const R1 &r1=R1(), const R2 &r2=R2())
Definition sig-info.hpp:257
bool create(Region &region, ISigInfo &si)
Assign SigInfo extension fields.
Definition sig-info.hpp:269
Require SigSeqNum field in Interest SigInfo.
Definition sig-info.hpp:223
SeqNum(uint64_t next=0)
Definition sig-info.hpp:227
std::integral_constant< int, 3 > Order
Definition sig-info.hpp:225
tlv::NniElement create()
Definition sig-info.hpp:230
void save(const Fields &f)
Definition sig-info.hpp:238
bool check(const Fields &f) const
Definition sig-info.hpp:234
Require SigTime field in Interest SigInfo.
Definition sig-info.hpp:192
void save(const Fields &f)
Definition sig-info.hpp:208
tlv::NniElement create()
Definition sig-info.hpp:196
std::integral_constant< int, 2 > Order
Definition sig-info.hpp:194
bool check(const Fields &f) const
Definition sig-info.hpp:202
Definition sig-info.hpp:90
void encodeTo(Encoder &) const
Definition sig-info.hpp:92
Definition sig-info.hpp:86
bool check(const Fields &) const
Definition sig-info.hpp:99
EncodeValue create()
Definition sig-info.hpp:95
std::integral_constant< int, order > Order
Definition sig-info.hpp:88
void save(const Fields &)
Definition sig-info.hpp:103
Encode to a TLV element where TLV-VALUE is a NonNegativeInteger.
Definition nni.hpp:170
A sequence of bytes, usually TLV-VALUE.
Definition value.hpp:11
Decoder makeDecoder() const
Create a Decoder over this value buffer.
Definition value.hpp:64
#define NDNPH_ASSERT(x)
Definition common.hpp:30
@ DSigInfo
Definition an.hpp:47
@ SigSeqNum
Definition an.hpp:55
@ SigTime
Definition an.hpp:54
@ ISigInfo
Definition an.hpp:38
@ SigNonce
Definition an.hpp:53
@ SigType
Definition an.hpp:50
Policy< R... > makePolicy(R &&... rule)
Create Interest SigInfo validation policy.
Definition sig-info.hpp:312
Definition fs.hpp:33