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