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 | |
| 9 | #ifndef NDN_VALIDATOR_HPP |
| 10 | #define NDN_VALIDATOR_HPP |
| 11 | |
| 12 | #include "../data.hpp" |
| 13 | #include "../face.hpp" |
| 14 | #include "public-key.hpp" |
| 15 | #include "signature-sha256-with-rsa.hpp" |
| 16 | #include "validation-request.hpp" |
| 17 | |
| 18 | namespace ndn { |
| 19 | /** |
| 20 | * Validator is one of the main classes of the security library. |
| 21 | * |
| 22 | * The Validator class provides the interfaces for packet validation. |
| 23 | */ |
| 24 | class Validator { |
| 25 | public: |
| 26 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 27 | |
| 28 | static const shared_ptr<Face> DefaultFace; |
| 29 | |
| 30 | Validator (shared_ptr<Face> face = DefaultFace); |
| 31 | |
| 32 | /** |
| 33 | * @brief Validate Data and call either onValidated or onValidationFailed. |
| 34 | * |
| 35 | * @param data The Data with the signature to check. |
| 36 | * @param onValidated If the Data is validated, this calls onValidated(data). |
| 37 | * @param onValidationFailed If the Data validation fails, this calls onValidationFailed(data). |
| 38 | */ |
| 39 | void |
| 40 | validate (const shared_ptr<const Data> &data, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed) |
| 41 | { validate (data, onValidated, onValidationFailed, 0); } |
| 42 | |
| 43 | /** |
| 44 | * @brief Validate Interest and call either onValidated or onValidationFailed. |
| 45 | * |
| 46 | * @param interest The Interest with the signature to check. |
| 47 | * @param onValidated If the Interest is validated, this calls onValidated(interest). |
| 48 | * @param onValidationFailed If the Interest validation fails, this calls onValidationFailed(interest). |
| 49 | */ |
| 50 | void |
| 51 | validate (const shared_ptr<const Interest> &interest, const OnInterestValidated &onValidated, const OnInterestValidationFailed &onValidationFailed) |
| 52 | { validate (interest, onValidated, onValidationFailed, 0); } |
| 53 | |
| 54 | /***************************************** |
| 55 | * verifySignature method set * |
| 56 | *****************************************/ |
| 57 | |
| 58 | /// @brief Verify the data using the publicKey. |
| 59 | static bool |
| 60 | verifySignature (const Data &data, const PublicKey &publicKey); |
| 61 | |
| 62 | /// @brief Verify the signed Interest using the publicKey. |
| 63 | static bool |
| 64 | verifySignature (const Interest &interest, const PublicKey &publicKey); |
| 65 | |
| 66 | /// @brief Verify the blob using the publicKey against the signature. |
| 67 | static bool |
| 68 | verifySignature (const Buffer &blob, const Signature &sig, const PublicKey &publicKey); |
| 69 | |
| 70 | /// @brief Verify the data using the publicKey against the SHA256-RSA signature. |
| 71 | static bool |
| 72 | verifySignature (const Data& data, const SignatureSha256WithRsa& sig, const PublicKey& publicKey); |
| 73 | |
| 74 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 75 | static bool |
| 76 | verifySignature (const Buffer &blob, const SignatureSha256WithRsa &sig, const PublicKey &publicKey); |
| 77 | |
| 78 | /// @brief Verify the blob using the publicKey against the SHA256-RSA signature. |
| 79 | static bool |
| 80 | verifySignature (const uint8_t* buf, const size_t size, const SignatureSha256WithRsa &sig, const PublicKey &publicKey); |
| 81 | |
| 82 | protected: |
| 83 | /** |
| 84 | * @brief Check the Data against validation policy and return the next validation step if necessary. |
| 85 | * |
| 86 | * If there is no next validation step, that validation MUST have been done. |
| 87 | * i.e., either onValidated or onValidationFailed callback is invoked. |
| 88 | * |
| 89 | * @param data The Data to check. |
| 90 | * @param stepCount The number of validation steps that have been done, used to track the validation progress. |
| 91 | * @param onDataValidated If the Data is validated, this calls onValidated(data). |
| 92 | * @param onDataValidationFailed If the Data validation fails, this calls onValidationFailed(data). |
| 93 | * @param nextSteps On return, contains the next validation step. |
| 94 | */ |
| 95 | virtual void |
| 96 | checkPolicy (const shared_ptr<const Data> &data, |
| 97 | int stepCount, |
| 98 | const OnDataValidated &onValidated, |
| 99 | const OnDataValidationFailed &onValidationFailed, |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 100 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * @brief Check the Interest against validation policy and return the next validation step if necessary. |
| 104 | * |
| 105 | * If there is no next validation step, that validation MUST have been done. |
| 106 | * i.e., either onValidated or onValidationFailed callback is invoked. |
| 107 | * |
| 108 | * @param data The Interest to check. |
| 109 | * @param stepCount The number of validation steps that have been done, used to track the validation progress. |
| 110 | * @param OnInterestValidated If the Interest is validated, this calls onValidated(data). |
| 111 | * @param OnInterestValidationFailed If the Interest validation fails, this calls onValidationFailed(data). |
| 112 | * @return the indication of next validation step, null if there is no further step. |
| 113 | */ |
| 114 | virtual void |
| 115 | checkPolicy (const shared_ptr<const Interest> &interest, |
| 116 | int stepCount, |
| 117 | const OnInterestValidated &onValidated, |
| 118 | const OnInterestValidationFailed &onValidationFailed, |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 119 | std::vector<shared_ptr<ValidationRequest> > &nextSteps) = 0; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | typedef function< void () > OnFailure; |
| 123 | |
| 124 | /// @brief Process the received certificate. |
| 125 | void |
| 126 | onData (const shared_ptr<const Interest> &interest, |
| 127 | const shared_ptr<const Data> &data, |
| 128 | shared_ptr<ValidationRequest> nextStep); |
| 129 | |
| 130 | /// @brief Re-express the interest if it times out. |
| 131 | void |
| 132 | onTimeout (const shared_ptr<const Interest> &interest, |
| 133 | int retry, |
| 134 | const OnFailure &onFailure, |
| 135 | shared_ptr<ValidationRequest> nextStep); |
| 136 | |
| 137 | void |
| 138 | validate (const shared_ptr<const Data> &data, |
| 139 | const OnDataValidated &onValidated, |
| 140 | const OnDataValidationFailed &onValidationFailed, |
| 141 | int stepCount); |
| 142 | |
| 143 | void |
| 144 | validate (const shared_ptr<const Interest> &interest, |
| 145 | const OnInterestValidated &onValidated, |
| 146 | const OnInterestValidationFailed &onValidationFailed, |
| 147 | int stepCount); |
| 148 | |
| 149 | protected: |
| 150 | shared_ptr<Face> m_face; |
| 151 | }; |
| 152 | |
| 153 | } |
| 154 | |
| 155 | #endif |