validation-policy-signed-interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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_CXX_SECURITY_VALIDATION_POLICY_SIGNED_INTEREST_HPP
23 #define NDN_CXX_SECURITY_VALIDATION_POLICY_SIGNED_INTEREST_HPP
24 
26 
27 #include <boost/multi_index_container.hpp>
28 #include <boost/multi_index/hashed_index.hpp>
29 #include <boost/multi_index/key_extractors.hpp>
30 #include <boost/multi_index/ordered_index.hpp>
31 #include <boost/multi_index/sequenced_index.hpp>
32 
33 namespace ndn {
34 namespace security {
35 inline namespace v2 {
36 
45 {
46 private:
47  using SigNonce = std::vector<uint8_t>;
48  struct NonceSet {};
49  struct NonceList {};
50 
51 public:
52  class Options
53  {
54  public:
56  {
57  }
58 
59  public:
69 
84 
90  bool shouldValidateSeqNums = false;
91 
100  bool shouldValidateNonces = true;
101 
113  ssize_t maxNonceRecordCount = 1000;
114 
135  ssize_t maxRecordCount = 1000;
136  };
137 
143  explicit
144  ValidationPolicySignedInterest(unique_ptr<ValidationPolicy> inner, const Options& options = {});
145 
146 protected:
147  void
148  checkPolicy(const Data& data, const shared_ptr<ValidationState>& state,
149  const ValidationContinuation& continueValidation) override;
150 
151  void
152  checkPolicy(const Interest& interest, const shared_ptr<ValidationState>& state,
153  const ValidationContinuation& continueValidation) override;
154 
155 private:
156  bool
157  checkIncomingInterest(const shared_ptr<ValidationState>& state, const Interest& interest);
158 
159  void
160  insertRecord(const Name& keyName,
161  optional<time::system_clock::TimePoint> timestamp,
162  optional<uint64_t> seqNum,
163  optional<SigNonce> nonce);
164 
165 private:
166  Options m_options;
167 
168  using NonceContainer = boost::multi_index_container<
169  SigNonce,
170  boost::multi_index::indexed_by<
171  boost::multi_index::hashed_unique<
172  boost::multi_index::tag<NonceSet>,
173  boost::multi_index::identity<SigNonce>
174  >,
175  boost::multi_index::sequenced<
176  boost::multi_index::tag<NonceList>
177  >
178  >
179  >;
180 
181  struct LastInterestRecord
182  {
183  LastInterestRecord(const Name& keyName,
184  optional<time::system_clock::TimePoint> timestamp,
185  optional<uint64_t> seqNum)
186  : keyName(keyName)
187  , timestamp(timestamp)
188  , seqNum(seqNum)
189  , lastRefreshed(time::steady_clock::now())
190  {
191  }
192 
193  Name keyName;
194  optional<time::system_clock::TimePoint> timestamp;
195  optional<uint64_t> seqNum;
196  NonceContainer observedNonces;
197  time::steady_clock::TimePoint lastRefreshed;
198  };
199 
200  using Container = boost::multi_index_container<
201  LastInterestRecord,
202  boost::multi_index::indexed_by<
203  boost::multi_index::ordered_unique<
204  boost::multi_index::member<LastInterestRecord, Name, &LastInterestRecord::keyName>
205  >,
206  boost::multi_index::ordered_non_unique<
207  boost::multi_index::member<LastInterestRecord, time::steady_clock::TimePoint,
208  &LastInterestRecord::lastRefreshed>
209  >
210  >
211  >;
212 
213  Container m_container;
214  Container::nth_index<0>::type& m_byKeyName;
215  Container::nth_index<1>::type& m_byLastRefreshed;
216 };
217 
218 } // inline namespace v2
219 } // namespace security
220 } // namespace ndn
221 
222 #endif // NDN_CXX_SECURITY_VALIDATION_POLICY_SIGNED_INTEREST_HPP
Represents a Data packet.
Definition: data.hpp:39
Represents an Interest packet.
Definition: interest.hpp:50
Represents an absolute name.
Definition: name.hpp:44
ssize_t maxNonceRecordCount
Number of previous nonces to track for each public key.
bool shouldValidateSeqNums
Whether to validate sequence numbers in signed Interests by ensuring they are present and are strictl...
ssize_t maxRecordCount
Max number of distinct public keys to track.
time::nanoseconds timestampGracePeriod
Tolerance of timestamp differences from the current time.
bool shouldValidateTimestamps
Whether to validate timestamps in signed Interests by ensuring they are not reordered for a given pub...
bool shouldValidateNonces
Whether to validate nonces by ensuring that they are present and do not overlap with one of the last ...
void checkPolicy(const Data &data, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Check data against the policy.
ValidationPolicySignedInterest(unique_ptr< ValidationPolicy > inner, const Options &options={})
Constructor.
Abstraction that implements a validation policy for Interest and Data packets.
std::function< void(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state)> ValidationContinuation
time_point TimePoint
Definition: time.hpp:233
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
Definition: data.cpp:25