Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 2 | /** |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 22 | * @author Zhiyi Zhang <zhangzhiyi1919@gmail.com> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 23 | */ |
| 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 | |
| 33 | namespace ndn { |
| 34 | |
| 35 | class ValidatorConfig : public Validator |
| 36 | { |
| 37 | public: |
| 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 Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 48 | /** |
| 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 59 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 60 | /// @deprecated Use the constructor taking Face* as parameter. |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 61 | explicit |
| 62 | ValidatorConfig(Face& face, |
| 63 | const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE, |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 64 | 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 Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 69 | virtual |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 70 | ~ValidatorConfig() = default; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 71 | |
| 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 Yu | dfa9d73 | 2014-04-09 09:53:01 -0700 | [diff] [blame] | 81 | void |
| 82 | load(const security::conf::ConfigSection& configSection, |
| 83 | const std::string& filename); |
| 84 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 85 | void |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 86 | reset(); |
| 87 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 88 | bool |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 89 | isEmpty(); |
| 90 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 91 | protected: |
| 92 | virtual void |
| 93 | checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 94 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 95 | const OnDataValidated& onValidated, |
| 96 | const OnDataValidationFailed& onValidationFailed, |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 97 | std::vector<shared_ptr<ValidationRequest>>& nextSteps); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 98 | |
| 99 | virtual void |
| 100 | checkPolicy(const Interest& interest, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 101 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 102 | const OnInterestValidated& onValidated, |
| 103 | const OnInterestValidationFailed& onValidationFailed, |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 104 | std::vector<shared_ptr<ValidationRequest>>& nextSteps); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | template<class Packet, class OnValidated, class OnFailed> |
| 108 | void |
| 109 | checkSignature(const Packet& packet, |
| 110 | const Signature& signature, |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 111 | size_t nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 112 | const OnValidated& onValidated, |
| 113 | const OnFailed& onValidationFailed, |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 114 | std::vector<shared_ptr<ValidationRequest>>& nextSteps); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 115 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 116 | void |
| 117 | checkTimestamp(const shared_ptr<const Interest>& interest, |
| 118 | const Name& keyName, |
| 119 | const OnInterestValidated& onValidated, |
| 120 | const OnInterestValidationFailed& onValidationFailed); |
| 121 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 122 | 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 137 | 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 Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 144 | time::nanoseconds |
| 145 | getRefreshPeriod(std::string refreshString); |
| 146 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 147 | time::nanoseconds |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 148 | getDefaultRefreshPeriod(); |
| 149 | |
| 150 | void |
| 151 | refreshAnchors(); |
| 152 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 153 | void |
| 154 | cleanOldKeys(); |
| 155 | |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 156 | class TrustAnchorContainer |
| 157 | { |
| 158 | public: |
| 159 | TrustAnchorContainer() |
| 160 | { |
| 161 | } |
| 162 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 163 | const std::list<shared_ptr<IdentityCertificate>>& |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 164 | 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 176 | std::list<shared_ptr<IdentityCertificate>> m_certificates; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 177 | }; |
| 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 Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 219 | static inline bool |
| 220 | compareDynamicContainer(const DynamicTrustAnchorContainer& containerA, |
| 221 | const DynamicTrustAnchorContainer& containerB) |
| 222 | { |
| 223 | return (containerA.getLastRefresh() < containerB.getLastRefresh()); |
| 224 | } |
| 225 | |
| 226 | public: |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 231 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 232 | typedef security::conf::Rule<Interest> InterestRule; |
| 233 | typedef security::conf::Rule<Data> DataRule; |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 234 | 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 Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 237 | typedef std::list<DynamicTrustAnchorContainer> DynamicContainers; // sorted by m_lastRefresh |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame^] | 238 | typedef std::list<shared_ptr<IdentityCertificate>> CertificateList; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 239 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 240 | |
Yingdi Yu | 44d190c | 2014-04-16 17:05:46 -0700 | [diff] [blame] | 241 | /** |
| 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 Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 248 | size_t m_stepLimit; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 249 | shared_ptr<CertificateCache> m_certificateCache; |
| 250 | |
| 251 | InterestRuleList m_interestRules; |
| 252 | DataRuleList m_dataRules; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 253 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 254 | AnchorList m_anchors; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 255 | TrustAnchorContainer m_staticContainer; |
| 256 | DynamicContainers m_dynamicContainers; |
| 257 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 258 | 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 265 | } // namespace ndn |
| 266 | |
| 267 | #endif // NDN_SECURITY_VALIDATOR_CONFIG_HPP |