blob: 55dee91c2a426f411f6579697ef0c3f86ffa666d [file] [log] [blame]
Zhiyi Zhang5f133622015-10-17 08:49:54 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Zhiyi Zhang5f133622015-10-17 08:49:54 +08004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * This file is part of NAC (Name-Based Access Control for NDN).
6 * See AUTHORS.md for complete list of NAC authors and contributors.
Zhiyi Zhang5f133622015-10-17 08:49:54 +08007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Zhiyi Zhang5f133622015-10-17 08:49:54 +08009 * 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 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Zhiyi Zhang5f133622015-10-17 08:49:54 +080013 * 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
Alexander Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Zhiyi Zhang5f133622015-10-17 08:49:54 +080018 *
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhang5f133622015-10-17 08:49:54 +080020 * @author Yingdi Yu <yingdi@cs.ucla.edu>
21 */
22
Alexander Afanasyev9091d832018-04-18 17:21:08 -040023#ifndef NDN_NAC_CONSUMER_HPP
24#define NDN_NAC_CONSUMER_HPP
Zhiyi Zhang5f133622015-10-17 08:49:54 +080025
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070026#include "common.hpp"
Zhiyi Zhang5f133622015-10-17 08:49:54 +080027#include "consumer-db.hpp"
28#include "error-code.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070029#include "algo/aes.hpp"
30#include "algo/rsa.hpp"
Zhiyi Zhang5f133622015-10-17 08:49:54 +080031#include <ndn-cxx/face.hpp>
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070032#include <ndn-cxx/security/validator-null.hpp>
Zhiyi Zhang5f133622015-10-17 08:49:54 +080033
34namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040035namespace nac {
Zhiyi Zhang5f133622015-10-17 08:49:54 +080036
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070037typedef function<void(const Data&, const Buffer&)> ConsumptionCallBack;
Zhiyi Zhang5f133622015-10-17 08:49:54 +080038
39/**
40 * @brief Consumer in group-based encryption protocol
41 */
42class Consumer
43{
44private:
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070045 typedef function<void(const Buffer&)> PlainTextCallBack;
Zhiyi Zhang5f133622015-10-17 08:49:54 +080046
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 */
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070058 Consumer(Face& face,
59 const Name& groupName,
60 const Name& consumerName,
61 const std::string& dbPath,
62 const Link& cKeyLink = NO_LINK,
63 const Link& dKeyLink = NO_LINK);
Zhiyi Zhang5f133622015-10-17 08:49:54 +080064
65 /**
66 * @brief Send out the Interest packet to fetch content packet with @p dataName.
67 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070068 * @param dataName name of the data packet to fetch
Zhiyi Zhang5f133622015-10-17 08:49:54 +080069 * @param consumptionCallBack The callback when requested data is decrypted
Yingdi Yu48967a62016-03-11 22:04:14 -080070 * @param errorCallback The callback when error happens in consumption
71 * @param link The link object for data retrieval
Zhiyi Zhang5f133622015-10-17 08:49:54 +080072 */
73 void
74 consume(const Name& dataName,
75 const ConsumptionCallBack& consumptionCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -080076 const ErrorCallBack& errorCallback,
77 const Link& link = NO_LINK);
Zhiyi Zhang5f133622015-10-17 08:49:54 +080078
79 /**
80 * @brief Set the group name to @p groupName.
81 */
82 void
83 setGroup(const Name& groupName);
84
85 /**
86 * @brief Add new decryption key with @p keyName and @p keyBuf.
87 */
88 void
89 addDecryptionKey(const Name& keyName, const Buffer& keyBuf);
90
91PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhang5f133622015-10-17 08:49:54 +080092 /**
93 * @brief Decrypt @p encryptedBlock using @p keyBits
94 *
Yingdi Yu48967a62016-03-11 22:04:14 -080095 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +080096 */
97 void
98 decrypt(const Block& encryptedBlock,
99 const Buffer& keyBits,
100 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800101 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800102
103 /**
104 * @brief Decrypt @p data.
105 *
Yingdi Yu48967a62016-03-11 22:04:14 -0800106 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800107 */
108 void
109 decryptContent(const Data& data,
110 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800111 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800112
113 /**
114 * @brief Decrypt @p cKeyData.
115 *
Yingdi Yu48967a62016-03-11 22:04:14 -0800116 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800117 */
118 void
119 decryptCKey(const Data& cKeyData,
120 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800121 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800122
123 /**
124 * @brief Decrypt @p dKeyData.
125 *
Yingdi Yu48967a62016-03-11 22:04:14 -0800126 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback.
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800127 */
128 void
129 decryptDKey(const Data& dKeyData,
130 const PlainTextCallBack& plainTextCallBack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800131 const ErrorCallBack& errorCallback);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800132
133
134 /**
135 * @brief Get the buffer of decryption key with @p decryptionKeyName from database.
136 *
137 * @return Null buffer when there is no decryption key with @p decryptionKeyName.
138 */
139 const Buffer
140 getDecryptionKey(const Name& decryptionKeyName);
141
Yingdi Yu48967a62016-03-11 22:04:14 -0800142 /**
143 * @brief Helper method for sending interest
144 *
145 * This method prepare the three callbacks: DataCallbak, NackCallback, TimeoutCallback
146 * for the @p interest.
147 *
148 * @param interest The interes to send out
149 * @param nRetrials The number of retrials left (if timeout)
150 * @param link The link object (used when NACK is received)
151 * @param validationCallback The callback when data is validated
152 * @param errorCallback The callback when error happens
153 */
154 void
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700155 sendInterest(const Interest& interest,
156 int nRetrials,
Yingdi Yu48967a62016-03-11 22:04:14 -0800157 const Link& link,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700158 const DataValidationSuccessCallback& validationCallback,
Yingdi Yu48967a62016-03-11 22:04:14 -0800159 const ErrorCallBack& errorCallback);
160
161 /**
162 * @brief Callback to handle NACK
163 *
164 * This method will check if there is another delegation to use. Otherwise report error
165 *
166 * @param interest The interes got NACKed
167 * @param nack The nack object
168 * @param link The link object (used when NACK is received)
169 * @param delegationIndex Current selected delegation
170 * @param validationCallback The callback when data is validated
171 * @param errorCallback The callback when error happens
172 */
173 void
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700174 handleNack(const Interest& interest,
175 const lp::Nack& nack,
Yingdi Yu48967a62016-03-11 22:04:14 -0800176 const Link& link,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700177 const DataValidationSuccessCallback& validationCallback,
Yingdi Yu48967a62016-03-11 22:04:14 -0800178 const ErrorCallBack& errorCallback);
179
180 /**
181 * @brief Callback to handle timeout
182 *
183 * This method will check if a retrial is allowed. Otherwise retreat the interest as NACKed
184 *
185 * @param interest The interes timed out
186 * @param nRetrials The number of retrials left
187 * @param link The link object (used when NACK is received)
188 * @param delegationIndex Current selected delegation
189 * @param validationCallback The callback when data is validated
190 * @param errorCallback The callback when error happens
191 */
192 void
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700193 handleTimeout(const Interest& interest,
194 int nRetrials,
Yingdi Yu48967a62016-03-11 22:04:14 -0800195 const Link& link,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700196 const DataValidationSuccessCallback& validationCallback,
Yingdi Yu48967a62016-03-11 22:04:14 -0800197 const ErrorCallBack& errorCallback);
198
199public:
200 static const Link NO_LINK;
201
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800202private:
203 ConsumerDB m_db;
204 unique_ptr<Validator> m_validator;
205 Face& m_face;
206 Name m_groupName;
207 Name m_consumerName;
208
Yingdi Yu48967a62016-03-11 22:04:14 -0800209 Link m_cKeyLink;
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800210 std::map<Name, Buffer> m_cKeyMap;
Yingdi Yu48967a62016-03-11 22:04:14 -0800211 Link m_dKeyLink;
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800212 std::map<Name, Buffer> m_dKeyMap;
213};
214
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400215} // namespace nac
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800216} // namespace ndn
217
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400218#endif // NDN_NAC_CONSUMER_HPP