blob: 40a5c115c0b836f45f3096f4895fcb7ec91a82a1 [file] [log] [blame]
Zhiyi Zhang5f133622015-10-17 08:49:54 +08001/* -*- 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
34namespace ndn {
35namespace gep {
36
37typedef function<void (const Data&, const Buffer&)> ConsumptionCallBack;
38
39/**
40 * @brief Consumer in group-based encryption protocol
41 */
42class Consumer
43{
44private:
45 typedef function<void (const Buffer&)> PlainTextCallBack;
46
47public:
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 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070061 * @param dataName name of the data packet to fetch
Zhiyi Zhang5f133622015-10-17 08:49:54 +080062 * @param consumptionCallBack The callback when requested data is decrypted
63 * @param errorCallBack The callback when error happens in consumption
64 */
65 void
66 consume(const Name& dataName,
67 const ConsumptionCallBack& consumptionCallBack,
68 const ErrorCallBack& errorCallBack);
69
70 /**
71 * @brief Set the group name to @p groupName.
72 */
73 void
74 setGroup(const Name& groupName);
75
76 /**
77 * @brief Add new decryption key with @p keyName and @p keyBuf.
78 */
79 void
80 addDecryptionKey(const Name& keyName, const Buffer& keyBuf);
81
82PUBLIC_WITH_TESTS_ELSE_PRIVATE:
83
84 /**
85 * @brief Decrypt @p encryptedBlock using @p keyBits
86 *
87 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack.
88 */
89 void
90 decrypt(const Block& encryptedBlock,
91 const Buffer& keyBits,
92 const PlainTextCallBack& plainTextCallBack,
93 const ErrorCallBack& errorCallBack);
94
95 /**
96 * @brief Decrypt @p data.
97 *
98 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack.
99 */
100 void
101 decryptContent(const Data& data,
102 const PlainTextCallBack& plainTextCallBack,
103 const ErrorCallBack& errorCallBack);
104
105 /**
106 * @brief Decrypt @p cKeyData.
107 *
108 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack.
109 */
110 void
111 decryptCKey(const Data& cKeyData,
112 const PlainTextCallBack& plainTextCallBack,
113 const ErrorCallBack& errorCallBack);
114
115 /**
116 * @brief Decrypt @p dKeyData.
117 *
118 * Invoke @p plainTextCallBack when block is decrypted, otherwise @p errorCallBack.
119 */
120 void
121 decryptDKey(const Data& dKeyData,
122 const PlainTextCallBack& plainTextCallBack,
123 const ErrorCallBack& errorCallBack);
124
125
126 /**
127 * @brief Get the buffer of decryption key with @p decryptionKeyName from database.
128 *
129 * @return Null buffer when there is no decryption key with @p decryptionKeyName.
130 */
131 const Buffer
132 getDecryptionKey(const Name& decryptionKeyName);
133
134private:
135 ConsumerDB m_db;
136 unique_ptr<Validator> m_validator;
137 Face& m_face;
138 Name m_groupName;
139 Name m_consumerName;
140
141 std::map<Name, Buffer> m_cKeyMap;
142 std::map<Name, Buffer> m_dKeyMap;
143};
144
145} // namespace gep
146} // namespace ndn
147
148#endif // NDN_GEP_CONSUMER_HPP