All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sec-rule-relative.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #include "common.hpp"
9 
10 #include "sec-rule-relative.hpp"
11 
13 #include "security-common.hpp"
14 
15 #include "../util/logging.hpp"
16 
17 INIT_LOGGER ("ndn.SecRuleRelative");
18 
19 using namespace std;
20 
21 namespace ndn {
22 
23 SecRuleRelative::SecRuleRelative (const string& dataRegex, const string& signerRegex,
24  const string& op,
25  const string& dataExpand, const string& signerExpand,
26  bool isPositive)
27  : SecRule(isPositive),
28  m_dataRegex(dataRegex),
29  m_signerRegex(signerRegex),
30  m_op(op),
31  m_dataExpand(dataExpand),
32  m_signerExpand(signerExpand),
33  m_dataNameRegex(dataRegex, dataExpand),
34  m_signerNameRegex(signerRegex, signerExpand)
35 {
36  if (op != ">" && op != ">=" && op != "==")
37  throw Error("op is wrong!");
38 }
39 
41 {
42 }
43 
44 bool
46 {
47  Name dataName = data.getName();
48  try
49  {
51  Name signerName = sig.getKeyLocator().getName ();
52  return satisfy (dataName, signerName);
53  }
54  catch (std::runtime_error& e)
55  {
56  return false;
57  }
58 }
59 
60 bool
61 SecRuleRelative::satisfy (const Name& dataName, const Name& signerName)
62 {
63  if (!m_dataNameRegex.match(dataName))
64  return false;
65  Name expandDataName = m_dataNameRegex.expand();
66 
67  if (!m_signerNameRegex.match(signerName))
68  return false;
69  Name expandSignerName = m_signerNameRegex.expand();
70 
71  bool matched = compare(expandDataName, expandSignerName);
72 
73  return matched;
74 }
75 
76 bool
78 {
79  return m_dataNameRegex.match(data.getName());
80 }
81 
82 bool
84 {
85  try
86  {
88  Name signerName = sig.getKeyLocator().getName ();
89  return m_signerNameRegex.match(signerName);
90  }
91  catch (std::runtime_error& e)
92  {
93  return false;
94  }
95 }
96 
97 bool
98 SecRuleRelative::compare(const Name& dataName, const Name& signerName)
99 {
100  if ((dataName == signerName) && ("==" == m_op || ">=" == m_op))
101  return true;
102 
103  Name::const_iterator i = dataName.begin ();
104  Name::const_iterator j = signerName.begin ();
105 
106  for (; i != dataName.end () && j != signerName.end (); i++, j++)
107  {
108  if ((i->compare(*j)) == 0)
109  continue;
110  else
111  return false;
112  }
113 
114  if (i == dataName.end())
115  return false;
116  else
117  return true;
118 }
119 
120 } // namespace ndn
Representing of SHA256-with-RSA signature in a data packet.
const_iterator begin() const
Begin iterator (const).
Definition: name.hpp:480
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
const Name & getName() const
Definition: data.hpp:346
virtual bool matchSignerName(const Data &data)
const_iterator end() const
End iterator (const).
Definition: name.hpp:491
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
#define INIT_LOGGER(name)
Copyright (C) 2013 Regents of the University of California.
Definition: logging.hpp:53
virtual bool matchDataName(const Data &data)
const Signature & getSignature() const
Definition: data.hpp:450
virtual Name expand(const std::string &expand="")
virtual bool satisfy(const Data &data)
Component holds a read-only name component value.
bool match(const Name &name)