blob: 47d536e2d947c38dd24daa28cda159ba3ca6f2a4 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu48e8c0c2014-03-19 12:01:55 -07002/**
Zhiyi Zhang48becde2017-01-05 16:41:38 -08003 * Copyright (c) 2013-2017 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.
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070020 */
21
22#ifndef NDN_SECURITY_VALIDATOR_CONFIG_HPP
23#define NDN_SECURITY_VALIDATOR_CONFIG_HPP
24
25#include "validator.hpp"
26#include "certificate-cache.hpp"
27#include "conf/rule.hpp"
28#include "conf/common.hpp"
29
30namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070031namespace security {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070032
Zhiyi Zhang7ba99e12016-11-10 14:26:16 -080033/**
34 * @brief The validator which can be set up via a configuration file.
35 */
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070036class ValidatorConfig : public Validator
37{
38public:
39 class Error : public Validator::Error
40 {
41 public:
42 explicit
43 Error(const std::string& what)
44 : Validator::Error(what)
45 {
46 }
47 };
48
Yingdi Yu4e9b0692014-11-04 16:13:56 -080049 /**
50 * @note When both certificate cache and face are not supplied, no cache will be used.
51 * However, if only face is supplied, a default cache will be created and used.
52 */
53 explicit
54 ValidatorConfig(Face* face = nullptr,
55 const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
56 const time::milliseconds& graceInterval = DEFAULT_GRACE_INTERVAL,
57 const size_t stepLimit = 10,
58 const size_t maxTrackedKeys = 1000,
59 const time::system_clock::Duration& keyTimestampTtl = DEFAULT_KEY_TIMESTAMP_TTL);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070060
Yingdi Yu4e9b0692014-11-04 16:13:56 -080061 /// @deprecated Use the constructor taking Face* as parameter.
Yingdi Yu96e64062014-04-15 19:57:33 -070062 explicit
63 ValidatorConfig(Face& face,
64 const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
Yingdi Yu0f5fb692014-06-10 12:07:28 -070065 const time::milliseconds& graceInterval = DEFAULT_GRACE_INTERVAL,
66 const size_t stepLimit = 10,
67 const size_t maxTrackedKeys = 1000,
68 const time::system_clock::Duration& keyTimestampTtl = DEFAULT_KEY_TIMESTAMP_TTL);
Yingdi Yu96e64062014-04-15 19:57:33 -070069
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070070 void
71 load(const std::string& filename);
72
73 void
74 load(const std::string& input, const std::string& filename);
75
76 void
77 load(std::istream& input, const std::string& filename);
78
Yingdi Yudfa9d732014-04-09 09:53:01 -070079 void
80 load(const security::conf::ConfigSection& configSection,
81 const std::string& filename);
82
Yingdi Yu4e9b0692014-11-04 16:13:56 -080083 void
Yingdi Yu58f33712014-04-16 16:57:47 -070084 reset();
85
Yingdi Yu4e9b0692014-11-04 16:13:56 -080086 bool
Yingdi Yu58f33712014-04-16 16:57:47 -070087 isEmpty();
88
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070089protected:
Davide Pesavento57c07df2016-12-11 18:41:45 -050090 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070091 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070093 const OnDataValidated& onValidated,
94 const OnDataValidationFailed& onValidationFailed,
Zhiyi Zhang7ba99e12016-11-10 14:26:16 -080095 std::vector<shared_ptr<ValidationRequest>>& nextSteps) override;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070096
Davide Pesavento57c07df2016-12-11 18:41:45 -050097 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070098 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070099 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700100 const OnInterestValidated& onValidated,
101 const OnInterestValidationFailed& onValidationFailed,
Zhiyi Zhang7ba99e12016-11-10 14:26:16 -0800102 std::vector<shared_ptr<ValidationRequest>>& nextSteps) override;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700103
104private:
105 template<class Packet, class OnValidated, class OnFailed>
106 void
107 checkSignature(const Packet& packet,
108 const Signature& signature,
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700109 size_t nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700110 const OnValidated& onValidated,
111 const OnFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700112 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700113
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700114 void
115 checkTimestamp(const shared_ptr<const Interest>& interest,
116 const Name& keyName,
117 const OnInterestValidated& onValidated,
118 const OnInterestValidationFailed& onValidationFailed);
119
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700120 template<class Packet, class OnValidated, class OnFailed>
121 void
122 onCertValidated(const shared_ptr<const Data>& signCertificate,
123 const shared_ptr<const Packet>& packet,
124 const OnValidated& onValidated,
125 const OnFailed& onValidationFailed);
126
127 template<class Packet, class OnFailed>
128 void
129 onCertFailed(const shared_ptr<const Data>& signCertificate,
130 const std::string& failureInfo,
131 const shared_ptr<const Packet>& packet,
132 const OnFailed& onValidationFailed);
133
134 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700135 onConfigRule(const security::conf::ConfigSection& section,
136 const std::string& filename);
137
138 void
139 onConfigTrustAnchor(const security::conf::ConfigSection& section,
140 const std::string& filename);
141
Yingdi Yub4650652014-04-17 10:19:59 -0700142 time::nanoseconds
143 getRefreshPeriod(std::string refreshString);
144
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800145 time::nanoseconds
Yingdi Yub4650652014-04-17 10:19:59 -0700146 getDefaultRefreshPeriod();
147
148 void
149 refreshAnchors();
150
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700151 void
152 cleanOldKeys();
153
Yingdi Yub4650652014-04-17 10:19:59 -0700154 class TrustAnchorContainer
155 {
156 public:
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700157 const std::list<shared_ptr<v1::IdentityCertificate>>&
Yingdi Yub4650652014-04-17 10:19:59 -0700158 getAll() const
159 {
160 return m_certificates;
161 }
162
163 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700164 add(shared_ptr<v1::IdentityCertificate> certificate)
Yingdi Yub4650652014-04-17 10:19:59 -0700165 {
166 m_certificates.push_back(certificate);
167 }
168
169 protected:
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700170 std::list<shared_ptr<v1::IdentityCertificate>> m_certificates;
Yingdi Yub4650652014-04-17 10:19:59 -0700171 };
172
173 class DynamicTrustAnchorContainer : public TrustAnchorContainer
174 {
175 public:
176 DynamicTrustAnchorContainer(const boost::filesystem::path& path, bool isDir,
177 time::nanoseconds refreshPeriod)
178 : m_path(path)
179 , m_isDir(isDir)
180 , m_refreshPeriod(refreshPeriod)
181 {
182 }
183
184 void
185 setLastRefresh(const time::system_clock::TimePoint& lastRefresh)
186 {
187 m_lastRefresh = lastRefresh;
188 }
189
190 const time::system_clock::TimePoint&
191 getLastRefresh() const
192 {
193 return m_lastRefresh;
194 }
195
196 const time::nanoseconds&
197 getRefreshPeriod() const
198 {
199 return m_refreshPeriod;
200 }
201
202 void
203 refresh();
204
205 private:
206 boost::filesystem::path m_path;
207 bool m_isDir;
208
209 time::system_clock::TimePoint m_lastRefresh;
210 time::nanoseconds m_refreshPeriod;
211 };
212
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800213 static inline bool
214 compareDynamicContainer(const DynamicTrustAnchorContainer& containerA,
215 const DynamicTrustAnchorContainer& containerB)
216 {
217 return (containerA.getLastRefresh() < containerB.getLastRefresh());
218 }
219
220public:
221 static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
222 static const time::milliseconds DEFAULT_GRACE_INTERVAL;
223 static const time::system_clock::Duration DEFAULT_KEY_TIMESTAMP_TTL;
224
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700225NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700226 typedef security::conf::Rule<Interest> InterestRule;
227 typedef security::conf::Rule<Data> DataRule;
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700228 typedef std::vector<shared_ptr<InterestRule>> InterestRuleList;
229 typedef std::vector<shared_ptr<DataRule>> DataRuleList;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700230 typedef std::map<Name, shared_ptr<v1::IdentityCertificate>> AnchorList;
Yingdi Yub4650652014-04-17 10:19:59 -0700231 typedef std::list<DynamicTrustAnchorContainer> DynamicContainers; // sorted by m_lastRefresh
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700232 typedef std::list<shared_ptr<v1::IdentityCertificate>> CertificateList;
Yingdi Yub4650652014-04-17 10:19:59 -0700233
Yingdi Yu44d190c2014-04-16 17:05:46 -0700234 /**
235 * @brief gives whether validation should be preformed
236 *
237 * If false, no validation occurs, and any packet is considered validated immediately.
238 */
239 bool m_shouldValidate;
240
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700241 size_t m_stepLimit;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700242 shared_ptr<CertificateCache> m_certificateCache;
243
244 InterestRuleList m_interestRules;
245 DataRuleList m_dataRules;
Yingdi Yub4650652014-04-17 10:19:59 -0700246
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700247 AnchorList m_anchors;
Yingdi Yub4650652014-04-17 10:19:59 -0700248 TrustAnchorContainer m_staticContainer;
249 DynamicContainers m_dynamicContainers;
250
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700251 time::milliseconds m_graceInterval;
252 size_t m_maxTrackedKeys;
253 typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap;
254 LastTimestampMap m_lastTimestamp;
255 const time::system_clock::Duration& m_keyTimestampTtl;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700256};
257
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700258} // namespace security
259
260using security::ValidatorConfig;
261
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700262} // namespace ndn
263
264#endif // NDN_SECURITY_VALIDATOR_CONFIG_HPP