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