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, |
| 75 | Face& face, const std::string& dbPath, uint8_t repeatAttempts = 3); |
| 76 | |
| 77 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 78 | * @brief Create content key corresponding to @p timeslot |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 79 | * |
| 80 | * This method will first check if the content key exists. For existing |
| 81 | * content key, the method will return content key name directly. |
| 82 | * If the key does not exist, the method will create one and encrypt |
| 83 | * it using corresponding E-KEY. The encrypted content keys will be |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 84 | * passed back through @p callback. In case of any error, @p errorCallBack |
| 85 | * will be invoked. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 86 | */ |
| 87 | Name |
| 88 | createContentKey(const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 89 | const ProducerEKeyCallback& callback, |
| 90 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 93 | * @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] | 94 | * |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 95 | * This method encrypts @p content of @p contentLen with a content key covering |
| 96 | * @p timeslot, and set @p data with the encrypted content and appropriate data name. |
| 97 | * In case of any error, @p errorCallBack will be invoked. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 98 | */ |
| 99 | void |
| 100 | produce(Data& data, const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 101 | const uint8_t* content, size_t contentLen, |
| 102 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
| 103 | |
| 104 | public: |
| 105 | /** |
| 106 | * @brief Default error callback |
| 107 | * |
| 108 | * @param code The error code. |
| 109 | * @param msg The error msg. |
| 110 | */ |
| 111 | static void |
| 112 | defaultErrorCallBack(const ErrorCode& code, const std::string& msg); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | |
| 116 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 117 | * @brief Send interest for E-KEY |
| 118 | * |
| 119 | * This method simply construct DataCallback, NackCallback, TiemoutCallback using |
| 120 | * @p timeslot, @p callback, and @p errorCallBack, and express @p interest with |
| 121 | * the created callbacks. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 122 | */ |
| 123 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 124 | sendKeyInterest(const Interest& interest, |
| 125 | const time::system_clock::TimePoint& timeslot, |
| 126 | const ProducerEKeyCallback& callback, |
| 127 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 130 | * @brief Handle received E-KEY retrieved using @p interest. |
| 131 | * |
| 132 | * This method first checks if the E-key contained in @p data fits @p timeslot. |
| 133 | * If true, encrypt the C-KEY for @p timeslot using the E-KEY, if the retrieval for |
| 134 | * all E-KEYs for the C-KEY have been done, invoke @p callback. Otherwise, narrow down |
| 135 | * the search scope through revising exclude filter and re-express the interest. In case |
| 136 | * of any error, invoke @p errorCallBack. |
| 137 | */ |
| 138 | void |
| 139 | handleCoveringKey(const Interest& interest, const Data& data, |
| 140 | const time::system_clock::TimePoint& timeslot, |
| 141 | const ProducerEKeyCallback& callback, |
| 142 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
| 143 | |
| 144 | /** |
| 145 | * @brief Handle timeout. |
| 146 | * |
| 147 | * Re-express @p interest if the number of retrials is less than max limit. |
| 148 | * The DataCallback, NackCallback, TiemoutCallback are created using @p timeslot, |
| 149 | * @p callback, and @p errorCallBack, |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 150 | */ |
| 151 | void |
| 152 | handleTimeout(const Interest& interest, |
| 153 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 154 | const ProducerEKeyCallback& callback, |
| 155 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 158 | * @brief Handle @p nack for the E-KEY requested through @p interest. |
| 159 | * |
| 160 | * This method will decrease the outstanding E-KEY interest count for the C-Key |
| 161 | * corresponding to @p timeCount. When there is no outstanding interest, invoke |
| 162 | * @p callback. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 163 | */ |
| 164 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 165 | handleNack(const Interest& interest, |
| 166 | const lp::Nack& nack, |
| 167 | const time::system_clock::TimePoint& timeslot, |
| 168 | const ProducerEKeyCallback& callback); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 171 | * @brief Decrease the count of outstanding E-KEY interests for C-KEY for @p timeCount |
| 172 | * |
| 173 | * If the count decrease to 0, invoke @p callback. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 174 | */ |
| 175 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 176 | updateKeyRequest(KeyRequest& keyRequest, uint64_t timeCount, |
| 177 | const ProducerEKeyCallback& callback); |
| 178 | |
| 179 | /** |
| 180 | * @brief Encrypts C-KEY for @p timeslot using @p encryptionKey of @p eKeyName |
| 181 | * |
| 182 | * Invoke @p callback if no more interests to process. |
| 183 | * invoke @p errorCallback in case of any error. |
| 184 | * |
| 185 | * @return true if encryption succeeds, otherwise false. |
| 186 | */ |
| 187 | bool |
| 188 | encryptContentKey(const Buffer& encryptionKey, const Name& eKeyName, |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 189 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame^] | 190 | const ProducerEKeyCallback& callback, |
| 191 | const ErrorCallBack& errorCallback = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 192 | |
| 193 | private: |
| 194 | Face& m_face; |
| 195 | Name m_namespace; |
| 196 | KeyChain m_keychain; |
| 197 | std::unordered_map<Name, KeyInfo> m_ekeyInfo; |
| 198 | std::unordered_map<uint64_t, KeyRequest> m_keyRequests; |
| 199 | ProducerDB m_db; |
| 200 | uint8_t m_maxRepeatAttempts; |
| 201 | }; |
| 202 | |
| 203 | } // namespace gep |
| 204 | } // namespace ndn |
| 205 | |
| 206 | #endif // NDN_GEP_PRODUCER_HPP |