blob: 6133aac496c3afc78512eff2e68aa8d0b9575210 [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
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023SecRuleRelative::SecRuleRelative (const string& dataRegex, const string& signerRegex, const string& op,
Yingdi Yu3715f8d2014-01-30 00:32:20 -080024 const string& dataExpand, const string& signerExpand, bool isPositive)
25 : SecRule(isPositive),
26 m_dataRegex(dataRegex),
27 m_signerRegex(signerRegex),
28 m_op(op),
29 m_dataExpand(dataExpand),
30 m_signerExpand(signerExpand),
31 m_dataNameRegex(dataRegex, dataExpand),
32 m_signerNameRegex(signerRegex, signerExpand)
33{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 if (op != ">" && op != ">=" && op != "==")
Yingdi Yu3715f8d2014-01-30 00:32:20 -080035 throw Error("op is wrong!");
36}
37
38SecRuleRelative::~SecRuleRelative()
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039{
40}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080041
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080043SecRuleRelative::satisfy (const Data& data)
44{
45 Name dataName = data.getName();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 try {
Yingdi Yu3715f8d2014-01-30 00:32:20 -080047 SignatureSha256WithRsa sig(data.getSignature());
48 Name signerName = sig.getKeyLocator().getName ();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 return satisfy (dataName, signerName);
50 }
51 catch (SignatureSha256WithRsa::Error& e){
Yingdi Yu3715f8d2014-01-30 00:32:20 -080052 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053 }
54 catch (KeyLocator::Error& e){
Yingdi Yu3715f8d2014-01-30 00:32:20 -080055 return false;
56 }
57}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058
59bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080060SecRuleRelative::satisfy (const Name& dataName, const Name& signerName)
61{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 if (!m_dataNameRegex.match(dataName))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080063 return false;
64 Name expandDataName = m_dataNameRegex.expand();
65
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 if (!m_signerNameRegex.match(signerName))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080067 return false;
68 Name expandSignerName = m_signerNameRegex.expand();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069
Yingdi Yu3715f8d2014-01-30 00:32:20 -080070 bool matched = compare(expandDataName, expandSignerName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071
Yingdi Yu3715f8d2014-01-30 00:32:20 -080072 return matched;
73}
74
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080076SecRuleRelative::matchDataName (const Data& data)
77{ return m_dataNameRegex.match(data.getName()); }
78
79bool
80SecRuleRelative::matchSignerName (const Data& data)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081{
82 try {
Yingdi Yu3715f8d2014-01-30 00:32:20 -080083 SignatureSha256WithRsa sig(data.getSignature());
84 Name signerName = sig.getKeyLocator().getName ();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070085 return m_signerNameRegex.match(signerName);
86 }
87 catch (SignatureSha256WithRsa::Error& e){
Yingdi Yu3715f8d2014-01-30 00:32:20 -080088 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 }
90 catch (KeyLocator::Error& e){
Yingdi Yu3715f8d2014-01-30 00:32:20 -080091 return false;
92 }
93}
94
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095bool
96SecRuleRelative::compare(const Name& dataName, const Name& signerName)
97{
98 if ((dataName == signerName) && ("==" == m_op || ">=" == m_op))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080099 return true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800101 Name::const_iterator i = dataName.begin ();
102 Name::const_iterator j = signerName.begin ();
103
104 for (; i != dataName.end () && j != signerName.end (); i++, j++)
105 {
106 if ((i->compare(*j)) == 0)
107 continue;
108 else
109 return false;
110 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111
112 if (i == dataName.end())
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800113 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 else
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800115 return true;
116}
117
Yingdi Yufc40d872014-02-18 12:56:04 -0800118} // namespace ndn