blob: 4aae1073a8f6687783894a241b2006c65f8836c1 [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"
18#include "validation-request.hpp"
19
20namespace ndn {
21/**
22 * Validator is one of the main classes of the security library.
23 *
24 * The Validator class provides the interfaces for packet validation.
25 */
26class Validator {
27public:
28 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
29
30 static const shared_ptr<Face> DefaultFace;
31
32 Validator (shared_ptr<Face> face = DefaultFace);
33
34 /**
35 * @brief Validate Data and call either onValidated or onValidationFailed.
36 *
37 * @param data The Data with the signature to check.
38 * @param onValidated If the Data is validated, this calls onValidated(data).
39 * @param onValidationFailed If the Data validation fails, this calls onValidationFailed(data).
40 */
41 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080042 validate (const Data& data, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed)
Yingdi Yu6ac97982014-01-30 14:49:21 -080043 { validate (data, onValidated, onValidationFailed, 0); }
44
45 /**
46 * @brief Validate Interest and call either onValidated or onValidationFailed.
47 *
48 * @param interest The Interest with the signature to check.
49 * @param onValidated If the Interest is validated, this calls onValidated(interest).
50 * @param onValidationFailed If the Interest validation fails, this calls onValidationFailed(interest).
51 */
52 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080053 validate (const Interest& interest, const OnInterestValidated &onValidated, const OnInterestValidationFailed &onValidationFailed)
Yingdi Yu6ac97982014-01-30 14:49:21 -080054 { validate (interest, onValidated, onValidationFailed, 0); }
55
56 /*****************************************
57 * verifySignature method set *
58 *****************************************/
59
60 /// @brief Verify the data using the publicKey.
61 static bool
62 verifySignature (const Data &data, const PublicKey &publicKey);
63
64 /// @brief Verify the signed Interest using the publicKey.
65 static bool
66 verifySignature (const Interest &interest, const PublicKey &publicKey);
67
68 /// @brief Verify the blob using the publicKey against the signature.
69 static bool
70 verifySignature (const Buffer &blob, const Signature &sig, const PublicKey &publicKey);
71
72 /// @brief Verify the data using the publicKey against the SHA256-RSA signature.
73 static bool
Yingdi Yu40587c02014-02-21 16:40:48 -080074 verifySignature (const Data& data, const SignatureSha256WithRsa& sig, const PublicKey& publicKey)
75 { return verifySignature (data.wireEncode().value(),
76 data.wireEncode().value_size() - data.getSignature().getValue().size(),
77 sig, publicKey); }
Yingdi Yu6ac97982014-01-30 14:49:21 -080078
79 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
80 static bool
Yingdi Yu40587c02014-02-21 16:40:48 -080081 verifySignature (const Buffer &blob, const SignatureSha256WithRsa &sig, const PublicKey &publicKey)
82 { return verifySignature (blob.buf(), blob.size(), sig, publicKey); }
Yingdi Yu6ac97982014-01-30 14:49:21 -080083
84 /// @brief Verify the blob using the publicKey against the SHA256-RSA signature.
85 static bool
86 verifySignature (const uint8_t* buf, const size_t size, const SignatureSha256WithRsa &sig, const PublicKey &publicKey);
87
88protected:
89 /**
90 * @brief Check the Data against validation policy and return the next validation step if necessary.
91 *
92 * If there is no next validation step, that validation MUST have been done.
93 * i.e., either onValidated or onValidationFailed callback is invoked.
94 *
95 * @param data The Data to check.
96 * @param stepCount The number of validation steps that have been done, used to track the validation progress.
97 * @param onDataValidated If the Data is validated, this calls onValidated(data).
98 * @param onDataValidationFailed If the Data validation fails, this calls onValidationFailed(data).
99 * @param nextSteps On return, contains the next validation step.
100 */
101 virtual void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800102 checkPolicy (const Data& data,
Yingdi Yu6ac97982014-01-30 14:49:21 -0800103 int stepCount,
104 const OnDataValidated &onValidated,
105 const OnDataValidationFailed &onValidationFailed,
Yingdi Yu9a335352014-01-31 11:57:46 -0800106 std::vector<shared_ptr<ValidationRequest> > &nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800107
108 /**
109 * @brief Check the Interest against validation policy and return the next validation step if necessary.
110 *
111 * If there is no next validation step, that validation MUST have been done.
112 * i.e., either onValidated or onValidationFailed callback is invoked.
113 *
114 * @param data The Interest to check.
115 * @param stepCount The number of validation steps that have been done, used to track the validation progress.
116 * @param OnInterestValidated If the Interest is validated, this calls onValidated(data).
117 * @param OnInterestValidationFailed If the Interest validation fails, this calls onValidationFailed(data).
118 * @return the indication of next validation step, null if there is no further step.
119 */
120 virtual void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800121 checkPolicy (const Interest& interest,
Yingdi Yu6ac97982014-01-30 14:49:21 -0800122 int stepCount,
123 const OnInterestValidated &onValidated,
124 const OnInterestValidationFailed &onValidationFailed,
Yingdi Yu9a335352014-01-31 11:57:46 -0800125 std::vector<shared_ptr<ValidationRequest> > &nextSteps) = 0;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800126
127private:
Yingdi Yu40587c02014-02-21 16:40:48 -0800128 typedef function< void (const std::string&) > OnFailure;
Yingdi Yu6ac97982014-01-30 14:49:21 -0800129
130 /// @brief Process the received certificate.
131 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800132 onData (const Interest& interest,
133 Data& data,
134 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800135
136 /// @brief Re-express the interest if it times out.
137 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800138 onTimeout (const Interest& interest,
Yingdi Yu6ac97982014-01-30 14:49:21 -0800139 int retry,
140 const OnFailure &onFailure,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800141 const shared_ptr<ValidationRequest>& nextStep);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800142
143 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800144 validate (const Data& data,
Yingdi Yu6ac97982014-01-30 14:49:21 -0800145 const OnDataValidated &onValidated,
146 const OnDataValidationFailed &onValidationFailed,
147 int stepCount);
148
149 void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800150 validate (const Interest& interest,
Yingdi Yu6ac97982014-01-30 14:49:21 -0800151 const OnInterestValidated &onValidated,
152 const OnInterestValidationFailed &onValidationFailed,
153 int stepCount);
154
155protected:
156 shared_ptr<Face> m_face;
157};
158
Yingdi Yufc40d872014-02-18 12:56:04 -0800159} // namespace ndn
Yingdi Yu6ac97982014-01-30 14:49:21 -0800160
Yingdi Yufc40d872014-02-18 12:56:04 -0800161#endif //NDN_SECURITY_VALIDATOR_HPP