blob: cdfdeac64e1b59ce229e96cf68de73ea4186e17d [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
17INIT_LOGGER ("SecRuleRelative");
18
19using namespace std;
20
21namespace ndn
22{
23
24SecRuleRelative::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
39SecRuleRelative::~SecRuleRelative()
40{ }
41
42bool
43SecRuleRelative::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
57bool
58SecRuleRelative::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
73bool
74SecRuleRelative::matchDataName (const Data& data)
75{ return m_dataNameRegex.match(data.getName()); }
76
77bool
78SecRuleRelative::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
91bool
92SecRuleRelative::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