Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 27680ae | 2019-04-06 19:40:01 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 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_ENCRYPTOR_HPP |
| 21 | #define NDN_NAC_ENCRYPTOR_HPP |
| 22 | |
| 23 | #include "common.hpp" |
| 24 | #include "encrypted-content.hpp" |
| 25 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 26 | namespace ndn::nac { |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * @brief NAC Encryptor |
| 30 | * |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 31 | * Encryptor encrypts the requested content and returns an EncryptedContent element. |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 32 | */ |
| 33 | class Encryptor |
| 34 | { |
| 35 | public: |
| 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 Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 42 | * (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 Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 46 | * @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 Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 58 | * @brief Synchronously encrypt supplied data |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 59 | * |
| 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 Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 74 | encrypt(span<const uint8_t> data); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 75 | |
| 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 Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 83 | regenerateCk(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 84 | |
| 85 | public: // accessor interface for published data packets |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 86 | /** |
| 87 | * @return number of packets stored in in-memory storage |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 88 | */ |
| 89 | size_t |
| 90 | size() const |
| 91 | { |
| 92 | return m_ims.size(); |
| 93 | } |
| 94 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 95 | /** |
| 96 | * @brief Returns begin iterator of the in-memory storage ordered by name with digest |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 97 | * |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 98 | * @return const_iterator pointing to the beginning of m_cache |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 99 | */ |
| 100 | InMemoryStorage::const_iterator |
| 101 | begin() const |
| 102 | { |
| 103 | return m_ims.begin(); |
| 104 | } |
| 105 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 106 | /** |
| 107 | * @brief Returns end iterator of the in-memory storage ordered by name with digest |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 108 | * |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 109 | * @return const_iterator pointing to the end of m_cache |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 110 | */ |
| 111 | InMemoryStorage::const_iterator |
| 112 | end() const |
| 113 | { |
| 114 | return m_ims.end(); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | void |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 119 | retryFetchingKek(); |
| 120 | |
| 121 | void |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 122 | fetchKekAndPublishCkData(const std::function<void()>& onReady, |
| 123 | const ErrorCallback& onFailure, |
| 124 | size_t nTriesLeft); |
| 125 | |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 126 | bool |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 127 | makeAndPublishCkData(const ErrorCallback& onFailure); |
| 128 | |
Davide Pesavento | 27680ae | 2019-04-06 19:40:01 -0400 | [diff] [blame] | 129 | NAC_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 130 | 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 Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 137 | std::optional<Data> m_kek; |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 138 | ErrorCallback m_onFailure; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 139 | |
| 140 | InMemoryStoragePersistent m_ims; // for encrypted CKs |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 141 | ScopedRegisteredPrefixHandle m_ckReg; |
| 142 | PendingInterestHandle m_kekPendingInterest; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 143 | |
| 144 | KeyChain& m_keyChain; |
| 145 | Face& m_face; |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 146 | Scheduler m_scheduler; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 147 | }; |
| 148 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 149 | } // namespace ndn::nac |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 150 | |
| 151 | #endif // NDN_NAC_ENCRYPTOR_HPP |