blob: 4c7ee913814204def9adf927fb0e8b7f889bdf49 [file] [log] [blame]
Zhiyi Zhang5f133622015-10-17 08:49:54 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
4 *
5 * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
7 *
8 * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
20 * @author Yingdi Yu <yingdi@cs.ucla.edu>
21 */
22
23#ifndef NDN_GEP_CONSUMER_HPP
24#define NDN_GEP_CONSUMER_HPP
25
26#include "algo/rsa.hpp"
27#include "algo/aes.hpp"
28#include "consumer-db.hpp"
29#include "error-code.hpp"
30
31#include <ndn-cxx/security/validator-null.hpp>
32#include <ndn-cxx/face.hpp>
33
34namespace ndn {
35namespace gep {
36
37typedef function<void (const Data&, const Buffer&)> ConsumptionCallBack;
38
39/**
40 * @brief Consumer in group-based encryption protocol
41 */
42class Consumer
43{
44private:
45 typedef function<void (const Buffer&)> PlainTextCallBack;
46
47public:
48 /**
49 * @brief Create a consumer instance
50 *
51 * @param face The face used for key fetching
52 * @param groupName The reading group name that the consumer belongs to
53 * @param consumerName The identity of the consumer
Yingdi Yu48967a62016-03-11 22:04:14 -080054 * @param dbPath The path to database storing decryption key
55 * @param cKeyLink The link object for C-KEY retrieval
56 * @param dKeyLink The link object for D-KEY retrieval
Zhiyi Zhang5f133622015-10-17 08:49:54 +080057 */
Yingdi Yu48967a62016-03-11 22:04:14 -080058 Consumer(Face& face, const Name& groupName, const Name& consumerName, const std::string& dbPath,
59 const Link& cKeyLink = NO_LINK, const Link& dKeyLink = NO_LINK);
Zhiyi Zhang5f133622015-10-17 08:49:54 +080060
61 /**
62 * @brief Send out the Interest packet to fetch content packet with @p dataName.
63 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070064 * @param dataName name of the data packet to fetch
Zhiyi Zhang5f133622015-10-17 08:49:54 +080065 * @param consumptionCallBack The callback when requested data is decrypted
Yingdi Yu48967a62016-03-11 22:04:14 -080066 * @param errorCallback The callback when error happens in consumption
67 * @param link The link object for data retrieval
Zhiyi Zhang5f133622015-10-17 08:49:54 +080068 */
69 void
70 consume(const Name& dataName,
71 const ConsumptionCallBack& consumptionCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -080072 const ErrorCallBack& errorCallback,
73 const Link& link = NO_LINK);
Zhiyi Zhang5f133622015-10-17 08:49:54 +080074
75 /**
76 * @brief Set the group name to @p groupName.
77 */
78 void
79 setGroup(const Name& groupName);
80
81 /**
82 * @brief Add new decryption key with @p keyName and @p keyBuf.
83 */
84 void
85 addDecryptionKey(const Name& keyName, const Buffer& keyBuf);
86
87PUBLIC_WITH_TESTS_ELSE_PRIVATE:
88
89 /**
90 * @brief Decrypt @p encryptedBlock using @p keyBits
91 *
Yingdi Yu48967a62016-03-11 22:04:14 -080092 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +080093 */
94 void
95 decrypt(const Block& encryptedBlock,
96 const Buffer& keyBits,
97 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -080098 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +080099
100 /**
101 * @brief Decrypt @p data.
102 *
Yingdi Yu48967a62016-03-11 22:04:14 -0800103 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800104 */
105 void
106 decryptContent(const Data& data,
107 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800108 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800109
110 /**
111 * @brief Decrypt @p cKeyData.
112 *
Yingdi Yu48967a62016-03-11 22:04:14 -0800113 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800114 */
115 void
116 decryptCKey(const Data& cKeyData,
117 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800118 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800119
120 /**
121 * @brief Decrypt @p dKeyData.
122 *
Yingdi Yu48967a62016-03-11 22:04:14 -0800123 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800124 */
125 void
126 decryptDKey(const Data& dKeyData,
127 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800128 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800129
130
131 /**
132 * @brief Get the buffer of decryption key with @p decryptionKeyName from database.
133 *
134 * @return Null buffer when there is no decryption key with @p decryptionKeyName.
135 */
136 const Buffer
137 getDecryptionKey(const Name& decryptionKeyName);
138
Yingdi Yu48967a62016-03-11 22:04:14 -0800139 /**
140 * @brief Helper method for sending interest
141 *
142 * This method prepare the three callbacks: DataCallbak, NackCallback, TimeoutCallback
143 * for the @p interest.
144 *
145 * @param interest The interes to send out
146 * @param nRetrials The number of retrials left (if timeout)
147 * @param link The link object (used when NACK is received)
148 * @param validationCallback The callback when data is validated
149 * @param errorCallback The callback when error happens
150 */
151 void
152 sendInterest(const Interest& interest, int nRetrials,
153 const Link& link,
154 const OnDataValidated& validationCallback,
155 const ErrorCallBack& errorCallback);
156
157 /**
158 * @brief Callback to handle NACK
159 *
160 * This method will check if there is another delegation to use. Otherwise report error
161 *
162 * @param interest The interes got NACKed
163 * @param nack The nack object
164 * @param link The link object (used when NACK is received)
165 * @param delegationIndex Current selected delegation
166 * @param validationCallback The callback when data is validated
167 * @param errorCallback The callback when error happens
168 */
169 void
170 handleNack(const Interest& interest, const lp::Nack& nack,
171 const Link& link,
172 const OnDataValidated& validationCallback,
173 const ErrorCallBack& errorCallback);
174
175 /**
176 * @brief Callback to handle timeout
177 *
178 * This method will check if a retrial is allowed. Otherwise retreat the interest as NACKed
179 *
180 * @param interest The interes timed out
181 * @param nRetrials The number of retrials left
182 * @param link The link object (used when NACK is received)
183 * @param delegationIndex Current selected delegation
184 * @param validationCallback The callback when data is validated
185 * @param errorCallback The callback when error happens
186 */
187 void
188 handleTimeout(const Interest& interest, int nRetrials,
189 const Link& link,
190 const OnDataValidated& validationCallback,
191 const ErrorCallBack& errorCallback);
192
193public:
194 static const Link NO_LINK;
195
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800196private:
197 ConsumerDB m_db;
198 unique_ptr<Validator> m_validator;
199 Face& m_face;
200 Name m_groupName;
201 Name m_consumerName;
202
Yingdi Yu48967a62016-03-11 22:04:14 -0800203 Link m_cKeyLink;
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800204 std::map<Name, Buffer> m_cKeyMap;
Yingdi Yu48967a62016-03-11 22:04:14 -0800205 Link m_dKeyLink;
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800206 std::map<Name, Buffer> m_dKeyMap;
207};
208
209} // namespace gep
210} // namespace ndn
211
212#endif // NDN_GEP_CONSUMER_HPP