Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 2 | /** |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [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 | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 22 | #ifndef NDN_UTIL_COMMAND_INTEREST_VALIDATOR_HPP |
| 23 | #define NDN_UTIL_COMMAND_INTEREST_VALIDATOR_HPP |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 24 | |
| 25 | #include "../security/validator.hpp" |
| 26 | #include "../security/identity-certificate.hpp" |
| 27 | #include "../security/sec-rule-specific.hpp" |
| 28 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | #include <list> |
| 30 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 31 | namespace ndn { |
| 32 | |
Yingdi Yu | 50b1390 | 2014-07-09 15:05:46 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @brief Helper class to validate CommandInterests |
| 35 | * |
| 36 | * @deprecated Use ValidatorConfig instead. |
| 37 | * See http://redmine.named-data.net/projects/ndn-cxx/wiki/CommandValidatorConf |
| 38 | * for more details about the configuration format of ValidatorConfig. |
| 39 | * |
| 40 | * @see http://redmine.named-data.net/projects/nfd/wiki/Command_Interests |
| 41 | */ |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 42 | class CommandInterestValidator : public Validator |
| 43 | { |
| 44 | public: |
| 45 | enum { |
| 46 | POS_SIG_VALUE = -1, |
| 47 | POS_SIG_INFO = -2, |
| 48 | POS_RANDOM_VAL = -3, |
| 49 | POS_TIMESTAMP = -4, |
| 50 | |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 51 | MIN_LENGTH = 4, |
| 52 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 53 | GRACE_INTERVAL = 3000 // ms |
| 54 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 55 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 56 | CommandInterestValidator(const time::milliseconds& graceInterval = |
| 57 | time::milliseconds(static_cast<int>(GRACE_INTERVAL))) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | : m_graceInterval(graceInterval < time::milliseconds::zero() ? |
| 59 | time::milliseconds(static_cast<int>(GRACE_INTERVAL)) : graceInterval) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @brief add an Interest rule that allows a specific certificate |
| 65 | * |
| 66 | * @param regex NDN Regex to match Interest Name |
| 67 | * @param certificate trusted certificate |
| 68 | */ |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 69 | void |
| 70 | addInterestRule(const std::string& regex, const IdentityCertificate& certificate); |
| 71 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 72 | /** |
| 73 | * @brief add an Interest rule that allows a specific public key |
| 74 | * |
| 75 | * @param regex NDN Regex to match Interest Name |
| 76 | * @param keyName KeyLocator.Name |
| 77 | * @param publicKey public key |
| 78 | */ |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 79 | void |
| 80 | addInterestRule(const std::string& regex, const Name& keyName, const PublicKey& publicKey); |
| 81 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @brief add an Interest rule that allows any signer |
| 84 | * |
| 85 | * @param regex NDN Regex to match Interest Name |
| 86 | * @note Command Interest matched by regex that is signed by any key will be accepted. |
| 87 | */ |
| 88 | void |
| 89 | addInterestBypassRule(const std::string& regex); |
| 90 | |
Alexander Afanasyev | 285c2cf | 2014-06-02 19:22:10 +0300 | [diff] [blame] | 91 | /** |
| 92 | * @brief Remove all installed Interest rules (e.g., when reinitialization needed) |
| 93 | */ |
| 94 | void |
| 95 | reset(); |
| 96 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 97 | protected: |
| 98 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 99 | checkPolicy(const Data& data, |
| 100 | int stepCount, |
| 101 | const OnDataValidated& onValidated, |
| 102 | const OnDataValidationFailed& onValidationFailed, |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 103 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) override |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 104 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 105 | onValidationFailed(data.shared_from_this(), "No policy for data checking"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 106 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 108 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 109 | checkPolicy(const Interest& interest, |
| 110 | int stepCount, |
| 111 | const OnInterestValidated& onValidated, |
| 112 | const OnInterestValidationFailed& onValidationFailed, |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 113 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) override; |
| 114 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 115 | private: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 116 | time::milliseconds m_graceInterval; //ms |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 117 | std::map<Name, PublicKey> m_trustAnchorsForInterest; |
| 118 | std::list<SecRuleSpecific> m_trustScopeForInterest; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 119 | |
| 120 | typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap; |
| 121 | LastTimestampMap m_lastTimestamp; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | inline void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 125 | CommandInterestValidator::addInterestRule(const std::string& regex, |
| 126 | const IdentityCertificate& certificate) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 127 | { |
| 128 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
| 129 | addInterestRule(regex, keyName, certificate.getPublicKeyInfo()); |
| 130 | } |
| 131 | |
| 132 | inline void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 133 | CommandInterestValidator::addInterestRule(const std::string& regex, |
| 134 | const Name& keyName, |
| 135 | const PublicKey& publicKey) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 136 | { |
| 137 | m_trustAnchorsForInterest[keyName] = publicKey; |
| 138 | shared_ptr<Regex> interestRegex = make_shared<Regex>(regex); |
| 139 | shared_ptr<Regex> signerRegex = Regex::fromName(keyName, true); |
| 140 | m_trustScopeForInterest.push_back(SecRuleSpecific(interestRegex, signerRegex)); |
| 141 | } |
| 142 | |
| 143 | inline void |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 144 | CommandInterestValidator::addInterestBypassRule(const std::string& regex) |
| 145 | { |
| 146 | shared_ptr<Regex> interestRegex = make_shared<Regex>(regex); |
| 147 | m_trustScopeForInterest.push_back(SecRuleSpecific(interestRegex)); |
| 148 | } |
| 149 | |
| 150 | inline void |
Alexander Afanasyev | 285c2cf | 2014-06-02 19:22:10 +0300 | [diff] [blame] | 151 | CommandInterestValidator::reset() |
| 152 | { |
| 153 | m_trustAnchorsForInterest.clear(); |
| 154 | m_trustScopeForInterest.clear(); |
| 155 | } |
| 156 | |
| 157 | inline void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 158 | CommandInterestValidator::checkPolicy(const Interest& interest, |
| 159 | int stepCount, |
| 160 | const OnInterestValidated& onValidated, |
| 161 | const OnInterestValidationFailed& onValidationFailed, |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 162 | std::vector<shared_ptr<ValidationRequest>>& nextSteps) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 163 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 164 | const Name& interestName = interest.getName(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 165 | |
| 166 | //Prepare |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 167 | if (interestName.size() < MIN_LENGTH) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 168 | return onValidationFailed(interest.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 169 | "Interest is not signed: " + interest.getName().toUri()); |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 170 | Name keyName; |
| 171 | try |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 172 | { |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 173 | Signature signature(interestName[POS_SIG_INFO].blockFromValue(), |
| 174 | interestName[POS_SIG_VALUE].blockFromValue()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 175 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 176 | if (signature.getType() != tlv::SignatureSha256WithRsa) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 177 | return onValidationFailed(interest.shared_from_this(), |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 178 | "Require SignatureSha256WithRsa"); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 179 | |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 180 | SignatureSha256WithRsa sig(signature); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 181 | |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 182 | const KeyLocator& keyLocator = sig.getKeyLocator(); |
| 183 | |
| 184 | if (keyLocator.getType() != KeyLocator::KeyLocator_Name) |
| 185 | return onValidationFailed(interest.shared_from_this(), |
| 186 | "Key Locator is not a name"); |
| 187 | |
| 188 | keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocator.getName()); |
| 189 | |
| 190 | //Check if command is in the trusted scope |
| 191 | bool isInScope = false; |
| 192 | for (std::list<SecRuleSpecific>::iterator scopeIt = m_trustScopeForInterest.begin(); |
| 193 | scopeIt != m_trustScopeForInterest.end(); |
| 194 | ++scopeIt) |
| 195 | { |
| 196 | if (scopeIt->satisfy(interestName, keyName)) |
| 197 | { |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 198 | if (scopeIt->isExempted()) |
| 199 | { |
| 200 | return onValidated(interest.shared_from_this()); |
| 201 | } |
| 202 | |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 203 | isInScope = true; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | if (isInScope == false) |
| 208 | return onValidationFailed(interest.shared_from_this(), |
| 209 | "Signer cannot be authorized for the command: " + |
| 210 | keyName.toUri()); |
| 211 | |
| 212 | //Check signature |
| 213 | if (!Validator::verifySignature(interestName.wireEncode().value(), |
| 214 | interestName.wireEncode().value_size() - |
| 215 | interestName[-1].size(), |
| 216 | sig, m_trustAnchorsForInterest[keyName])) |
| 217 | return onValidationFailed(interest.shared_from_this(), |
| 218 | "Signature cannot be validated: " + |
| 219 | interest.getName().toUri()); |
| 220 | |
| 221 | //Check if timestamp is valid |
| 222 | time::system_clock::TimePoint interestTime = |
| 223 | time::fromUnixTimestamp(time::milliseconds(interestName.get(POS_TIMESTAMP).toNumber())); |
| 224 | |
| 225 | time::system_clock::TimePoint currentTime = time::system_clock::now(); |
| 226 | |
| 227 | LastTimestampMap::iterator timestampIt = m_lastTimestamp.find(keyName); |
| 228 | if (timestampIt == m_lastTimestamp.end()) |
| 229 | { |
| 230 | if (!(currentTime - m_graceInterval <= interestTime && |
| 231 | interestTime <= currentTime + m_graceInterval)) |
| 232 | return onValidationFailed(interest.shared_from_this(), |
| 233 | "The command is not in grace interval: " + |
| 234 | interest.getName().toUri()); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | if (interestTime <= timestampIt->second) |
| 239 | return onValidationFailed(interest.shared_from_this(), |
| 240 | "The command is outdated: " + |
| 241 | interest.getName().toUri()); |
| 242 | } |
| 243 | |
| 244 | //Update timestamp |
| 245 | if (timestampIt == m_lastTimestamp.end()) |
| 246 | { |
| 247 | m_lastTimestamp[keyName] = interestTime; |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | timestampIt->second = interestTime; |
| 252 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 253 | } |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 254 | catch (const Signature::Error&) |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 255 | { |
| 256 | return onValidationFailed(interest.shared_from_this(), |
| 257 | "No valid signature"); |
| 258 | } |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 259 | catch (const IdentityCertificate::Error&) |
Yingdi Yu | 449e114 | 2014-03-28 18:54:42 -0700 | [diff] [blame] | 260 | { |
| 261 | return onValidationFailed(interest.shared_from_this(), |
| 262 | "Cannot locate the signing key"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 263 | } |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 264 | catch (const tlv::Error&) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 265 | { |
| 266 | return onValidationFailed(interest.shared_from_this(), |
| 267 | "Cannot decode signature related TLVs"); |
| 268 | } |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 269 | |
| 270 | return onValidated(interest.shared_from_this()); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 273 | } // namespace ndn |
| 274 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 275 | #endif // NDN_UTIL_COMMAND_INTEREST_VALIDATOR_HPP |