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