blob: 8e6486f24be64d0cfb0ad240ecaf6056c1737e86 [file] [log] [blame]
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod51d9602019-07-20 23:33:06 -04002/*
Davide Pesaventobde084f2022-04-17 00:21:35 -04003 * Copyright (c) 2014-2022, Regents of the University of California
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -04004 *
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
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040026#include <list>
27#include <map>
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040028
Davide Pesaventobde084f2022-04-17 00:21:35 -040029namespace ndn::nac {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040030
31/**
32 * @brief NAC Decryptor
33 *
34 * Encryptor decrypts (asynchronous operation, contingent on successful retrieval of CK data,
35 * KDK, and decryption of both) the supplied ``EncryptedContent`` element.
36 */
37class Decryptor
38{
39public:
40 using DecryptSuccessCallback = std::function<void(ConstBufferPtr)>;
41
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040042 /**
43 * @brief Constructor
44 * @param credentialsKey Credentials key to be used to retrieve and decrypt KDK
45 * @param validator Validation policy to ensure validity of KDK and CK
46 * @param keyChain KeyChain
47 * @param face Face that will be used to fetch CK and KDK
48 */
49 Decryptor(const Key& credentialsKey, Validator& validator, KeyChain& keyChain, Face& face);
50
51 ~Decryptor();
52
53 /**
54 * @brief Asynchronously decrypt @p encryptedContent
55 */
56 void
57 decrypt(const Block& encryptedContent,
58 const DecryptSuccessCallback& onSuccess, const ErrorCallback& onFailure);
59
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040060private:
61 struct ContentKey
62 {
63 bool isRetrieved = false;
64 Buffer bits;
Davide Pesaventobde084f2022-04-17 00:21:35 -040065 std::optional<PendingInterestHandle> pendingInterest;
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040066
67 struct PendingDecrypt
68 {
69 EncryptedContent encryptedContent;
70 DecryptSuccessCallback onSuccess;
71 ErrorCallback onFailure;
72 };
73 std::list<PendingDecrypt> pendingDecrypts;
74 };
75
76 using ContentKeys = std::map<Name, ContentKey>;
77
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040078 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
93 /**
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040094 * @brief Synchronously decrypt
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040095 */
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040096 static void
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040097 doDecrypt(const EncryptedContent& encryptedContent, const Buffer& ckBits,
98 const DecryptSuccessCallback& onSuccess,
99 const ErrorCallback& onFailure);
100
101private:
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
Davide Pesavento2e5b7b12022-09-19 23:30:44 -0400109 // TODO: add some expiration, so they are not stored forever
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400110 ContentKeys m_cks;
111};
112
Davide Pesaventobde084f2022-04-17 00:21:35 -0400113} // namespace ndn::nac
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400114
115#endif // NDN_NAC_DECRYPTOR_HPP