blob: 88f1d6367a348461e84eec9f4f6391633fab6534 [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
Yingdi Yu6ac97982014-01-30 14:49:21 -0800187protected:
188 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700189 * @brief Check the Data against policy and return the next validation step if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800190 *
191 * If there is no next validation step, that validation MUST have been done.
192 * i.e., either onValidated or onValidationFailed callback is invoked.
193 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700194 * @param data The Data to check.
195 * @param nSteps The number of validation steps that have been done.
196 * @param onValidated If the Data is validated, this calls onValidated(data)
197 * @param onValidationFailed If validation fails, this calls onValidationFailed(data)
198 * @param nextSteps On return, contains the next validation step
Yingdi Yu6ac97982014-01-30 14:49:21 -0800199 */
200 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700201 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700202 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700203 const OnDataValidated& onValidated,
204 const OnDataValidationFailed& onValidationFailed,
205 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800206
207 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700208 * @brief Check the Interest against validation policy and return the next validation step
209 * if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800210 *
211 * If there is no next validation step, that validation MUST have been done.
212 * i.e., either onValidated or onValidationFailed callback is invoked.
213 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700214 * @param interest The Interest to check.
215 * @param nSteps The number of validation steps that have been done.
216 * @param onValidated If the Interest is validated, this calls onValidated(data)
217 * @param onValidationFailed If validation fails, this calls onValidationFailed(data)
218 * @param nextSteps On return, contains the next validation step
Yingdi Yu6ac97982014-01-30 14:49:21 -0800219 */
220 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700221 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700222 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700223 const OnInterestValidated& onValidated,
224 const OnInterestValidationFailed& onValidationFailed,
225 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800226
227private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700228 typedef function<void(const std::string&)> OnFailure;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700229
Yingdi Yu6ac97982014-01-30 14:49:21 -0800230 /// @brief Process the received certificate.
231 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700232 onData(const Interest& interest,
233 const Data& data,
234 const shared_ptr<ValidationRequest>& nextStep);
235
Yingdi Yu6ac97982014-01-30 14:49:21 -0800236 /// @brief Re-express the interest if it times out.
237 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700238 onTimeout(const Interest& interest,
239 int retry,
240 const OnFailure& onFailure,
241 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800242
243 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700244 validate(const Data& data,
245 const OnDataValidated& onValidated,
246 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700247 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800248
249 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700250 validate(const Interest& interest,
251 const OnInterestValidated& onValidated,
252 const OnInterestValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700253 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800254
255protected:
Yingdi Yu96e64062014-04-15 19:57:33 -0700256 bool m_hasFace;
257 Face& m_face;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800258};
259
Yingdi Yufc40d872014-02-18 12:56:04 -0800260} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800261
Yingdi Yufc40d872014-02-18 12:56:04 -0800262#endif //NDN_SECURITY_VALIDATOR_HPP