Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California |
| 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 |
| 43 | * (failed to fetch KEK, failed to encrypt with KEK, etc.) |
| 44 | * @param validator Validation policy to ensure correctness of KEK |
| 45 | * @param keyChain KeyChain |
| 46 | * @param face Face that will be used to fetch KEK and publish CK data |
| 47 | */ |
| 48 | Encryptor(const Name& accessPrefix, |
| 49 | const Name& ckPrefix, SigningInfo ckDataSigningInfo, |
| 50 | const ErrorCallback& onFailure, |
| 51 | Validator& validator, KeyChain& keyChain, Face& face); |
| 52 | |
| 53 | ~Encryptor(); |
| 54 | |
| 55 | /** |
| 56 | * Synchronously encrypt supplied data |
| 57 | * |
| 58 | * If KEK has not been fetched already, this method will trigger async fetching of it. |
| 59 | * After KEK successfully fetched, CK data will be automatically published. |
| 60 | * |
| 61 | * @todo For now, CK is being published in InMemoryStorage and can be fetched only while |
| 62 | * Encryptor instance is alive. |
| 63 | * |
| 64 | * The actual encryption is done synchronously, but the exact KDK name is not known |
| 65 | * until KEK is fetched. |
| 66 | * |
| 67 | * Note that if the KDK name is already known, this method will call onReady right away. |
| 68 | * |
| 69 | * @return Encrypted content |
| 70 | */ |
| 71 | EncryptedContent |
| 72 | encrypt(const uint8_t* data, size_t size); |
| 73 | |
| 74 | /** |
| 75 | * @brief Create a new content key and publish the corresponding CK data |
| 76 | * |
| 77 | * @todo Ensure that CK data packet for the old CK is published, when CK updated |
| 78 | * before KEK fetched |
| 79 | */ |
| 80 | void |
| 81 | regenerateCk(const ErrorCallback& onFailure); |
| 82 | |
| 83 | public: // accessor interface for published data packets |
| 84 | |
| 85 | /** @return{ number of packets stored in in-memory storage } |
| 86 | */ |
| 87 | size_t |
| 88 | size() const |
| 89 | { |
| 90 | return m_ims.size(); |
| 91 | } |
| 92 | |
| 93 | /** @brief Returns begin iterator of the in-memory storage ordered by |
| 94 | * name with digest |
| 95 | * |
| 96 | * @return{ const_iterator pointing to the beginning of m_cache } |
| 97 | */ |
| 98 | InMemoryStorage::const_iterator |
| 99 | begin() const |
| 100 | { |
| 101 | return m_ims.begin(); |
| 102 | } |
| 103 | |
| 104 | /** @brief Returns end iterator of the in-memory storage ordered by |
| 105 | * name with digest |
| 106 | * |
| 107 | * @return{ const_iterator pointing to the end of m_cache } |
| 108 | */ |
| 109 | InMemoryStorage::const_iterator |
| 110 | end() const |
| 111 | { |
| 112 | return m_ims.end(); |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | void |
| 117 | fetchKekAndPublishCkData(const std::function<void()>& onReady, |
| 118 | const ErrorCallback& onFailure, |
| 119 | size_t nTriesLeft); |
| 120 | |
| 121 | void |
| 122 | makeAndPublishCkData(const ErrorCallback& onFailure); |
| 123 | |
| 124 | private: |
| 125 | Name m_accessPrefix; |
| 126 | Name m_ckPrefix; |
| 127 | Name m_ckName; |
| 128 | Buffer m_ckBits; |
| 129 | SigningInfo m_ckDataSigningInfo; |
| 130 | |
| 131 | bool m_isKekRetrievalInProgress; |
| 132 | optional<Data> m_kek; |
| 133 | |
| 134 | InMemoryStoragePersistent m_ims; // for encrypted CKs |
| 135 | const RegisteredPrefixId* m_ckRegId = nullptr; |
| 136 | const PendingInterestId* m_kekPendingInterest = nullptr; |
| 137 | |
| 138 | KeyChain& m_keyChain; |
| 139 | Face& m_face; |
| 140 | }; |
| 141 | |
| 142 | } // namespace nac |
| 143 | } // namespace ndn |
| 144 | |
| 145 | #endif // NDN_NAC_ENCRYPTOR_HPP |