blob: e1ae5fd43347b1020e094cf652a2ea013ae6a686 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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/>
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070022 */
23
24#ifndef NDN_SECURITY_VALIDATOR_CONFIG_HPP
25#define NDN_SECURITY_VALIDATOR_CONFIG_HPP
26
27#include "validator.hpp"
28#include "certificate-cache.hpp"
29#include "conf/rule.hpp"
30#include "conf/common.hpp"
31
32namespace ndn {
33
34class ValidatorConfig : public Validator
35{
36public:
37 class Error : public Validator::Error
38 {
39 public:
40 explicit
41 Error(const std::string& what)
42 : Validator::Error(what)
43 {
44 }
45 };
46
Yingdi Yu4e9b0692014-11-04 16:13:56 -080047 /**
48 * @note When both certificate cache and face are not supplied, no cache will be used.
49 * However, if only face is supplied, a default cache will be created and used.
50 */
51 explicit
52 ValidatorConfig(Face* face = nullptr,
53 const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
54 const time::milliseconds& graceInterval = DEFAULT_GRACE_INTERVAL,
55 const size_t stepLimit = 10,
56 const size_t maxTrackedKeys = 1000,
57 const time::system_clock::Duration& keyTimestampTtl = DEFAULT_KEY_TIMESTAMP_TTL);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070058
Yingdi Yu4e9b0692014-11-04 16:13:56 -080059 /// @deprecated Use the constructor taking Face* as parameter.
Yingdi Yu96e64062014-04-15 19:57:33 -070060 explicit
61 ValidatorConfig(Face& face,
62 const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
Yingdi Yu0f5fb692014-06-10 12:07:28 -070063 const time::milliseconds& graceInterval = DEFAULT_GRACE_INTERVAL,
64 const size_t stepLimit = 10,
65 const size_t maxTrackedKeys = 1000,
66 const time::system_clock::Duration& keyTimestampTtl = DEFAULT_KEY_TIMESTAMP_TTL);
Yingdi Yu96e64062014-04-15 19:57:33 -070067
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070068 virtual
69 ~ValidatorConfig()
70 {
71 }
72
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,
98 std::vector<shared_ptr<ValidationRequest> >& nextSteps);
99
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,
105 std::vector<shared_ptr<ValidationRequest> >& nextSteps);
106
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,
115 std::vector<shared_ptr<ValidationRequest> >& nextSteps);
116
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
157#ifdef NDN_CXX_HAVE_TESTS
158 size_t
159 getTimestampMapSize()
160 {
161 return m_lastTimestamp.size();
162 }
163#endif
164
Yingdi Yub4650652014-04-17 10:19:59 -0700165 class TrustAnchorContainer
166 {
167 public:
168 TrustAnchorContainer()
169 {
170 }
171
172 const std::list<shared_ptr<IdentityCertificate> >&
173 getAll() const
174 {
175 return m_certificates;
176 }
177
178 void
179 add(shared_ptr<IdentityCertificate> certificate)
180 {
181 m_certificates.push_back(certificate);
182 }
183
184 protected:
185 std::list<shared_ptr<IdentityCertificate> > m_certificates;
186 };
187
188 class DynamicTrustAnchorContainer : public TrustAnchorContainer
189 {
190 public:
191 DynamicTrustAnchorContainer(const boost::filesystem::path& path, bool isDir,
192 time::nanoseconds refreshPeriod)
193 : m_path(path)
194 , m_isDir(isDir)
195 , m_refreshPeriod(refreshPeriod)
196 {
197 }
198
199 void
200 setLastRefresh(const time::system_clock::TimePoint& lastRefresh)
201 {
202 m_lastRefresh = lastRefresh;
203 }
204
205 const time::system_clock::TimePoint&
206 getLastRefresh() const
207 {
208 return m_lastRefresh;
209 }
210
211 const time::nanoseconds&
212 getRefreshPeriod() const
213 {
214 return m_refreshPeriod;
215 }
216
217 void
218 refresh();
219
220 private:
221 boost::filesystem::path m_path;
222 bool m_isDir;
223
224 time::system_clock::TimePoint m_lastRefresh;
225 time::nanoseconds m_refreshPeriod;
226 };
227
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800228 static inline bool
229 compareDynamicContainer(const DynamicTrustAnchorContainer& containerA,
230 const DynamicTrustAnchorContainer& containerB)
231 {
232 return (containerA.getLastRefresh() < containerB.getLastRefresh());
233 }
234
235public:
236 static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
237 static const time::milliseconds DEFAULT_GRACE_INTERVAL;
238 static const time::system_clock::Duration DEFAULT_KEY_TIMESTAMP_TTL;
239
240private:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700241 typedef security::conf::Rule<Interest> InterestRule;
242 typedef security::conf::Rule<Data> DataRule;
243 typedef std::vector<shared_ptr<InterestRule> > InterestRuleList;
244 typedef std::vector<shared_ptr<DataRule> > DataRuleList;
245 typedef std::map<Name, shared_ptr<IdentityCertificate> > AnchorList;
Yingdi Yub4650652014-04-17 10:19:59 -0700246 typedef std::list<DynamicTrustAnchorContainer> DynamicContainers; // sorted by m_lastRefresh
247 typedef std::list<shared_ptr<IdentityCertificate> > CertificateList;
248
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700249
Yingdi Yu44d190c2014-04-16 17:05:46 -0700250 /**
251 * @brief gives whether validation should be preformed
252 *
253 * If false, no validation occurs, and any packet is considered validated immediately.
254 */
255 bool m_shouldValidate;
256
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700257 size_t m_stepLimit;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700258 shared_ptr<CertificateCache> m_certificateCache;
259
260 InterestRuleList m_interestRules;
261 DataRuleList m_dataRules;
Yingdi Yub4650652014-04-17 10:19:59 -0700262
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700263 AnchorList m_anchors;
Yingdi Yub4650652014-04-17 10:19:59 -0700264 TrustAnchorContainer m_staticContainer;
265 DynamicContainers m_dynamicContainers;
266
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700267 time::milliseconds m_graceInterval;
268 size_t m_maxTrackedKeys;
269 typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap;
270 LastTimestampMap m_lastTimestamp;
271 const time::system_clock::Duration& m_keyTimestampTtl;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700272};
273
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700274} // namespace ndn
275
276#endif // NDN_SECURITY_VALIDATOR_CONFIG_HPP