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 | |
| 17 | INIT_LOGGER ("SecRuleRelative"); |
| 18 | |
| 19 | using namespace std; |
| 20 | |
| 21 | namespace ndn |
| 22 | { |
| 23 | |
| 24 | SecRuleRelative::SecRuleRelative (const string& dataRegex, const string& signerRegex, const string& op, |
| 25 | const string& dataExpand, const string& signerExpand, bool isPositive) |
| 26 | : SecRule(isPositive), |
| 27 | m_dataRegex(dataRegex), |
| 28 | m_signerRegex(signerRegex), |
| 29 | m_op(op), |
| 30 | m_dataExpand(dataExpand), |
| 31 | m_signerExpand(signerExpand), |
| 32 | m_dataNameRegex(dataRegex, dataExpand), |
| 33 | m_signerNameRegex(signerRegex, signerExpand) |
| 34 | { |
| 35 | if(op != ">" && op != ">=" && op != "==") |
| 36 | throw Error("op is wrong!"); |
| 37 | } |
| 38 | |
| 39 | SecRuleRelative::~SecRuleRelative() |
| 40 | { } |
| 41 | |
| 42 | bool |
| 43 | SecRuleRelative::satisfy (const Data& data) |
| 44 | { |
| 45 | Name dataName = data.getName(); |
| 46 | try{ |
| 47 | SignatureSha256WithRsa sig(data.getSignature()); |
| 48 | Name signerName = sig.getKeyLocator().getName (); |
| 49 | return satisfy (dataName, signerName); |
| 50 | }catch(SignatureSha256WithRsa::Error &e){ |
| 51 | return false; |
| 52 | }catch(KeyLocator::Error &e){ |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | bool |
| 58 | SecRuleRelative::satisfy (const Name& dataName, const Name& signerName) |
| 59 | { |
| 60 | if(!m_dataNameRegex.match(dataName)) |
| 61 | return false; |
| 62 | Name expandDataName = m_dataNameRegex.expand(); |
| 63 | |
| 64 | if(!m_signerNameRegex.match(signerName)) |
| 65 | return false; |
| 66 | Name expandSignerName = m_signerNameRegex.expand(); |
| 67 | |
| 68 | bool matched = compare(expandDataName, expandSignerName); |
| 69 | |
| 70 | return matched; |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | SecRuleRelative::matchDataName (const Data& data) |
| 75 | { return m_dataNameRegex.match(data.getName()); } |
| 76 | |
| 77 | bool |
| 78 | SecRuleRelative::matchSignerName (const Data& data) |
| 79 | { |
| 80 | try{ |
| 81 | SignatureSha256WithRsa sig(data.getSignature()); |
| 82 | Name signerName = sig.getKeyLocator().getName (); |
| 83 | return m_signerNameRegex.match(signerName); |
| 84 | }catch(SignatureSha256WithRsa::Error &e){ |
| 85 | return false; |
| 86 | }catch(KeyLocator::Error &e){ |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | SecRuleRelative::compare(const Name & dataName, const Name & signerName) |
| 93 | { |
| 94 | if((dataName == signerName) && ("==" == m_op || ">=" == m_op)) |
| 95 | return true; |
| 96 | |
| 97 | Name::const_iterator i = dataName.begin (); |
| 98 | Name::const_iterator j = signerName.begin (); |
| 99 | |
| 100 | for (; i != dataName.end () && j != signerName.end (); i++, j++) |
| 101 | { |
| 102 | if ((i->compare(*j)) == 0) |
| 103 | continue; |
| 104 | else |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if(i == dataName.end()) |
| 109 | return false; |
| 110 | else |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | }//ndn |