NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
validator.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #ifndef NDN_SECURITY_VALIDATOR_HPP
26 #define NDN_SECURITY_VALIDATOR_HPP
27 
28 #include "../common.hpp"
29 
30 #include "../data.hpp"
31 #include "../face.hpp"
32 #include "public-key.hpp"
35 #include "digest-sha256.hpp"
36 #include "validation-request.hpp"
37 #include "identity-certificate.hpp"
38 
39 namespace ndn {
40 
46 class Validator
47 {
48 public:
49  class Error : public std::runtime_error
50  {
51  public:
52  explicit
53  Error(const std::string& what)
54  : std::runtime_error(what)
55  {
56  }
57  };
58 
67  explicit
68  Validator(Face* face = nullptr);
69 
71  explicit
72  Validator(Face& face);
73 
81  void
82  validate(const Data& data,
83  const OnDataValidated& onValidated,
84  const OnDataValidationFailed& onValidationFailed)
85  {
86  validate(data, onValidated, onValidationFailed, 0);
87  }
88 
96  void
97  validate(const Interest& interest,
98  const OnInterestValidated& onValidated,
99  const OnInterestValidationFailed& onValidationFailed)
100  {
101  validate(interest, onValidated, onValidationFailed, 0);
102  }
103 
104  /*****************************************
105  * verifySignature method set *
106  *****************************************/
107 
109  static bool
110  verifySignature(const Data& data, const PublicKey& publicKey);
111 
117  static bool
118  verifySignature(const Interest& interest, const PublicKey& publicKey);
119 
121  static bool
122  verifySignature(const Buffer& blob, const Signature& sig, const PublicKey& publicKey)
123  {
124  return verifySignature(blob.buf(), blob.size(), sig, publicKey);
125  }
126 
128  static bool
129  verifySignature(const Data& data,
130  const Signature& sig,
131  const PublicKey& publicKey)
132  {
133  return verifySignature(data.wireEncode().value(),
134  data.wireEncode().value_size() - data.getSignature().getValue().size(),
135  sig, publicKey);
136  }
137 
142  static bool
143  verifySignature(const Interest& interest,
144  const Signature& sig,
145  const PublicKey& publicKey)
146  {
147  if (interest.getName().size() < 2)
148  return false;
149 
150  const Name& name = interest.getName();
151 
152  return verifySignature(name.wireEncode().value(),
153  name.wireEncode().value_size() - name[-1].size(),
154  sig, publicKey);
155  }
156 
158  static bool
159  verifySignature(const uint8_t* buf,
160  const size_t size,
161  const Signature& sig,
162  const PublicKey& publicKey);
163 
164 
166  static bool
167  verifySignature(const Data& data, const DigestSha256& sig)
168  {
169  return verifySignature(data.wireEncode().value(),
170  data.wireEncode().value_size() -
171  data.getSignature().getValue().size(),
172  sig);
173  }
174 
179  static bool
180  verifySignature(const Interest& interest, const DigestSha256& sig)
181  {
182  if (interest.getName().size() < 2)
183  return false;
184 
185  const Name& name = interest.getName();
186 
187  return verifySignature(name.wireEncode().value(),
188  name.wireEncode().value_size() - name[-1].size(),
189  sig);
190  }
191 
193  static bool
194  verifySignature(const Buffer& blob, const DigestSha256& sig)
195  {
196  return verifySignature (blob.buf(), blob.size(), sig);
197  }
198 
200  static bool
201  verifySignature(const uint8_t* buf, const size_t size, const DigestSha256& sig);
202 
203 protected:
216  virtual void
217  checkPolicy(const Data& data,
218  int nSteps,
219  const OnDataValidated& onValidated,
220  const OnDataValidationFailed& onValidationFailed,
221  std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
222 
236  virtual void
237  checkPolicy(const Interest& interest,
238  int nSteps,
239  const OnInterestValidated& onValidated,
240  const OnInterestValidationFailed& onValidationFailed,
241  std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
242 
243  typedef function<void(const std::string&)> OnFailure;
244 
246  void
247  onData(const Interest& interest,
248  const Data& data,
249  const shared_ptr<ValidationRequest>& nextStep);
250 
251  void
252  validate(const Data& data,
253  const OnDataValidated& onValidated,
254  const OnDataValidationFailed& onValidationFailed,
255  int nSteps);
256 
257  void
258  validate(const Interest& interest,
259  const OnInterestValidated& onValidated,
260  const OnInterestValidationFailed& onValidationFailed,
261  int nSteps);
262 
264 
278  virtual shared_ptr<const Data>
280  {
281  return data.shared_from_this();
282  }
283 
295  virtual void
296  onTimeout(const Interest& interest,
297  int nRemainingRetries,
298  const OnFailure& onFailure,
299  const shared_ptr<ValidationRequest>& validationRequest);
300 
310  virtual void
311  afterCheckPolicy(const std::vector<shared_ptr<ValidationRequest> >& nextSteps,
312  const OnFailure& onFailure);
313 
314 protected:
316 };
317 
318 } // namespace ndn
319 
320 #endif // NDN_SECURITY_VALIDATOR_HPP
Error(const std::string &what)
Definition: validator.hpp:53
const Name & getName() const
Definition: interest.hpp:216
Copyright (c) 2011-2015 Regents of the University of California.
static bool verifySignature(const Interest &interest, const DigestSha256 &sig)
Verify the interest against the SHA256 signature.
Definition: validator.hpp:180
virtual void afterCheckPolicy(const std::vector< shared_ptr< ValidationRequest > > &nextSteps, const OnFailure &onFailure)
trigger after checkPolicy is done.
Definition: validator.cpp:286
void onData(const Interest &interest, const Data &data, const shared_ptr< ValidationRequest > &nextStep)
Process the received certificate.
Definition: validator.cpp:90
static bool verifySignature(const Data &data, const DigestSha256 &sig)
Verify the data against the SHA256 signature.
Definition: validator.hpp:167
static bool verifySignature(const Buffer &blob, const DigestSha256 &sig)
Verify the blob against the SHA256 signature.
Definition: validator.hpp:194
Validator(Face *face=nullptr)
Validator constructor.
Definition: validator.cpp:37
Represent a SHA256 digest.
static bool verifySignature(const Buffer &blob, const Signature &sig, const PublicKey &publicKey)
Verify the blob using the publicKey against the signature.
Definition: validator.hpp:122
STL namespace.
static bool verifySignature(const Interest &interest, const Signature &sig, const PublicKey &publicKey)
Verify the interest using the publicKey against the SHA256-RSA signature.
Definition: validator.hpp:143
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:69
represents an Interest packet
Definition: interest.hpp:45
const Block & getValue() const
Get SignatureValue in the wire format.
Definition: signature.hpp:96
virtual void onTimeout(const Interest &interest, int nRemainingRetries, const OnFailure &onFailure, const shared_ptr< ValidationRequest > &validationRequest)
trigger when interest for certificate times out.
Definition: validator.cpp:269
size_t size() const
Definition: block.cpp:504
void validate(const Interest &interest, const OnInterestValidated &onValidated, const OnInterestValidationFailed &onValidationFailed)
Validate Interest and call either onValidated or onValidationFailed.
Definition: validator.hpp:97
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:52
function< void(const shared_ptr< const Data > &)> OnDataValidated
Callback to report a successful Data validation.
void validate(const Data &data, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed)
Validate Data and call either onValidated or onValidationFailed.
Definition: validator.hpp:82
virtual void checkPolicy(const Data &data, int nSteps, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed, std::vector< shared_ptr< ValidationRequest > > &nextSteps)=0
Check the Data against policy and return the next validation step if necessary.
uint8_t * buf()
Definition: buffer.hpp:87
function< void(const shared_ptr< const Data > &, const std::string &)> OnDataValidationFailed
Callback to report a failed Data validation.
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:100
size_t size() const
Get the number of components.
Definition: name.hpp:408
Name abstraction to represent an absolute name.
Definition: name.hpp:46
static bool verifySignature(const Data &data, const Signature &sig, const PublicKey &publicKey)
Verify the data using the publicKey against the SHA256-RSA signature.
Definition: validator.hpp:129
const Signature & getSignature() const
Definition: data.hpp:373
function< void(const shared_ptr< const Interest > &, const std::string &)> OnInterestValidationFailed
Callback to report a failed Interest validation.
static bool verifySignature(const Data &data, const PublicKey &publicKey)
Verify the data using the publicKey.
Definition: validator.cpp:106
function< void(const std::string &)> OnFailure
Definition: validator.hpp:243
represents a Data packet
Definition: data.hpp:39
virtual shared_ptr< const Data > preCertificateValidation(const Data &data)
Hooks.
Definition: validator.hpp:279
Class representing a general-use automatically managed/resized buffer.
Definition: buffer.hpp:44
Validator is one of the main classes of the security library.
Definition: validator.hpp:46
function< void(const shared_ptr< const Interest > &)> OnInterestValidated
Callback to report a successful Interest validation.
A Signature is storage for the signature-related information (info and value) in a Data packet...
Definition: signature.hpp:33