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" |
| 18 | #include "validation-request.hpp" |
| 19 | |
| 20 | namespace 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 | */ |
| 26 | class Validator { |
| 27 | public: |
| 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 Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 42 | validate (const Data& data, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 43 | { 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 Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 53 | validate (const Interest& interest, const OnInterestValidated &onValidated, const OnInterestValidationFailed &onValidationFailed) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 54 | { 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 Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 74 | 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 Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 78 | |
| 79 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 80 | static bool |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 81 | verifySignature (const Buffer &blob, const SignatureSha256WithRsa &sig, const PublicKey &publicKey) |
| 82 | { return verifySignature (blob.buf(), blob.size(), sig, publicKey); } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 83 | |
| 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 | |
| 88 | protected: |
| 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 Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 102 | checkPolicy (const Data& data, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 103 | int stepCount, |
| 104 | const OnDataValidated &onValidated, |
| 105 | const OnDataValidationFailed &onValidationFailed, |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 106 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 107 | |
| 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 Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 121 | checkPolicy (const Interest& interest, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 122 | int stepCount, |
| 123 | const OnInterestValidated &onValidated, |
| 124 | const OnInterestValidationFailed &onValidationFailed, |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 125 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 126 | |
| 127 | private: |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 128 | typedef function< void (const std::string&) > OnFailure; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 129 | |
| 130 | /// @brief Process the received certificate. |
| 131 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 132 | onData (const Interest& interest, |
| 133 | Data& data, |
| 134 | const shared_ptr<ValidationRequest>& nextStep); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 135 | |
| 136 | /// @brief Re-express the interest if it times out. |
| 137 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 138 | onTimeout (const Interest& interest, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 139 | int retry, |
| 140 | const OnFailure &onFailure, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 141 | const shared_ptr<ValidationRequest>& nextStep); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 142 | |
| 143 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 144 | validate (const Data& data, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 145 | const OnDataValidated &onValidated, |
| 146 | const OnDataValidationFailed &onValidationFailed, |
| 147 | int stepCount); |
| 148 | |
| 149 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 150 | validate (const Interest& interest, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 151 | const OnInterestValidated &onValidated, |
| 152 | const OnInterestValidationFailed &onValidationFailed, |
| 153 | int stepCount); |
| 154 | |
| 155 | protected: |
| 156 | shared_ptr<Face> m_face; |
| 157 | }; |
| 158 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 159 | } // namespace ndn |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 160 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 161 | #endif //NDN_SECURITY_VALIDATOR_HPP |