decryptor.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California
4  *
5  * NAC library is free software: you can redistribute it and/or modify it under the
6  * terms of the GNU Lesser General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later version.
8  *
9  * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12  *
13  * You should have received copies of the GNU General Public License and GNU Lesser
14  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * See AUTHORS.md for complete list of NAC library authors and contributors.
18  */
19 
20 #ifndef NDN_NAC_DECRYPTOR_HPP
21 #define NDN_NAC_DECRYPTOR_HPP
22 
23 #include "common.hpp"
24 #include "encrypted-content.hpp"
25 
26 #include <list>
27 #include <map>
28 
29 namespace ndn::nac {
30 
37 class Decryptor
38 {
39 public:
40  using DecryptSuccessCallback = std::function<void(ConstBufferPtr)>;
41 
49  Decryptor(const Key& credentialsKey, Validator& validator, KeyChain& keyChain, Face& face);
50 
51  ~Decryptor();
52 
56  void
57  decrypt(const Block& encryptedContent,
58  const DecryptSuccessCallback& onSuccess, const ErrorCallback& onFailure);
59 
60 private:
61  struct ContentKey
62  {
63  bool isRetrieved = false;
64  Buffer bits;
65  std::optional<PendingInterestHandle> pendingInterest;
66 
68  {
72  };
73  std::list<PendingDecrypt> pendingDecrypts;
74  };
75 
76  using ContentKeys = std::map<Name, ContentKey>;
77 
78  void
79  fetchCk(ContentKeys::iterator ck, const ErrorCallback& onFailure, size_t nTriesLeft);
80 
81  void
82  fetchKdk(ContentKeys::iterator ck, const Name& kdkPrefix, const Data& ckData,
83  const ErrorCallback& onFailure, size_t nTriesLeft);
84 
85  bool
86  decryptAndImportKdk(const Data& kdkData, const ErrorCallback& onFailure);
87 
88  void
89  decryptCkAndProcessPendingDecrypts(ContentKeys::iterator ck, const Data& ckData,
90  const Name& kdkKeyName/* local keyChain name for KDK key*/,
91  const ErrorCallback& onFailure);
92 
96  static void
97  doDecrypt(const EncryptedContent& encryptedContent, const Buffer& ckBits,
98  const DecryptSuccessCallback& onSuccess,
99  const ErrorCallback& onFailure);
100 
101 private:
102  Key m_credentialsKey;
103  // Validator& m_validator;
104  Face& m_face;
105  KeyChain& m_keyChain; // external keychain with access credentials
106  KeyChain m_internalKeyChain; // internal in-memory keychain for temporarily storing KDKs
107 
108  // a set of Content Keys
109  // TODO: add some expiration, so they are not stored forever
110  ContentKeys m_cks;
111 };
112 
113 } // namespace ndn::nac
114 
115 #endif // NDN_NAC_DECRYPTOR_HPP
NAC Decryptor.
Definition: decryptor.hpp:38
Decryptor(const Key &credentialsKey, Validator &validator, KeyChain &keyChain, Face &face)
Constructor.
Definition: decryptor.cpp:36
void decrypt(const Block &encryptedContent, const DecryptSuccessCallback &onSuccess, const ErrorCallback &onFailure)
Asynchronously decrypt encryptedContent.
Definition: decryptor.cpp:59
std::function< void(ConstBufferPtr)> DecryptSuccessCallback
Definition: decryptor.hpp:40
std::function< void(const ErrorCode &, const std::string &)> ErrorCallback
Definition: common.hpp:117