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 | |
| 26 | namespace ndn { |
| 27 | namespace nac { |
| 28 | |
| 29 | /** |
| 30 | * @brief NAC Encryptor |
| 31 | * |
| 32 | * Encryptor encrypts the requested content and returns ``EncryptedContent`` element. |
| 33 | */ |
| 34 | class Encryptor |
| 35 | { |
| 36 | public: |
| 37 | /** |
| 38 | * @param accessPrefix NAC prefix to fetch KEK (e.g., /access/prefix/NAC/data/subset) |
| 39 | * @param ckPrefix Prefix under which Content Keys will be generated |
| 40 | * (each will have unique version appended) |
| 41 | * @param ckDataSigningInfo SigningInfo parameters to sign CK Data |
| 42 | * @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] | 43 | * (failed to fetch KEK, failed to encrypt with KEK, etc.). |
| 44 | * Note that Encryptor will continue trying to retrieve KEK until success |
| 45 | * (each attempt separated by `RETRY_DELAY_KEK_RETRIEVAL`) and @p onFailure |
| 46 | * may be called multiple times. |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 47 | * @param validator Validation policy to ensure correctness of KEK |
| 48 | * @param keyChain KeyChain |
| 49 | * @param face Face that will be used to fetch KEK and publish CK data |
| 50 | */ |
| 51 | Encryptor(const Name& accessPrefix, |
| 52 | const Name& ckPrefix, SigningInfo ckDataSigningInfo, |
| 53 | const ErrorCallback& onFailure, |
| 54 | Validator& validator, KeyChain& keyChain, Face& face); |
| 55 | |
| 56 | ~Encryptor(); |
| 57 | |
| 58 | /** |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 59 | * @brief Synchronously encrypt supplied data |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 60 | * |
| 61 | * If KEK has not been fetched already, this method will trigger async fetching of it. |
| 62 | * After KEK successfully fetched, CK data will be automatically published. |
| 63 | * |
| 64 | * @todo For now, CK is being published in InMemoryStorage and can be fetched only while |
| 65 | * Encryptor instance is alive. |
| 66 | * |
| 67 | * The actual encryption is done synchronously, but the exact KDK name is not known |
| 68 | * until KEK is fetched. |
| 69 | * |
| 70 | * Note that if the KDK name is already known, this method will call onReady right away. |
| 71 | * |
| 72 | * @return Encrypted content |
| 73 | */ |
| 74 | EncryptedContent |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 75 | encrypt(span<const uint8_t> data); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * @brief Create a new content key and publish the corresponding CK data |
| 79 | * |
| 80 | * @todo Ensure that CK data packet for the old CK is published, when CK updated |
| 81 | * before KEK fetched |
| 82 | */ |
| 83 | void |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 84 | regenerateCk(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 85 | |
| 86 | public: // accessor interface for published data packets |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 87 | /** |
| 88 | * @return number of packets stored in in-memory storage |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 89 | */ |
| 90 | size_t |
| 91 | size() const |
| 92 | { |
| 93 | return m_ims.size(); |
| 94 | } |
| 95 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 96 | /** |
| 97 | * @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] | 98 | * |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 99 | * @return const_iterator pointing to the beginning of m_cache |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 100 | */ |
| 101 | InMemoryStorage::const_iterator |
| 102 | begin() const |
| 103 | { |
| 104 | return m_ims.begin(); |
| 105 | } |
| 106 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 107 | /** |
| 108 | * @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] | 109 | * |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 110 | * @return const_iterator pointing to the end of m_cache |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 111 | */ |
| 112 | InMemoryStorage::const_iterator |
| 113 | end() const |
| 114 | { |
| 115 | return m_ims.end(); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | void |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 120 | retryFetchingKek(); |
| 121 | |
| 122 | void |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 123 | fetchKekAndPublishCkData(const std::function<void()>& onReady, |
| 124 | const ErrorCallback& onFailure, |
| 125 | size_t nTriesLeft); |
| 126 | |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 127 | bool |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 128 | makeAndPublishCkData(const ErrorCallback& onFailure); |
| 129 | |
Davide Pesavento | 27680ae | 2019-04-06 19:40:01 -0400 | [diff] [blame] | 130 | NAC_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 131 | Name m_accessPrefix; |
| 132 | Name m_ckPrefix; |
| 133 | Name m_ckName; |
| 134 | Buffer m_ckBits; |
| 135 | SigningInfo m_ckDataSigningInfo; |
| 136 | |
| 137 | bool m_isKekRetrievalInProgress; |
| 138 | optional<Data> m_kek; |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 139 | ErrorCallback m_onFailure; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 140 | |
| 141 | InMemoryStoragePersistent m_ims; // for encrypted CKs |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 142 | ScopedRegisteredPrefixHandle m_ckReg; |
| 143 | PendingInterestHandle m_kekPendingInterest; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 144 | |
| 145 | KeyChain& m_keyChain; |
| 146 | Face& m_face; |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 147 | Scheduler m_scheduler; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // namespace nac |
| 151 | } // namespace ndn |
| 152 | |
| 153 | #endif // NDN_NAC_ENCRYPTOR_HPP |