blob: b207bbc05ac3b4a17dea5cc845afef625dc53f88 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu6ac97982014-01-30 14:49:21 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu6ac97982014-01-30 14:49:21 -080022 */
23
Yingdi Yufc40d872014-02-18 12:56:04 -080024#ifndef NDN_SECURITY_VALIDATOR_REGEX_HPP
25#define NDN_SECURITY_VALIDATOR_REGEX_HPP
Yingdi Yu6ac97982014-01-30 14:49:21 -080026
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 Yu6ac97982014-01-30 14:49:21 -080033namespace ndn {
34
35class ValidatorRegex : public Validator
36{
37public:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070038 class Error : public Validator::Error
39 {
40 public:
41 explicit
42 Error(const std::string& what)
43 : Validator::Error(what)
44 {
45 }
46 };
Yingdi Yu6ac97982014-01-30 14:49:21 -080047
Yingdi Yu4e9b0692014-11-04 16:13:56 -080048 /**
49 * @note When both certificate cache and face are not supplied, no cache will be used.
50 * However, if only face is supplied, a default cache will be created and used.
51 */
52 explicit
53 ValidatorRegex(Face* face = nullptr,
54 shared_ptr<CertificateCache> certificateCache = DEFAULT_CERTIFICATE_CACHE,
55 const int stepLimit = 3);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070056
Yingdi Yu4e9b0692014-11-04 16:13:56 -080057 /// @deprecated Use the constructor taking Face* as parameter.
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070058 explicit
Yingdi Yu96e64062014-04-15 19:57:33 -070059 ValidatorRegex(Face& face,
60 shared_ptr<CertificateCache> certificateCache = DEFAULT_CERTIFICATE_CACHE,
61 const int stepLimit = 3);
62
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070063 virtual
64 ~ValidatorRegex()
65 {
66 }
67
Yingdi Yu6ac97982014-01-30 14:49:21 -080068 /**
69 * @brief Add a rule for data verification.
70 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070071 * @param rule The verification rule
Yingdi Yu6ac97982014-01-30 14:49:21 -080072 */
Yingdi Yu4e9b0692014-11-04 16:13:56 -080073 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070074 addDataVerificationRule(shared_ptr<SecRuleRelative> rule);
75
Yingdi Yu6ac97982014-01-30 14:49:21 -080076 /**
77 * @brief Add a trust anchor
78 *
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070079 * @param certificate The trust anchor
Yingdi Yu6ac97982014-01-30 14:49:21 -080080 */
Yingdi Yu4e9b0692014-11-04 16:13:56 -080081 void
Yingdi Yu6ac97982014-01-30 14:49:21 -080082 addTrustAnchor(shared_ptr<IdentityCertificate> certificate);
83
84protected:
85 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070086 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070087 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070088 const OnDataValidated& onValidated,
89 const OnDataValidationFailed& onValidationFailed,
90 std::vector<shared_ptr<ValidationRequest> >& nextSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -080091
Yingdi Yu9a335352014-01-31 11:57:46 -080092 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070093 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070095 const OnInterestValidated& onValidated,
96 const OnInterestValidationFailed& onValidationFailed,
97 std::vector<shared_ptr<ValidationRequest> >& nextSteps)
98 {
99 onValidationFailed(interest.shared_from_this(), "No policy for signed interest checking");
100 }
Yingdi Yu9a335352014-01-31 11:57:46 -0800101
Yingdi Yu6ac97982014-01-30 14:49:21 -0800102 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700103 onCertificateValidated(const shared_ptr<const Data>& signCertificate,
104 const shared_ptr<const Data>& data,
105 const OnDataValidated& onValidated,
106 const OnDataValidationFailed& onValidationFailed);
107
Yingdi Yu6ac97982014-01-30 14:49:21 -0800108 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700109 onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate,
Yingdi Yu40587c02014-02-21 16:40:48 -0800110 const std::string& failureInfo,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700111 const shared_ptr<const Data>& data,
112 const OnDataValidationFailed& onValidationFailed);
113
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800114public:
115 static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
116
Yingdi Yu6ac97982014-01-30 14:49:21 -0800117protected:
118 typedef std::vector< shared_ptr<SecRuleRelative> > RuleList;
119 typedef std::vector< shared_ptr<Regex> > RegexList;
120
121 int m_stepLimit;
122 shared_ptr<CertificateCache> m_certificateCache;
123 RuleList m_mustFailVerify;
124 RuleList m_verifyPolicies;
125 std::map<Name, shared_ptr<IdentityCertificate> > m_trustAnchors;
126};
127
Yingdi Yufc40d872014-02-18 12:56:04 -0800128} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800129
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800130#endif // NDN_SECURITY_VALIDATOR_REGEX_HPP