Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_HELPERS_COMMAND_INTEREST_VALIDATOR_HPP |
| 8 | #define NDN_HELPERS_COMMAND_INTEREST_VALIDATOR_HPP |
| 9 | |
| 10 | #include "../security/validator.hpp" |
| 11 | #include "../security/identity-certificate.hpp" |
| 12 | #include "../security/sec-rule-specific.hpp" |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | class CommandInterestValidator : public Validator |
| 17 | { |
| 18 | public: |
| 19 | enum { |
| 20 | POS_SIG_VALUE = -1, |
| 21 | POS_SIG_INFO = -2, |
| 22 | POS_RANDOM_VAL = -3, |
| 23 | POS_TIMESTAMP = -4, |
| 24 | |
| 25 | GRACE_INTERVAL = 3000 // ms |
| 26 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 27 | |
| 28 | CommandInterestValidator(const time::milliseconds& graceInterval = time::milliseconds(static_cast<int>(GRACE_INTERVAL))) |
| 29 | : m_graceInterval(graceInterval < time::milliseconds::zero() ? |
| 30 | time::milliseconds(static_cast<int>(GRACE_INTERVAL)) : graceInterval) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 31 | { |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | virtual |
| 35 | ~CommandInterestValidator() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | addInterestRule(const std::string& regex, const IdentityCertificate& certificate); |
| 41 | |
| 42 | void |
| 43 | addInterestRule(const std::string& regex, const Name& keyName, const PublicKey& publicKey); |
| 44 | |
| 45 | protected: |
| 46 | virtual void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 47 | checkPolicy (const Data& data, |
| 48 | int stepCount, |
| 49 | const OnDataValidated &onValidated, |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 50 | const OnDataValidationFailed &onValidationFailed, |
| 51 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) |
| 52 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 53 | onValidationFailed(data.shared_from_this(), "No policy for data checking"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 54 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 56 | virtual void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 57 | checkPolicy (const Interest& interest, |
| 58 | int stepCount, |
| 59 | const OnInterestValidated &onValidated, |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 60 | const OnInterestValidationFailed &onValidationFailed, |
| 61 | std::vector<shared_ptr<ValidationRequest> > &nextSteps); |
| 62 | private: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | time::milliseconds m_graceInterval; //ms |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 64 | std::map<Name, PublicKey> m_trustAnchorsForInterest; |
| 65 | std::list<SecRuleSpecific> m_trustScopeForInterest; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 66 | |
| 67 | typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap; |
| 68 | LastTimestampMap m_lastTimestamp; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | inline void |
| 72 | CommandInterestValidator::addInterestRule(const std::string& regex, const IdentityCertificate& certificate) |
| 73 | { |
| 74 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
| 75 | addInterestRule(regex, keyName, certificate.getPublicKeyInfo()); |
| 76 | } |
| 77 | |
| 78 | inline void |
| 79 | CommandInterestValidator::addInterestRule(const std::string& regex, const Name& keyName, const PublicKey& publicKey) |
| 80 | { |
| 81 | m_trustAnchorsForInterest[keyName] = publicKey; |
| 82 | shared_ptr<Regex> interestRegex = make_shared<Regex>(regex); |
| 83 | shared_ptr<Regex> signerRegex = Regex::fromName(keyName, true); |
| 84 | m_trustScopeForInterest.push_back(SecRuleSpecific(interestRegex, signerRegex)); |
| 85 | } |
| 86 | |
| 87 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | CommandInterestValidator::checkPolicy (const Interest& interest, |
| 89 | int stepCount, |
| 90 | const OnInterestValidated &onValidated, |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 91 | const OnInterestValidationFailed &onValidationFailed, |
| 92 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) |
| 93 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 94 | const Name& interestName = interest.getName(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 95 | |
| 96 | //Prepare |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 97 | if (interestName.size() < 4) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 98 | return onValidationFailed(interest.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 99 | "Interest is not signed: " + interest.getName().toUri()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 100 | |
| 101 | Signature signature(interestName[POS_SIG_INFO].blockFromValue(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 102 | interestName[POS_SIG_VALUE].blockFromValue()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 103 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 104 | SignatureSha256WithRsa sig(signature); |
| 105 | const Name& keyLocatorName = sig.getKeyLocator().getName(); |
| 106 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 108 | //Check if command is in the trusted scope |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 109 | bool inScope = false; |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 110 | for(std::list<SecRuleSpecific>::iterator scopeIt = m_trustScopeForInterest.begin(); |
| 111 | scopeIt != m_trustScopeForInterest.end(); |
| 112 | ++scopeIt) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 113 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 114 | if(scopeIt->satisfy(interestName, keyName)) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 115 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 116 | inScope = true; |
| 117 | break; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 118 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 119 | } |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 120 | if(inScope == false) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 121 | return onValidationFailed(interest.shared_from_this(), |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 122 | "Signer cannot be authorized for the command: " + keyName.toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 123 | |
| 124 | //Check if timestamp is valid |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 125 | time::system_clock::TimePoint interestTime = time::fromUnixTimestamp( |
| 126 | time::milliseconds(interestName.get(POS_TIMESTAMP).toNumber())); |
| 127 | time::system_clock::TimePoint currentTime = time::system_clock::now(); |
| 128 | |
| 129 | LastTimestampMap::iterator timestampIt = m_lastTimestamp.find(keyName); |
| 130 | if (timestampIt == m_lastTimestamp.end()) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 131 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 132 | if (!(currentTime - m_graceInterval <= interestTime && |
| 133 | interestTime <= currentTime + m_graceInterval)) |
| 134 | { |
| 135 | return onValidationFailed(interest.shared_from_this(), |
| 136 | "The command is not in grace interval: " + interest.getName().toUri()); |
| 137 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 138 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 139 | else |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 140 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 141 | if(interestTime <= timestampIt->second) |
| 142 | return onValidationFailed(interest.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 143 | "The command is outdated: " + interest.getName().toUri()); |
| 144 | } |
| 145 | |
| 146 | //Check signature |
| 147 | if(!Validator::verifySignature(interestName.wireEncode().value(), |
| 148 | interestName.wireEncode().value_size() - interestName[-1].size(), |
| 149 | sig, m_trustAnchorsForInterest[keyName])) |
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 | "Signature cannot be validated: " + interest.getName().toUri()); |
| 152 | |
| 153 | //Update timestamp |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 154 | if (timestampIt == m_lastTimestamp.end()) |
| 155 | { |
| 156 | m_lastTimestamp[keyName] = interestTime; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | timestampIt->second = interestTime; |
| 161 | } |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 162 | |
| 163 | return onValidated(interest.shared_from_this()); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 166 | } // namespace ndn |
| 167 | |
| 168 | #endif // NDN_HELPERS_COMMAND_INTEREST_VALIDATOR_HPP |