blob: 34e2582a5df03794223563e8bcd667417eb45426 [file] [log] [blame]
Yingdi Yu7d773322015-03-22 21:32:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yu7d773322015-03-22 21:32:48 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * This file is part of NDN DeLorean, An Authentication System for Data Archives in
6 * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors
7 * and contributors.
Yingdi Yu7d773322015-03-22 21:32:48 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * NDN DeLorean is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
Yingdi Yu7d773322015-03-22 21:32:48 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Yingdi Yu7d773322015-03-22 21:32:48 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * You should have received a copy of the GNU General Public License along with NDN
19 * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu7d773322015-03-22 21:32:48 -070020 */
21
22#include "policy-checker.hpp"
23#include <ndn-cxx/util/time.hpp>
24#include <ndn-cxx/security/validator.hpp>
25#include <boost/algorithm/string.hpp>
26
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070027namespace ndn {
28namespace delorean {
Yingdi Yu7d773322015-03-22 21:32:48 -070029
30using ndn::time::system_clock;
31
32PolicyChecker::PolicyChecker()
33{
34}
35
36void
37PolicyChecker::reset()
38{
39 m_dataRules.clear();
40}
41
42void
43PolicyChecker::loadPolicy(const conf::ConfigSection& configSection)
44{
45 reset();
46
47 for (const auto& section : configSection) {
48 if (boost::iequals(section.first, "rule")) {
49 onConfigRule(section.second);
50 }
51 else
52 throw Error("Error in loading policy checker: unrecognized section " + section.first);
53 }
54}
55
56void
57PolicyChecker::onConfigRule(const conf::ConfigSection& section)
58{
Yingdi Yu7d773322015-03-22 21:32:48 -070059 auto it = section.begin();
60
61 // Get rule.id
62 if (it == section.end() || !boost::iequals(it->first, "id"))
63 throw Error("Expect <rule.id>");
64
65 std::string ruleId = it->second.data();
66 it++;
67
68 // Get rule.for
69 if (it == section.end() || !boost::iequals(it->first, "for"))
70 throw Error("Expect <rule.for> in rule: " + ruleId);
71
72 std::string usage = it->second.data();
73 it++;
74
75 bool isForData;
76 if (boost::iequals(usage, "data"))
77 isForData = true;
78 else if (boost::iequals(usage, "interest"))
79 isForData = false;
80 else
81 throw Error("Unrecognized <rule.for>: " + usage + " in rule: " + ruleId);
82
83 // Get rule.filter(s)
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070084 std::vector<shared_ptr<conf::Filter> > filters;
Yingdi Yu7d773322015-03-22 21:32:48 -070085 for (; it != section.end(); it++) {
86 if (!boost::iequals(it->first, "filter")) {
87 if (boost::iequals(it->first, "checker"))
88 break;
89 throw Error("Expect <rule.filter> in rule: " + ruleId);
90 }
91
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070092 filters.push_back(conf::FilterFactory::create(it->second));
Yingdi Yu7d773322015-03-22 21:32:48 -070093 continue;
94 }
95
96 // Get rule.checker(s)
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070097 std::vector<shared_ptr<conf::Checker> > checkers;
Yingdi Yu7d773322015-03-22 21:32:48 -070098 for (; it != section.end(); it++) {
99 if (!boost::iequals(it->first, "checker"))
100 throw Error("Expect <rule.checker> in rule: " + ruleId);
101
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700102 checkers.push_back(conf::CheckerFactory::create(it->second));
Yingdi Yu7d773322015-03-22 21:32:48 -0700103 continue;
104 }
105
106 // Check other stuff
107 if (it != section.end())
108 throw Error("Expect the end of rule: " + ruleId);
109
110 if (checkers.size() == 0)
111 throw Error("No <rule.checker> is specified in rule: " + ruleId);
112
113 if (isForData) {
114 auto rule = make_shared<conf::Rule>(ruleId);
115 for (size_t i = 0; i < filters.size(); i++)
116 rule->addFilter(filters[i]);
117 for (size_t i = 0; i < checkers.size(); i++)
118 rule->addChecker(checkers[i]);
119
120 m_dataRules.push_back(rule);
121 }
122}
123
124bool
125PolicyChecker::check(const Timestamp& dataTimestamp, const Data& data,
126 const Timestamp& keyTimestamp, const ndn::IdentityCertificate& cert)
127{
128 system_clock::TimePoint dataTs((time::seconds(dataTimestamp)));
129 system_clock::TimePoint keyTs((time::seconds(keyTimestamp)));
130 system_clock::TimePoint endTs = cert.getNotAfter();
131 system_clock::TimePoint startTs = cert.getNotBefore();
132
133 if (dataTs > endTs || dataTs < keyTs || dataTs < startTs)
134 return false;
135
136 if (!checkRule(data))
137 return false;
138
139 Name keyLocatorName;
140 try {
141 keyLocatorName = data.getSignature().getKeyLocator().getName();
142 }
143 catch (tlv::Error&) {
144 return false;
145 }
146
147 if (!keyLocatorName.isPrefixOf(cert.getName()))
148 return false;
149
150 if (!ndn::Validator::verifySignature(data, cert.getPublicKeyInfo()))
151 return false;
152
153 return true;
154}
155
156bool
157PolicyChecker::checkRule(const Data& data)
158{
159 for (auto& rule : m_dataRules) {
160 if (rule->match(data)) {
161 return rule->check(data);
162 }
163 }
164
165 return false;
166}
167
168
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700169} // namespace delorean
170} // namespace ndn