NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
sec-rule-relative.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #include "common.hpp"
25 
26 #include "sec-rule-relative.hpp"
27 
29 #include "security-common.hpp"
30 
31 namespace ndn {
32 
33 using std::string;
34 
35 SecRuleRelative::SecRuleRelative(const string& dataRegex, const string& signerRegex,
36  const string& op,
37  const string& dataExpand, const string& signerExpand,
38  bool isPositive)
39  : SecRule(isPositive),
40  m_dataRegex(dataRegex),
41  m_signerRegex(signerRegex),
42  m_op(op),
43  m_dataExpand(dataExpand),
44  m_signerExpand(signerExpand),
45  m_dataNameRegex(dataRegex, dataExpand),
46  m_signerNameRegex(signerRegex, signerExpand)
47 {
48  if (op != ">" && op != ">=" && op != "==")
49  BOOST_THROW_EXCEPTION(Error("op is wrong"));
50 }
51 
53 {
54 }
55 
56 bool
58 {
59  Name dataName = data.getName();
60  try
61  {
62  if (!data.getSignature().hasKeyLocator())
63  return false;
64 
65  const KeyLocator& keyLocator = data.getSignature().getKeyLocator();
66  if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
67  return false;
68 
69  const Name& signerName = keyLocator.getName();
70  return satisfy(dataName, signerName);
71  }
72  catch (tlv::Error& e)
73  {
74  return false;
75  }
76  catch (RegexMatcher::Error& e)
77  {
78  return false;
79  }
80 }
81 
82 bool
83 SecRuleRelative::satisfy(const Name& dataName, const Name& signerName)
84 {
85  if (!m_dataNameRegex.match(dataName))
86  return false;
87  Name expandDataName = m_dataNameRegex.expand();
88 
89  if (!m_signerNameRegex.match(signerName))
90  return false;
91  Name expandSignerName = m_signerNameRegex.expand();
92 
93  bool matched = compare(expandDataName, expandSignerName);
94 
95  return matched;
96 }
97 
98 bool
100 {
101  return m_dataNameRegex.match(data.getName());
102 }
103 
104 bool
106 {
107  try
108  {
109  if (!data.getSignature().hasKeyLocator())
110  return false;
111 
112  const KeyLocator& keyLocator = data.getSignature().getKeyLocator();
113  if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
114  return false;
115 
116  const Name& signerName = keyLocator.getName();
117  return m_signerNameRegex.match(signerName);
118  }
119  catch (tlv::Error& e)
120  {
121  return false;
122  }
123  catch (RegexMatcher::Error& e)
124  {
125  return false;
126  }
127 }
128 
129 bool
130 SecRuleRelative::compare(const Name& dataName, const Name& signerName)
131 {
132  if ((dataName == signerName) && ("==" == m_op || ">=" == m_op))
133  return true;
134 
135  Name::const_iterator i = dataName.begin();
136  Name::const_iterator j = signerName.begin();
137 
138  for (; i != dataName.end() && j != signerName.end(); i++, j++)
139  {
140  if (i->compare(*j) == 0)
141  continue;
142  else
143  return false;
144  }
145 
146  if (i == dataName.end())
147  return false;
148  else
149  return true;
150 }
151 
152 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
const_iterator end() const
End iterator (const).
Definition: name.hpp:579
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
const Name & getName() const
get Name element
indicates KeyLocator contains a Name
Definition: key-locator.hpp:49
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
bool hasKeyLocator() const
Check if SignatureInfo block has a KeyLocator.
Definition: signature.hpp:132
virtual bool matchSignerName(const Data &data)
SecRuleRelative(const std::string &dataRegex, const std::string &signerRegex, const std::string &op, const std::string &dataExpand, const std::string &signerExpand, bool isPositive)
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const_iterator begin() const
Begin iterator (const).
Definition: name.hpp:568
virtual bool matchDataName(const Data &data)
virtual Name expand(const std::string &expand="")
virtual bool satisfy(const Data &data)
Component holds a read-only name component value.
Type getType() const
bool match(const Name &name)
const Signature & getSignature() const
Definition: data.hpp:348
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Definition: signature.hpp:143
represents a Data packet
Definition: data.hpp:37
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50