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). |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 49 | * @param onValidationFailed If validation fails, this calls onValidationFailed(data). |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 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). |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 64 | * @param onValidationFailed If validation fails, this calls onValidationFailed(interest). |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 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 | |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 117 | const Name& name = interest.getName(); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 118 | |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 119 | return verifySignature(name.wireEncode().value(), |
| 120 | name.wireEncode().value_size() - name[-1].size(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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. |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 188 | * @param nSteps The number of validation steps that have been done. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 189 | * @param onDataValidated If the Data is validated, this calls onValidated(data). |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 190 | * @param onDataValidationFailed If validation fails, this calls onValidationFailed(data). |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 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, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 195 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 201 | * @brief Check the Interest against validation policy and return the next validation step |
| 202 | * if necessary. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 203 | * |
| 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 Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 208 | * @param nSteps The number of validation steps that have been done. |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 209 | * @param OnInterestValidated If the Interest is validated, this calls onValidated(data). |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 210 | * @param OnInterestValidationFailed If validation fails, this calls onValidationFailed(data). |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 211 | * @return the indication of next validation step, null if there is no further step. |
| 212 | */ |
| 213 | virtual void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 214 | checkPolicy(const Interest& interest, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 215 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 216 | const OnInterestValidated& onValidated, |
| 217 | const OnInterestValidationFailed& onValidationFailed, |
| 218 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 219 | |
| 220 | private: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 221 | typedef function<void(const std::string&)> OnFailure; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 222 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 223 | /// @brief Process the received certificate. |
| 224 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 225 | onData(const Interest& interest, |
| 226 | const Data& data, |
| 227 | const shared_ptr<ValidationRequest>& nextStep); |
| 228 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 229 | /// @brief Re-express the interest if it times out. |
| 230 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 231 | onTimeout(const Interest& interest, |
| 232 | int retry, |
| 233 | const OnFailure& onFailure, |
| 234 | const shared_ptr<ValidationRequest>& nextStep); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 235 | |
| 236 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 237 | validate(const Data& data, |
| 238 | const OnDataValidated& onValidated, |
| 239 | const OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 240 | int nSteps); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 241 | |
| 242 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 243 | validate(const Interest& interest, |
| 244 | const OnInterestValidated& onValidated, |
| 245 | const OnInterestValidationFailed& onValidationFailed, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 246 | int nSteps); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 247 | |
| 248 | protected: |
| 249 | shared_ptr<Face> m_face; |
| 250 | }; |
| 251 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 252 | } // namespace ndn |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 253 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 254 | #endif //NDN_SECURITY_VALIDATOR_HPP |