blob: 8d801adee82bda7be4f7a665c83b09f07f0c8877 [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 {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070034namespace security {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070035
36class 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 virtual
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070071 ~ValidatorConfig() = default;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070072
73 void
74 load(const std::string& filename);
75
76 void
77 load(const std::string& input, const std::string& filename);
78
79 void
80 load(std::istream& input, const std::string& filename);
81
Yingdi Yudfa9d732014-04-09 09:53:01 -070082 void
83 load(const security::conf::ConfigSection& configSection,
84 const std::string& filename);
85
Yingdi Yu4e9b0692014-11-04 16:13:56 -080086 void
Yingdi Yu58f33712014-04-16 16:57:47 -070087 reset();
88
Yingdi Yu4e9b0692014-11-04 16:13:56 -080089 bool
Yingdi Yu58f33712014-04-16 16:57:47 -070090 isEmpty();
91
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070092protected:
93 virtual void
94 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070095 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070096 const OnDataValidated& onValidated,
97 const OnDataValidationFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070098 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070099
100 virtual void
101 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700102 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700103 const OnInterestValidated& onValidated,
104 const OnInterestValidationFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700105 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700106
107private:
108 template<class Packet, class OnValidated, class OnFailed>
109 void
110 checkSignature(const Packet& packet,
111 const Signature& signature,
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700112 size_t nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700113 const OnValidated& onValidated,
114 const OnFailed& onValidationFailed,
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700115 std::vector<shared_ptr<ValidationRequest>>& nextSteps);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700116
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700117 void
118 checkTimestamp(const shared_ptr<const Interest>& interest,
119 const Name& keyName,
120 const OnInterestValidated& onValidated,
121 const OnInterestValidationFailed& onValidationFailed);
122
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700123 template<class Packet, class OnValidated, class OnFailed>
124 void
125 onCertValidated(const shared_ptr<const Data>& signCertificate,
126 const shared_ptr<const Packet>& packet,
127 const OnValidated& onValidated,
128 const OnFailed& onValidationFailed);
129
130 template<class Packet, class OnFailed>
131 void
132 onCertFailed(const shared_ptr<const Data>& signCertificate,
133 const std::string& failureInfo,
134 const shared_ptr<const Packet>& packet,
135 const OnFailed& onValidationFailed);
136
137 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700138 onConfigRule(const security::conf::ConfigSection& section,
139 const std::string& filename);
140
141 void
142 onConfigTrustAnchor(const security::conf::ConfigSection& section,
143 const std::string& filename);
144
Yingdi Yub4650652014-04-17 10:19:59 -0700145 time::nanoseconds
146 getRefreshPeriod(std::string refreshString);
147
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800148 time::nanoseconds
Yingdi Yub4650652014-04-17 10:19:59 -0700149 getDefaultRefreshPeriod();
150
151 void
152 refreshAnchors();
153
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700154 void
155 cleanOldKeys();
156
Yingdi Yub4650652014-04-17 10:19:59 -0700157 class TrustAnchorContainer
158 {
159 public:
160 TrustAnchorContainer()
161 {
162 }
163
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700164 const std::list<shared_ptr<v1::IdentityCertificate>>&
Yingdi Yub4650652014-04-17 10:19:59 -0700165 getAll() const
166 {
167 return m_certificates;
168 }
169
170 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700171 add(shared_ptr<v1::IdentityCertificate> certificate)
Yingdi Yub4650652014-04-17 10:19:59 -0700172 {
173 m_certificates.push_back(certificate);
174 }
175
176 protected:
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700177 std::list<shared_ptr<v1::IdentityCertificate>> m_certificates;
Yingdi Yub4650652014-04-17 10:19:59 -0700178 };
179
180 class DynamicTrustAnchorContainer : public TrustAnchorContainer
181 {
182 public:
183 DynamicTrustAnchorContainer(const boost::filesystem::path& path, bool isDir,
184 time::nanoseconds refreshPeriod)
185 : m_path(path)
186 , m_isDir(isDir)
187 , m_refreshPeriod(refreshPeriod)
188 {
189 }
190
191 void
192 setLastRefresh(const time::system_clock::TimePoint& lastRefresh)
193 {
194 m_lastRefresh = lastRefresh;
195 }
196
197 const time::system_clock::TimePoint&
198 getLastRefresh() const
199 {
200 return m_lastRefresh;
201 }
202
203 const time::nanoseconds&
204 getRefreshPeriod() const
205 {
206 return m_refreshPeriod;
207 }
208
209 void
210 refresh();
211
212 private:
213 boost::filesystem::path m_path;
214 bool m_isDir;
215
216 time::system_clock::TimePoint m_lastRefresh;
217 time::nanoseconds m_refreshPeriod;
218 };
219
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800220 static inline bool
221 compareDynamicContainer(const DynamicTrustAnchorContainer& containerA,
222 const DynamicTrustAnchorContainer& containerB)
223 {
224 return (containerA.getLastRefresh() < containerB.getLastRefresh());
225 }
226
227public:
228 static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
229 static const time::milliseconds DEFAULT_GRACE_INTERVAL;
230 static const time::system_clock::Duration DEFAULT_KEY_TIMESTAMP_TTL;
231
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700232NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700233 typedef security::conf::Rule<Interest> InterestRule;
234 typedef security::conf::Rule<Data> DataRule;
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700235 typedef std::vector<shared_ptr<InterestRule>> InterestRuleList;
236 typedef std::vector<shared_ptr<DataRule>> DataRuleList;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700237 typedef std::map<Name, shared_ptr<v1::IdentityCertificate>> AnchorList;
Yingdi Yub4650652014-04-17 10:19:59 -0700238 typedef std::list<DynamicTrustAnchorContainer> DynamicContainers; // sorted by m_lastRefresh
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700239 typedef std::list<shared_ptr<v1::IdentityCertificate>> CertificateList;
Yingdi Yub4650652014-04-17 10:19:59 -0700240
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700241
Yingdi Yu44d190c2014-04-16 17:05:46 -0700242 /**
243 * @brief gives whether validation should be preformed
244 *
245 * If false, no validation occurs, and any packet is considered validated immediately.
246 */
247 bool m_shouldValidate;
248
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700249 size_t m_stepLimit;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700250 shared_ptr<CertificateCache> m_certificateCache;
251
252 InterestRuleList m_interestRules;
253 DataRuleList m_dataRules;
Yingdi Yub4650652014-04-17 10:19:59 -0700254
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700255 AnchorList m_anchors;
Yingdi Yub4650652014-04-17 10:19:59 -0700256 TrustAnchorContainer m_staticContainer;
257 DynamicContainers m_dynamicContainers;
258
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700259 time::milliseconds m_graceInterval;
260 size_t m_maxTrackedKeys;
261 typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap;
262 LastTimestampMap m_lastTimestamp;
263 const time::system_clock::Duration& m_keyTimestampTtl;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700264};
265
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700266} // namespace security
267
268using security::ValidatorConfig;
269
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700270} // namespace ndn
271
272#endif // NDN_SECURITY_VALIDATOR_CONFIG_HPP