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 <ndn-cpp/security/policy/certificate-cache.hpp>
31 #include "policy-manager.hpp"
32 
34 
35 namespace ndn {
36 
37 class BoostInfoTree;
38 class BoostInfoParser;
39 class IdentityCertificate;
40 
57 public:
78  (const std::string& configFileName,
79  const ptr_lib::shared_ptr<CertificateCache>& certificateCache =
80  ptr_lib::shared_ptr<CertificateCache>(), int searchDepth = 5,
81  Milliseconds graceInterval = 3000, Milliseconds keyTimestampTtl = 3600000,
82  int maxTrackedKeys = 1000);
83 
87  virtual
89 
98  virtual bool
99  skipVerifyAndTrust(const Data& data);
100 
109  virtual bool
110  skipVerifyAndTrust(const Interest& interest);
111 
119  virtual bool
120  requireVerify(const Data& data);
121 
130  virtual bool
131  requireVerify(const Interest& interest);
132 
145  virtual ptr_lib::shared_ptr<ValidationRequest>
147  (const ptr_lib::shared_ptr<Data>& data, int stepCount,
148  const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed);
149 
162  virtual ptr_lib::shared_ptr<ValidationRequest>
164  (const ptr_lib::shared_ptr<Interest>& interest, int stepCount,
165  const OnVerifiedInterest& onVerified,
166  const OnVerifyInterestFailed& onVerifyFailed, WireFormat& wireFormat);
167 
176  virtual bool
177  checkSigningPolicy(const Name& dataName, const Name& certificateName);
178 
185  virtual Name
186  inferSigningIdentity(const Name& dataName);
187 
188 private:
189  // Allow the unit tests to call private members.
190  friend class ::TestVerificationRulesFriend;
191 
196  class TrustAnchorRefreshManager {
197  public:
198  TrustAnchorRefreshManager()
199  {
200  }
201 
202  static ptr_lib::shared_ptr<IdentityCertificate>
203  loadIdentityCertificateFromFile(const std::string& filename);
204 
205  ptr_lib::shared_ptr<IdentityCertificate>
206  getCertificate(Name certificateName) const
207  {
208  // Assume the timestamp is already removed.
209  return certificateCache_.getCertificate(certificateName);
210  }
211 
212  void
213  addDirectory(const std::string& directoryName, Milliseconds refreshPeriod);
214 
215  void
216  refreshAnchors();
217 
218  private:
219  class DirectoryInfo {
220  public:
221  DirectoryInfo
222  (const std::vector<std::string>& certificateNames,
223  MillisecondsSince1970 nextRefresh, Milliseconds refreshPeriod)
224  : certificateNames_(certificateNames), nextRefresh_(nextRefresh),
225  refreshPeriod_(refreshPeriod)
226  {
227  }
228 
229  std::vector<std::string> certificateNames_;
230  MillisecondsSince1970 nextRefresh_;
231  Milliseconds refreshPeriod_;
232  };
233 
234  CertificateCache certificateCache_;
235  // refreshDirectories_ maps the directory name to certificate names so they
236  // can be deleted when necessary, and the next refresh time.
237  std::map<std::string, ptr_lib::shared_ptr<DirectoryInfo> > refreshDirectories_;
238  };
239 
246  void
247  loadTrustAnchorCertificates();
248 
261  bool
262  checkSignatureMatch
263  (const Name& signatureName, const Name& objectName, const BoostInfoTree& rule);
264 
273  ptr_lib::shared_ptr<IdentityCertificate>
274  lookupCertificate(const std::string& certID, bool isPath);
275 
285  const BoostInfoTree*
286  findMatchingRule(const Name& objName, const std::string& matchType) const;
287 
301  static bool
302  matchesRelation
303  (const Name& name, const Name& matchName, const std::string& matchRelation);
304 
312  static ptr_lib::shared_ptr<Signature>
313  extractSignature(const Interest& interest, WireFormat& wireFormat);
314 
322  bool
323  interestTimestampIsFresh(const Name& keyName, MillisecondsSince1970 timestamp) const;
324 
333  void
334  updateTimestampForKey(const Name& keyName, MillisecondsSince1970 timestamp);
335 
347  bool
348  verify(const Signature* signatureInfo, const SignedBlob& signedBlob) const;
349 
364  ptr_lib::shared_ptr<Interest>
365  getCertificateInterest
366  (int stepCount, const std::string& matchType, const Name& objectName,
367  const Signature* signature);
368 
381  void
382  onCertificateDownloadComplete
383  (const ptr_lib::shared_ptr<Data> &data,
384  const ptr_lib::shared_ptr<Data> &originalData, int stepCount,
385  const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed);
386 
399  void
400  onCertificateDownloadCompleteForInterest
401  (const ptr_lib::shared_ptr<Data> &data,
402  const ptr_lib::shared_ptr<Interest> &originalInterest, int stepCount,
403  const OnVerifiedInterest& onVerified,
404  const OnVerifyInterestFailed& onVerifyFailed, WireFormat& wireFormat);
405 
406  ptr_lib::shared_ptr<CertificateCache> certificateCache_;
407  int maxDepth_;
408  Milliseconds keyGraceInterval_;
409  Milliseconds keyTimestampTtl_;
410  int maxTrackedKeys_;
411  // fixedCertificateCache_ stores the fixed-signer certificate name associated with
412  // validation rules so we don't keep loading from files.
413  std::map<std::string, std::string> fixedCertificateCache_;
414  // keyTimestamps_ stores the timestamps for each public key used in command
415  // interests to avoid replay attacks.
416  // key is the public key name, value is the last timestamp.
417  std::map<std::string, MillisecondsSince1970> keyTimestamps_;
418  ptr_lib::shared_ptr<BoostInfoParser> config_;
419  bool requiresVerification_;
420  TrustAnchorRefreshManager refreshManager_;
421 };
422 
423 }
424 
425 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
A ConfigPolicyManager manages trust according to a configuration file in the Validator Configuration ...
Definition: config-policy-manager.hpp:56
func_lib::function< void(const ptr_lib::shared_ptr< Interest > &interest)> OnVerifyInterestFailed
An OnVerifyInterestFailed function object is used to pass a callback to verifyInterest to report a fa...
Definition: validation-request.hpp:52
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
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:45
virtual ptr_lib::shared_ptr< ValidationRequest > checkVerificationPolicy(const ptr_lib::shared_ptr< Data > &data, int stepCount, const OnVerified &onVerified, const OnVerifyFailed &onVerifyFailed)
Check whether the received data packet complies with the verification policy, and get the indication ...
virtual bool skipVerifyAndTrust(const Data &data)
Check if the received data packet can escape from verification and be trusted as valid.
Definition: data.hpp:36
virtual bool requireVerify(const Data &data)
Check if this PolicyManager has a verification rule for the received data.
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:45
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 acts on the rules specified in the configuration file and down...
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:42
A Signature is an abstract base class providing methods to work with the signature information in a D...
Definition: signature.hpp:36
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:41
TestVerificationRulesFriend is a friend of ConfigPolicyManager so we can call its private members thr...
Definition: test-verification-rules.cpp:140
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:116
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data)> OnVerifyFailed
An OnVerifyFailed function object is used to pass a callback to verifyData to report a failed verific...
Definition: validation-request.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:37
A PolicyManager is an abstract base class to represent the policy for verifying data packets...
Definition: policy-manager.hpp:37
A CertificateCache is used to save other users' certificate during verification.
Definition: certificate-cache.hpp:36