blob: 3b0b40f33bb7ba8d6a85d64f8363855648ac9118 [file] [log] [blame]
Yingdi Yu3715f8d2014-01-30 00:32:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08008#include "common.hpp"
9
Yingdi Yu3715f8d2014-01-30 00:32:20 -080010#include "sec-rule-relative.hpp"
11
12#include "signature-sha256-with-rsa.hpp"
13#include "security-common.hpp"
14
15#include "../util/logging.hpp"
16
Yingdi Yu21157162014-02-28 13:02:34 -080017INIT_LOGGER ("ndn.SecRuleRelative");
Yingdi Yu3715f8d2014-01-30 00:32:20 -080018
19using namespace std;
20
Yingdi Yufc40d872014-02-18 12:56:04 -080021namespace ndn {
Yingdi Yu3715f8d2014-01-30 00:32:20 -080022
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070023SecRuleRelative::SecRuleRelative (const string& dataRegex, const string& signerRegex,
24 const string& op,
25 const string& dataExpand, const string& signerExpand,
26 bool isPositive)
Yingdi Yu3715f8d2014-01-30 00:32:20 -080027 : 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{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036 if (op != ">" && op != ">=" && op != "==")
Yingdi Yu3715f8d2014-01-30 00:32:20 -080037 throw Error("op is wrong!");
38}
39
40SecRuleRelative::~SecRuleRelative()
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041{
42}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080043
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080045SecRuleRelative::satisfy (const Data& data)
46{
47 Name dataName = data.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070048 try
49 {
50 SignatureSha256WithRsa sig(data.getSignature());
51 Name signerName = sig.getKeyLocator().getName ();
52 return satisfy (dataName, signerName);
53 }
54 catch (std::runtime_error& e)
55 {
56 return false;
57 }
Yingdi Yu3715f8d2014-01-30 00:32:20 -080058}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059
60bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080061SecRuleRelative::satisfy (const Name& dataName, const Name& signerName)
62{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 if (!m_dataNameRegex.match(dataName))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080064 return false;
65 Name expandDataName = m_dataNameRegex.expand();
66
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 if (!m_signerNameRegex.match(signerName))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080068 return false;
69 Name expandSignerName = m_signerNameRegex.expand();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070
Yingdi Yu3715f8d2014-01-30 00:32:20 -080071 bool matched = compare(expandDataName, expandSignerName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072
Yingdi Yu3715f8d2014-01-30 00:32:20 -080073 return matched;
74}
75
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080077SecRuleRelative::matchDataName (const Data& data)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070078{
79 return m_dataNameRegex.match(data.getName());
80}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080081
82bool
83SecRuleRelative::matchSignerName (const Data& data)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070085 try
86 {
87 SignatureSha256WithRsa sig(data.getSignature());
88 Name signerName = sig.getKeyLocator().getName ();
89 return m_signerNameRegex.match(signerName);
90 }
91 catch (std::runtime_error& e)
92 {
93 return false;
94 }
Yingdi Yu3715f8d2014-01-30 00:32:20 -080095}
96
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097bool
98SecRuleRelative::compare(const Name& dataName, const Name& signerName)
99{
100 if ((dataName == signerName) && ("==" == m_op || ">=" == m_op))
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800101 return true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800103 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 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113
114 if (i == dataName.end())
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800115 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116 else
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800117 return true;
118}
119
Yingdi Yufc40d872014-02-18 12:56:04 -0800120} // namespace ndn