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-2023 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::security {
34 
43 {
44 private:
45  using SigNonce = std::vector<uint8_t>;
46  struct NonceSet {};
47  struct NonceList {};
48 
49 public:
50  class Options
51  {
52  public:
54  {
55  }
56 
57  public:
67 
82 
88  bool shouldValidateSeqNums = false;
89 
98  bool shouldValidateNonces = true;
99 
111  ssize_t maxNonceRecordCount = 1000;
112 
133  ssize_t maxRecordCount = 1000;
134  };
135 
141  explicit
142  ValidationPolicySignedInterest(unique_ptr<ValidationPolicy> inner, const Options& options = {});
143 
144 protected:
145  void
146  checkPolicy(const Data& data, const shared_ptr<ValidationState>& state,
147  const ValidationContinuation& continueValidation) override;
148 
149  void
150  checkPolicy(const Interest& interest, const shared_ptr<ValidationState>& state,
151  const ValidationContinuation& continueValidation) override;
152 
153 private:
154  bool
155  checkIncomingInterest(const shared_ptr<ValidationState>& state, const Interest& interest);
156 
157  void
158  insertRecord(const Name& keyName,
159  std::optional<time::system_clock::time_point> timestamp,
160  std::optional<uint64_t> seqNum,
161  std::optional<SigNonce> nonce);
162 
163 private:
164  Options m_options;
165 
166  using NonceContainer = boost::multi_index_container<
167  SigNonce,
168  boost::multi_index::indexed_by<
169  boost::multi_index::hashed_unique<
170  boost::multi_index::tag<NonceSet>,
171  boost::multi_index::identity<SigNonce>
172  >,
173  boost::multi_index::sequenced<
174  boost::multi_index::tag<NonceList>
175  >
176  >
177  >;
178 
179  struct LastInterestRecord
180  {
181  LastInterestRecord(const Name& keyName,
182  std::optional<time::system_clock::time_point> timestamp,
183  std::optional<uint64_t> seqNum)
184  : keyName(keyName)
185  , timestamp(timestamp)
186  , seqNum(seqNum)
187  , lastRefreshed(time::steady_clock::now())
188  {
189  }
190 
191  Name keyName;
192  std::optional<time::system_clock::time_point> timestamp;
193  std::optional<uint64_t> seqNum;
194  NonceContainer observedNonces;
195  time::steady_clock::time_point lastRefreshed;
196  };
197 
198  using Container = boost::multi_index_container<
199  LastInterestRecord,
200  boost::multi_index::indexed_by<
201  boost::multi_index::ordered_unique<
202  boost::multi_index::member<LastInterestRecord, Name, &LastInterestRecord::keyName>
203  >,
204  boost::multi_index::ordered_non_unique<
205  boost::multi_index::member<LastInterestRecord, time::steady_clock::time_point,
206  &LastInterestRecord::lastRefreshed>
207  >
208  >
209  >;
210 
211  Container m_container;
212  Container::nth_index<0>::type& m_byKeyName;
213  Container::nth_index<1>::type& m_byLastRefreshed;
214 };
215 
216 } // namespace ndn::security
217 
218 #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:45
bool shouldValidateSeqNums
Whether to validate sequence numbers in signed Interests by ensuring they are present and are strictl...
ssize_t maxNonceRecordCount
Number of previous nonces to track for each public key.
time::nanoseconds timestampGracePeriod
Tolerance of timestamp differences from the current time.
ssize_t maxRecordCount
Max number of distinct public keys to track.
bool shouldValidateNonces
Whether to validate nonces by ensuring that they are present and do not overlap with one of the last ...
bool shouldValidateTimestamps
Whether to validate timestamps in signed Interests by ensuring they are not reordered for a given pub...
ValidationPolicySignedInterest(unique_ptr< ValidationPolicy > inner, const Options &options={})
Constructor.
void checkPolicy(const Data &data, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Check data against the policy.
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
::boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:232
Contains the ndn-cxx security framework.
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54