blob: 2dbd99578063d01b66320cd1e97dc9353d2866cc [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 "sec-policy.hpp"
15#include "validation-request.hpp"
16#include "public-key.hpp"
17#include "signature-sha256-with-rsa.hpp"
Yingdi Yu2abd73f2014-01-08 23:34:11 -080018
19namespace ndn {
Yingdi Yu2abd73f2014-01-08 23:34:11 -080020
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 */
26class Verifier {
27public:
28 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
29
Yingdi Yu4f324632014-01-15 18:10:03 -080030 Verifier(const ptr_lib::shared_ptr<SecPolicy> &policy = DefaultPolicy);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080031
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 Yu4f324632014-01-15 18:10:03 -080042 * @brief Get the policy.
43 * @return The Policy.
Yingdi Yu2abd73f2014-01-08 23:34:11 -080044 */
Yingdi Yu4f324632014-01-15 18:10:03 -080045 inline SecPolicy&
Yingdi Yub4bb85a2014-01-16 10:11:04 -080046 policy()
Yingdi Yu2abd73f2014-01-08 23:34:11 -080047 {
Yingdi Yu4f324632014-01-15 18:10:03 -080048 if (!policy_)
49 throw Error("policy is not assigned to the KeyChain");
Yingdi Yu2abd73f2014-01-08 23:34:11 -080050
Yingdi Yu4f324632014-01-15 18:10:03 -080051 return *policy_;
Yingdi Yu2abd73f2014-01-08 23:34:11 -080052 }
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 Yu913b0c72014-01-10 18:02:55 -080070 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 Yu2abd73f2014-01-08 23:34:11 -080076 static bool
77 verifySignature(const Data& data, const SignatureSha256WithRsa& sig, const PublicKey& publicKey);
78
Yingdi Yu913b0c72014-01-10 18:02:55 -080079 static bool
80 verifySignature(const Buffer &data, const SignatureSha256WithRsa &sig, const PublicKey &publicKey);
Yingdi Yu2abd73f2014-01-08 23:34:11 -080081
82public:
Yingdi Yu4f324632014-01-15 18:10:03 -080083 static const ptr_lib::shared_ptr<SecPolicy> DefaultPolicy;
Yingdi Yu2abd73f2014-01-08 23:34:11 -080084
85private:
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
95private:
Yingdi Yu4f324632014-01-15 18:10:03 -080096 ptr_lib::shared_ptr<SecPolicy> policy_;
Yingdi Yu2abd73f2014-01-08 23:34:11 -080097 ptr_lib::shared_ptr<Face> face_;
98};
99
100}
101
102#endif