blob: 32a5ab6b9368c5d8c3e84fe3d701b6fdb8ae0a1e [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 */
27class Validator {
28public:
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070029 class Error : public std::runtime_error
30 {
31 public:
32 explicit
33 Error(const std::string& what)
34 : std::runtime_error(what)
35 {
36 }
37 };
Yingdi Yu6ac97982014-01-30 14:49:21 -080038
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070039 static const shared_ptr<Face> DEFAULT_FACE;
Yingdi Yu6ac97982014-01-30 14:49:21 -080040
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070041 explicit
42 Validator(shared_ptr<Face> face = DEFAULT_FACE);
Yingdi Yu6ac97982014-01-30 14:49:21 -080043
44 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070045 * @brief Validate Data and call either onValidated or onValidationFailed.
46 *
Yingdi Yu6ac97982014-01-30 14:49:21 -080047 * @param data The Data with the signature to check.
48 * @param onValidated If the Data is validated, this calls onValidated(data).
49 * @param onValidationFailed If the Data validation fails, this calls onValidationFailed(data).
50 */
51 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070052 validate(const Data& data,
53 const OnDataValidated& onValidated,
54 const OnDataValidationFailed& onValidationFailed)
55 {
56 validate(data, onValidated, onValidationFailed, 0);
57 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080058
59 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070060 * @brief Validate Interest and call either onValidated or onValidationFailed.
61 *
Yingdi Yu6ac97982014-01-30 14:49:21 -080062 * @param interest The Interest with the signature to check.
63 * @param onValidated If the Interest is validated, this calls onValidated(interest).
64 * @param onValidationFailed If the Interest validation fails, this calls onValidationFailed(interest).
65 */
66 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070067 validate(const Interest& interest,
68 const OnInterestValidated& onValidated,
69 const OnInterestValidationFailed& onValidationFailed)
70 {
71 validate(interest, onValidated, onValidationFailed, 0);
72 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080073
74 /*****************************************
75 * verifySignature method set *
76 *****************************************/
77
78 /// @brief Verify the data using the publicKey.
79 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070080 verifySignature(const Data& data, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080081
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070082 /**
83 * @brief Verify the signed Interest using the publicKey.
84 *
85 * (Note the signature covers the first n-2 name components).
86 */
Yingdi Yu6ac97982014-01-30 14:49:21 -080087 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070088 verifySignature(const Interest& interest, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080089
90 /// @brief Verify the blob using the publicKey against the signature.
91 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070092 verifySignature(const Buffer& blob, const Signature& sig, const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -080093
94 /// @brief Verify the data using the publicKey against the SHA256-RSA signature.
95 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070096 verifySignature(const Data& data,
97 const SignatureSha256WithRsa& sig,
98 const PublicKey& publicKey)
99 {
100 return verifySignature(data.wireEncode().value(),
101 data.wireEncode().value_size() - data.getSignature().getValue().size(),
102 sig, publicKey);
103 }
104
105 /** @brief Verify the interest using the publicKey against the SHA256-RSA signature.
106 *
107 * (Note the signature covers the first n-2 name components).
108 */
109 static bool
110 verifySignature(const Interest& interest,
111 const SignatureSha256WithRsa& sig,
112 const PublicKey& publicKey)
113 {
114 if (interest.getName().size() < 2)
115 return false;
116
Yingdi Yu3cca4ab2014-04-11 12:46:53 -0700117 const Name& name = interest.getName();
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700118
Yingdi Yu3cca4ab2014-04-11 12:46:53 -0700119 return verifySignature(name.wireEncode().value(),
120 name.wireEncode().value_size() - name[-1].size(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700121 sig, publicKey);
122 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800123
124 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
125 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700126 verifySignature(const Buffer& blob,
127 const SignatureSha256WithRsa& sig,
128 const PublicKey& publicKey)
129 {
130 return verifySignature(blob.buf(), blob.size(), sig, publicKey);
131 }
132
Yingdi Yu6ac97982014-01-30 14:49:21 -0800133 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
134 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700135 verifySignature(const uint8_t* buf,
136 const size_t size,
137 const SignatureSha256WithRsa& sig,
138 const PublicKey& publicKey);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800139
Yingdi Yu21157162014-02-28 13:02:34 -0800140
141 /// @brief Verify the data against the SHA256 signature.
142 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700143 verifySignature(const Data& data, const SignatureSha256& sig)
144 {
145 return verifySignature(data.wireEncode().value(),
146 data.wireEncode().value_size() -
147 data.getSignature().getValue().size(),
148 sig);
149 }
150
151 /** @brief Verify the interest against the SHA256 signature.
152 *
153 * (Note the signature covers the first n-2 name components).
154 */
155 static bool
156 verifySignature(const Interest& interest, const SignatureSha256& sig)
157 {
158 if (interest.getName().size() < 2)
159 return false;
160
161 Name signedName = interest.getName().getPrefix(-2);
162
163 return verifySignature(signedName.wireEncode().value(),
164 signedName.wireEncode().value_size(),
165 sig);
166 }
Yingdi Yu21157162014-02-28 13:02:34 -0800167
168 /// @brief Verify the blob against the SHA256 signature.
169 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700170 verifySignature(const Buffer& blob, const SignatureSha256& sig)
171 {
172 return verifySignature (blob.buf(), blob.size(), sig);
173 }
174
Yingdi Yu21157162014-02-28 13:02:34 -0800175 /// @brief Verify the blob against the SHA256 signature.
176 static bool
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700177 verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256& sig);
Yingdi Yu21157162014-02-28 13:02:34 -0800178
179
Yingdi Yu6ac97982014-01-30 14:49:21 -0800180protected:
181 /**
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700182 * @brief Check the Data against policy and return the next validation step if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800183 *
184 * If there is no next validation step, that validation MUST have been done.
185 * i.e., either onValidated or onValidationFailed callback is invoked.
186 *
187 * @param data The Data to check.
188 * @param stepCount The number of validation steps that have been done, used to track the validation progress.
189 * @param onDataValidated If the Data is validated, this calls onValidated(data).
190 * @param onDataValidationFailed If the Data validation fails, this calls onValidationFailed(data).
191 * @param nextSteps On return, contains the next validation step.
192 */
193 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700194 checkPolicy(const Data& data,
195 int stepCount,
196 const OnDataValidated& onValidated,
197 const OnDataValidationFailed& onValidationFailed,
198 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800199
200 /**
201 * @brief Check the Interest against validation policy and return the next validation step if necessary.
202 *
203 * If there is no next validation step, that validation MUST have been done.
204 * i.e., either onValidated or onValidationFailed callback is invoked.
205 *
206 * @param data The Interest to check.
207 * @param stepCount The number of validation steps that have been done, used to track the validation progress.
208 * @param OnInterestValidated If the Interest is validated, this calls onValidated(data).
209 * @param OnInterestValidationFailed If the Interest validation fails, this calls onValidationFailed(data).
210 * @return the indication of next validation step, null if there is no further step.
211 */
212 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700213 checkPolicy(const Interest& interest,
214 int stepCount,
215 const OnInterestValidated& onValidated,
216 const OnInterestValidationFailed& onValidationFailed,
217 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800218
219private:
Yingdi Yu40587c02014-02-21 16:40:48 -0800220 typedef function< void (const std::string&) > OnFailure;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700221
Yingdi Yu6ac97982014-01-30 14:49:21 -0800222 /// @brief Process the received certificate.
223 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700224 onData(const Interest& interest,
225 const Data& data,
226 const shared_ptr<ValidationRequest>& nextStep);
227
Yingdi Yu6ac97982014-01-30 14:49:21 -0800228 /// @brief Re-express the interest if it times out.
229 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700230 onTimeout(const Interest& interest,
231 int retry,
232 const OnFailure& onFailure,
233 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800234
235 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700236 validate(const Data& data,
237 const OnDataValidated& onValidated,
238 const OnDataValidationFailed& onValidationFailed,
239 int stepCount);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800240
241 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700242 validate(const Interest& interest,
243 const OnInterestValidated& onValidated,
244 const OnInterestValidationFailed& onValidationFailed,
245 int stepCount);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800246
247protected:
248 shared_ptr<Face> m_face;
249};
250
Yingdi Yufc40d872014-02-18 12:56:04 -0800251} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800252
Yingdi Yufc40d872014-02-18 12:56:04 -0800253#endif //NDN_SECURITY_VALIDATOR_HPP