blob: e65d7a8d8d45ad67b9d3bca8b0eb12fb14b374e8 [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).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070049 * @param onValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -080050 */
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).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 * @param onValidationFailed If validation fails, this calls onValidationFailed(interest).
Yingdi Yu6ac97982014-01-30 14:49:21 -080065 */
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.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700188 * @param nSteps The number of validation steps that have been done.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800189 * @param onDataValidated If the Data is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700190 * @param onDataValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -0800191 * @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,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700195 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700196 const OnDataValidated& onValidated,
197 const OnDataValidationFailed& onValidationFailed,
198 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800199
200 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700201 * @brief Check the Interest against validation policy and return the next validation step
202 * if necessary.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800203 *
204 * If there is no next validation step, that validation MUST have been done.
205 * i.e., either onValidated or onValidationFailed callback is invoked.
206 *
207 * @param data The Interest to check.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700208 * @param nSteps The number of validation steps that have been done.
Yingdi Yu6ac97982014-01-30 14:49:21 -0800209 * @param OnInterestValidated If the Interest is validated, this calls onValidated(data).
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700210 * @param OnInterestValidationFailed If validation fails, this calls onValidationFailed(data).
Yingdi Yu6ac97982014-01-30 14:49:21 -0800211 * @return the indication of next validation step, null if there is no further step.
212 */
213 virtual void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700214 checkPolicy(const Interest& interest,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700215 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700216 const OnInterestValidated& onValidated,
217 const OnInterestValidationFailed& onValidationFailed,
218 std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800219
220private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700221 typedef function<void(const std::string&)> OnFailure;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700222
Yingdi Yu6ac97982014-01-30 14:49:21 -0800223 /// @brief Process the received certificate.
224 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700225 onData(const Interest& interest,
226 const Data& data,
227 const shared_ptr<ValidationRequest>& nextStep);
228
Yingdi Yu6ac97982014-01-30 14:49:21 -0800229 /// @brief Re-express the interest if it times out.
230 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700231 onTimeout(const Interest& interest,
232 int retry,
233 const OnFailure& onFailure,
234 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800235
236 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700237 validate(const Data& data,
238 const OnDataValidated& onValidated,
239 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700240 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800241
242 void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700243 validate(const Interest& interest,
244 const OnInterestValidated& onValidated,
245 const OnInterestValidationFailed& onValidationFailed,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700246 int nSteps);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800247
248protected:
249 shared_ptr<Face> m_face;
250};
251
Yingdi Yufc40d872014-02-18 12:56:04 -0800252} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800253
Yingdi Yufc40d872014-02-18 12:56:04 -0800254#endif //NDN_SECURITY_VALIDATOR_HPP