Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 5 | * This file is part of NAC (Name-Based Access Control for NDN). |
| 6 | * See AUTHORS.md for complete list of NAC authors and contributors. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 23 | #ifndef NDN_NAC_PRODUCER_HPP |
| 24 | #define NDN_NAC_PRODUCER_HPP |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 25 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 26 | #include "error-code.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 27 | #include "producer-db.hpp" |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 28 | |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/face.hpp> |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/security/key-chain.hpp> |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 33 | namespace nac { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 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: |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 44 | struct KeyInfo |
| 45 | { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 46 | time::system_clock::TimePoint beginTimeslot; |
| 47 | time::system_clock::TimePoint endTimeslot; |
| 48 | Buffer keyBits; |
| 49 | }; |
| 50 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 51 | struct KeyRequest |
| 52 | { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 53 | KeyRequest(size_t interests) |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 54 | : interestCount(interests) |
| 55 | { |
| 56 | } |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 57 | size_t interestCount; |
| 58 | std::unordered_map<Name, size_t> repeatAttempts; |
| 59 | std::vector<Data> encryptedKeys; |
| 60 | }; |
| 61 | |
| 62 | public: |
| 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 Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 77 | Producer(const Name& prefix, |
| 78 | const Name& dataType, |
| 79 | Face& face, |
| 80 | const std::string& dbPath, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 81 | uint8_t repeatAttempts = 3, |
| 82 | const Link& keyRetrievalLink = NO_LINK); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 85 | * @brief Create content key corresponding to @p timeslot |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 86 | * |
| 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 Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 91 | * passed back through @p callback. In case of any error, @p errorCallBack |
| 92 | * will be invoked. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 93 | */ |
| 94 | Name |
| 95 | createContentKey(const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 96 | const ProducerEKeyCallback& callback, |
| 97 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 98 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 99 | |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 100 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 101 | * @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] | 102 | * |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 103 | * 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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 106 | */ |
| 107 | void |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 108 | produce(Data& data, |
| 109 | const time::system_clock::TimePoint& timeslot, |
| 110 | const uint8_t* content, |
| 111 | size_t contentLen, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 112 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
| 113 | |
| 114 | public: |
| 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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 123 | |
| 124 | private: |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 125 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 126 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 131 | */ |
| 132 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 133 | sendKeyInterest(const Interest& interest, |
| 134 | const time::system_clock::TimePoint& timeslot, |
| 135 | const ProducerEKeyCallback& callback, |
| 136 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 139 | * @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 Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 148 | handleCoveringKey(const Interest& interest, |
| 149 | const Data& data, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 150 | 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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 160 | */ |
| 161 | void |
| 162 | handleTimeout(const Interest& interest, |
| 163 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 164 | const ProducerEKeyCallback& callback, |
| 165 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 168 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 173 | */ |
| 174 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 175 | handleNack(const Interest& interest, |
| 176 | const lp::Nack& nack, |
| 177 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 178 | const ProducerEKeyCallback& callback, |
| 179 | const ErrorCallBack& errorCallBack = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 182 | * @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 Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 185 | */ |
| 186 | void |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 187 | updateKeyRequest(KeyRequest& keyRequest, uint64_t timeCount, const ProducerEKeyCallback& callback); |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 188 | |
| 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 Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 198 | encryptContentKey(const Buffer& encryptionKey, |
| 199 | const Name& eKeyName, |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 200 | const time::system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 201 | const ProducerEKeyCallback& callback, |
| 202 | const ErrorCallBack& errorCallback = Producer::defaultErrorCallBack); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 203 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 204 | public: |
| 205 | static const Link NO_LINK; |
| 206 | |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 207 | private: |
| 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 Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 215 | |
| 216 | Link m_keyRetrievalLink; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 219 | } // namespace nac |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 220 | } // namespace ndn |
| 221 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 222 | #endif // NDN_NAC_PRODUCER_HPP |