blob: a9bded4b468c4fde2d686217b06c9c5d0a70890b [file] [log] [blame]
Yingdi Yu2abd73f2014-01-08 23:34:11 -08001/* -*- 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 Yu4f324632014-01-15 18:10:03 -080014#include "validation-request.hpp"
15#include "public-key.hpp"
16#include "signature-sha256-with-rsa.hpp"
Yingdi Yu2abd73f2014-01-08 23:34:11 -080017
18namespace ndn {
Yingdi Yue07e3392014-01-28 10:29:27 -080019
20class SecPolicy;
21
22/**
23 * An OnVerified function object is used to pass a callback to verifyData to report a successful verification.
24 */
25typedef func_lib::function<void()> OnVerified;
26
27/**
28 * An OnVerifyFailed function object is used to pass a callback to verifyData to report a failed verification.
29 */
30typedef func_lib::function<void()> OnVerifyFailed;
31
Yingdi Yu2abd73f2014-01-08 23:34:11 -080032
33/**
34 * Verifier is one of the main classes of the security librar .
35 *
36 * The Verifier class provides the interfaces for packet verification.
37 */
38class Verifier {
39public:
40 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
41
Yingdi Yu4f324632014-01-15 18:10:03 -080042 Verifier(const ptr_lib::shared_ptr<SecPolicy> &policy = DefaultPolicy);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080043
44 /**
45 * @brief Set the Face which will be used to fetch required certificates.
46 * @param face A pointer to the Face object.
47 *
48 * Setting face is necessary for verifier operation that involve fetching data.
49 */
50 void
Yingdi Yue07e3392014-01-28 10:29:27 -080051 setFace(const ptr_lib::shared_ptr<Face> &face) { m_face = face; }
Yingdi Yu2abd73f2014-01-08 23:34:11 -080052
53 /**
Yingdi Yu4f324632014-01-15 18:10:03 -080054 * @brief Get the policy.
55 * @return The Policy.
Yingdi Yu2abd73f2014-01-08 23:34:11 -080056 */
Yingdi Yu4f324632014-01-15 18:10:03 -080057 inline SecPolicy&
Yingdi Yub4bb85a2014-01-16 10:11:04 -080058 policy()
Yingdi Yu2abd73f2014-01-08 23:34:11 -080059 {
Yingdi Yue07e3392014-01-28 10:29:27 -080060 if (static_cast<bool>(m_policy))
Yingdi Yu4f324632014-01-15 18:10:03 -080061 throw Error("policy is not assigned to the KeyChain");
Yingdi Yu2abd73f2014-01-08 23:34:11 -080062
Yingdi Yue07e3392014-01-28 10:29:27 -080063 return *m_policy;
Yingdi Yu2abd73f2014-01-08 23:34:11 -080064 }
65
66
67 /**
68 * Check the signature on the Data object and call either onVerify or onVerifyFailed.
69 * We use callback functions because verify may fetch information to check the signature.
70 * @param data The Data object with the signature to check. It is an error if data does not have a wireEncoding.
71 * To set the wireEncoding, you can call data.wireDecode.
72 * @param onVerified If the signature is verified, this calls onVerified(data).
73 * @param onVerifyFailed If the signature check fails, this calls onVerifyFailed(data).
74 */
75 void
Yingdi Yue07e3392014-01-28 10:29:27 -080076 verify
77 (const ptr_lib::shared_ptr<const Data> &data, const OnVerified &onVerified, const OnVerifyFailed &onVerifyFailed, int stepCount = 0);
78
79 void
80 verify
81 (const ptr_lib::shared_ptr<const Interest> &Interest, const OnVerified &onVerified, const OnVerifyFailed &onVerifyFailed, int stepCount = 0);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080082
83 /*****************************************
84 * verifySignature method set *
85 *****************************************/
Yingdi Yu4270f202014-01-28 14:19:16 -080086 static bool
Yingdi Yu913b0c72014-01-10 18:02:55 -080087 verifySignature(const Data &data, const Signature &sig, const PublicKey &publicKey);
88
89 static bool
Yingdi Yu4270f202014-01-28 14:19:16 -080090 verifySignature(const Interest &interest, const PublicKey &publicKey);
91
92 static bool
Yingdi Yu913b0c72014-01-10 18:02:55 -080093 verifySignature(const Buffer &data, const Signature &sig, const PublicKey &publicKey);
94
Yingdi Yu2abd73f2014-01-08 23:34:11 -080095 static bool
96 verifySignature(const Data& data, const SignatureSha256WithRsa& sig, const PublicKey& publicKey);
97
Yingdi Yu913b0c72014-01-10 18:02:55 -080098 static bool
99 verifySignature(const Buffer &data, const SignatureSha256WithRsa &sig, const PublicKey &publicKey);
Yingdi Yu4270f202014-01-28 14:19:16 -0800100
101 static bool
102 verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256WithRsa &sig, const PublicKey &publicKey);
103
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800104
105public:
Yingdi Yu4f324632014-01-15 18:10:03 -0800106 static const ptr_lib::shared_ptr<SecPolicy> DefaultPolicy;
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800107
108private:
109 void
110 onCertificateData
111 (const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
112
113 void
114 onCertificateInterestTimeout
Yingdi Yue07e3392014-01-28 10:29:27 -0800115 (const ptr_lib::shared_ptr<const Interest> &interest, int retry, const OnVerifyFailed& onVerifyFailed, ptr_lib::shared_ptr<ValidationRequest> nextStep);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800116
117private:
Yingdi Yue07e3392014-01-28 10:29:27 -0800118 ptr_lib::shared_ptr<SecPolicy> m_policy;
119 ptr_lib::shared_ptr<Face> m_face;
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800120};
121
122}
123
124#endif