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 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 23 | SecRuleRelative::SecRuleRelative (const string& dataRegex, const string& signerRegex, const string& op, |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 24 | 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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | if (op != ">" && op != ">=" && op != "==") |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 35 | throw Error("op is wrong!"); |
| 36 | } |
| 37 | |
| 38 | SecRuleRelative::~SecRuleRelative() |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 42 | bool |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 43 | SecRuleRelative::satisfy (const Data& data) |
| 44 | { |
| 45 | Name dataName = data.getName(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | try { |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 47 | SignatureSha256WithRsa sig(data.getSignature()); |
| 48 | Name signerName = sig.getKeyLocator().getName (); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 49 | return satisfy (dataName, signerName); |
| 50 | } |
| 51 | catch (SignatureSha256WithRsa::Error& e){ |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 52 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | } |
| 54 | catch (KeyLocator::Error& e){ |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 58 | |
| 59 | bool |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 60 | SecRuleRelative::satisfy (const Name& dataName, const Name& signerName) |
| 61 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | if (!m_dataNameRegex.match(dataName)) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 63 | return false; |
| 64 | Name expandDataName = m_dataNameRegex.expand(); |
| 65 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | if (!m_signerNameRegex.match(signerName)) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 67 | return false; |
| 68 | Name expandSignerName = m_signerNameRegex.expand(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 70 | bool matched = compare(expandDataName, expandSignerName); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 71 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 72 | return matched; |
| 73 | } |
| 74 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 75 | bool |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 76 | SecRuleRelative::matchDataName (const Data& data) |
| 77 | { return m_dataNameRegex.match(data.getName()); } |
| 78 | |
| 79 | bool |
| 80 | SecRuleRelative::matchSignerName (const Data& data) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | { |
| 82 | try { |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 83 | SignatureSha256WithRsa sig(data.getSignature()); |
| 84 | Name signerName = sig.getKeyLocator().getName (); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 85 | return m_signerNameRegex.match(signerName); |
| 86 | } |
| 87 | catch (SignatureSha256WithRsa::Error& e){ |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 88 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | } |
| 90 | catch (KeyLocator::Error& e){ |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | bool |
| 96 | SecRuleRelative::compare(const Name& dataName, const Name& signerName) |
| 97 | { |
| 98 | if ((dataName == signerName) && ("==" == m_op || ">=" == m_op)) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 99 | return true; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 100 | |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 101 | 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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | |
| 112 | if (i == dataName.end()) |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 113 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 114 | else |
Yingdi Yu | 3715f8d | 2014-01-30 00:32:20 -0800 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 118 | } // namespace ndn |