blob: 8724d1b0a4a39ed43280459f0d3bc4147b0c5681 [file] [log] [blame]
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07001/* -*- 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
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07004 *
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 Prashanth Swaminathan <prashanthsw@gmail.com>
Yingdi Yu79ce2392016-03-10 10:21:55 -080020 * @author Yingdi Yu <yuyingdi@gmail.com>
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070021 */
22
23#ifndef NDN_GEP_PRODUCER_HPP
24#define NDN_GEP_PRODUCER_HPP
25
Yingdi Yu79ce2392016-03-10 10:21:55 -080026#include "error-code.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070027#include "producer-db.hpp"
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070028
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070029#include <ndn-cxx/face.hpp>
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070030#include <ndn-cxx/security/key-chain.hpp>
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070031
32namespace ndn {
33namespace gep {
34
35// @brief Callback returns vector of Data contains content keys encrypted by E-KEYS
36typedef function<void(const std::vector<Data>&)> ProducerEKeyCallback;
37
38/**
39 * @brief Manage content key and data encryption
40 */
41class Producer
42{
43public:
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070044 struct KeyInfo
45 {
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070046 time::system_clock::TimePoint beginTimeslot;
47 time::system_clock::TimePoint endTimeslot;
48 Buffer keyBits;
49 };
50
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070051 struct KeyRequest
52 {
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070053 KeyRequest(size_t interests)
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070054 : interestCount(interests)
55 {
56 }
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070057 size_t interestCount;
58 std::unordered_map<Name, size_t> repeatAttempts;
59 std::vector<Data> encryptedKeys;
60 };
61
62public:
63 /**
64 * @brief Construct a producer
65 *
66 * A producer can produce data with a naming convention:
67 * /<@p prefix>/SAMPLES/<@p dataType>/[timestamp]
68 *
69 * The produced data packet is encrypted with a content key,
70 * which is stored in a database at @p dbPath.
71 *
72 * A producer also need to produce data containing content key
73 * encrypted with E-KEYs. A producer can retrieve E-KEYs through
74 * @p face, and will re-try for at most @p repeatAttemps times when
75 * E-KEY retrieval fails.
76 */
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070077 Producer(const Name& prefix,
78 const Name& dataType,
79 Face& face,
80 const std::string& dbPath,
Yingdi Yu48967a62016-03-11 22:04:14 -080081 uint8_t repeatAttempts = 3,
82 const Link& keyRetrievalLink = NO_LINK);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070083
84 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -080085 * @brief Create content key corresponding to @p timeslot
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070086 *
87 * This method will first check if the content key exists. For existing
88 * content key, the method will return content key name directly.
89 * If the key does not exist, the method will create one and encrypt
90 * it using corresponding E-KEY. The encrypted content keys will be
Yingdi Yu79ce2392016-03-10 10:21:55 -080091 * passed back through @p callback. In case of any error, @p errorCallBack
92 * will be invoked.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070093 */
94 Name
95 createContentKey(const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -080096 const ProducerEKeyCallback& callback,
97 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070098
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070099
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700100 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800101 * @brief Produce an data packet encrypted using the content key corresponding @p timeslot
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700102 *
Yingdi Yu79ce2392016-03-10 10:21:55 -0800103 * This method encrypts @p content of @p contentLen with a content key covering
104 * @p timeslot, and set @p data with the encrypted content and appropriate data name.
105 * In case of any error, @p errorCallBack will be invoked.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700106 */
107 void
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700108 produce(Data& data,
109 const time::system_clock::TimePoint& timeslot,
110 const uint8_t* content,
111 size_t contentLen,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800112 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
113
114public:
115 /**
116 * @brief Default error callback
117 *
118 * @param code The error code.
119 * @param msg The error msg.
120 */
121 static void
122 defaultErrorCallBack(const ErrorCode& code, const std::string& msg);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700123
124private:
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700125 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800126 * @brief Send interest for E-KEY
127 *
128 * This method simply construct DataCallback, NackCallback, TiemoutCallback using
129 * @p timeslot, @p callback, and @p errorCallBack, and express @p interest with
130 * the created callbacks.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700131 */
132 void
Yingdi Yu79ce2392016-03-10 10:21:55 -0800133 sendKeyInterest(const Interest& interest,
134 const time::system_clock::TimePoint& timeslot,
135 const ProducerEKeyCallback& callback,
136 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700137
138 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800139 * @brief Handle received E-KEY retrieved using @p interest.
140 *
141 * This method first checks if the E-key contained in @p data fits @p timeslot.
142 * If true, encrypt the C-KEY for @p timeslot using the E-KEY, if the retrieval for
143 * all E-KEYs for the C-KEY have been done, invoke @p callback. Otherwise, narrow down
144 * the search scope through revising exclude filter and re-express the interest. In case
145 * of any error, invoke @p errorCallBack.
146 */
147 void
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700148 handleCoveringKey(const Interest& interest,
149 const Data& data,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800150 const time::system_clock::TimePoint& timeslot,
151 const ProducerEKeyCallback& callback,
152 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
153
154 /**
155 * @brief Handle timeout.
156 *
157 * Re-express @p interest if the number of retrials is less than max limit.
158 * The DataCallback, NackCallback, TiemoutCallback are created using @p timeslot,
159 * @p callback, and @p errorCallBack,
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700160 */
161 void
162 handleTimeout(const Interest& interest,
163 const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800164 const ProducerEKeyCallback& callback,
165 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700166
167 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800168 * @brief Handle @p nack for the E-KEY requested through @p interest.
169 *
170 * This method will decrease the outstanding E-KEY interest count for the C-Key
171 * corresponding to @p timeCount. When there is no outstanding interest, invoke
172 * @p callback.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700173 */
174 void
Yingdi Yu79ce2392016-03-10 10:21:55 -0800175 handleNack(const Interest& interest,
176 const lp::Nack& nack,
177 const time::system_clock::TimePoint& timeslot,
Yingdi Yu48967a62016-03-11 22:04:14 -0800178 const ProducerEKeyCallback& callback,
179 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700180
181 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800182 * @brief Decrease the count of outstanding E-KEY interests for C-KEY for @p timeCount
183 *
184 * If the count decrease to 0, invoke @p callback.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700185 */
186 void
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700187 updateKeyRequest(KeyRequest& keyRequest, uint64_t timeCount, const ProducerEKeyCallback& callback);
Yingdi Yu79ce2392016-03-10 10:21:55 -0800188
189 /**
190 * @brief Encrypts C-KEY for @p timeslot using @p encryptionKey of @p eKeyName
191 *
192 * Invoke @p callback if no more interests to process.
193 * invoke @p errorCallback in case of any error.
194 *
195 * @return true if encryption succeeds, otherwise false.
196 */
197 bool
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700198 encryptContentKey(const Buffer& encryptionKey,
199 const Name& eKeyName,
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700200 const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800201 const ProducerEKeyCallback& callback,
202 const ErrorCallBack& errorCallback = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700203
Yingdi Yu48967a62016-03-11 22:04:14 -0800204public:
205 static const Link NO_LINK;
206
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700207private:
208 Face& m_face;
209 Name m_namespace;
210 KeyChain m_keychain;
211 std::unordered_map<Name, KeyInfo> m_ekeyInfo;
212 std::unordered_map<uint64_t, KeyRequest> m_keyRequests;
213 ProducerDB m_db;
214 uint8_t m_maxRepeatAttempts;
Yingdi Yu48967a62016-03-11 22:04:14 -0800215
216 Link m_keyRetrievalLink;
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700217};
218
219} // namespace gep
220} // namespace ndn
221
222#endif // NDN_GEP_PRODUCER_HPP