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 |
| 54 | * @param dbDir The path to database storing decryption key |
| 55 | */ |
| 56 | Consumer(Face& face, const Name& groupName, const Name& consumerName, const std::string& dbDir); |
| 57 | |
| 58 | /** |
| 59 | * @brief Send out the Interest packet to fetch content packet with @p dataName. |
| 60 | * |
| 61 | * @param consumptionCallBack The callback when requested data is decrypted |
| 62 | * @param errorCallBack The callback when error happens in consumption |
| 63 | */ |
| 64 | void |
| 65 | consume(const Name& dataName, |
| 66 | const ConsumptionCallBack& consumptionCallBack, |
| 67 | const ErrorCallBack& errorCallBack); |
| 68 | |
| 69 | /** |
| 70 | * @brief Set the group name to @p groupName. |
| 71 | */ |
| 72 | void |
| 73 | setGroup(const Name& groupName); |
| 74 | |
| 75 | /** |
| 76 | * @brief Add new decryption key with @p keyName and @p keyBuf. |
| 77 | */ |
| 78 | void |
| 79 | addDecryptionKey(const Name& keyName, const Buffer& keyBuf); |
| 80 | |
| 81 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 82 | |
| 83 | /** |
| 84 | * @brief Decrypt @p encryptedBlock using @p keyBits |
| 85 | * |
| 86 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack. |
| 87 | */ |
| 88 | void |
| 89 | decrypt(const Block& encryptedBlock, |
| 90 | const Buffer& keyBits, |
| 91 | const PlainTextCallBack& plainTextCallBack, |
| 92 | const ErrorCallBack& errorCallBack); |
| 93 | |
| 94 | /** |
| 95 | * @brief Decrypt @p data. |
| 96 | * |
| 97 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack. |
| 98 | */ |
| 99 | void |
| 100 | decryptContent(const Data& data, |
| 101 | const PlainTextCallBack& plainTextCallBack, |
| 102 | const ErrorCallBack& errorCallBack); |
| 103 | |
| 104 | /** |
| 105 | * @brief Decrypt @p cKeyData. |
| 106 | * |
| 107 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack. |
| 108 | */ |
| 109 | void |
| 110 | decryptCKey(const Data& cKeyData, |
| 111 | const PlainTextCallBack& plainTextCallBack, |
| 112 | const ErrorCallBack& errorCallBack); |
| 113 | |
| 114 | /** |
| 115 | * @brief Decrypt @p dKeyData. |
| 116 | * |
| 117 | * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack. |
| 118 | */ |
| 119 | void |
| 120 | decryptDKey(const Data& dKeyData, |
| 121 | const PlainTextCallBack& plainTextCallBack, |
| 122 | const ErrorCallBack& errorCallBack); |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * @brief Get the buffer of decryption key with @p decryptionKeyName from database. |
| 127 | * |
| 128 | * @return Null buffer when there is no decryption key with @p decryptionKeyName. |
| 129 | */ |
| 130 | const Buffer |
| 131 | getDecryptionKey(const Name& decryptionKeyName); |
| 132 | |
| 133 | private: |
| 134 | ConsumerDB m_db; |
| 135 | unique_ptr<Validator> m_validator; |
| 136 | Face& m_face; |
| 137 | Name m_groupName; |
| 138 | Name m_consumerName; |
| 139 | |
| 140 | std::map<Name, Buffer> m_cKeyMap; |
| 141 | std::map<Name, Buffer> m_dKeyMap; |
| 142 | }; |
| 143 | |
| 144 | } // namespace gep |
| 145 | } // namespace ndn |
| 146 | |
| 147 | #endif // NDN_GEP_CONSUMER_HPP |