Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -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_VERIFIER_HPP |
| 10 | #define NDN_VERIFIER_HPP |
| 11 | |
| 12 | #include "../data.hpp" |
| 13 | #include "../face.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 14 | #include "sec-policy.hpp" |
| 15 | #include "validation-request.hpp" |
| 16 | #include "public-key.hpp" |
| 17 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Verifier is one of the main classes of the security librar . |
| 23 | * |
| 24 | * The Verifier class provides the interfaces for packet verification. |
| 25 | */ |
| 26 | class Verifier { |
| 27 | public: |
| 28 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 29 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 30 | Verifier(const ptr_lib::shared_ptr<SecPolicy> &policy = DefaultPolicy); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * @brief Set the Face which will be used to fetch required certificates. |
| 34 | * @param face A pointer to the Face object. |
| 35 | * |
| 36 | * Setting face is necessary for verifier operation that involve fetching data. |
| 37 | */ |
| 38 | void |
| 39 | setFace(const ptr_lib::shared_ptr<Face> &face) { face_ = face; } |
| 40 | |
| 41 | /** |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 42 | * @brief Get the policy. |
| 43 | * @return The Policy. |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 44 | */ |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 45 | inline SecPolicy& |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 46 | policies() |
| 47 | { |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 48 | if (!policy_) |
| 49 | throw Error("policy is not assigned to the KeyChain"); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 50 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 51 | return *policy_; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Check the signature on the Data object and call either onVerify or onVerifyFailed. |
| 57 | * We use callback functions because verify may fetch information to check the signature. |
| 58 | * @param data The Data object with the signature to check. It is an error if data does not have a wireEncoding. |
| 59 | * To set the wireEncoding, you can call data.wireDecode. |
| 60 | * @param onVerified If the signature is verified, this calls onVerified(data). |
| 61 | * @param onVerifyFailed If the signature check fails, this calls onVerifyFailed(data). |
| 62 | */ |
| 63 | void |
| 64 | verifyData |
| 65 | (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount = 0); |
| 66 | |
| 67 | /***************************************** |
| 68 | * verifySignature method set * |
| 69 | *****************************************/ |
Yingdi Yu | 913b0c7 | 2014-01-10 18:02:55 -0800 | [diff] [blame] | 70 | static bool |
| 71 | verifySignature(const Data &data, const Signature &sig, const PublicKey &publicKey); |
| 72 | |
| 73 | static bool |
| 74 | verifySignature(const Buffer &data, const Signature &sig, const PublicKey &publicKey); |
| 75 | |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 76 | static bool |
| 77 | verifySignature(const Data& data, const SignatureSha256WithRsa& sig, const PublicKey& publicKey); |
| 78 | |
Yingdi Yu | 913b0c7 | 2014-01-10 18:02:55 -0800 | [diff] [blame] | 79 | static bool |
| 80 | verifySignature(const Buffer &data, const SignatureSha256WithRsa &sig, const PublicKey &publicKey); |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 81 | |
| 82 | public: |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 83 | static const ptr_lib::shared_ptr<SecPolicy> DefaultPolicy; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | void |
| 87 | onCertificateData |
| 88 | (const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep); |
| 89 | |
| 90 | void |
| 91 | onCertificateInterestTimeout |
| 92 | (const ptr_lib::shared_ptr<const Interest> &interest, int retry, const OnVerifyFailed& onVerifyFailed, |
| 93 | const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep); |
| 94 | |
| 95 | private: |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 96 | ptr_lib::shared_ptr<SecPolicy> policy_; |
Yingdi Yu | 2abd73f | 2014-01-08 23:34:11 -0800 | [diff] [blame] | 97 | ptr_lib::shared_ptr<Face> face_; |
| 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | #endif |