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