Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 24 | #ifndef NDN_SECURITY_VALIDATOR_REGEX_HPP |
| 25 | #define NDN_SECURITY_VALIDATOR_REGEX_HPP |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 26 | |
| 27 | #include "validator.hpp" |
| 28 | #include "identity-certificate.hpp" |
| 29 | #include "sec-rule-relative.hpp" |
| 30 | #include "certificate-cache.hpp" |
| 31 | #include "../util/regex.hpp" |
| 32 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 33 | namespace ndn { |
| 34 | |
| 35 | class ValidatorRegex : public Validator |
| 36 | { |
| 37 | public: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 38 | class Error : public Validator::Error |
| 39 | { |
| 40 | public: |
| 41 | explicit |
| 42 | Error(const std::string& what) |
| 43 | : Validator::Error(what) |
| 44 | { |
| 45 | } |
| 46 | }; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 47 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 48 | static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE; |
| 49 | |
Alexander Afanasyev | a4297a6 | 2014-06-19 13:29:34 -0700 | [diff] [blame] | 50 | explicit |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 51 | ValidatorRegex(Face& face, |
| 52 | shared_ptr<CertificateCache> certificateCache = DEFAULT_CERTIFICATE_CACHE, |
| 53 | const int stepLimit = 3); |
| 54 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 55 | virtual |
| 56 | ~ValidatorRegex() |
| 57 | { |
| 58 | } |
| 59 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 60 | /** |
| 61 | * @brief Add a rule for data verification. |
| 62 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 63 | * @param rule The verification rule |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 64 | */ |
| 65 | inline void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 66 | addDataVerificationRule(shared_ptr<SecRuleRelative> rule); |
| 67 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 68 | /** |
| 69 | * @brief Add a trust anchor |
| 70 | * |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 71 | * @param certificate The trust anchor |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 72 | */ |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 73 | inline void |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 74 | addTrustAnchor(shared_ptr<IdentityCertificate> certificate); |
| 75 | |
| 76 | protected: |
| 77 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 78 | checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 79 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 80 | const OnDataValidated& onValidated, |
| 81 | const OnDataValidationFailed& onValidationFailed, |
| 82 | std::vector<shared_ptr<ValidationRequest> >& nextSteps); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 83 | |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 84 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 85 | checkPolicy(const Interest& interest, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 86 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 87 | const OnInterestValidated& onValidated, |
| 88 | const OnInterestValidationFailed& onValidationFailed, |
| 89 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) |
| 90 | { |
| 91 | onValidationFailed(interest.shared_from_this(), "No policy for signed interest checking"); |
| 92 | } |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 93 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 94 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 95 | onCertificateValidated(const shared_ptr<const Data>& signCertificate, |
| 96 | const shared_ptr<const Data>& data, |
| 97 | const OnDataValidated& onValidated, |
| 98 | const OnDataValidationFailed& onValidationFailed); |
| 99 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 100 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 101 | onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate, |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 102 | const std::string& failureInfo, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 103 | const shared_ptr<const Data>& data, |
| 104 | const OnDataValidationFailed& onValidationFailed); |
| 105 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 106 | protected: |
| 107 | typedef std::vector< shared_ptr<SecRuleRelative> > RuleList; |
| 108 | typedef std::vector< shared_ptr<Regex> > RegexList; |
| 109 | |
| 110 | int m_stepLimit; |
| 111 | shared_ptr<CertificateCache> m_certificateCache; |
| 112 | RuleList m_mustFailVerify; |
| 113 | RuleList m_verifyPolicies; |
| 114 | std::map<Name, shared_ptr<IdentityCertificate> > m_trustAnchors; |
| 115 | }; |
| 116 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 117 | inline void |
| 118 | ValidatorRegex::addDataVerificationRule(shared_ptr<SecRuleRelative> rule) |
| 119 | { |
| 120 | rule->isPositive() ? m_verifyPolicies.push_back(rule) : m_mustFailVerify.push_back(rule); |
| 121 | } |
| 122 | |
| 123 | inline void |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 124 | ValidatorRegex::addTrustAnchor(shared_ptr<IdentityCertificate> certificate) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 125 | { |
| 126 | m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate; |
| 127 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 128 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 129 | } // namespace ndn |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 130 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 131 | #endif //NDN_SECURITY_VALIDATOR_REGEX_HPP |