blob: d0d1415c21b1e6052b179dc7a20e69ef20e7bc97 [file] [log] [blame]
Alexander Afanasyev1a21e102018-06-13 20:33:21 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento27680ae2019-04-06 19:40:01 -04002/*
Davide Pesavento714dba02022-03-17 20:46:28 -04003 * Copyright (c) 2014-2022, Regents of the University of California
Alexander Afanasyev1a21e102018-06-13 20:33:21 -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_ENCRYPTOR_HPP
21#define NDN_NAC_ENCRYPTOR_HPP
22
23#include "common.hpp"
24#include "encrypted-content.hpp"
25
Davide Pesaventobde084f2022-04-17 00:21:35 -040026namespace ndn::nac {
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040027
28/**
29 * @brief NAC Encryptor
30 *
Davide Pesaventobde084f2022-04-17 00:21:35 -040031 * Encryptor encrypts the requested content and returns an EncryptedContent element.
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040032 */
33class Encryptor
34{
35public:
36 /**
37 * @param accessPrefix NAC prefix to fetch KEK (e.g., /access/prefix/NAC/data/subset)
38 * @param ckPrefix Prefix under which Content Keys will be generated
39 * (each will have unique version appended)
40 * @param ckDataSigningInfo SigningInfo parameters to sign CK Data
41 * @param onFailure Callback to notify application of a failure to create CK data
Alexander Afanasyevda366d82018-06-29 18:18:02 -040042 * (failed to fetch KEK, failed to encrypt with KEK, etc.).
43 * Note that Encryptor will continue trying to retrieve KEK until success
44 * (each attempt separated by `RETRY_DELAY_KEK_RETRIEVAL`) and @p onFailure
45 * may be called multiple times.
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040046 * @param validator Validation policy to ensure correctness of KEK
47 * @param keyChain KeyChain
48 * @param face Face that will be used to fetch KEK and publish CK data
49 */
50 Encryptor(const Name& accessPrefix,
51 const Name& ckPrefix, SigningInfo ckDataSigningInfo,
52 const ErrorCallback& onFailure,
53 Validator& validator, KeyChain& keyChain, Face& face);
54
55 ~Encryptor();
56
57 /**
Davide Pesavento714dba02022-03-17 20:46:28 -040058 * @brief Synchronously encrypt supplied data
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040059 *
60 * If KEK has not been fetched already, this method will trigger async fetching of it.
61 * After KEK successfully fetched, CK data will be automatically published.
62 *
63 * @todo For now, CK is being published in InMemoryStorage and can be fetched only while
64 * Encryptor instance is alive.
65 *
66 * The actual encryption is done synchronously, but the exact KDK name is not known
67 * until KEK is fetched.
68 *
69 * Note that if the KDK name is already known, this method will call onReady right away.
70 *
71 * @return Encrypted content
72 */
73 EncryptedContent
Davide Pesavento714dba02022-03-17 20:46:28 -040074 encrypt(span<const uint8_t> data);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040075
76 /**
77 * @brief Create a new content key and publish the corresponding CK data
78 *
79 * @todo Ensure that CK data packet for the old CK is published, when CK updated
80 * before KEK fetched
81 */
82 void
Alexander Afanasyevda366d82018-06-29 18:18:02 -040083 regenerateCk();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040084
85public: // accessor interface for published data packets
Davide Pesavento714dba02022-03-17 20:46:28 -040086 /**
87 * @return number of packets stored in in-memory storage
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040088 */
89 size_t
90 size() const
91 {
92 return m_ims.size();
93 }
94
Davide Pesavento714dba02022-03-17 20:46:28 -040095 /**
96 * @brief Returns begin iterator of the in-memory storage ordered by name with digest
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040097 *
Davide Pesavento714dba02022-03-17 20:46:28 -040098 * @return const_iterator pointing to the beginning of m_cache
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040099 */
100 InMemoryStorage::const_iterator
101 begin() const
102 {
103 return m_ims.begin();
104 }
105
Davide Pesavento714dba02022-03-17 20:46:28 -0400106 /**
107 * @brief Returns end iterator of the in-memory storage ordered by name with digest
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400108 *
Davide Pesavento714dba02022-03-17 20:46:28 -0400109 * @return const_iterator pointing to the end of m_cache
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400110 */
111 InMemoryStorage::const_iterator
112 end() const
113 {
114 return m_ims.end();
115 }
116
117private:
118 void
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400119 retryFetchingKek();
120
121 void
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400122 fetchKekAndPublishCkData(const std::function<void()>& onReady,
123 const ErrorCallback& onFailure,
124 size_t nTriesLeft);
125
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400126 bool
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400127 makeAndPublishCkData(const ErrorCallback& onFailure);
128
Davide Pesavento27680ae2019-04-06 19:40:01 -0400129NAC_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400130 Name m_accessPrefix;
131 Name m_ckPrefix;
132 Name m_ckName;
133 Buffer m_ckBits;
134 SigningInfo m_ckDataSigningInfo;
135
136 bool m_isKekRetrievalInProgress;
Davide Pesaventobde084f2022-04-17 00:21:35 -0400137 std::optional<Data> m_kek;
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400138 ErrorCallback m_onFailure;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400139
140 InMemoryStoragePersistent m_ims; // for encrypted CKs
Davide Pesaventod51d9602019-07-20 23:33:06 -0400141 ScopedRegisteredPrefixHandle m_ckReg;
142 PendingInterestHandle m_kekPendingInterest;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400143
144 KeyChain& m_keyChain;
145 Face& m_face;
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400146 Scheduler m_scheduler;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400147};
148
Davide Pesaventobde084f2022-04-17 00:21:35 -0400149} // namespace ndn::nac
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400150
151#endif // NDN_NAC_ENCRYPTOR_HPP