/
command-interest-validator.hpp
279 lines (242 loc) · 9.61 KB
/
command-interest-validator.hpp
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 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.
*/
#ifndef NDN_UTIL_COMMAND_INTEREST_VALIDATOR_HPP
#define NDN_UTIL_COMMAND_INTEREST_VALIDATOR_HPP
#include "../security/validator.hpp"
#include "../security/identity-certificate.hpp"
#include "../security/sec-rule-specific.hpp"
#include <list>
namespace ndn {
/**
* @brief Helper class to validate CommandInterests
*
* @deprecated Use ValidatorConfig instead.
* See http://redmine.named-data.net/projects/ndn-cxx/wiki/CommandValidatorConf
* for more details about the configuration format of ValidatorConfig.
*
* @see http://redmine.named-data.net/projects/nfd/wiki/Command_Interests
*/
class CommandInterestValidator : public Validator
{
public:
enum {
POS_SIG_VALUE = -1,
POS_SIG_INFO = -2,
POS_RANDOM_VAL = -3,
POS_TIMESTAMP = -4,
MIN_LENGTH = 4,
GRACE_INTERVAL = 3000 // ms
};
CommandInterestValidator(const time::milliseconds& graceInterval =
time::milliseconds(static_cast<int>(GRACE_INTERVAL)))
: m_graceInterval(graceInterval < time::milliseconds::zero() ?
time::milliseconds(static_cast<int>(GRACE_INTERVAL)) : graceInterval)
{
}
virtual
~CommandInterestValidator()
{
}
/**
* @brief add an Interest rule that allows a specific certificate
*
* @param regex NDN Regex to match Interest Name
* @param certificate trusted certificate
*/
void
addInterestRule(const std::string& regex, const IdentityCertificate& certificate);
/**
* @brief add an Interest rule that allows a specific public key
*
* @param regex NDN Regex to match Interest Name
* @param keyName KeyLocator.Name
* @param publicKey public key
*/
void
addInterestRule(const std::string& regex, const Name& keyName, const PublicKey& publicKey);
/**
* @brief add an Interest rule that allows any signer
*
* @param regex NDN Regex to match Interest Name
* @note Command Interest matched by regex that is signed by any key will be accepted.
*/
void
addInterestBypassRule(const std::string& regex);
/**
* @brief Remove all installed Interest rules (e.g., when reinitialization needed)
*/
void
reset();
protected:
virtual void
checkPolicy(const Data& data,
int stepCount,
const OnDataValidated& onValidated,
const OnDataValidationFailed& onValidationFailed,
std::vector<shared_ptr<ValidationRequest> >& nextSteps)
{
onValidationFailed(data.shared_from_this(), "No policy for data checking");
}
virtual void
checkPolicy(const Interest& interest,
int stepCount,
const OnInterestValidated& onValidated,
const OnInterestValidationFailed& onValidationFailed,
std::vector<shared_ptr<ValidationRequest> >& nextSteps);
private:
time::milliseconds m_graceInterval; //ms
std::map<Name, PublicKey> m_trustAnchorsForInterest;
std::list<SecRuleSpecific> m_trustScopeForInterest;
typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap;
LastTimestampMap m_lastTimestamp;
};
inline void
CommandInterestValidator::addInterestRule(const std::string& regex,
const IdentityCertificate& certificate)
{
Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
addInterestRule(regex, keyName, certificate.getPublicKeyInfo());
}
inline void
CommandInterestValidator::addInterestRule(const std::string& regex,
const Name& keyName,
const PublicKey& publicKey)
{
m_trustAnchorsForInterest[keyName] = publicKey;
shared_ptr<Regex> interestRegex = make_shared<Regex>(regex);
shared_ptr<Regex> signerRegex = Regex::fromName(keyName, true);
m_trustScopeForInterest.push_back(SecRuleSpecific(interestRegex, signerRegex));
}
inline void
CommandInterestValidator::addInterestBypassRule(const std::string& regex)
{
shared_ptr<Regex> interestRegex = make_shared<Regex>(regex);
m_trustScopeForInterest.push_back(SecRuleSpecific(interestRegex));
}
inline void
CommandInterestValidator::reset()
{
m_trustAnchorsForInterest.clear();
m_trustScopeForInterest.clear();
}
inline void
CommandInterestValidator::checkPolicy(const Interest& interest,
int stepCount,
const OnInterestValidated& onValidated,
const OnInterestValidationFailed& onValidationFailed,
std::vector<shared_ptr<ValidationRequest> >& nextSteps)
{
const Name& interestName = interest.getName();
//Prepare
if (interestName.size() < MIN_LENGTH)
return onValidationFailed(interest.shared_from_this(),
"Interest is not signed: " + interest.getName().toUri());
Name keyName;
try
{
Signature signature(interestName[POS_SIG_INFO].blockFromValue(),
interestName[POS_SIG_VALUE].blockFromValue());
if (signature.getType() != tlv::SignatureSha256WithRsa)
return onValidationFailed(interest.shared_from_this(),
"Require SignatureSha256WithRsa");
SignatureSha256WithRsa sig(signature);
const KeyLocator& keyLocator = sig.getKeyLocator();
if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
return onValidationFailed(interest.shared_from_this(),
"Key Locator is not a name");
keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocator.getName());
//Check if command is in the trusted scope
bool isInScope = false;
for (std::list<SecRuleSpecific>::iterator scopeIt = m_trustScopeForInterest.begin();
scopeIt != m_trustScopeForInterest.end();
++scopeIt)
{
if (scopeIt->satisfy(interestName, keyName))
{
if (scopeIt->isExempted())
{
return onValidated(interest.shared_from_this());
}
isInScope = true;
break;
}
}
if (isInScope == false)
return onValidationFailed(interest.shared_from_this(),
"Signer cannot be authorized for the command: " +
keyName.toUri());
//Check signature
if (!Validator::verifySignature(interestName.wireEncode().value(),
interestName.wireEncode().value_size() -
interestName[-1].size(),
sig, m_trustAnchorsForInterest[keyName]))
return onValidationFailed(interest.shared_from_this(),
"Signature cannot be validated: " +
interest.getName().toUri());
//Check if timestamp is valid
time::system_clock::TimePoint interestTime =
time::fromUnixTimestamp(time::milliseconds(interestName.get(POS_TIMESTAMP).toNumber()));
time::system_clock::TimePoint currentTime = time::system_clock::now();
LastTimestampMap::iterator timestampIt = m_lastTimestamp.find(keyName);
if (timestampIt == m_lastTimestamp.end())
{
if (!(currentTime - m_graceInterval <= interestTime &&
interestTime <= currentTime + m_graceInterval))
return onValidationFailed(interest.shared_from_this(),
"The command is not in grace interval: " +
interest.getName().toUri());
}
else
{
if (interestTime <= timestampIt->second)
return onValidationFailed(interest.shared_from_this(),
"The command is outdated: " +
interest.getName().toUri());
}
//Update timestamp
if (timestampIt == m_lastTimestamp.end())
{
m_lastTimestamp[keyName] = interestTime;
}
else
{
timestampIt->second = interestTime;
}
}
catch (Signature::Error& e)
{
return onValidationFailed(interest.shared_from_this(),
"No valid signature");
}
catch (IdentityCertificate::Error& e)
{
return onValidationFailed(interest.shared_from_this(),
"Cannot locate the signing key");
}
catch (tlv::Error& e)
{
return onValidationFailed(interest.shared_from_this(),
"Cannot decode signature related TLVs");
}
return onValidated(interest.shared_from_this());
}
} // namespace ndn
#endif // NDN_UTIL_COMMAND_INTEREST_VALIDATOR_HPP