Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -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 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 8 | #ifndef NDN_SECURITY_VALIDATOR_REGEX_HPP |
| 9 | #define NDN_SECURITY_VALIDATOR_REGEX_HPP |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 10 | |
| 11 | #include "validator.hpp" |
| 12 | #include "identity-certificate.hpp" |
| 13 | #include "sec-rule-relative.hpp" |
| 14 | #include "certificate-cache.hpp" |
| 15 | #include "../util/regex.hpp" |
| 16 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
| 19 | class ValidatorRegex : public Validator |
| 20 | { |
| 21 | public: |
| 22 | struct Error : public Validator::Error { Error(const std::string &what) : Validator::Error(what) {} }; |
| 23 | |
| 24 | static const shared_ptr<CertificateCache> DefaultCertificateCache; |
| 25 | |
| 26 | ValidatorRegex(shared_ptr<Face> face, |
| 27 | shared_ptr<CertificateCache> certificateCache = DefaultCertificateCache, |
| 28 | const int stepLimit = 3); |
| 29 | |
| 30 | virtual |
| 31 | ~ValidatorRegex() {} |
| 32 | |
| 33 | /** |
| 34 | * @brief Add a rule for data verification. |
| 35 | * |
| 36 | * @param policy The verification rule |
| 37 | */ |
| 38 | inline void |
| 39 | addDataVerificationRule (shared_ptr<SecRuleRelative> rule); |
| 40 | |
| 41 | /** |
| 42 | * @brief Add a trust anchor |
| 43 | * |
| 44 | * @param certificate The trust anchor |
| 45 | */ |
| 46 | inline void |
| 47 | addTrustAnchor(shared_ptr<IdentityCertificate> certificate); |
| 48 | |
| 49 | protected: |
| 50 | virtual void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 51 | checkPolicy (const Data& data, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 52 | int stepCount, |
| 53 | const OnDataValidated &onValidated, |
| 54 | const OnDataValidationFailed &onValidationFailed, |
| 55 | std::vector<shared_ptr<ValidationRequest> > &nextSteps); |
| 56 | |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 57 | virtual void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 58 | checkPolicy (const Interest& interest, |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 59 | int stepCount, |
| 60 | const OnInterestValidated &onValidated, |
| 61 | const OnInterestValidationFailed &onValidationFailed, |
| 62 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 63 | { onValidationFailed(interest.shared_from_this(), "No policy for signed interest checking"); } |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 64 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 65 | void |
| 66 | onCertificateValidated(const shared_ptr<const Data> &signCertificate, |
| 67 | const shared_ptr<const Data> &data, |
| 68 | const OnDataValidated &onValidated, |
| 69 | const OnDataValidationFailed &onValidationFailed); |
| 70 | |
| 71 | void |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 72 | onCertificateValidationFailed(const shared_ptr<const Data> &signCertificate, |
| 73 | const std::string& failureInfo, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 74 | const shared_ptr<const Data> &data, |
| 75 | const OnDataValidationFailed &onValidationFailed); |
| 76 | |
| 77 | protected: |
| 78 | typedef std::vector< shared_ptr<SecRuleRelative> > RuleList; |
| 79 | typedef std::vector< shared_ptr<Regex> > RegexList; |
| 80 | |
| 81 | int m_stepLimit; |
| 82 | shared_ptr<CertificateCache> m_certificateCache; |
| 83 | RuleList m_mustFailVerify; |
| 84 | RuleList m_verifyPolicies; |
| 85 | std::map<Name, shared_ptr<IdentityCertificate> > m_trustAnchors; |
| 86 | }; |
| 87 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 88 | inline void |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 89 | ValidatorRegex::addDataVerificationRule (shared_ptr<SecRuleRelative> rule) |
| 90 | { rule->isPositive() ? m_verifyPolicies.push_back(rule) : m_mustFailVerify.push_back(rule); } |
| 91 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 92 | inline void |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 93 | ValidatorRegex::addTrustAnchor(shared_ptr<IdentityCertificate> certificate) |
| 94 | { m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate; } |
| 95 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 96 | } // namespace ndn |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 97 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 98 | #endif //NDN_SECURITY_VALIDATOR_REGEX_HPP |