blob: 38a8db284001a6a4c4d407ee59944dd06416cedf [file] [log] [blame]
Yingdi Yu6ac97982014-01-30 14:49:21 -08001/* -*- 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 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_VALIDATOR_HPP
10#define NDN_SECURITY_VALIDATOR_HPP
Yingdi Yu6ac97982014-01-30 14:49:21 -080011
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../common.hpp"
13
Yingdi Yu6ac97982014-01-30 14:49:21 -080014#include "../data.hpp"
15#include "../face.hpp"
16#include "public-key.hpp"
17#include "signature-sha256-with-rsa.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080018#include "signature-sha256.hpp"
Yingdi Yu6ac97982014-01-30 14:49:21 -080019#include "validation-request.hpp"
20
21namespace ndn {
22/**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070023 * @brief Validator is one of the main classes of the security library.
Yingdi Yu6ac97982014-01-30 14:49:21 -080024 *
25 * The Validator class provides the interfaces for packet validation.
26 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070027class Validator
28{
Yingdi Yu6ac97982014-01-30 14:49:21 -080029public:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070030 class Error : public std::runtime_error
31 {
32 public:
33 explicit
34 Error(const std::string& what)
35 : std::runtime_error(what)
36 {
37 }
38 };
Yingdi Yu6ac97982014-01-30 14:49:21 -080039
Yingdi Yu96e64062014-04-15 19:57:33 -070040 Validator();
Yingdi Yu6ac97982014-01-30 14:49:21 -080041
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070042 explicit
Yingdi Yu96e64062014-04-15 19:57:33 -070043 Validator(Face& face);
Yingdi Yu6ac97982014-01-30 14:49:21 -080044
45 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070046 * @brief Validate Data and call either onValidated or onValidationFailed.
47 *
Yingdi Yu6ac97982014-01-30 14:49:21 -080048 * @param data The Data with the signature to check.
49 * @param onValidated If the Data is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070050 * @param onValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -080051 */
52 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070053 validate(const Data& data,
54 const OnDataValidated& onValidated,
55 const OnDataValidationFailed& onValidationFailed)
56 {
57 validate(data, onValidated, onValidationFailed, 0);
58 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080059
60 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070061 * @brief Validate Interest and call either onValidated or onValidationFailed.
62 *
Yingdi Yu6ac97982014-01-30 14:49:21 -080063 * @param interest The Interest with the signature to check.
64 * @param onValidated If the Interest is validated, this calls onValidated(interest).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070065 * @param onValidationFailed If validation fails, this calls onValidationFailed(interest).
Yingdi Yu6ac97982014-01-30 14:49:21 -080066 */
67 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070068 validate(const Interest& interest,
69 const OnInterestValidated& onValidated,
70 const OnInterestValidationFailed& onValidationFailed)
71 {
72 validate(interest, onValidated, onValidationFailed, 0);
73 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080074
75 /*****************************************
76 * verifySignature method set *
77 *****************************************/
78
79 /// @brief Verify the data using the publicKey.
80 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070081 verifySignature(const Data& data, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080082
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070083 /**
84 * @brief Verify the signed Interest using the publicKey.
85 *
86 * (Note the signature covers the first n-2 name components).
87 */
Yingdi Yu6ac97982014-01-30 14:49:21 -080088 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070089 verifySignature(const Interest& interest, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080090
91 /// @brief Verify the blob using the publicKey against the signature.
92 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070093 verifySignature(const Buffer& blob, const Signature& sig, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080094
95 /// @brief Verify the data using the publicKey against the SHA256-RSA signature.
96 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070097 verifySignature(const Data& data,
98 const SignatureSha256WithRsa& sig,
99 const PublicKey& publicKey)
100 {
101 return verifySignature(data.wireEncode().value(),
102 data.wireEncode().value_size() - data.getSignature().getValue().size(),
103 sig, publicKey);
104 }
105
106 /** @brief Verify the interest using the publicKey against the SHA256-RSA signature.
107 *
108 * (Note the signature covers the first n-2 name components).
109 */
110 static bool
111 verifySignature(const Interest& interest,
112 const SignatureSha256WithRsa& sig,
113 const PublicKey& publicKey)
114 {
115 if (interest.getName().size() < 2)
116 return false;
117
Yingdi Yu3cca4ab2014-04-11 12:46:53 -0700118 const Name& name = interest.getName();
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700119
Yingdi Yu3cca4ab2014-04-11 12:46:53 -0700120 return verifySignature(name.wireEncode().value(),
121 name.wireEncode().value_size() - name[-1].size(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700122 sig, publicKey);
123 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800124
125 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
126 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700127 verifySignature(const Buffer& blob,
128 const SignatureSha256WithRsa& sig,
129 const PublicKey& publicKey)
130 {
131 return verifySignature(blob.buf(), blob.size(), sig, publicKey);
132 }
133
Yingdi Yu6ac97982014-01-30 14:49:21 -0800134 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
135 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700136 verifySignature(const uint8_t* buf,
137 const size_t size,
138 const SignatureSha256WithRsa& sig,
139 const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800140
Yingdi Yu21157162014-02-28 13:02:34 -0800141
142 /// @brief Verify the data against the SHA256 signature.
143 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700144 verifySignature(const Data& data, const SignatureSha256& sig)
145 {
146 return verifySignature(data.wireEncode().value(),
147 data.wireEncode().value_size() -
148 data.getSignature().getValue().size(),
149 sig);
150 }
151
152 /** @brief Verify the interest against the SHA256 signature.
153 *
154 * (Note the signature covers the first n-2 name components).
155 */
156 static bool
157 verifySignature(const Interest& interest, const SignatureSha256& sig)
158 {
159 if (interest.getName().size() < 2)
160 return false;
161
162 Name signedName = interest.getName().getPrefix(-2);
163
164 return verifySignature(signedName.wireEncode().value(),
165 signedName.wireEncode().value_size(),
166 sig);
167 }
Yingdi Yu21157162014-02-28 13:02:34 -0800168
169 /// @brief Verify the blob against the SHA256 signature.
170 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700171 verifySignature(const Buffer& blob, const SignatureSha256& sig)
172 {
173 return verifySignature (blob.buf(), blob.size(), sig);
174 }
175
Yingdi Yu21157162014-02-28 13:02:34 -0800176 /// @brief Verify the blob against the SHA256 signature.
177 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700178 verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256& sig);
Yingdi Yu21157162014-02-28 13:02:34 -0800179
180
Yingdi Yu6ac97982014-01-30 14:49:21 -0800181protected:
182 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700183 * @brief Check the Data against policy and return the next validation step if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800184 *
185 * If there is no next validation step, that validation MUST have been done.
186 * i.e., either onValidated or onValidationFailed callback is invoked.
187 *
188 * @param data The Data to check.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700189 * @param nSteps The number of validation steps that have been done.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800190 * @param onDataValidated If the Data is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700191 * @param onDataValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -0800192 * @param nextSteps On return, contains the next validation step.
193 */
194 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700195 checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700196 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700197 const OnDataValidated& onValidated,
198 const OnDataValidationFailed& onValidationFailed,
199 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800200
201 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700202 * @brief Check the Interest against validation policy and return the next validation step
203 * if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800204 *
205 * If there is no next validation step, that validation MUST have been done.
206 * i.e., either onValidated or onValidationFailed callback is invoked.
207 *
208 * @param data The Interest to check.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700209 * @param nSteps The number of validation steps that have been done.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800210 * @param OnInterestValidated If the Interest is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700211 * @param OnInterestValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -0800212 * @return the indication of next validation step, null if there is no further step.
213 */
214 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700215 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700216 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700217 const OnInterestValidated& onValidated,
218 const OnInterestValidationFailed& onValidationFailed,
219 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800220
221private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700222 typedef function<void(const std::string&)> OnFailure;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700223
Yingdi Yu6ac97982014-01-30 14:49:21 -0800224 /// @brief Process the received certificate.
225 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700226 onData(const Interest& interest,
227 const Data& data,
228 const shared_ptr<ValidationRequest>& nextStep);
229
Yingdi Yu6ac97982014-01-30 14:49:21 -0800230 /// @brief Re-express the interest if it times out.
231 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700232 onTimeout(const Interest& interest,
233 int retry,
234 const OnFailure& onFailure,
235 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800236
237 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700238 validate(const Data& data,
239 const OnDataValidated& onValidated,
240 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700241 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800242
243 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700244 validate(const Interest& interest,
245 const OnInterestValidated& onValidated,
246 const OnInterestValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700247 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800248
249protected:
Yingdi Yu96e64062014-04-15 19:57:33 -0700250 bool m_hasFace;
251 Face& m_face;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800252};
253
Yingdi Yufc40d872014-02-18 12:56:04 -0800254} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800255
Yingdi Yufc40d872014-02-18 12:56:04 -0800256#endif //NDN_SECURITY_VALIDATOR_HPP