Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 13 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 16 | #ifndef NDN_SECURITY_VALIDATOR_HPP |
| 17 | #define NDN_SECURITY_VALIDATOR_HPP |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 18 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 19 | #include "../common.hpp" |
| 20 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 21 | #include "../data.hpp" |
| 22 | #include "../face.hpp" |
| 23 | #include "public-key.hpp" |
| 24 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 25 | #include "signature-sha256.hpp" |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 26 | #include "validation-request.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 30 | * @brief Validator is one of the main classes of the security library. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 31 | * |
| 32 | * The Validator class provides the interfaces for packet validation. |
| 33 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 34 | class Validator |
| 35 | { |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 36 | public: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 37 | class Error : public std::runtime_error |
| 38 | { |
| 39 | public: |
| 40 | explicit |
| 41 | Error(const std::string& what) |
| 42 | : std::runtime_error(what) |
| 43 | { |
| 44 | } |
| 45 | }; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 46 | |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 47 | Validator(); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 48 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 49 | explicit |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 50 | Validator(Face& face); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 53 | * @brief Validate Data and call either onValidated or onValidationFailed. |
| 54 | * |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 55 | * @param data The Data with the signature to check. |
| 56 | * @param onValidated If the Data is validated, this calls onValidated(data). |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 57 | * @param onValidationFailed If validation fails, this calls onValidationFailed(data). |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 58 | */ |
| 59 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 60 | validate(const Data& data, |
| 61 | const OnDataValidated& onValidated, |
| 62 | const OnDataValidationFailed& onValidationFailed) |
| 63 | { |
| 64 | validate(data, onValidated, onValidationFailed, 0); |
| 65 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 68 | * @brief Validate Interest and call either onValidated or onValidationFailed. |
| 69 | * |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 70 | * @param interest The Interest with the signature to check. |
| 71 | * @param onValidated If the Interest is validated, this calls onValidated(interest). |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 72 | * @param onValidationFailed If validation fails, this calls onValidationFailed(interest). |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 73 | */ |
| 74 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 75 | validate(const Interest& interest, |
| 76 | const OnInterestValidated& onValidated, |
| 77 | const OnInterestValidationFailed& onValidationFailed) |
| 78 | { |
| 79 | validate(interest, onValidated, onValidationFailed, 0); |
| 80 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 81 | |
| 82 | /***************************************** |
| 83 | * verifySignature method set * |
| 84 | *****************************************/ |
| 85 | |
| 86 | /// @brief Verify the data using the publicKey. |
| 87 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 88 | verifySignature(const Data& data, const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 89 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 90 | /** |
| 91 | * @brief Verify the signed Interest using the publicKey. |
| 92 | * |
| 93 | * (Note the signature covers the first n-2 name components). |
| 94 | */ |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 95 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 96 | verifySignature(const Interest& interest, const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 97 | |
| 98 | /// @brief Verify the blob using the publicKey against the signature. |
| 99 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 100 | verifySignature(const Buffer& blob, const Signature& sig, const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 101 | |
| 102 | /// @brief Verify the data using the publicKey against the SHA256-RSA signature. |
| 103 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 104 | verifySignature(const Data& data, |
| 105 | const SignatureSha256WithRsa& sig, |
| 106 | const PublicKey& publicKey) |
| 107 | { |
| 108 | return verifySignature(data.wireEncode().value(), |
| 109 | data.wireEncode().value_size() - data.getSignature().getValue().size(), |
| 110 | sig, publicKey); |
| 111 | } |
| 112 | |
| 113 | /** @brief Verify the interest using the publicKey against the SHA256-RSA signature. |
| 114 | * |
| 115 | * (Note the signature covers the first n-2 name components). |
| 116 | */ |
| 117 | static bool |
| 118 | verifySignature(const Interest& interest, |
| 119 | const SignatureSha256WithRsa& sig, |
| 120 | const PublicKey& publicKey) |
| 121 | { |
| 122 | if (interest.getName().size() < 2) |
| 123 | return false; |
| 124 | |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 125 | const Name& name = interest.getName(); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 127 | return verifySignature(name.wireEncode().value(), |
| 128 | name.wireEncode().value_size() - name[-1].size(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 129 | sig, publicKey); |
| 130 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 131 | |
| 132 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 133 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 134 | verifySignature(const Buffer& blob, |
| 135 | const SignatureSha256WithRsa& sig, |
| 136 | const PublicKey& publicKey) |
| 137 | { |
| 138 | return verifySignature(blob.buf(), blob.size(), sig, publicKey); |
| 139 | } |
| 140 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 141 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 142 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 143 | verifySignature(const uint8_t* buf, |
| 144 | const size_t size, |
| 145 | const SignatureSha256WithRsa& sig, |
| 146 | const PublicKey& publicKey); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 147 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 148 | |
| 149 | /// @brief Verify the data against the SHA256 signature. |
| 150 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 151 | verifySignature(const Data& data, const SignatureSha256& sig) |
| 152 | { |
| 153 | return verifySignature(data.wireEncode().value(), |
| 154 | data.wireEncode().value_size() - |
| 155 | data.getSignature().getValue().size(), |
| 156 | sig); |
| 157 | } |
| 158 | |
| 159 | /** @brief Verify the interest against the SHA256 signature. |
| 160 | * |
| 161 | * (Note the signature covers the first n-2 name components). |
| 162 | */ |
| 163 | static bool |
| 164 | verifySignature(const Interest& interest, const SignatureSha256& sig) |
| 165 | { |
| 166 | if (interest.getName().size() < 2) |
| 167 | return false; |
| 168 | |
| 169 | Name signedName = interest.getName().getPrefix(-2); |
| 170 | |
| 171 | return verifySignature(signedName.wireEncode().value(), |
| 172 | signedName.wireEncode().value_size(), |
| 173 | sig); |
| 174 | } |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 175 | |
| 176 | /// @brief Verify the blob against the SHA256 signature. |
| 177 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 178 | verifySignature(const Buffer& blob, const SignatureSha256& sig) |
| 179 | { |
| 180 | return verifySignature (blob.buf(), blob.size(), sig); |
| 181 | } |
| 182 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 183 | /// @brief Verify the blob against the SHA256 signature. |
| 184 | static bool |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 185 | verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256& sig); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 186 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 187 | protected: |
| 188 | /** |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 189 | * @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] | 190 | * |
| 191 | * If there is no next validation step, that validation MUST have been done. |
| 192 | * i.e., either onValidated or onValidationFailed callback is invoked. |
| 193 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 194 | * @param data The Data to check. |
| 195 | * @param nSteps The number of validation steps that have been done. |
| 196 | * @param onValidated If the Data is validated, this calls onValidated(data) |
| 197 | * @param onValidationFailed If validation fails, this calls onValidationFailed(data) |
| 198 | * @param nextSteps On return, contains the next validation step |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 199 | */ |
| 200 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 201 | checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 202 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 203 | const OnDataValidated& onValidated, |
| 204 | const OnDataValidationFailed& onValidationFailed, |
| 205 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 206 | |
| 207 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 208 | * @brief Check the Interest against validation policy and return the next validation step |
| 209 | * if necessary. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 210 | * |
| 211 | * If there is no next validation step, that validation MUST have been done. |
| 212 | * i.e., either onValidated or onValidationFailed callback is invoked. |
| 213 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 214 | * @param interest The Interest to check. |
| 215 | * @param nSteps The number of validation steps that have been done. |
| 216 | * @param onValidated If the Interest is validated, this calls onValidated(data) |
| 217 | * @param onValidationFailed If validation fails, this calls onValidationFailed(data) |
| 218 | * @param nextSteps On return, contains the next validation step |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 219 | */ |
| 220 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 221 | checkPolicy(const Interest& interest, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 222 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 223 | const OnInterestValidated& onValidated, |
| 224 | const OnInterestValidationFailed& onValidationFailed, |
| 225 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 226 | |
| 227 | private: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 228 | typedef function<void(const std::string&)> OnFailure; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 229 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 230 | /// @brief Process the received certificate. |
| 231 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 232 | onData(const Interest& interest, |
| 233 | const Data& data, |
| 234 | const shared_ptr<ValidationRequest>& nextStep); |
| 235 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 236 | /// @brief Re-express the interest if it times out. |
| 237 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 238 | onTimeout(const Interest& interest, |
| 239 | int retry, |
| 240 | const OnFailure& onFailure, |
| 241 | const shared_ptr<ValidationRequest>& nextStep); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 242 | |
| 243 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 244 | validate(const Data& data, |
| 245 | const OnDataValidated& onValidated, |
| 246 | const OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 247 | int nSteps); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 248 | |
| 249 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 250 | validate(const Interest& interest, |
| 251 | const OnInterestValidated& onValidated, |
| 252 | const OnInterestValidationFailed& onValidationFailed, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 253 | int nSteps); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 254 | |
| 255 | protected: |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 256 | bool m_hasFace; |
| 257 | Face& m_face; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 258 | }; |
| 259 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 260 | } // namespace ndn |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 261 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 262 | #endif //NDN_SECURITY_VALIDATOR_HPP |