All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
consumer.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_CONSUMER_HPP
24 #define NDN_CONSUMER_HPP
25 
26 #include <map>
27 #include "../data.hpp"
28 #include "../face.hpp"
29 #include "../security/key-chain.hpp"
30 #include "encrypt-error.hpp"
31 #include "encrypted-content.hpp"
32 #include "consumer-db.hpp"
33 
34 // Give friend access to the tests.
35 class TestConsumer_DecryptContent_Test;
36 
37 namespace ndn {
38 
44 class Consumer {
45 public:
68  Consumer
69  (Face* face, KeyChain* keyChain, const Name& groupName,
70  const Name& consumerName, const ptr_lib::shared_ptr<ConsumerDb>& database,
71  const Link& cKeyLink = getNO_LINK(), const Link& dKeyLink = getNO_LINK())
72  : impl_(new Impl
73  (face, keyChain, groupName, consumerName, database, cKeyLink, dKeyLink))
74  {
75  }
76 
77  typedef func_lib::function<void
78  (const ptr_lib::shared_ptr<Data>& contentData,
79  const Blob& result)> OnConsumeComplete;
80 
81  typedef func_lib::function<void(const Blob& decryptedBlob)> OnPlainText;
82 
102  void
103  consume
104  (const Name& contentName, const OnConsumeComplete& onConsumeComplete,
105  const EncryptError::OnError& onError, const Link& link = getNO_LINK())
106  {
107  impl_->consume(contentName, onConsumeComplete, onError, link);
108  }
109 
115  void
116  setGroup(const Name& groupName) { impl_->setGroup(groupName); }
117 
126  void
127  addDecryptionKey(const Name& keyName, const Blob& keyBlob)
128  {
129  impl_->addDecryptionKey(keyName, keyBlob);
130  }
131 
141  void
143  (const Data& data, const OnPlainText& onPlainText,
144  const EncryptError::OnError& onError)
145  {
146  impl_-> decryptContent(data, onPlainText, onError);
147  }
148 
149 private:
150  // Give friend access to the tests.
151  friend TestConsumer_DecryptContent_Test;
152 
157  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
158  public:
163  Impl
164  (Face* face, KeyChain* keyChain, const Name& groupName,
165  const Name& consumerName, const ptr_lib::shared_ptr<ConsumerDb>& database,
166  const Link& cKeyLink, const Link& dKeyLink);
167 
168  void
169  consume
170  (const Name& contentName, const OnConsumeComplete& onConsumeComplete,
171  const EncryptError::OnError& onError, const Link& link);
172 
173  void
174  setGroup(const Name& groupName) { groupName_ = groupName; }
175 
176  void
177  addDecryptionKey(const Name& keyName, const Blob& keyBlob);
178 
179  void
181  (const Data& data, const OnPlainText& onPlainText,
182  const EncryptError::OnError& onError);
183 
184  private:
185  // Give friend access to the tests.
186  friend TestConsumer_DecryptContent_Test;
187 
196  static void
197  decrypt
198  (const Blob& encryptedBlob, const Blob& keyBits,
199  const OnPlainText& onPlainText, const EncryptError::OnError& onError);
200 
209  static void
210  decryptEncryptedContent
211  (const EncryptedContent& encryptedContent, const Blob& keyBits,
212  const OnPlainText& onPlainText, const EncryptError::OnError& onError);
213 
221  void
222  decryptCKey
223  (const Data& cKeyData, const OnPlainText& onPlainText,
224  const EncryptError::OnError& onError);
225 
233  void
234  decryptDKey
235  (const Data& dKeyData, const OnPlainText& onPlainText,
236  const EncryptError::OnError& onError);
237 
246  Blob
247  getDecryptionKey(const Name& decryptionKeyName)
248  {
249  return database_->getKey(decryptionKeyName);
250  }
251 
268  void
269  sendInterest
270  (const ptr_lib::shared_ptr<const Interest>& interest, int nRetrials,
271  const ptr_lib::shared_ptr<Link>& link, const OnVerified& onVerified,
272  const EncryptError::OnError& onError);
273 
278  static void
279  onValidationFailed
280  (const ptr_lib::shared_ptr<Data>& data, const std::string& reason,
281  const EncryptError::OnError& onError);
282 
283  ptr_lib::shared_ptr<ConsumerDb> database_;
284  KeyChain* keyChain_;
285  Face* face_;
286  Name groupName_;
287  Name consumerName_;
288 
289  const ptr_lib::shared_ptr<Link> cKeyLink_;
290  // The map key is the C-KEY name. The value is the encoded key Blob.
291  std::map<Name, Blob> cKeyMap_;
292  const ptr_lib::shared_ptr<Link> dKeyLink_;
293  // The map key is the D-KEY name. The value is the encoded key Blob.
294  std::map<Name, Blob> dKeyMap_;
295  };
296 
302  static Link&
303  getNO_LINK()
304  {
305  if (!noLink_)
306  noLink_ = new Link();
307 
308  return *noLink_;
309  }
310 
311  ptr_lib::shared_ptr<Impl> impl_;
312  static Link* noLink_;
313 };
314 
315 }
316 
317 #endif
Definition: data.hpp:37
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
func_lib::function< void(ErrorCode errorCode, const std::string &message)> OnError
A method calls onError(errorCode, message) for an error.
Definition: encrypt-error.hpp:50
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data)> OnVerified
An OnVerified function object is used to pass a callback to verifyData to report a successful verific...
Definition: validation-request.hpp:33
KeyChain is the main class of the security library.
Definition: key-chain.hpp:53
A Consumer manages fetched group keys used to decrypt a data packet in the group-based encryption pro...
Definition: consumer.hpp:44
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
void setGroup(const Name &groupName)
Set the group name.
Definition: consumer.hpp:116
Consumer(Face *face, KeyChain *keyChain, const Name &groupName, const Name &consumerName, const ptr_lib::shared_ptr< ConsumerDb > &database, const Link &cKeyLink=getNO_LINK(), const Link &dKeyLink=getNO_LINK())
Create a Consumer to use the given ConsumerDb, Face and other values.
Definition: consumer.hpp:69
void addDecryptionKey(const Name &keyName, const Blob &keyBlob)
Add a new decryption key with keyName and keyBlob to the database.
Definition: consumer.hpp:127
void consume(const Name &contentName, const OnConsumeComplete &onConsumeComplete, const EncryptError::OnError &onError, const Link &link=getNO_LINK())
Express an Interest to fetch the content packet with contentName, and decrypt it, fetching keys as ne...
Definition: consumer.hpp:104
void decryptContent(const Data &data, const OnPlainText &onPlainText, const EncryptError::OnError &onError)
A utility method to decrypt the data packet, retrieving the C-KEY Data from the network if necessary...
Definition: consumer.hpp:143