Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 10 | #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 Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 17 | INIT_LOGGER ("ndn.SecRuleRelative"); |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 18 | |
| 19 | using namespace std; |
| 20 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 21 | namespace ndn { |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 22 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 23 | SecRuleRelative::SecRuleRelative (const string& dataRegex, const string& signerRegex, |
| 24 | const string& op, |
| 25 | const string& dataExpand, const string& signerExpand, |
| 26 | bool isPositive) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | if (op != ">" && op != ">=" && op != "==") |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 37 | throw Error("op is wrong!"); |
| 38 | } |
| 39 | |
| 40 | SecRuleRelative::~SecRuleRelative() |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 41 | { |
| 42 | } |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | bool |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 45 | SecRuleRelative::satisfy (const Data& data) |
| 46 | { |
| 47 | Name dataName = data.getName(); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 48 | 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 Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 58 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | |
| 60 | bool |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 61 | SecRuleRelative::satisfy (const Name& dataName, const Name& signerName) |
| 62 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 63 | if (!m_dataNameRegex.match(dataName)) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 64 | return false; |
| 65 | Name expandDataName = m_dataNameRegex.expand(); |
| 66 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | if (!m_signerNameRegex.match(signerName)) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 68 | return false; |
| 69 | Name expandSignerName = m_signerNameRegex.expand(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 71 | bool matched = compare(expandDataName, expandSignerName); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 73 | return matched; |
| 74 | } |
| 75 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | bool |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 77 | SecRuleRelative::matchDataName (const Data& data) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 78 | { |
| 79 | return m_dataNameRegex.match(data.getName()); |
| 80 | } |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 81 | |
| 82 | bool |
| 83 | SecRuleRelative::matchSignerName (const Data& data) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 85 | 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 Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 97 | bool |
| 98 | SecRuleRelative::compare(const Name& dataName, const Name& signerName) |
| 99 | { |
| 100 | if ((dataName == signerName) && ("==" == m_op || ">=" == m_op)) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 101 | return true; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 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 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 113 | |
| 114 | if (i == dataName.end()) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 115 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | else |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 120 | } // namespace ndn |