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 | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 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. |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 20 | */ |
| 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 | |
| 30 | namespace ndn { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 31 | namespace security { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 32 | |
Zhiyi Zhang | 7ba99e1 | 2016-11-10 14:26:16 -0800 | [diff] [blame] | 33 | /** |
| 34 | * @brief The validator which can be set up via a configuration file. |
| 35 | */ |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 36 | class ValidatorConfig : public Validator |
| 37 | { |
| 38 | public: |
| 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 Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 49 | /** |
| 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 61 | /// @deprecated Use the constructor taking Face* as parameter. |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 62 | explicit |
| 63 | ValidatorConfig(Face& face, |
| 64 | const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE, |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 65 | 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 Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 70 | 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 Yu | dfa9d73 | 2014-04-09 09:53:01 -0700 | [diff] [blame] | 79 | void |
| 80 | load(const security::conf::ConfigSection& configSection, |
| 81 | const std::string& filename); |
| 82 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 83 | void |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 84 | reset(); |
| 85 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 86 | bool |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 87 | isEmpty(); |
| 88 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 89 | protected: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 90 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 91 | checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 92 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 93 | const OnDataValidated& onValidated, |
| 94 | const OnDataValidationFailed& onValidationFailed, |
Zhiyi Zhang | 7ba99e1 | 2016-11-10 14:26:16 -0800 | [diff] [blame] | 95 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) override; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 96 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 97 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 98 | checkPolicy(const Interest& interest, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 99 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 100 | const OnInterestValidated& onValidated, |
| 101 | const OnInterestValidationFailed& onValidationFailed, |
Zhiyi Zhang | 7ba99e1 | 2016-11-10 14:26:16 -0800 | [diff] [blame] | 102 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) override; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
| 105 | template<class Packet, class OnValidated, class OnFailed> |
| 106 | void |
| 107 | checkSignature(const Packet& packet, |
| 108 | const Signature& signature, |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 109 | size_t nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 110 | const OnValidated& onValidated, |
| 111 | const OnFailed& onValidationFailed, |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 112 | std::vector<shared_ptr<ValidationRequest>>& nextSteps); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 114 | void |
| 115 | checkTimestamp(const shared_ptr<const Interest>& interest, |
| 116 | const Name& keyName, |
| 117 | const OnInterestValidated& onValidated, |
| 118 | const OnInterestValidationFailed& onValidationFailed); |
| 119 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 120 | 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 135 | 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 Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 142 | time::nanoseconds |
| 143 | getRefreshPeriod(std::string refreshString); |
| 144 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 145 | time::nanoseconds |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 146 | getDefaultRefreshPeriod(); |
| 147 | |
| 148 | void |
| 149 | refreshAnchors(); |
| 150 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 151 | void |
| 152 | cleanOldKeys(); |
| 153 | |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 154 | class TrustAnchorContainer |
| 155 | { |
| 156 | public: |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 157 | const std::list<shared_ptr<v1::IdentityCertificate>>& |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 158 | getAll() const |
| 159 | { |
| 160 | return m_certificates; |
| 161 | } |
| 162 | |
| 163 | void |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 164 | add(shared_ptr<v1::IdentityCertificate> certificate) |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 165 | { |
| 166 | m_certificates.push_back(certificate); |
| 167 | } |
| 168 | |
| 169 | protected: |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 170 | std::list<shared_ptr<v1::IdentityCertificate>> m_certificates; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 171 | }; |
| 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 Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 213 | static inline bool |
| 214 | compareDynamicContainer(const DynamicTrustAnchorContainer& containerA, |
| 215 | const DynamicTrustAnchorContainer& containerB) |
| 216 | { |
| 217 | return (containerA.getLastRefresh() < containerB.getLastRefresh()); |
| 218 | } |
| 219 | |
| 220 | public: |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 225 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 226 | typedef security::conf::Rule<Interest> InterestRule; |
| 227 | typedef security::conf::Rule<Data> DataRule; |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 228 | typedef std::vector<shared_ptr<InterestRule>> InterestRuleList; |
| 229 | typedef std::vector<shared_ptr<DataRule>> DataRuleList; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 230 | typedef std::map<Name, shared_ptr<v1::IdentityCertificate>> AnchorList; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 231 | typedef std::list<DynamicTrustAnchorContainer> DynamicContainers; // sorted by m_lastRefresh |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 232 | typedef std::list<shared_ptr<v1::IdentityCertificate>> CertificateList; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 233 | |
Yingdi Yu | 44d190c | 2014-04-16 17:05:46 -0700 | [diff] [blame] | 234 | /** |
| 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 Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 241 | size_t m_stepLimit; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 242 | shared_ptr<CertificateCache> m_certificateCache; |
| 243 | |
| 244 | InterestRuleList m_interestRules; |
| 245 | DataRuleList m_dataRules; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 246 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 247 | AnchorList m_anchors; |
Yingdi Yu | b465065 | 2014-04-17 10:19:59 -0700 | [diff] [blame] | 248 | TrustAnchorContainer m_staticContainer; |
| 249 | DynamicContainers m_dynamicContainers; |
| 250 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame] | 251 | 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 258 | } // namespace security |
| 259 | |
| 260 | using security::ValidatorConfig; |
| 261 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 262 | } // namespace ndn |
| 263 | |
| 264 | #endif // NDN_SECURITY_VALIDATOR_CONFIG_HPP |