blob: 07c149449774bd6e495bdda2ce319fd52d7c7456 [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 Yu913b0c72014-01-10 18:02:55 -080086 static bool
87 verifySignature(const Data &data, const Signature &sig, const PublicKey &publicKey);
88
89 static bool
90 verifySignature(const Buffer &data, const Signature &sig, const PublicKey &publicKey);
91
Yingdi Yu2abd73f2014-01-08 23:34:11 -080092 static bool
93 verifySignature(const Data& data, const SignatureSha256WithRsa& sig, const PublicKey& publicKey);
94
Yingdi Yu913b0c72014-01-10 18:02:55 -080095 static bool
96 verifySignature(const Buffer &data, const SignatureSha256WithRsa &sig, const PublicKey &publicKey);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080097
98public:
Yingdi Yu4f324632014-01-15 18:10:03 -080099 static const ptr_lib::shared_ptr<SecPolicy> DefaultPolicy;
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800100
101private:
102 void
103 onCertificateData
104 (const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
105
106 void
107 onCertificateInterestTimeout
Yingdi Yue07e3392014-01-28 10:29:27 -0800108 (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 -0800109
110private:
Yingdi Yue07e3392014-01-28 10:29:27 -0800111 ptr_lib::shared_ptr<SecPolicy> m_policy;
112 ptr_lib::shared_ptr<Face> m_face;
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800113};
114
115}
116
117#endif