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:
60  Consumer
61  (Face* face, KeyChain* keyChain, const Name& groupName,
62  const Name& consumerName, const ptr_lib::shared_ptr<ConsumerDb>& database)
63  : impl_(new Impl(face, keyChain, groupName, consumerName, database))
64  {
65  }
66 
67  typedef func_lib::function<void
68  (const ptr_lib::shared_ptr<Data>& contentData,
69  const Blob& result)> OnConsumeComplete;
70 
86  void
87  consume
88  (const Name& contentName, const OnConsumeComplete& onConsumeComplete,
89  const EncryptError::OnError& onError)
90  {
91  impl_->consume(contentName, onConsumeComplete, onError);
92  }
93 
99  void
100  setGroup(const Name& groupName) { impl_->setGroup(groupName); }
101 
110  void
111  addDecryptionKey(const Name& keyName, const Blob& keyBlob)
112  {
113  impl_->addDecryptionKey(keyName, keyBlob);
114  }
115 
116 private:
117  // Give friend access to the tests.
118  friend TestConsumer_DecryptContent_Test;
119 
124  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
125  public:
130  Impl
131  (Face* face, KeyChain* keyChain, const Name& groupName,
132  const Name& consumerName, const ptr_lib::shared_ptr<ConsumerDb>& database);
133 
134  void
135  consume
136  (const Name& contentName, const OnConsumeComplete& onConsumeComplete,
137  const EncryptError::OnError& onError);
138 
139  void
140  setGroup(const Name& groupName) { groupName_ = groupName; }
141 
142  void
143  addDecryptionKey(const Name& keyName, const Blob& keyBlob);
144 
145  private:
146  // Give friend access to the tests.
147  friend TestConsumer_DecryptContent_Test;
148 
149  typedef func_lib::function<void(const Blob& decryptedBlob)> OnPlainText;
150 
159  static void
160  decrypt
161  (const Blob& encryptedBlob, const Blob& keyBits,
162  const OnPlainText& onPlainText, const EncryptError::OnError& onError);
163 
172  static void
173  decryptEncryptedContent
174  (const EncryptedContent& encryptedContent, const Blob& keyBits,
175  const OnPlainText& onPlainText, const EncryptError::OnError& onError);
176 
184  void
185  decryptContent
186  (const Data& data, const OnPlainText& onPlainText,
187  const EncryptError::OnError& onError);
188 
196  void
197  decryptCKey
198  (const Data& cKeyData, const OnPlainText& onPlainText,
199  const EncryptError::OnError& onError);
200 
208  void
209  decryptDKey
210  (const Data& dKeyData, const OnPlainText& onPlainText,
211  const EncryptError::OnError& onError);
212 
221  Blob
222  getDecryptionKey(const Name& decryptionKeyName)
223  {
224  return database_->getKey(decryptionKeyName);
225  }
226 
231  static void
232  onValidationFailed
233  (const ptr_lib::shared_ptr<Data>& data, const std::string& reason,
234  const EncryptError::OnError& onError);
235 
240  static void
241  onFinalTimeout
242  (const ptr_lib::shared_ptr<const Interest>& interest,
243  const EncryptError::OnError& onError);
244 
245  ptr_lib::shared_ptr<ConsumerDb> database_;
246  KeyChain* keyChain_;
247  Face* face_;
248  Name groupName_;
249  Name consumerName_;
250  // The map key is the C-KEY name. The value is the encoded key Blob.
251  std::map<Name, Blob> cKeyMap_;
252  // The map key is the D-KEY name. The value is the encoded key Blob.
253  std::map<Name, Blob> dKeyMap_;
254  };
255 
256  ptr_lib::shared_ptr<Impl> impl_;
257 };
258 
259 }
260 
261 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
Consumer(Face *face, KeyChain *keyChain, const Name &groupName, const Name &consumerName, const ptr_lib::shared_ptr< ConsumerDb > &database)
Create a Consumer to use the given ConsumerDb, Face and other values.
Definition: consumer.hpp:61
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:49
KeyChain is the main class of the security library.
Definition: key-chain.hpp:45
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.
Definition: blob.hpp:42
void consume(const Name &contentName, const OnConsumeComplete &onConsumeComplete, const EncryptError::OnError &onError)
Express an Interest to fetch the content packet with contentName, and decrypt it, fetching keys as ne...
Definition: consumer.hpp:88
void setGroup(const Name &groupName)
Set the group name.
Definition: consumer.hpp:100
void addDecryptionKey(const Name &keyName, const Blob &keyBlob)
Add a new decryption key with keyName and keyBlob to the database.
Definition: consumer.hpp:111