blob: a44058188f3861bc5347992f3a4a1ebb61e3899f [file] [log] [blame]
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07001/* -*- 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 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
26#include "producer-db.hpp"
Yingdi Yu79ce2392016-03-10 10:21:55 -080027#include "error-code.hpp"
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070028
29#include <ndn-cxx/security/key-chain.hpp>
30#include <ndn-cxx/face.hpp>
31
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:
44 struct KeyInfo {
45 time::system_clock::TimePoint beginTimeslot;
46 time::system_clock::TimePoint endTimeslot;
47 Buffer keyBits;
48 };
49
50 struct KeyRequest {
51 KeyRequest(size_t interests)
52 : interestCount(interests)
53 {}
54 size_t interestCount;
55 std::unordered_map<Name, size_t> repeatAttempts;
56 std::vector<Data> encryptedKeys;
57 };
58
59public:
60 /**
61 * @brief Construct a producer
62 *
63 * A producer can produce data with a naming convention:
64 * /<@p prefix>/SAMPLES/<@p dataType>/[timestamp]
65 *
66 * The produced data packet is encrypted with a content key,
67 * which is stored in a database at @p dbPath.
68 *
69 * A producer also need to produce data containing content key
70 * encrypted with E-KEYs. A producer can retrieve E-KEYs through
71 * @p face, and will re-try for at most @p repeatAttemps times when
72 * E-KEY retrieval fails.
73 */
74 Producer(const Name& prefix, const Name& dataType,
Yingdi Yu48967a62016-03-11 22:04:14 -080075 Face& face, const std::string& dbPath,
76 uint8_t repeatAttempts = 3,
77 const Link& keyRetrievalLink = NO_LINK);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070078
79 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -080080 * @brief Create content key corresponding to @p timeslot
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070081 *
82 * This method will first check if the content key exists. For existing
83 * content key, the method will return content key name directly.
84 * If the key does not exist, the method will create one and encrypt
85 * it using corresponding E-KEY. The encrypted content keys will be
Yingdi Yu79ce2392016-03-10 10:21:55 -080086 * passed back through @p callback. In case of any error, @p errorCallBack
87 * will be invoked.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070088 */
89 Name
90 createContentKey(const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -080091 const ProducerEKeyCallback& callback,
92 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070093
94 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -080095 * @brief Produce an data packet encrypted using the content key corresponding @p timeslot
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070096 *
Yingdi Yu79ce2392016-03-10 10:21:55 -080097 * This method encrypts @p content of @p contentLen with a content key covering
98 * @p timeslot, and set @p data with the encrypted content and appropriate data name.
99 * In case of any error, @p errorCallBack will be invoked.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700100 */
101 void
102 produce(Data& data, const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800103 const uint8_t* content, size_t contentLen,
104 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
105
106public:
107 /**
108 * @brief Default error callback
109 *
110 * @param code The error code.
111 * @param msg The error msg.
112 */
113 static void
114 defaultErrorCallBack(const ErrorCode& code, const std::string& msg);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700115
116private:
117
118 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800119 * @brief Send interest for E-KEY
120 *
121 * This method simply construct DataCallback, NackCallback, TiemoutCallback using
122 * @p timeslot, @p callback, and @p errorCallBack, and express @p interest with
123 * the created callbacks.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700124 */
125 void
Yingdi Yu79ce2392016-03-10 10:21:55 -0800126 sendKeyInterest(const Interest& interest,
127 const time::system_clock::TimePoint& timeslot,
128 const ProducerEKeyCallback& callback,
129 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700130
131 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800132 * @brief Handle received E-KEY retrieved using @p interest.
133 *
134 * This method first checks if the E-key contained in @p data fits @p timeslot.
135 * If true, encrypt the C-KEY for @p timeslot using the E-KEY, if the retrieval for
136 * all E-KEYs for the C-KEY have been done, invoke @p callback. Otherwise, narrow down
137 * the search scope through revising exclude filter and re-express the interest. In case
138 * of any error, invoke @p errorCallBack.
139 */
140 void
141 handleCoveringKey(const Interest& interest, const Data& data,
142 const time::system_clock::TimePoint& timeslot,
143 const ProducerEKeyCallback& callback,
144 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
145
146 /**
147 * @brief Handle timeout.
148 *
149 * Re-express @p interest if the number of retrials is less than max limit.
150 * The DataCallback, NackCallback, TiemoutCallback are created using @p timeslot,
151 * @p callback, and @p errorCallBack,
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700152 */
153 void
154 handleTimeout(const Interest& interest,
155 const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800156 const ProducerEKeyCallback& callback,
157 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700158
159 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800160 * @brief Handle @p nack for the E-KEY requested through @p interest.
161 *
162 * This method will decrease the outstanding E-KEY interest count for the C-Key
163 * corresponding to @p timeCount. When there is no outstanding interest, invoke
164 * @p callback.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700165 */
166 void
Yingdi Yu79ce2392016-03-10 10:21:55 -0800167 handleNack(const Interest& interest,
168 const lp::Nack& nack,
169 const time::system_clock::TimePoint& timeslot,
Yingdi Yu48967a62016-03-11 22:04:14 -0800170 const ProducerEKeyCallback& callback,
171 const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700172
173 /**
Yingdi Yu79ce2392016-03-10 10:21:55 -0800174 * @brief Decrease the count of outstanding E-KEY interests for C-KEY for @p timeCount
175 *
176 * If the count decrease to 0, invoke @p callback.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700177 */
178 void
Yingdi Yu79ce2392016-03-10 10:21:55 -0800179 updateKeyRequest(KeyRequest& keyRequest, uint64_t timeCount,
180 const ProducerEKeyCallback& callback);
181
182 /**
183 * @brief Encrypts C-KEY for @p timeslot using @p encryptionKey of @p eKeyName
184 *
185 * Invoke @p callback if no more interests to process.
186 * invoke @p errorCallback in case of any error.
187 *
188 * @return true if encryption succeeds, otherwise false.
189 */
190 bool
191 encryptContentKey(const Buffer& encryptionKey, const Name& eKeyName,
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700192 const time::system_clock::TimePoint& timeslot,
Yingdi Yu79ce2392016-03-10 10:21:55 -0800193 const ProducerEKeyCallback& callback,
194 const ErrorCallBack& errorCallback = Producer::defaultErrorCallBack);
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700195
Yingdi Yu48967a62016-03-11 22:04:14 -0800196public:
197 static const Link NO_LINK;
198
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700199private:
200 Face& m_face;
201 Name m_namespace;
202 KeyChain m_keychain;
203 std::unordered_map<Name, KeyInfo> m_ekeyInfo;
204 std::unordered_map<uint64_t, KeyRequest> m_keyRequests;
205 ProducerDB m_db;
206 uint8_t m_maxRepeatAttempts;
Yingdi Yu48967a62016-03-11 22:04:14 -0800207
208 Link m_keyRetrievalLink;
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700209};
210
211} // namespace gep
212} // namespace ndn
213
214#endif // NDN_GEP_PRODUCER_HPP