blob: 042d04f073d58c9e25e5653a2c71a9f85b4898e8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu3715f8d2014-01-30 00:32:20 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu3715f8d2014-01-30 00:32:20 -080022 */
23
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "common.hpp"
25
Yingdi Yu3715f8d2014-01-30 00:32:20 -080026#include "sec-rule-relative.hpp"
27
28#include "signature-sha256-with-rsa.hpp"
29#include "security-common.hpp"
30
Yingdi Yufc40d872014-02-18 12:56:04 -080031namespace ndn {
Yingdi Yu3715f8d2014-01-30 00:32:20 -080032
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070033using std::string;
34
35SecRuleRelative::SecRuleRelative(const string& dataRegex, const string& signerRegex,
36 const string& op,
37 const string& dataExpand, const string& signerExpand,
38 bool isPositive)
Yingdi Yu3715f8d2014-01-30 00:32:20 -080039 : SecRule(isPositive),
40 m_dataRegex(dataRegex),
41 m_signerRegex(signerRegex),
42 m_op(op),
43 m_dataExpand(dataExpand),
44 m_signerExpand(signerExpand),
45 m_dataNameRegex(dataRegex, dataExpand),
46 m_signerNameRegex(signerRegex, signerExpand)
47{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048 if (op != ">" && op != ">=" && op != "==")
Yingdi Yu3715f8d2014-01-30 00:32:20 -080049 throw Error("op is wrong!");
50}
51
52SecRuleRelative::~SecRuleRelative()
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053{
54}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080055
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056bool
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070057SecRuleRelative::satisfy(const Data& data)
Yingdi Yu3715f8d2014-01-30 00:32:20 -080058{
59 Name dataName = data.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070060 try
61 {
Yingdi Yu4a557052014-07-09 16:40:37 -070062 if (!data.getSignature().hasKeyLocator())
63 return false;
64
65 const KeyLocator& keyLocator = data.getSignature().getKeyLocator();
66 if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
67 return false;
68
69 const Name& signerName = keyLocator.getName();
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070070 return satisfy(dataName, signerName);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070071 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060072 catch (tlv::Error& e)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070073 {
74 return false;
75 }
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070076 catch (RegexMatcher::Error& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070077 {
78 return false;
79 }
Yingdi Yu3715f8d2014-01-30 00:32:20 -080080}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081
82bool
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070083SecRuleRelative::satisfy(const Name& dataName, const Name& signerName)
Yingdi Yu3715f8d2014-01-30 00:32:20 -080084{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070085 if (!m_dataNameRegex.match(dataName))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080086 return false;
87 Name expandDataName = m_dataNameRegex.expand();
88
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 if (!m_signerNameRegex.match(signerName))
Yingdi Yu3715f8d2014-01-30 00:32:20 -080090 return false;
91 Name expandSignerName = m_signerNameRegex.expand();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092
Yingdi Yu3715f8d2014-01-30 00:32:20 -080093 bool matched = compare(expandDataName, expandSignerName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070094
Yingdi Yu3715f8d2014-01-30 00:32:20 -080095 return matched;
96}
97
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098bool
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070099SecRuleRelative::matchDataName(const Data& data)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700100{
101 return m_dataNameRegex.match(data.getName());
102}
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800103
104bool
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700105SecRuleRelative::matchSignerName(const Data& data)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 try
108 {
Yingdi Yu4a557052014-07-09 16:40:37 -0700109 if (!data.getSignature().hasKeyLocator())
110 return false;
111
112 const KeyLocator& keyLocator = data.getSignature().getKeyLocator();
113 if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
114 return false;
115
116 const Name& signerName = keyLocator.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700117 return m_signerNameRegex.match(signerName);
118 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600119 catch (tlv::Error& e)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700120 {
121 return false;
122 }
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700123 catch (RegexMatcher::Error& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 {
125 return false;
126 }
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800127}
128
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129bool
130SecRuleRelative::compare(const Name& dataName, const Name& signerName)
131{
132 if ((dataName == signerName) && ("==" == m_op || ">=" == m_op))
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800133 return true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700135 Name::const_iterator i = dataName.begin();
136 Name::const_iterator j = signerName.begin();
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800137
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700138 for (; i != dataName.end() && j != signerName.end(); i++, j++)
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800139 {
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700140 if (i->compare(*j) == 0)
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800141 continue;
142 else
143 return false;
144 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145
146 if (i == dataName.end())
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800147 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148 else
Yingdi Yu3715f8d2014-01-30 00:32:20 -0800149 return true;
150}
151
Yingdi Yufc40d872014-02-18 12:56:04 -0800152} // namespace ndn