Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -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_COMMAND_INTEREST_VALIDATOR_HPP |
| 8 | #define NDN_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 | |
| 15 | namespace ndn |
| 16 | { |
| 17 | |
| 18 | class CommandInterestValidator : public Validator |
| 19 | { |
| 20 | public: |
| 21 | static const ssize_t POS_SIG_VALUE; |
| 22 | static const ssize_t POS_SIG_INFO; |
| 23 | static const ssize_t POS_RANDOM_VAL; |
| 24 | static const ssize_t POS_TIMESTAMP; |
| 25 | static const int64_t GRACE_INTERVAL; |
| 26 | |
| 27 | CommandInterestValidator(int64_t graceInterval = GRACE_INTERVAL) |
| 28 | { m_graceInterval = (graceInterval < 0 ? GRACE_INTERVAL : graceInterval); } |
| 29 | |
| 30 | virtual |
| 31 | ~CommandInterestValidator() {} |
| 32 | |
| 33 | inline void |
| 34 | addInterestRule(const std::string& regex, const IdentityCertificate& certificate); |
| 35 | |
| 36 | inline void |
| 37 | addInterestRule(const std::string& regex, const Name& keyName, const PublicKey& publicKey); |
| 38 | |
| 39 | protected: |
| 40 | virtual void |
| 41 | checkPolicy (const Data& data, |
| 42 | int stepCount, |
| 43 | const OnDataValidated &onValidated, |
| 44 | const OnDataValidationFailed &onValidationFailed, |
| 45 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) |
| 46 | { onValidationFailed(data.shared_from_this()); } |
| 47 | |
| 48 | virtual void |
| 49 | checkPolicy (const Interest& interest, |
| 50 | int stepCount, |
| 51 | const OnInterestValidated &onValidated, |
| 52 | const OnInterestValidationFailed &onValidationFailed, |
| 53 | std::vector<shared_ptr<ValidationRequest> > &nextSteps); |
| 54 | private: |
| 55 | int64_t m_graceInterval; //ms |
| 56 | std::map<Name, PublicKey> m_trustAnchorsForInterest; |
| 57 | std::list<SecRuleSpecific> m_trustScopeForInterest; |
| 58 | std::map<Name, uint64_t> m_lastTimestamp; |
| 59 | }; |
| 60 | |
| 61 | void |
| 62 | CommandInterestValidator::addInterestRule(const std::string& regex, const IdentityCertificate& certificate) |
| 63 | { |
| 64 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
| 65 | addInterestRule(regex, keyName, certificate.getPublicKeyInfo()); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | CommandInterestValidator::addInterestRule(const std::string& regex, const Name& keyName, const PublicKey& publicKey) |
| 70 | { |
| 71 | m_trustAnchorsForInterest[keyName] = publicKey; |
| 72 | shared_ptr<Regex> interestRegex = make_shared<Regex>(regex); |
| 73 | shared_ptr<Regex> signerRegex = Regex::fromName(keyName, true); |
| 74 | m_trustScopeForInterest.push_back(SecRuleSpecific(interestRegex, signerRegex)); |
| 75 | } |
| 76 | |
| 77 | }//ndn |
| 78 | |
| 79 | #endif |