Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 1 | /* -*- 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 Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 9 | #ifndef NDN_SECURITY_VALIDATOR_HPP |
| 10 | #define NDN_SECURITY_VALIDATOR_HPP |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 12 | #include "../common.hpp" |
| 13 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 14 | #include "../data.hpp" |
| 15 | #include "../face.hpp" |
| 16 | #include "public-key.hpp" |
| 17 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 18 | #include "signature-sha256.hpp" |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 19 | #include "validation-request.hpp" |
| 20 | |
| 21 | namespace ndn { |
| 22 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 23 | * @brief Validator is one of the main classes of the security library. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 24 | * |
| 25 | * The Validator class provides the interfaces for packet validation. |
| 26 | */ |
| 27 | class Validator { |
| 28 | public: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 29 | 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 Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 38 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 39 | static const shared_ptr<Face> DEFAULT_FACE; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 40 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 41 | explicit |
| 42 | Validator(shared_ptr<Face> face = DEFAULT_FACE); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 45 | * @brief Validate Data and call either onValidated or onValidationFailed. |
| 46 | * |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 47 | * @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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 52 | validate(const Data& data, |
| 53 | const OnDataValidated& onValidated, |
| 54 | const OnDataValidationFailed& onValidationFailed) |
| 55 | { |
| 56 | validate(data, onValidated, onValidationFailed, 0); |
| 57 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 60 | * @brief Validate Interest and call either onValidated or onValidationFailed. |
| 61 | * |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 62 | * @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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 67 | validate(const Interest& interest, |
| 68 | const OnInterestValidated& onValidated, |
| 69 | const OnInterestValidationFailed& onValidationFailed) |
| 70 | { |
| 71 | validate(interest, onValidated, onValidationFailed, 0); |
| 72 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 73 | |
| 74 | /***************************************** |
| 75 | * verifySignature method set * |
| 76 | *****************************************/ |
| 77 | |
| 78 | /// @brief Verify the data using the publicKey. |
| 79 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 80 | verifySignature(const Data& data, const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 81 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @brief Verify the signed Interest using the publicKey. |
| 84 | * |
| 85 | * (Note the signature covers the first n-2 name components). |
| 86 | */ |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 87 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 88 | verifySignature(const Interest& interest, const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 89 | |
| 90 | /// @brief Verify the blob using the publicKey against the signature. |
| 91 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 92 | verifySignature(const Buffer& blob, const Signature& sig, const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 93 | |
| 94 | /// @brief Verify the data using the publicKey against the SHA256-RSA signature. |
| 95 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 96 | 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 | |
| 117 | Name signedName = interest.getName().getPrefix(-2); |
| 118 | |
| 119 | return verifySignature(signedName.wireEncode().value(), |
| 120 | signedName.wireEncode().value_size(), |
| 121 | sig, publicKey); |
| 122 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 123 | |
| 124 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 125 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 126 | 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 Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 133 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 134 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 135 | verifySignature(const uint8_t* buf, |
| 136 | const size_t size, |
| 137 | const SignatureSha256WithRsa& sig, |
| 138 | const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 139 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 140 | |
| 141 | /// @brief Verify the data against the SHA256 signature. |
| 142 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 143 | 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 Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 167 | |
| 168 | /// @brief Verify the blob against the SHA256 signature. |
| 169 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 170 | verifySignature(const Buffer& blob, const SignatureSha256& sig) |
| 171 | { |
| 172 | return verifySignature (blob.buf(), blob.size(), sig); |
| 173 | } |
| 174 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 175 | /// @brief Verify the blob against the SHA256 signature. |
| 176 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 177 | verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256& sig); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 178 | |
| 179 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 180 | protected: |
| 181 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 182 | * @brief Check the Data against policy and return the next validation step if necessary. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 183 | * |
| 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 194 | checkPolicy(const Data& data, |
| 195 | int stepCount, |
| 196 | const OnDataValidated& onValidated, |
| 197 | const OnDataValidationFailed& onValidationFailed, |
| 198 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 199 | |
| 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 Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 213 | checkPolicy(const Interest& interest, |
| 214 | int stepCount, |
| 215 | const OnInterestValidated& onValidated, |
| 216 | const OnInterestValidationFailed& onValidationFailed, |
| 217 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 218 | |
| 219 | private: |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 220 | typedef function< void (const std::string&) > OnFailure; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 221 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 222 | /// @brief Process the received certificate. |
| 223 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 224 | onData(const Interest& interest, |
| 225 | const Data& data, |
| 226 | const shared_ptr<ValidationRequest>& nextStep); |
| 227 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 228 | /// @brief Re-express the interest if it times out. |
| 229 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 230 | onTimeout(const Interest& interest, |
| 231 | int retry, |
| 232 | const OnFailure& onFailure, |
| 233 | const shared_ptr<ValidationRequest>& nextStep); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 234 | |
| 235 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 236 | validate(const Data& data, |
| 237 | const OnDataValidated& onValidated, |
| 238 | const OnDataValidationFailed& onValidationFailed, |
| 239 | int stepCount); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 240 | |
| 241 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 242 | validate(const Interest& interest, |
| 243 | const OnInterestValidated& onValidated, |
| 244 | const OnInterestValidationFailed& onValidationFailed, |
| 245 | int stepCount); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 246 | |
| 247 | protected: |
| 248 | shared_ptr<Face> m_face; |
| 249 | }; |
| 250 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 251 | } // namespace ndn |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 252 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 253 | #endif //NDN_SECURITY_VALIDATOR_HPP |