Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 20 | * @author Yingdi Yu <yuyingdi@gmail.com> |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef NDN_GEP_PRODUCER_HPP |
| 24 | #define NDN_GEP_PRODUCER_HPP |
| 25 | |
| 26 | #include "producer-db.hpp" |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 27 | #include "error-code.hpp" |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 28 | |
| 29 | #include <ndn-cxx/security/key-chain.hpp> |
| 30 | #include <ndn-cxx/face.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace gep { |
| 34 | |
| 35 | // @brief Callback returns vector of Data contains content keys encrypted by E-KEYS |
| 36 | typedef function<void(const std::vector<Data>&)> ProducerEKeyCallback; |
| 37 | |
| 38 | /** |
| 39 | * @brief Manage content key and data encryption |
| 40 | */ |
| 41 | class Producer |
| 42 | { |
| 43 | public: |
| 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 | |
| 59 | public: |
| 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 Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 75 | Face& face, const std::string& dbPath, |
| 76 | uint8_t repeatAttempts = 3, |
| 77 | const Link& keyRetrievalLink = NO_LINK); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 80 | * @brief Create content key corresponding to @p timeslot |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 81 | * |
| 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 Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 86 | * passed back through @p callback. In case of any error, @p errorCallBack |
| 87 | * will be invoked. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 88 | */ |
| 89 | Name |
| 90 | createContentKey(const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 91 | const ProducerEKeyCallback& callback, |
| 92 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 95 | * @brief Produce an data packet encrypted using the content key corresponding @p timeslot |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 96 | * |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 97 | * 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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 100 | */ |
| 101 | void |
| 102 | produce(Data& data, const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 103 | const uint8_t* content, size_t contentLen, |
| 104 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
| 105 | |
| 106 | public: |
| 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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | |
| 118 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 119 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 124 | */ |
| 125 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 126 | sendKeyInterest(const Interest& interest, |
| 127 | const time::system_clock::TimePoint& timeslot, |
| 128 | const ProducerEKeyCallback& callback, |
| 129 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 132 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 152 | */ |
| 153 | void |
| 154 | handleTimeout(const Interest& interest, |
| 155 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 156 | const ProducerEKeyCallback& callback, |
| 157 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 160 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 165 | */ |
| 166 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 167 | handleNack(const Interest& interest, |
| 168 | const lp::Nack& nack, |
| 169 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 170 | const ProducerEKeyCallback& callback, |
| 171 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 174 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 177 | */ |
| 178 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 179 | 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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 192 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 193 | const ProducerEKeyCallback& callback, |
| 194 | const ErrorCallBack& errorCallback = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 195 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 196 | public: |
| 197 | static const Link NO_LINK; |
| 198 | |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 199 | private: |
| 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 Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 207 | |
| 208 | Link m_keyRetrievalLink; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | } // namespace gep |
| 212 | } // namespace ndn |
| 213 | |
| 214 | #endif // NDN_GEP_PRODUCER_HPP |