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 | #include "producer.hpp" |
| 24 | #include "random-number-generator.hpp" |
| 25 | #include "algo/encryptor.hpp" |
| 26 | #include "algo/aes.hpp" |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 27 | #include "algo/error.hpp" |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace gep { |
| 31 | |
| 32 | using time::system_clock; |
| 33 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 34 | static const int START_TS_INDEX = -2; |
| 35 | static const int END_TS_INDEX = -1; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 36 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 37 | const Link Producer::NO_LINK = Link(); |
| 38 | |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 39 | /** |
| 40 | @brief Method to round the provided @p timeslot to the nearest whole |
| 41 | hour, so that we can store content keys uniformly (by start of the hour). |
| 42 | */ |
| 43 | static const system_clock::TimePoint |
| 44 | getRoundedTimeslot(const system_clock::TimePoint& timeslot) { |
| 45 | return time::fromUnixTimestamp( |
| 46 | (time::toUnixTimestamp(timeslot) / 3600000) * 3600000); |
| 47 | } |
| 48 | |
| 49 | Producer::Producer(const Name& prefix, const Name& dataType, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 50 | Face& face, const std::string& dbPath, |
| 51 | uint8_t repeatAttempts, |
| 52 | const Link& keyRetrievalLink) |
| 53 | : m_face(face) |
| 54 | , m_db(dbPath) |
| 55 | , m_maxRepeatAttempts(repeatAttempts) |
| 56 | , m_keyRetrievalLink(keyRetrievalLink) |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 57 | { |
| 58 | Name fixedPrefix = prefix; |
| 59 | Name fixedDataType = dataType; |
| 60 | KeyInfo keyInfo; |
| 61 | /** |
| 62 | Fill m_ekeyInfo vector with all permutations of dataType, including the 'E-KEY' |
| 63 | component of the name. This will be used in DataProducer::createContentKey to |
| 64 | send interests without reconstructing names every time. |
| 65 | */ |
| 66 | fixedPrefix.append(NAME_COMPONENT_READ); |
| 67 | while (!fixedDataType.empty()) { |
| 68 | Name nodeName = fixedPrefix; |
| 69 | nodeName.append(fixedDataType); |
| 70 | nodeName.append(NAME_COMPONENT_E_KEY); |
| 71 | |
| 72 | m_ekeyInfo[nodeName] = keyInfo; |
| 73 | fixedDataType = fixedDataType.getPrefix(-1); |
| 74 | } |
| 75 | fixedPrefix.append(dataType); |
| 76 | m_namespace = prefix; |
| 77 | m_namespace.append(NAME_COMPONENT_SAMPLE); |
| 78 | m_namespace.append(dataType); |
| 79 | } |
| 80 | |
| 81 | Name |
| 82 | Producer::createContentKey(const system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 83 | const ProducerEKeyCallback& callback, |
| 84 | const ErrorCallBack& errorCallback) |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 85 | { |
| 86 | const system_clock::TimePoint hourSlot = getRoundedTimeslot(timeslot); |
| 87 | |
| 88 | // Create content key name. |
| 89 | Name contentKeyName = m_namespace; |
| 90 | contentKeyName.append(NAME_COMPONENT_C_KEY); |
| 91 | contentKeyName.append(time::toIsoString(hourSlot)); |
| 92 | |
| 93 | Buffer contentKeyBits; |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 94 | |
| 95 | // Check if we have created the content key before. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 96 | if (m_db.hasContentKey(timeslot)) { |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 97 | // We have created the content key, return its name directly. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 98 | return contentKeyName; |
| 99 | } |
| 100 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 101 | // We haven't created the content key, create one and add it into the database. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 102 | RandomNumberGenerator rng; |
| 103 | AesKeyParams aesParams(128); |
| 104 | contentKeyBits = algo::Aes::generateKey(rng, aesParams).getKeyBits(); |
| 105 | m_db.addContentKey(timeslot, contentKeyBits); |
| 106 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 107 | // Now we need to retrieve the E-KEYs for content key encryption. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 108 | uint64_t timeCount = toUnixTimestamp(timeslot).count(); |
| 109 | m_keyRequests.insert({timeCount, KeyRequest(m_ekeyInfo.size())}); |
| 110 | KeyRequest& keyRequest = m_keyRequests.at(timeCount); |
| 111 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 112 | // Check if current E-KEYs can cover the content key. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 113 | Exclude timeRange; |
| 114 | timeRange.excludeAfter(name::Component(time::toIsoString(timeslot))); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 115 | std::unordered_map<Name, KeyInfo>::iterator it; |
| 116 | for (it = m_ekeyInfo.begin(); it != m_ekeyInfo.end(); ++it) { |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 117 | // for each current E-KEY |
| 118 | if (timeslot < it->second.beginTimeslot || timeslot >= it->second.endTimeslot) { |
| 119 | // current E-KEY cannot cover the content key, retrieve one. |
| 120 | keyRequest.repeatAttempts[it->first] = 0; |
| 121 | sendKeyInterest(Interest(it->first).setExclude(timeRange).setChildSelector(1), |
| 122 | timeslot, callback, errorCallback); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 123 | } |
| 124 | else { |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 125 | // current E-KEY can cover the content key, encrypt the content key directly. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 126 | Name eKeyName(it->first); |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 127 | eKeyName.append(time::toIsoString(it->second.beginTimeslot)); |
| 128 | eKeyName.append(time::toIsoString(it->second.endTimeslot)); |
| 129 | encryptContentKey(it->second.keyBits, eKeyName, timeslot, callback, errorCallback); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | return contentKeyName; |
| 134 | } |
| 135 | |
| 136 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 137 | Producer::defaultErrorCallBack(const ErrorCode& code, const std::string& msg) |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 138 | { |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 139 | // do nothing. |
| 140 | } |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 141 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 142 | void |
| 143 | Producer::produce(Data& data, const system_clock::TimePoint& timeslot, |
| 144 | const uint8_t* content, size_t contentLen, |
| 145 | const ErrorCallBack& errorCallBack) |
| 146 | { |
| 147 | // Get a content key |
| 148 | Name contentKeyName = createContentKey(timeslot, nullptr, errorCallBack); |
| 149 | Buffer contentKey = m_db.getContentKey(timeslot); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 150 | |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 151 | // Produce data |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 152 | Name dataName = m_namespace; |
Yingdi Yu | 0c530b7 | 2016-03-20 18:19:14 -0700 | [diff] [blame] | 153 | dataName.append(time::toIsoString(timeslot)); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 154 | data.setName(dataName); |
| 155 | algo::EncryptParams params(tlv::AlgorithmAesCbc, 16); |
| 156 | algo::encryptData(data, content, contentLen, contentKeyName, |
| 157 | contentKey.buf(), contentKey.size(), params); |
| 158 | m_keychain.sign(data); |
| 159 | } |
| 160 | |
| 161 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 162 | Producer::sendKeyInterest(const Interest& interest, |
| 163 | const system_clock::TimePoint& timeslot, |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 164 | const ProducerEKeyCallback& callback, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 165 | const ErrorCallBack& errorCallback) |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 166 | { |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 167 | Interest request(interest); |
| 168 | if (m_keyRetrievalLink.getDelegations().size() > 0) { |
| 169 | request.setLink(m_keyRetrievalLink.wireEncode()); |
| 170 | } |
| 171 | m_face.expressInterest(request, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 172 | std::bind(&Producer::handleCoveringKey, this, _1, _2, |
| 173 | timeslot, callback, errorCallback), |
| 174 | std::bind(&Producer::handleNack, this, _1, _2, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 175 | timeslot, callback, errorCallback), |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 176 | std::bind(&Producer::handleTimeout, this, _1, |
| 177 | timeslot, callback, errorCallback)); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 181 | Producer::handleCoveringKey(const Interest& interest, const Data& data, |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 182 | const system_clock::TimePoint& timeslot, |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 183 | const ProducerEKeyCallback& callback, |
| 184 | const ErrorCallBack& errorCallback) |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 185 | { |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 186 | uint64_t timeCount = toUnixTimestamp(timeslot).count(); |
| 187 | KeyRequest& keyRequest = m_keyRequests.at(timeCount); |
| 188 | |
| 189 | Name interestName = interest.getName(); |
| 190 | Name keyName = data.getName(); |
| 191 | |
| 192 | system_clock::TimePoint begin = time::fromIsoString(keyName.get(START_TS_INDEX).toUri()); |
| 193 | system_clock::TimePoint end = time::fromIsoString(keyName.get(END_TS_INDEX).toUri()); |
| 194 | |
| 195 | if (timeslot >= end) { |
| 196 | // if received E-KEY covers some earlier period, try to retrieve an E-KEY covering later one. |
| 197 | keyRequest.repeatAttempts[interestName] = 0; |
| 198 | |
| 199 | Exclude timeRange = interest.getSelectors().getExclude(); |
| 200 | timeRange.excludeBefore(keyName.get(START_TS_INDEX)); |
| 201 | |
| 202 | sendKeyInterest(Interest(interestName).setExclude(timeRange).setChildSelector(1), |
| 203 | timeslot, callback, errorCallback); |
| 204 | } |
| 205 | else { |
| 206 | // if received E-KEY covers the content key, encrypt the content |
| 207 | Buffer encryptionKey(data.getContent().value(), data.getContent().value_size()); |
| 208 | // if everything is correct, save the E-KEY as the current key |
| 209 | if (encryptContentKey(encryptionKey, keyName, timeslot, callback, errorCallback)) { |
| 210 | m_ekeyInfo[interestName].beginTimeslot = begin; |
| 211 | m_ekeyInfo[interestName].endTimeslot = end; |
| 212 | m_ekeyInfo[interestName].keyBits = encryptionKey; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | Producer::handleTimeout(const Interest& interest, |
| 219 | const system_clock::TimePoint& timeslot, |
| 220 | const ProducerEKeyCallback& callback, |
| 221 | const ErrorCallBack& errorCallback) |
| 222 | { |
| 223 | uint64_t timeCount = toUnixTimestamp(timeslot).count(); |
| 224 | KeyRequest& keyRequest = m_keyRequests.at(timeCount); |
| 225 | |
| 226 | Name interestName = interest.getName(); |
| 227 | if (keyRequest.repeatAttempts[interestName] < m_maxRepeatAttempts) { |
| 228 | // increase retrial count |
| 229 | keyRequest.repeatAttempts[interestName]++; |
| 230 | sendKeyInterest(interest, timeslot, callback, errorCallback); |
| 231 | } |
| 232 | else { |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 233 | // treat eventual timeout as a NACK |
| 234 | handleNack(interest, lp::Nack(), timeslot, callback, errorCallback); |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | Producer::handleNack(const Interest& interest, |
| 240 | const lp::Nack& nack, |
| 241 | const system_clock::TimePoint& timeslot, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 242 | const ProducerEKeyCallback& callback, |
| 243 | const ErrorCallBack& errorCallback) |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 244 | { |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 245 | // we run out of options... |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 246 | uint64_t timeCount = toUnixTimestamp(timeslot).count(); |
| 247 | updateKeyRequest(m_keyRequests.at(timeCount), timeCount, callback); |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | Producer::updateKeyRequest(KeyRequest& keyRequest, uint64_t timeCount, |
| 252 | const ProducerEKeyCallback& callback) |
| 253 | { |
| 254 | keyRequest.interestCount--; |
| 255 | if (keyRequest.interestCount == 0 && callback) { |
| 256 | callback(keyRequest.encryptedKeys); |
| 257 | m_keyRequests.erase(timeCount); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | bool |
| 262 | Producer::encryptContentKey(const Buffer& encryptionKey, const Name& eKeyName, |
| 263 | const system_clock::TimePoint& timeslot, |
| 264 | const ProducerEKeyCallback& callback, |
| 265 | const ErrorCallBack& errorCallBack) |
| 266 | { |
| 267 | uint64_t timeCount = toUnixTimestamp(timeslot).count(); |
| 268 | KeyRequest& keyRequest = m_keyRequests.at(timeCount); |
| 269 | |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 270 | Name keyName = m_namespace; |
| 271 | keyName.append(NAME_COMPONENT_C_KEY); |
| 272 | keyName.append(time::toIsoString(getRoundedTimeslot(timeslot))); |
| 273 | |
| 274 | Buffer contentKey = m_db.getContentKey(timeslot); |
| 275 | |
| 276 | Data cKeyData; |
| 277 | cKeyData.setName(keyName); |
| 278 | algo::EncryptParams params(tlv::AlgorithmRsaOaep); |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 279 | try { |
| 280 | algo::encryptData(cKeyData, contentKey.buf(), contentKey.size(), eKeyName, |
| 281 | encryptionKey.buf(), encryptionKey.size(), params); |
| 282 | } |
| 283 | catch (algo::Error& e) { |
| 284 | errorCallBack(ErrorCode::EncryptionFailure, e.what()); |
| 285 | return false; |
| 286 | } |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 287 | m_keychain.sign(cKeyData); |
| 288 | keyRequest.encryptedKeys.push_back(cKeyData); |
Yingdi Yu | 79ce239 | 2016-03-10 10:21:55 -0800 | [diff] [blame] | 289 | updateKeyRequest(keyRequest, timeCount, callback); |
| 290 | return true; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | } // namespace gep |
Yingdi Yu | 266badb | 2016-03-09 18:58:27 -0800 | [diff] [blame] | 294 | } // namespace ndn |