All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
config-policy-manager.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
24 #ifndef NDN_CONFIG_POLICY_MANAGER_HPP
25 #define NDN_CONFIG_POLICY_MANAGER_HPP
26 
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include "certificate-cache.hpp"
31 #include "../v2/certificate-cache-v2.hpp"
32 #include "policy-manager.hpp"
33 
34 // Give friend access to the tests.
35 class TestVerificationRules_NameRelation_Test;
36 class TestVerificationRules_SimpleRegex_Test;
37 class TestVerificationRules_Hierarchical_Test;
38 class TestVerificationRules_HyperRelation_Test;
39 
40 namespace ndn {
41 
42 class BoostInfoTree;
43 class BoostInfoParser;
44 class IdentityCertificate;
45 
62 public:
87  (const std::string& configFileName = "",
88  const ptr_lib::shared_ptr<CertificateCache>& certificateCache =
89  ptr_lib::shared_ptr<CertificateCache>(), int searchDepth = 5,
90  Milliseconds graceInterval = 3000, Milliseconds keyTimestampTtl = 3600000,
91  int maxTrackedKeys = 1000);
92 
113  (const std::string& configFileName,
114  const ptr_lib::shared_ptr<CertificateCacheV2>& certificateCache,
115  int searchDepth = 5, Milliseconds graceInterval = 3000,
116  Milliseconds keyTimestampTtl = 3600000, int maxTrackedKeys = 1000);
117 
121  virtual
123 
127  void
128  reset();
129 
135  void
136  load(const std::string& configFileName);
137 
144  void
145  load(const std::string& input, const std::string& inputName);
146 
155  virtual bool
156  skipVerifyAndTrust(const Data& data);
157 
166  virtual bool
167  skipVerifyAndTrust(const Interest& interest);
168 
176  virtual bool
177  requireVerify(const Data& data);
178 
187  virtual bool
188  requireVerify(const Interest& interest);
189 
208  virtual ptr_lib::shared_ptr<ValidationRequest>
210  (const ptr_lib::shared_ptr<Data>& data, int stepCount,
211  const OnVerified& onVerified,
212  const OnDataValidationFailed& onValidationFailed);
213 
232  virtual ptr_lib::shared_ptr<ValidationRequest>
234  (const ptr_lib::shared_ptr<Interest>& interest, int stepCount,
235  const OnVerifiedInterest& onVerified,
236  const OnInterestValidationFailed& onValidationFailed,
237  WireFormat& wireFormat);
238 
247  virtual bool
248  checkSigningPolicy(const Name& dataName, const Name& certificateName);
249 
256  virtual Name
257  inferSigningIdentity(const Name& dataName);
258 
259 private:
260  // Give friend access to the tests.
261  friend TestVerificationRules_NameRelation_Test;
262  friend TestVerificationRules_SimpleRegex_Test;
263  friend TestVerificationRules_Hierarchical_Test;
264  friend TestVerificationRules_HyperRelation_Test;
265 
270  class TrustAnchorRefreshManager {
271  public:
272  TrustAnchorRefreshManager(bool isSecurityV1)
273  : isSecurityV1_(isSecurityV1)
274  {
275  }
276 
277  static ptr_lib::shared_ptr<IdentityCertificate>
278  loadIdentityCertificateFromFile(const std::string& filename);
279 
280  static ptr_lib::shared_ptr<CertificateV2>
281  loadCertificateV2FromFile(const std::string& filename);
282 
283  ptr_lib::shared_ptr<IdentityCertificate>
284  getCertificate(Name certificateName) const;
285 
286  ptr_lib::shared_ptr<CertificateV2>
287  getCertificateV2(Name certificateName) const;
288 
289  void
290  addDirectory(const std::string& directoryName, Milliseconds refreshPeriod);
291 
292  void
293  refreshAnchors();
294 
295  private:
296  class DirectoryInfo {
297  public:
298  DirectoryInfo
299  (const std::vector<std::string>& certificateNames,
300  MillisecondsSince1970 nextRefresh, Milliseconds refreshPeriod)
301  : certificateNames_(certificateNames), nextRefresh_(nextRefresh),
302  refreshPeriod_(refreshPeriod)
303  {
304  }
305 
306  std::vector<std::string> certificateNames_;
307  MillisecondsSince1970 nextRefresh_;
308  Milliseconds refreshPeriod_;
309  };
310 
311  bool isSecurityV1_;
312  CertificateCache certificateCache_;
313  CertificateCacheV2 certificateCacheV2_;
314  // refreshDirectories_ maps the directory name to certificate names so they
315  // can be deleted when necessary, and the next refresh time.
316  std::map<std::string, ptr_lib::shared_ptr<DirectoryInfo> > refreshDirectories_;
317  };
318 
325  void
326  loadTrustAnchorCertificates();
327 
342  bool
343  checkSignatureMatch
344  (const Name& signatureName, const Name& objectName,
345  const BoostInfoTree& rule, std::string& failureReason);
346 
355  ptr_lib::shared_ptr<IdentityCertificate>
356  lookupCertificate(const std::string& certID, bool isPath);
357 
366  ptr_lib::shared_ptr<CertificateV2>
367  lookupCertificateV2(const std::string& certID, bool isPath);
368 
378  const BoostInfoTree*
379  findMatchingRule(const Name& objName, const std::string& matchType) const;
380 
394  static bool
395  matchesRelation
396  (const Name& name, const Name& matchName, const std::string& matchRelation);
397 
407  static ptr_lib::shared_ptr<Signature>
408  extractSignature
409  (const Interest& interest, WireFormat& wireFormat,
410  std::string& failureReason);
411 
421  bool
422  interestTimestampIsFresh
423  (const Name& keyName, MillisecondsSince1970 timestamp,
424  std::string& failureReason) const;
425 
434  void
435  updateTimestampForKey(const Name& keyName, MillisecondsSince1970 timestamp);
436 
450  bool
451  verify
452  (const Signature* signatureInfo, const SignedBlob& signedBlob,
453  std::string& failureReason) const;
454 
471  ptr_lib::shared_ptr<Interest>
472  getCertificateInterest
473  (int stepCount, const std::string& matchType, const Name& objectName,
474  const Signature* signature, std::string& failureReason);
475 
488  void
489  onCertificateDownloadComplete
490  (const ptr_lib::shared_ptr<Data> &data,
491  const ptr_lib::shared_ptr<Data> &originalData, int stepCount,
492  const OnVerified& onVerified,
493  const OnDataValidationFailed& onValidationFailed);
494 
507  void
508  onCertificateDownloadCompleteForInterest
509  (const ptr_lib::shared_ptr<Data> &data,
510  const ptr_lib::shared_ptr<Interest> &originalInterest, int stepCount,
511  const OnVerifiedInterest& onVerified,
512  const OnInterestValidationFailed& onValidationFailed,
513  WireFormat& wireFormat);
514 
515  bool isSecurityV1_;
516  ptr_lib::shared_ptr<CertificateCache> certificateCache_;
517  ptr_lib::shared_ptr<CertificateCacheV2> certificateCacheV2_;
518  int maxDepth_;
519  Milliseconds keyGraceInterval_;
520  Milliseconds keyTimestampTtl_;
521  int maxTrackedKeys_;
522  // fixedCertificateCache_ stores the fixed-signer certificate name associated with
523  // validation rules so we don't keep loading from files.
524  std::map<std::string, std::string> fixedCertificateCache_;
525  // keyTimestamps_ stores the timestamps for each public key used in command
526  // interests to avoid replay attacks.
527  // key is the public key name, value is the last timestamp.
528  std::map<std::string, MillisecondsSince1970> keyTimestamps_;
529  ptr_lib::shared_ptr<BoostInfoParser> config_;
530  bool requiresVerification_;
531  ptr_lib::shared_ptr<TrustAnchorRefreshManager> refreshManager_;
532 };
533 
534 }
535 
536 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:114
A ConfigPolicyManager manages trust according to a configuration file in the Validator Configuration ...
Definition: config-policy-manager.hpp:61
func_lib::function< void(const ptr_lib::shared_ptr< Interest > &interest)> OnVerifiedInterest
An OnVerifiedInterest function object is used to pass a callback to verifyInterest to report a succes...
Definition: validation-request.hpp:53
virtual bool skipVerifyAndTrust(const Data &data)
Check if the received data packet can escape from verification and be trusted as valid.
Definition: data.hpp:37
void reset()
Reset the certificate cache and other fields to the constructor state.
virtual bool requireVerify(const Data &data)
Check if this PolicyManager has a verification rule for the received data.
ConfigPolicyManager(const std::string &configFileName="", const ptr_lib::shared_ptr< CertificateCache > &certificateCache=ptr_lib::shared_ptr< CertificateCache >(), int searchDepth=5, Milliseconds graceInterval=3000, Milliseconds keyTimestampTtl=3600000, int maxTrackedKeys=1000)
Create a new ConfigPolicyManager which will act on the rules specified in the configuration and downl...
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data)> OnVerified
An OnVerified function object is used to pass a callback to verifyData to report a successful verific...
Definition: validation-request.hpp:33
BoostInfoTree is provided for compatibility with the Boost INFO property list format used in ndn-cxx...
Definition: boost-info-parser.hpp:46
virtual Name inferSigningIdentity(const Name &dataName)
Infer the signing identity name according to the policy.
virtual ~ConfigPolicyManager()
The virtual destructor.
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Signature is an abstract base class providing methods to work with the signature information in a D...
Definition: signature.hpp:35
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:43
void load(const std::string &configFileName)
Call reset() and load the configuration rules from the file.
virtual ptr_lib::shared_ptr< ValidationRequest > checkVerificationPolicy(const ptr_lib::shared_ptr< Data > &data, int stepCount, const OnVerified &onVerified, const OnDataValidationFailed &onValidationFailed)
Check whether the received data packet complies with the verification policy, and get the indication ...
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:119
func_lib::function< void(const ptr_lib::shared_ptr< Interest > &interest, const std::string &reason)> OnInterestValidationFailed
An OnInterestValidationFailed function object is used to pass a callback to verifyInterest to report ...
Definition: validation-request.hpp:61
A CertificateCacheV2 holds other user's verified certificates in security v2 format CertificateV2...
Definition: certificate-cache-v2.hpp:38
virtual bool checkSigningPolicy(const Name &dataName, const Name &certificateName)
Override to always indicate that the signing certificate name and data name satisfy the signing polic...
A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet)...
Definition: signed-blob.hpp:34
Definition: wire-format.hpp:39
A PolicyManager is an abstract base class to represent the policy for verifying data packets...
Definition: policy-manager.hpp:37
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data, const std::string &reason)> OnDataValidationFailed
An OnDataValidationFailed function object is used to pass a callback to verifyData to report a failed...
Definition: validation-request.hpp:41
A CertificateCache is used to save other users' certificate during verification.
Definition: certificate-cache.hpp:36