/
checker.cpp
340 lines (288 loc) · 11.4 KB
/
checker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2021 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#include "ndn-cxx/security/validator-config/checker.hpp"
#include "ndn-cxx/security/validation-policy.hpp"
#include "ndn-cxx/security/validation-state.hpp"
#include "ndn-cxx/security/verification-helpers.hpp"
#include "ndn-cxx/security/pib/key.hpp"
#include <boost/algorithm/string/predicate.hpp>
namespace ndn {
namespace security {
inline namespace v2 {
namespace validator_config {
bool
Checker::check(uint32_t pktType, const Name& pktName, const Name& klName,
const shared_ptr<ValidationState>& state)
{
BOOST_ASSERT(pktType == tlv::Interest || pktType == tlv::Data);
if (pktType == tlv::Interest) {
auto fmt = state->getTag<SignedInterestFormatTag>();
BOOST_ASSERT(fmt);
if (*fmt == SignedInterestFormat::V03) {
// This check is redundant if parameter digest checking is enabled. However, the parameter
// digest checking can be disabled in API.
if (pktName.size() == 0 || pktName[-1].type() != tlv::ParametersSha256DigestComponent) {
return false;
}
return checkNames(pktName.getPrefix(-1), klName, state);
}
else {
if (pktName.size() < signed_interest::MIN_SIZE)
return false;
return checkNames(pktName.getPrefix(-signed_interest::MIN_SIZE), klName, state);
}
}
else {
return checkNames(pktName, klName, state);
}
}
NameRelationChecker::NameRelationChecker(const Name& name, const NameRelation& relation)
: m_name(name)
, m_relation(relation)
{
}
bool
NameRelationChecker::checkNames(const Name& pktName, const Name& klName,
const shared_ptr<ValidationState>& state)
{
// pktName not used in this check
Name identity = extractIdentityNameFromKeyLocator(klName);
bool result = checkNameRelation(m_relation, m_name, identity);
if (!result) {
std::ostringstream os;
os << "KeyLocator check failed: name relation " << m_name << " " << m_relation
<< " for packet " << pktName << " is invalid"
<< " (KeyLocator=" << klName << ", identity=" << identity << ")";
state->fail({ValidationError::POLICY_ERROR, os.str()});
}
return result;
}
RegexChecker::RegexChecker(const Regex& regex)
: m_regex(regex)
{
}
bool
RegexChecker::checkNames(const Name& pktName, const Name& klName, const shared_ptr<ValidationState>& state)
{
bool result = m_regex.match(klName);
if (!result) {
std::ostringstream os;
os << "KeyLocator check failed: regex " << m_regex << " for packet " << pktName << " is invalid"
<< " (KeyLocator=" << klName << ")";
state->fail({ValidationError::POLICY_ERROR, os.str()});
}
return result;
}
HyperRelationChecker::HyperRelationChecker(const std::string& pktNameExpr, const std::string pktNameExpand,
const std::string& klNameExpr, const std::string klNameExpand,
const NameRelation& hyperRelation)
: m_hyperPRegex(pktNameExpr, pktNameExpand)
, m_hyperKRegex(klNameExpr, klNameExpand)
, m_hyperRelation(hyperRelation)
{
}
bool
HyperRelationChecker::checkNames(const Name& pktName, const Name& klName,
const shared_ptr<ValidationState>& state)
{
if (!m_hyperPRegex.match(pktName) || !m_hyperKRegex.match(klName)) {
std::ostringstream os;
os << "Packet " << pktName << " (" << "KeyLocator=" << klName << ") does not match "
<< "the hyper relation rule pkt=" << m_hyperPRegex << ", key=" << m_hyperKRegex;
state->fail({ValidationError::POLICY_ERROR, os.str()});
return false;
}
bool result = checkNameRelation(m_hyperRelation, m_hyperKRegex.expand(), m_hyperPRegex.expand());
if (!result) {
std::ostringstream os;
os << "KeyLocator check failed: hyper relation " << m_hyperRelation
<< " pkt=" << m_hyperPRegex << ", key=" << m_hyperKRegex
<< " of packet " << pktName << " (KeyLocator=" << klName << ") is invalid";
state->fail({ValidationError::POLICY_ERROR, os.str()});
}
return result;
}
unique_ptr<Checker>
Checker::create(const ConfigSection& configSection, const std::string& configFilename)
{
auto propertyIt = configSection.begin();
// Get checker.type
if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) {
NDN_THROW(Error("Expecting <checker.type>"));
}
std::string type = propertyIt->second.data();
if (boost::iequals(type, "customized")) {
return createCustomizedChecker(configSection, configFilename);
}
else if (boost::iequals(type, "hierarchical")) {
return createHierarchicalChecker(configSection, configFilename);
}
else {
NDN_THROW(Error("Unrecognized <checker.type>: " + type));
}
}
unique_ptr<Checker>
Checker::createCustomizedChecker(const ConfigSection& configSection,
const std::string& configFilename)
{
auto propertyIt = configSection.begin();
propertyIt++;
// TODO implement restrictions based on signature type (outside this checker)
if (propertyIt != configSection.end() && boost::iequals(propertyIt->first, "sig-type")) {
// ignore sig-type
propertyIt++;
}
// Get checker.key-locator
if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "key-locator")) {
NDN_THROW(Error("Expecting <checker.key-locator>"));
}
auto checker = createKeyLocatorChecker(propertyIt->second, configFilename);
propertyIt++;
if (propertyIt != configSection.end()) {
NDN_THROW(Error("Expecting end of <checker>"));
}
return checker;
}
unique_ptr<Checker>
Checker::createHierarchicalChecker(const ConfigSection& configSection,
const std::string& configFilename)
{
auto propertyIt = configSection.begin();
propertyIt++;
// TODO implement restrictions based on signature type (outside this checker)
if (propertyIt != configSection.end() && boost::iequals(propertyIt->first, "sig-type")) {
// ignore sig-type
propertyIt++;
}
if (propertyIt != configSection.end()) {
NDN_THROW(Error("Expecting end of <checker>"));
}
return make_unique<HyperRelationChecker>("^(<>*)$", "\\1",
"^(<>*)<KEY><>{1,3}$", "\\1",
NameRelation::IS_PREFIX_OF);
}
unique_ptr<Checker>
Checker::createKeyLocatorChecker(const ConfigSection& configSection,
const std::string& configFilename)
{
auto propertyIt = configSection.begin();
// Get checker.key-locator.type
if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type"))
NDN_THROW(Error("Expecting <checker.key-locator.type>"));
std::string type = propertyIt->second.data();
if (boost::iequals(type, "name"))
return createKeyLocatorNameChecker(configSection, configFilename);
else
NDN_THROW(Error("Unrecognized <checker.key-locator.type>: " + type));
}
unique_ptr<Checker>
Checker::createKeyLocatorNameChecker(const ConfigSection& configSection,
const std::string& configFilename)
{
auto propertyIt = configSection.begin();
propertyIt++;
if (propertyIt == configSection.end())
NDN_THROW(Error("Unexpected end of <checker.key-locator>"));
if (boost::iequals(propertyIt->first, "name")) {
Name name;
try {
name = Name(propertyIt->second.data());
}
catch (const Name::Error&) {
NDN_THROW_NESTED(Error("Invalid <checker.key-locator.name>: " + propertyIt->second.data()));
}
propertyIt++;
if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation")) {
NDN_THROW(Error("Expecting <checker.key-locator.relation>"));
}
std::string relationString = propertyIt->second.data();
propertyIt++;
NameRelation relation = getNameRelationFromString(relationString);
if (propertyIt != configSection.end()) {
NDN_THROW(Error("Expecting end of <checker.key-locator>"));
}
return make_unique<NameRelationChecker>(name, relation);
}
else if (boost::iequals(propertyIt->first, "regex")) {
std::string regexString = propertyIt->second.data();
propertyIt++;
if (propertyIt != configSection.end()) {
NDN_THROW(Error("Expecting end of <checker.key-locator>"));
}
try {
return make_unique<RegexChecker>(Regex(regexString));
}
catch (const Regex::Error&) {
NDN_THROW_NESTED(Error("Invalid <checker.key-locator.regex>: " + regexString));
}
}
else if (boost::iequals(propertyIt->first, "hyper-relation")) {
const ConfigSection& hSection = propertyIt->second;
auto hPropertyIt = hSection.begin();
// Get k-regex
if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "k-regex")) {
NDN_THROW(Error("Expecting <checker.key-locator.hyper-relation.k-regex>"));
}
std::string kRegex = hPropertyIt->second.data();
hPropertyIt++;
// Get k-expand
if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "k-expand")) {
NDN_THROW(Error("Expecting <checker.key-locator.hyper-relation.k-expand>"));
}
std::string kExpand = hPropertyIt->second.data();
hPropertyIt++;
// Get h-relation
if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "h-relation")) {
NDN_THROW(Error("Expecting <checker.key-locator.hyper-relation.h-relation>"));
}
std::string hRelation = hPropertyIt->second.data();
hPropertyIt++;
// Get p-regex
if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "p-regex")) {
NDN_THROW(Error("Expecting <checker.key-locator.hyper-relation.p-regex>"));
}
std::string pRegex = hPropertyIt->second.data();
hPropertyIt++;
// Get p-expand
if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first, "p-expand")) {
NDN_THROW(Error("Expecting <checker.key-locator.hyper-relation.p-expand>"));
}
std::string pExpand = hPropertyIt->second.data();
hPropertyIt++;
if (hPropertyIt != hSection.end()) {
NDN_THROW(Error("Expecting end of <checker.key-locator.hyper-relation>"));
}
NameRelation relation = getNameRelationFromString(hRelation);
try {
return make_unique<HyperRelationChecker>(pRegex, pExpand, kRegex, kExpand, relation);
}
catch (const Regex::Error&) {
NDN_THROW_NESTED(Error("Invalid regex for <key-locator.hyper-relation>"));
}
}
else {
NDN_THROW(Error("Unrecognized <checker.key-locator>: " + propertyIt->first));
}
}
} // namespace validator_config
} // inline namespace v2
} // namespace security
} // namespace ndn