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