blob: a8ea7c3355f56706fafe294c4410355e9bdbe104 [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
26#include <ndn-cxx/face.hpp>
27
Davide Pesaventobde084f2022-04-17 00:21:35 -040028namespace ndn::nac {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040029
30/**
31 * @brief NAC Decryptor
32 *
33 * Encryptor decrypts (asynchronous operation, contingent on successful retrieval of CK data,
34 * KDK, and decryption of both) the supplied ``EncryptedContent`` element.
35 */
36class Decryptor
37{
38public:
39 using DecryptSuccessCallback = std::function<void(ConstBufferPtr)>;
40
41private:
42 struct ContentKey
43 {
44 bool isRetrieved = false;
45 Buffer bits;
Davide Pesaventobde084f2022-04-17 00:21:35 -040046 std::optional<PendingInterestHandle> pendingInterest;
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040047
48 struct PendingDecrypt
49 {
50 EncryptedContent encryptedContent;
51 DecryptSuccessCallback onSuccess;
52 ErrorCallback onFailure;
53 };
54 std::list<PendingDecrypt> pendingDecrypts;
55 };
56
57 using ContentKeys = std::map<Name, ContentKey>;
58
59public:
60 /**
61 * @brief Constructor
62 * @param credentialsKey Credentials key to be used to retrieve and decrypt KDK
63 * @param validator Validation policy to ensure validity of KDK and CK
64 * @param keyChain KeyChain
65 * @param face Face that will be used to fetch CK and KDK
66 */
67 Decryptor(const Key& credentialsKey, Validator& validator, KeyChain& keyChain, Face& face);
68
69 ~Decryptor();
70
71 /**
72 * @brief Asynchronously decrypt @p encryptedContent
73 */
74 void
75 decrypt(const Block& encryptedContent, const DecryptSuccessCallback& onSuccess, const ErrorCallback& onFailure);
76
77private:
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
93 /**
94 * @brief Synchronously decrypt (assume CK exists)
95 */
96 void
97 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
109 // @TODO add some expiration, so they are not stored forever
110 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