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 "policy-manager.hpp"
32 
33 // Give friend access to the tests.
34 class TestVerificationRules_NameRelation_Test;
35 class TestVerificationRules_SimpleRegex_Test;
36 class TestVerificationRules_Hierarchical_Test;
37 class TestVerificationRules_HyperRelation_Test;
38 
39 namespace ndn {
40 
41 class BoostInfoTree;
42 class BoostInfoParser;
43 class IdentityCertificate;
44 
61 public:
83  (const std::string& configFileName = "",
84  const ptr_lib::shared_ptr<CertificateCache>& certificateCache =
85  ptr_lib::shared_ptr<CertificateCache>(), int searchDepth = 5,
86  Milliseconds graceInterval = 3000, Milliseconds keyTimestampTtl = 3600000,
87  int maxTrackedKeys = 1000);
88 
92  virtual
94 
98  void
99  reset();
100 
106  void
107  load(const std::string& configFileName);
108 
115  void
116  load(const std::string& input, const std::string& inputName);
117 
126  virtual bool
127  skipVerifyAndTrust(const Data& data);
128 
137  virtual bool
138  skipVerifyAndTrust(const Interest& interest);
139 
147  virtual bool
148  requireVerify(const Data& data);
149 
158  virtual bool
159  requireVerify(const Interest& interest);
160 
179  virtual ptr_lib::shared_ptr<ValidationRequest>
181  (const ptr_lib::shared_ptr<Data>& data, int stepCount,
182  const OnVerified& onVerified,
183  const OnDataValidationFailed& onValidationFailed);
184 
203  virtual ptr_lib::shared_ptr<ValidationRequest>
205  (const ptr_lib::shared_ptr<Interest>& interest, int stepCount,
206  const OnVerifiedInterest& onVerified,
207  const OnInterestValidationFailed& onValidationFailed,
208  WireFormat& wireFormat);
209 
218  virtual bool
219  checkSigningPolicy(const Name& dataName, const Name& certificateName);
220 
227  virtual Name
228  inferSigningIdentity(const Name& dataName);
229 
230 private:
231  // Give friend access to the tests.
232  friend TestVerificationRules_NameRelation_Test;
233  friend TestVerificationRules_SimpleRegex_Test;
234  friend TestVerificationRules_Hierarchical_Test;
235  friend TestVerificationRules_HyperRelation_Test;
236 
241  class TrustAnchorRefreshManager {
242  public:
243  TrustAnchorRefreshManager()
244  {
245  }
246 
247  static ptr_lib::shared_ptr<IdentityCertificate>
248  loadIdentityCertificateFromFile(const std::string& filename);
249 
250  ptr_lib::shared_ptr<IdentityCertificate>
251  getCertificate(Name certificateName) const
252  {
253  // Assume the timestamp is already removed.
254  return certificateCache_.getCertificate(certificateName);
255  }
256 
257  void
258  addDirectory(const std::string& directoryName, Milliseconds refreshPeriod);
259 
260  void
261  refreshAnchors();
262 
263  private:
264  class DirectoryInfo {
265  public:
266  DirectoryInfo
267  (const std::vector<std::string>& certificateNames,
268  MillisecondsSince1970 nextRefresh, Milliseconds refreshPeriod)
269  : certificateNames_(certificateNames), nextRefresh_(nextRefresh),
270  refreshPeriod_(refreshPeriod)
271  {
272  }
273 
274  std::vector<std::string> certificateNames_;
275  MillisecondsSince1970 nextRefresh_;
276  Milliseconds refreshPeriod_;
277  };
278 
279  CertificateCache certificateCache_;
280  // refreshDirectories_ maps the directory name to certificate names so they
281  // can be deleted when necessary, and the next refresh time.
282  std::map<std::string, ptr_lib::shared_ptr<DirectoryInfo> > refreshDirectories_;
283  };
284 
291  void
292  loadTrustAnchorCertificates();
293 
308  bool
309  checkSignatureMatch
310  (const Name& signatureName, const Name& objectName,
311  const BoostInfoTree& rule, std::string& failureReason);
312 
321  ptr_lib::shared_ptr<IdentityCertificate>
322  lookupCertificate(const std::string& certID, bool isPath);
323 
333  const BoostInfoTree*
334  findMatchingRule(const Name& objName, const std::string& matchType) const;
335 
349  static bool
350  matchesRelation
351  (const Name& name, const Name& matchName, const std::string& matchRelation);
352 
362  static ptr_lib::shared_ptr<Signature>
363  extractSignature
364  (const Interest& interest, WireFormat& wireFormat,
365  std::string& failureReason);
366 
376  bool
377  interestTimestampIsFresh
378  (const Name& keyName, MillisecondsSince1970 timestamp,
379  std::string& failureReason) const;
380 
389  void
390  updateTimestampForKey(const Name& keyName, MillisecondsSince1970 timestamp);
391 
405  bool
406  verify
407  (const Signature* signatureInfo, const SignedBlob& signedBlob,
408  std::string& failureReason) const;
409 
426  ptr_lib::shared_ptr<Interest>
427  getCertificateInterest
428  (int stepCount, const std::string& matchType, const Name& objectName,
429  const Signature* signature, std::string& failureReason);
430 
443  void
444  onCertificateDownloadComplete
445  (const ptr_lib::shared_ptr<Data> &data,
446  const ptr_lib::shared_ptr<Data> &originalData, int stepCount,
447  const OnVerified& onVerified,
448  const OnDataValidationFailed& onValidationFailed);
449 
462  void
463  onCertificateDownloadCompleteForInterest
464  (const ptr_lib::shared_ptr<Data> &data,
465  const ptr_lib::shared_ptr<Interest> &originalInterest, int stepCount,
466  const OnVerifiedInterest& onVerified,
467  const OnInterestValidationFailed& onValidationFailed,
468  WireFormat& wireFormat);
469 
470  ptr_lib::shared_ptr<CertificateCache> certificateCache_;
471  int maxDepth_;
472  Milliseconds keyGraceInterval_;
473  Milliseconds keyTimestampTtl_;
474  int maxTrackedKeys_;
475  // fixedCertificateCache_ stores the fixed-signer certificate name associated with
476  // validation rules so we don't keep loading from files.
477  std::map<std::string, std::string> fixedCertificateCache_;
478  // keyTimestamps_ stores the timestamps for each public key used in command
479  // interests to avoid replay attacks.
480  // key is the public key name, value is the last timestamp.
481  std::map<std::string, MillisecondsSince1970> keyTimestamps_;
482  ptr_lib::shared_ptr<BoostInfoParser> config_;
483  bool requiresVerification_;
484  ptr_lib::shared_ptr<TrustAnchorRefreshManager> refreshManager_;
485 };
486 
487 }
488 
489 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:112
A ConfigPolicyManager manages trust according to a configuration file in the Validator Configuration ...
Definition: config-policy-manager.hpp:60
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
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:42
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:117
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
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