blob: c6fac7a0748bca442b2a92781cc486978b47cf02 [file] [log] [blame]
Yingdi Yu6ac97982014-01-30 14:49:21 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
13 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Yingdi Yu6ac97982014-01-30 14:49:21 -080014 */
15
Yingdi Yufc40d872014-02-18 12:56:04 -080016#ifndef NDN_SECURITY_VALIDATOR_HPP
17#define NDN_SECURITY_VALIDATOR_HPP
Yingdi Yu6ac97982014-01-30 14:49:21 -080018
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080019#include "../common.hpp"
20
Yingdi Yu6ac97982014-01-30 14:49:21 -080021#include "../data.hpp"
22#include "../face.hpp"
23#include "public-key.hpp"
24#include "signature-sha256-with-rsa.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080025#include "signature-sha256.hpp"
Yingdi Yu6ac97982014-01-30 14:49:21 -080026#include "validation-request.hpp"
27
28namespace ndn {
29/**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070030 * @brief Validator is one of the main classes of the security library.
Yingdi Yu6ac97982014-01-30 14:49:21 -080031 *
32 * The Validator class provides the interfaces for packet validation.
33 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070034class Validator
35{
Yingdi Yu6ac97982014-01-30 14:49:21 -080036public:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070037 class Error : public std::runtime_error
38 {
39 public:
40 explicit
41 Error(const std::string& what)
42 : std::runtime_error(what)
43 {
44 }
45 };
Yingdi Yu6ac97982014-01-30 14:49:21 -080046
Yingdi Yu96e64062014-04-15 19:57:33 -070047 Validator();
Yingdi Yu6ac97982014-01-30 14:49:21 -080048
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070049 explicit
Yingdi Yu96e64062014-04-15 19:57:33 -070050 Validator(Face& face);
Yingdi Yu6ac97982014-01-30 14:49:21 -080051
52 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070053 * @brief Validate Data and call either onValidated or onValidationFailed.
54 *
Yingdi Yu6ac97982014-01-30 14:49:21 -080055 * @param data The Data with the signature to check.
56 * @param onValidated If the Data is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070057 * @param onValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -080058 */
59 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070060 validate(const Data& data,
61 const OnDataValidated& onValidated,
62 const OnDataValidationFailed& onValidationFailed)
63 {
64 validate(data, onValidated, onValidationFailed, 0);
65 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080066
67 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070068 * @brief Validate Interest and call either onValidated or onValidationFailed.
69 *
Yingdi Yu6ac97982014-01-30 14:49:21 -080070 * @param interest The Interest with the signature to check.
71 * @param onValidated If the Interest is validated, this calls onValidated(interest).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070072 * @param onValidationFailed If validation fails, this calls onValidationFailed(interest).
Yingdi Yu6ac97982014-01-30 14:49:21 -080073 */
74 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070075 validate(const Interest& interest,
76 const OnInterestValidated& onValidated,
77 const OnInterestValidationFailed& onValidationFailed)
78 {
79 validate(interest, onValidated, onValidationFailed, 0);
80 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080081
82 /*****************************************
83 * verifySignature method set *
84 *****************************************/
85
86 /// @brief Verify the data using the publicKey.
87 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070088 verifySignature(const Data& data, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080089
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070090 /**
91 * @brief Verify the signed Interest using the publicKey.
92 *
93 * (Note the signature covers the first n-2 name components).
94 */
Yingdi Yu6ac97982014-01-30 14:49:21 -080095 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070096 verifySignature(const Interest& interest, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080097
98 /// @brief Verify the blob using the publicKey against the signature.
99 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700100 verifySignature(const Buffer& blob, const Signature& sig, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800101
102 /// @brief Verify the data using the publicKey against the SHA256-RSA signature.
103 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700104 verifySignature(const Data& data,
105 const SignatureSha256WithRsa& sig,
106 const PublicKey& publicKey)
107 {
108 return verifySignature(data.wireEncode().value(),
109 data.wireEncode().value_size() - data.getSignature().getValue().size(),
110 sig, publicKey);
111 }
112
113 /** @brief Verify the interest using the publicKey against the SHA256-RSA signature.
114 *
115 * (Note the signature covers the first n-2 name components).
116 */
117 static bool
118 verifySignature(const Interest& interest,
119 const SignatureSha256WithRsa& sig,
120 const PublicKey& publicKey)
121 {
122 if (interest.getName().size() < 2)
123 return false;
124
Yingdi Yu3cca4ab2014-04-11 12:46:53 -0700125 const Name& name = interest.getName();
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700126
Yingdi Yu3cca4ab2014-04-11 12:46:53 -0700127 return verifySignature(name.wireEncode().value(),
128 name.wireEncode().value_size() - name[-1].size(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700129 sig, publicKey);
130 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800131
132 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
133 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700134 verifySignature(const Buffer& blob,
135 const SignatureSha256WithRsa& sig,
136 const PublicKey& publicKey)
137 {
138 return verifySignature(blob.buf(), blob.size(), sig, publicKey);
139 }
140
Yingdi Yu6ac97982014-01-30 14:49:21 -0800141 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
142 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700143 verifySignature(const uint8_t* buf,
144 const size_t size,
145 const SignatureSha256WithRsa& sig,
146 const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800147
Yingdi Yu21157162014-02-28 13:02:34 -0800148
149 /// @brief Verify the data against the SHA256 signature.
150 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700151 verifySignature(const Data& data, const SignatureSha256& sig)
152 {
153 return verifySignature(data.wireEncode().value(),
154 data.wireEncode().value_size() -
155 data.getSignature().getValue().size(),
156 sig);
157 }
158
159 /** @brief Verify the interest against the SHA256 signature.
160 *
161 * (Note the signature covers the first n-2 name components).
162 */
163 static bool
164 verifySignature(const Interest& interest, const SignatureSha256& sig)
165 {
166 if (interest.getName().size() < 2)
167 return false;
168
169 Name signedName = interest.getName().getPrefix(-2);
170
171 return verifySignature(signedName.wireEncode().value(),
172 signedName.wireEncode().value_size(),
173 sig);
174 }
Yingdi Yu21157162014-02-28 13:02:34 -0800175
176 /// @brief Verify the blob against the SHA256 signature.
177 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700178 verifySignature(const Buffer& blob, const SignatureSha256& sig)
179 {
180 return verifySignature (blob.buf(), blob.size(), sig);
181 }
182
Yingdi Yu21157162014-02-28 13:02:34 -0800183 /// @brief Verify the blob against the SHA256 signature.
184 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700185 verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256& sig);
Yingdi Yu21157162014-02-28 13:02:34 -0800186
187
Yingdi Yu6ac97982014-01-30 14:49:21 -0800188protected:
189 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700190 * @brief Check the Data against policy and return the next validation step if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800191 *
192 * If there is no next validation step, that validation MUST have been done.
193 * i.e., either onValidated or onValidationFailed callback is invoked.
194 *
195 * @param data The Data to check.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700196 * @param nSteps The number of validation steps that have been done.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800197 * @param onDataValidated If the Data is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700198 * @param onDataValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -0800199 * @param nextSteps On return, contains the next validation step.
200 */
201 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700202 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700203 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700204 const OnDataValidated& onValidated,
205 const OnDataValidationFailed& onValidationFailed,
206 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800207
208 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700209 * @brief Check the Interest against validation policy and return the next validation step
210 * if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800211 *
212 * If there is no next validation step, that validation MUST have been done.
213 * i.e., either onValidated or onValidationFailed callback is invoked.
214 *
215 * @param data The Interest to check.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700216 * @param nSteps The number of validation steps that have been done.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800217 * @param OnInterestValidated If the Interest is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700218 * @param OnInterestValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -0800219 * @return the indication of next validation step, null if there is no further step.
220 */
221 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700222 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700223 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700224 const OnInterestValidated& onValidated,
225 const OnInterestValidationFailed& onValidationFailed,
226 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800227
228private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700229 typedef function<void(const std::string&)> OnFailure;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700230
Yingdi Yu6ac97982014-01-30 14:49:21 -0800231 /// @brief Process the received certificate.
232 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700233 onData(const Interest& interest,
234 const Data& data,
235 const shared_ptr<ValidationRequest>& nextStep);
236
Yingdi Yu6ac97982014-01-30 14:49:21 -0800237 /// @brief Re-express the interest if it times out.
238 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700239 onTimeout(const Interest& interest,
240 int retry,
241 const OnFailure& onFailure,
242 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800243
244 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700245 validate(const Data& data,
246 const OnDataValidated& onValidated,
247 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700248 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800249
250 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700251 validate(const Interest& interest,
252 const OnInterestValidated& onValidated,
253 const OnInterestValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700254 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800255
256protected:
Yingdi Yu96e64062014-04-15 19:57:33 -0700257 bool m_hasFace;
258 Face& m_face;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800259};
260
Yingdi Yufc40d872014-02-18 12:56:04 -0800261} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800262
Yingdi Yufc40d872014-02-18 12:56:04 -0800263#endif //NDN_SECURITY_VALIDATOR_HPP