Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [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 |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 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 | * |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 19 | * @author Zhiyi Zhang <zhiyi@cs.ucla.edu> |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 20 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #ifndef NDN_GEP_CONSUMER_HPP |
| 24 | #define NDN_GEP_CONSUMER_HPP |
| 25 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 26 | #include "common.hpp" |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 27 | #include "consumer-db.hpp" |
| 28 | #include "error-code.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 29 | #include "algo/aes.hpp" |
| 30 | #include "algo/rsa.hpp" |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 31 | #include <ndn-cxx/face.hpp> |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 32 | #include <ndn-cxx/security/validator-null.hpp> |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | namespace gep { |
| 36 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 37 | typedef function<void(const Data&, const Buffer&)> ConsumptionCallBack; |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * @brief Consumer in group-based encryption protocol |
| 41 | */ |
| 42 | class Consumer |
| 43 | { |
| 44 | private: |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 45 | typedef function<void(const Buffer&)> PlainTextCallBack; |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 46 | |
| 47 | public: |
| 48 | /** |
| 49 | * @brief Create a consumer instance |
| 50 | * |
| 51 | * @param face The face used for key fetching |
| 52 | * @param groupName The reading group name that the consumer belongs to |
| 53 | * @param consumerName The identity of the consumer |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 54 | * @param dbPath The path to database storing decryption key |
| 55 | * @param cKeyLink The link object for C-KEY retrieval |
| 56 | * @param dKeyLink The link object for D-KEY retrieval |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 57 | */ |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 58 | Consumer(Face& face, |
| 59 | const Name& groupName, |
| 60 | const Name& consumerName, |
| 61 | const std::string& dbPath, |
| 62 | const Link& cKeyLink = NO_LINK, |
| 63 | const Link& dKeyLink = NO_LINK); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @brief Send out the Interest packet to fetch content packet with @p dataName. |
| 67 | * |
Alexander Afanasyev | 9d7f8fe | 2016-08-05 11:28:06 -0700 | [diff] [blame] | 68 | * @param dataName name of the data packet to fetch |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 69 | * @param consumptionCallBack The callback when requested data is decrypted |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 70 | * @param errorCallback The callback when error happens in consumption |
| 71 | * @param link The link object for data retrieval |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 72 | */ |
| 73 | void |
| 74 | consume(const Name& dataName, |
| 75 | const ConsumptionCallBack& consumptionCallBack, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 76 | const ErrorCallBack& errorCallback, |
| 77 | const Link& link = NO_LINK); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief Set the group name to @p groupName. |
| 81 | */ |
| 82 | void |
| 83 | setGroup(const Name& groupName); |
| 84 | |
| 85 | /** |
| 86 | * @brief Add new decryption key with @p keyName and @p keyBuf. |
| 87 | */ |
| 88 | void |
| 89 | addDecryptionKey(const Name& keyName, const Buffer& keyBuf); |
| 90 | |
| 91 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 92 | /** |
| 93 | * @brief Decrypt @p encryptedBlock using @p keyBits |
| 94 | * |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 95 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback. |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 96 | */ |
| 97 | void |
| 98 | decrypt(const Block& encryptedBlock, |
| 99 | const Buffer& keyBits, |
| 100 | const PlainTextCallBack& plainTextCallBack, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 101 | const ErrorCallBack& errorCallback); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * @brief Decrypt @p data. |
| 105 | * |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 106 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback. |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 107 | */ |
| 108 | void |
| 109 | decryptContent(const Data& data, |
| 110 | const PlainTextCallBack& plainTextCallBack, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 111 | const ErrorCallBack& errorCallback); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * @brief Decrypt @p cKeyData. |
| 115 | * |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 116 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback. |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 117 | */ |
| 118 | void |
| 119 | decryptCKey(const Data& cKeyData, |
| 120 | const PlainTextCallBack& plainTextCallBack, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 121 | const ErrorCallBack& errorCallback); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * @brief Decrypt @p dKeyData. |
| 125 | * |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 126 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallback. |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 127 | */ |
| 128 | void |
| 129 | decryptDKey(const Data& dKeyData, |
| 130 | const PlainTextCallBack& plainTextCallBack, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 131 | const ErrorCallBack& errorCallback); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 132 | |
| 133 | |
| 134 | /** |
| 135 | * @brief Get the buffer of decryption key with @p decryptionKeyName from database. |
| 136 | * |
| 137 | * @return Null buffer when there is no decryption key with @p decryptionKeyName. |
| 138 | */ |
| 139 | const Buffer |
| 140 | getDecryptionKey(const Name& decryptionKeyName); |
| 141 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 142 | /** |
| 143 | * @brief Helper method for sending interest |
| 144 | * |
| 145 | * This method prepare the three callbacks: DataCallbak, NackCallback, TimeoutCallback |
| 146 | * for the @p interest. |
| 147 | * |
| 148 | * @param interest The interes to send out |
| 149 | * @param nRetrials The number of retrials left (if timeout) |
| 150 | * @param link The link object (used when NACK is received) |
| 151 | * @param validationCallback The callback when data is validated |
| 152 | * @param errorCallback The callback when error happens |
| 153 | */ |
| 154 | void |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 155 | sendInterest(const Interest& interest, |
| 156 | int nRetrials, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 157 | const Link& link, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 158 | const DataValidationSuccessCallback& validationCallback, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 159 | const ErrorCallBack& errorCallback); |
| 160 | |
| 161 | /** |
| 162 | * @brief Callback to handle NACK |
| 163 | * |
| 164 | * This method will check if there is another delegation to use. Otherwise report error |
| 165 | * |
| 166 | * @param interest The interes got NACKed |
| 167 | * @param nack The nack object |
| 168 | * @param link The link object (used when NACK is received) |
| 169 | * @param delegationIndex Current selected delegation |
| 170 | * @param validationCallback The callback when data is validated |
| 171 | * @param errorCallback The callback when error happens |
| 172 | */ |
| 173 | void |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 174 | handleNack(const Interest& interest, |
| 175 | const lp::Nack& nack, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 176 | const Link& link, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 177 | const DataValidationSuccessCallback& validationCallback, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 178 | const ErrorCallBack& errorCallback); |
| 179 | |
| 180 | /** |
| 181 | * @brief Callback to handle timeout |
| 182 | * |
| 183 | * This method will check if a retrial is allowed. Otherwise retreat the interest as NACKed |
| 184 | * |
| 185 | * @param interest The interes timed out |
| 186 | * @param nRetrials The number of retrials left |
| 187 | * @param link The link object (used when NACK is received) |
| 188 | * @param delegationIndex Current selected delegation |
| 189 | * @param validationCallback The callback when data is validated |
| 190 | * @param errorCallback The callback when error happens |
| 191 | */ |
| 192 | void |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 193 | handleTimeout(const Interest& interest, |
| 194 | int nRetrials, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 195 | const Link& link, |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 196 | const DataValidationSuccessCallback& validationCallback, |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 197 | const ErrorCallBack& errorCallback); |
| 198 | |
| 199 | public: |
| 200 | static const Link NO_LINK; |
| 201 | |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 202 | private: |
| 203 | ConsumerDB m_db; |
| 204 | unique_ptr<Validator> m_validator; |
| 205 | Face& m_face; |
| 206 | Name m_groupName; |
| 207 | Name m_consumerName; |
| 208 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 209 | Link m_cKeyLink; |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 210 | std::map<Name, Buffer> m_cKeyMap; |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 211 | Link m_dKeyLink; |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 212 | std::map<Name, Buffer> m_dKeyMap; |
| 213 | }; |
| 214 | |
| 215 | } // namespace gep |
| 216 | } // namespace ndn |
| 217 | |
| 218 | #endif // NDN_GEP_CONSUMER_HPP |