blob: db8d17d7b8ba85c5e049072f8907b213e123e825 [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 Zhang044bb7e2016-06-10 00:02:37 -07003 * Copyright (c) 2013-2016 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/>
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070022 * @author Zhiyi Zhang <zhangzhiyi1919@gmail.com>
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070023 */
24
25#ifndef NDN_SECURITY_VALIDATOR_CONFIG_HPP
26#define NDN_SECURITY_VALIDATOR_CONFIG_HPP
27
28#include "validator.hpp"
29#include "certificate-cache.hpp"
30#include "conf/rule.hpp"
31#include "conf/common.hpp"
32
33namespace ndn {
34
35class ValidatorConfig : public Validator
36{
37public:
38 class Error : public Validator::Error
39 {
40 public:
41 explicit
42 Error(const std::string& what)
43 : Validator::Error(what)
44 {
45 }
46 };
47
Yingdi Yu4e9b0692014-11-04 16:13:56 -080048 /**
49 * @note When both certificate cache and face are not supplied, no cache will be used.
50 * However, if only face is supplied, a default cache will be created and used.
51 */
52 explicit
53 ValidatorConfig(Face* face = nullptr,
54 const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
55 const time::milliseconds& graceInterval = DEFAULT_GRACE_INTERVAL,
56 const size_t stepLimit = 10,
57 const size_t maxTrackedKeys = 1000,
58 const time::system_clock::Duration& keyTimestampTtl = DEFAULT_KEY_TIMESTAMP_TTL);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070059
Yingdi Yu4e9b0692014-11-04 16:13:56 -080060 /// @deprecated Use the constructor taking Face* as parameter.
Yingdi Yu96e64062014-04-15 19:57:33 -070061 explicit
62 ValidatorConfig(Face& face,
63 const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
Yingdi Yu0f5fb692014-06-10 12:07:28 -070064 const time::milliseconds& graceInterval = DEFAULT_GRACE_INTERVAL,
65 const size_t stepLimit = 10,
66 const size_t maxTrackedKeys = 1000,
67 const time::system_clock::Duration& keyTimestampTtl = DEFAULT_KEY_TIMESTAMP_TTL);
Yingdi Yu96e64062014-04-15 19:57:33 -070068
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070069 virtual
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070070 ~ValidatorConfig() = default;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070071
72 void
73 load(const std::string& filename);
74
75 void
76 load(const std::string& input, const std::string& filename);
77
78 void
79 load(std::istream& input, const std::string& filename);
80
Yingdi Yudfa9d732014-04-09 09:53:01 -070081 void
82 load(const security::conf::ConfigSection& configSection,
83 const std::string& filename);
84
Yingdi Yu4e9b0692014-11-04 16:13:56 -080085 void
Yingdi Yu58f33712014-04-16 16:57:47 -070086 reset();
87
Yingdi Yu4e9b0692014-11-04 16:13:56 -080088 bool
Yingdi Yu58f33712014-04-16 16:57:47 -070089 isEmpty();
90
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070091protected:
92 virtual void
93 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070095 const OnDataValidated& onValidated,
96 const OnDataValidationFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070097 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070098
99 virtual void
100 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700101 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700102 const OnInterestValidated& onValidated,
103 const OnInterestValidationFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700104 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700105
106private:
107 template<class Packet, class OnValidated, class OnFailed>
108 void
109 checkSignature(const Packet& packet,
110 const Signature& signature,
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700111 size_t nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700112 const OnValidated& onValidated,
113 const OnFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700114 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700115
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700116 void
117 checkTimestamp(const shared_ptr<const Interest>& interest,
118 const Name& keyName,
119 const OnInterestValidated& onValidated,
120 const OnInterestValidationFailed& onValidationFailed);
121
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700122 template<class Packet, class OnValidated, class OnFailed>
123 void
124 onCertValidated(const shared_ptr<const Data>& signCertificate,
125 const shared_ptr<const Packet>& packet,
126 const OnValidated& onValidated,
127 const OnFailed& onValidationFailed);
128
129 template<class Packet, class OnFailed>
130 void
131 onCertFailed(const shared_ptr<const Data>& signCertificate,
132 const std::string& failureInfo,
133 const shared_ptr<const Packet>& packet,
134 const OnFailed& onValidationFailed);
135
136 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700137 onConfigRule(const security::conf::ConfigSection& section,
138 const std::string& filename);
139
140 void
141 onConfigTrustAnchor(const security::conf::ConfigSection& section,
142 const std::string& filename);
143
Yingdi Yub4650652014-04-17 10:19:59 -0700144 time::nanoseconds
145 getRefreshPeriod(std::string refreshString);
146
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800147 time::nanoseconds
Yingdi Yub4650652014-04-17 10:19:59 -0700148 getDefaultRefreshPeriod();
149
150 void
151 refreshAnchors();
152
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700153 void
154 cleanOldKeys();
155
Yingdi Yub4650652014-04-17 10:19:59 -0700156 class TrustAnchorContainer
157 {
158 public:
159 TrustAnchorContainer()
160 {
161 }
162
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700163 const std::list<shared_ptr<IdentityCertificate>>&
Yingdi Yub4650652014-04-17 10:19:59 -0700164 getAll() const
165 {
166 return m_certificates;
167 }
168
169 void
170 add(shared_ptr<IdentityCertificate> certificate)
171 {
172 m_certificates.push_back(certificate);
173 }
174
175 protected:
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700176 std::list<shared_ptr<IdentityCertificate>> m_certificates;
Yingdi Yub4650652014-04-17 10:19:59 -0700177 };
178
179 class DynamicTrustAnchorContainer : public TrustAnchorContainer
180 {
181 public:
182 DynamicTrustAnchorContainer(const boost::filesystem::path& path, bool isDir,
183 time::nanoseconds refreshPeriod)
184 : m_path(path)
185 , m_isDir(isDir)
186 , m_refreshPeriod(refreshPeriod)
187 {
188 }
189
190 void
191 setLastRefresh(const time::system_clock::TimePoint& lastRefresh)
192 {
193 m_lastRefresh = lastRefresh;
194 }
195
196 const time::system_clock::TimePoint&
197 getLastRefresh() const
198 {
199 return m_lastRefresh;
200 }
201
202 const time::nanoseconds&
203 getRefreshPeriod() const
204 {
205 return m_refreshPeriod;
206 }
207
208 void
209 refresh();
210
211 private:
212 boost::filesystem::path m_path;
213 bool m_isDir;
214
215 time::system_clock::TimePoint m_lastRefresh;
216 time::nanoseconds m_refreshPeriod;
217 };
218
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800219 static inline bool
220 compareDynamicContainer(const DynamicTrustAnchorContainer& containerA,
221 const DynamicTrustAnchorContainer& containerB)
222 {
223 return (containerA.getLastRefresh() < containerB.getLastRefresh());
224 }
225
226public:
227 static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
228 static const time::milliseconds DEFAULT_GRACE_INTERVAL;
229 static const time::system_clock::Duration DEFAULT_KEY_TIMESTAMP_TTL;
230
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700231NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700232 typedef security::conf::Rule<Interest> InterestRule;
233 typedef security::conf::Rule<Data> DataRule;
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700234 typedef std::vector<shared_ptr<InterestRule>> InterestRuleList;
235 typedef std::vector<shared_ptr<DataRule>> DataRuleList;
236 typedef std::map<Name, shared_ptr<IdentityCertificate>> AnchorList;
Yingdi Yub4650652014-04-17 10:19:59 -0700237 typedef std::list<DynamicTrustAnchorContainer> DynamicContainers; // sorted by m_lastRefresh
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700238 typedef std::list<shared_ptr<IdentityCertificate>> CertificateList;
Yingdi Yub4650652014-04-17 10:19:59 -0700239
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700240
Yingdi Yu44d190c2014-04-16 17:05:46 -0700241 /**
242 * @brief gives whether validation should be preformed
243 *
244 * If false, no validation occurs, and any packet is considered validated immediately.
245 */
246 bool m_shouldValidate;
247
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700248 size_t m_stepLimit;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700249 shared_ptr<CertificateCache> m_certificateCache;
250
251 InterestRuleList m_interestRules;
252 DataRuleList m_dataRules;
Yingdi Yub4650652014-04-17 10:19:59 -0700253
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700254 AnchorList m_anchors;
Yingdi Yub4650652014-04-17 10:19:59 -0700255 TrustAnchorContainer m_staticContainer;
256 DynamicContainers m_dynamicContainers;
257
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700258 time::milliseconds m_graceInterval;
259 size_t m_maxTrackedKeys;
260 typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap;
261 LastTimestampMap m_lastTimestamp;
262 const time::system_clock::Duration& m_keyTimestampTtl;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700263};
264
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700265} // namespace ndn
266
267#endif // NDN_SECURITY_VALIDATOR_CONFIG_HPP