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 | 899b278 | 2019-04-29 20:00:03 -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 | #include "encryptor.hpp" |
| 21 | |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 22 | #include <ndn-cxx/security/transform/block-cipher.hpp> |
| 23 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
| 24 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 25 | #include <ndn-cxx/util/logger.hpp> |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 27 | namespace ndn::nac { |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 28 | |
| 29 | NDN_LOG_INIT(nac.Encryptor); |
| 30 | |
| 31 | const size_t N_RETRIES = 3; |
| 32 | |
| 33 | Encryptor::Encryptor(const Name& accessPrefix, |
| 34 | const Name& ckPrefix, SigningInfo ckDataSigningInfo, |
| 35 | const ErrorCallback& onFailure, |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 36 | Validator&, KeyChain& keyChain, Face& face) |
| 37 | : m_accessPrefix(accessPrefix) |
| 38 | , m_ckPrefix(ckPrefix) |
| 39 | , m_ckBits(AES_KEY_SIZE) |
| 40 | , m_ckDataSigningInfo(std::move(ckDataSigningInfo)) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 41 | , m_isKekRetrievalInProgress(false) |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 42 | , m_onFailure(onFailure) |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 43 | , m_keyChain(keyChain) |
| 44 | , m_face(face) |
| 45 | , m_scheduler(face.getIoService()) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 46 | { |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 47 | regenerateCk(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 48 | |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 49 | auto serveFromIms = [this] (const Name&, const Interest& interest) { |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 50 | auto data = m_ims.find(interest); |
| 51 | if (data != nullptr) { |
| 52 | NDN_LOG_DEBUG("Serving " << data->getName() << " from InMemoryStorage"); |
| 53 | m_face.put(*data); |
| 54 | } |
| 55 | else { |
| 56 | NDN_LOG_DEBUG("Didn't find CK data for " << interest.getName()); |
| 57 | // send NACK? |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | auto handleError = [] (const Name& prefix, const std::string& msg) { |
| 62 | NDN_LOG_ERROR("Failed to register prefix " << prefix << ": " << msg); |
| 63 | }; |
| 64 | |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 65 | m_ckReg = m_face.setInterestFilter(Name(ckPrefix).append(CK), serveFromIms, handleError); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | Encryptor::~Encryptor() |
| 69 | { |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 70 | m_kekPendingInterest.cancel(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 74 | Encryptor::retryFetchingKek() |
| 75 | { |
| 76 | if (m_isKekRetrievalInProgress) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | NDN_LOG_DEBUG("Retrying fetching of KEK"); |
| 81 | m_isKekRetrievalInProgress = true; |
| 82 | fetchKekAndPublishCkData([&] { |
| 83 | NDN_LOG_DEBUG("KEK retrieved and published"); |
| 84 | m_isKekRetrievalInProgress = false; |
| 85 | }, |
| 86 | [=] (const ErrorCode& code, const std::string& msg) { |
| 87 | NDN_LOG_ERROR("Failed to retrieved KEK: " + msg); |
| 88 | m_isKekRetrievalInProgress = false; |
| 89 | m_onFailure(code, msg); |
| 90 | }, |
| 91 | N_RETRIES); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | Encryptor::regenerateCk() |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 96 | { |
| 97 | m_ckName = m_ckPrefix; |
| 98 | m_ckName |
| 99 | .append(CK) |
| 100 | .appendVersion(); // version = ID of CK |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 101 | NDN_LOG_DEBUG("Generating new CK: " << m_ckName); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 102 | random::generateSecureBytes(m_ckBits); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 103 | |
| 104 | // one implication: if CK updated before KEK fetched, KDK for the old CK will not be published |
| 105 | if (!m_kek) { |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 106 | retryFetchingKek(); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 107 | } |
| 108 | else { |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 109 | makeAndPublishCkData(m_onFailure); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | EncryptedContent |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 114 | Encryptor::encrypt(span<const uint8_t> data) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 115 | { |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 116 | // Generate IV |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 117 | auto iv = make_shared<Buffer>(AES_IV_SIZE); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 118 | random::generateSecureBytes(*iv); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 119 | |
| 120 | OBufferStream os; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 121 | security::transform::bufferSource(data) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 122 | >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC, |
| 123 | CipherOperator::ENCRYPT, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 124 | m_ckBits, *iv) |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 125 | >> security::transform::streamSink(os); |
| 126 | |
| 127 | EncryptedContent content; |
| 128 | content.setIv(iv); |
| 129 | content.setPayload(os.buf()); |
| 130 | content.setKeyLocator(m_ckName); |
| 131 | |
| 132 | return content; |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | Encryptor::fetchKekAndPublishCkData(const std::function<void()>& onReady, |
| 137 | const ErrorCallback& onFailure, |
| 138 | size_t nTriesLeft) |
| 139 | { |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 140 | // interest for <access-prefix>/KEK to retrieve <access-prefix>/KEK/<key-id> KekData |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 141 | |
| 142 | NDN_LOG_DEBUG("Fetching KEK " << Name(m_accessPrefix).append(KEK)); |
| 143 | |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 144 | auto kekInterest = Interest(Name(m_accessPrefix).append(KEK)) |
| 145 | .setCanBePrefix(true) |
| 146 | .setMustBeFresh(true); |
| 147 | m_kekPendingInterest = m_face.expressInterest(kekInterest, |
| 148 | [=] (const Interest&, const Data& kek) { |
| 149 | // @todo verify if the key is legit |
| 150 | m_kek = kek; |
| 151 | if (makeAndPublishCkData(onFailure)) { |
| 152 | onReady(); |
| 153 | } |
| 154 | // otherwise, failure has been already declared |
| 155 | }, |
| 156 | [=] (const Interest& i, const lp::Nack& nack) { |
| 157 | if (nTriesLeft > 1) { |
| 158 | m_scheduler.schedule(RETRY_DELAY_AFTER_NACK, [=] { |
| 159 | fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1); |
| 160 | }); |
| 161 | } |
| 162 | else { |
| 163 | onFailure(ErrorCode::KekRetrievalFailure, "Retrieval of KEK [" + i.getName().toUri() + |
| 164 | "] failed. Got NACK with reason " + boost::lexical_cast<std::string>(nack.getReason())); |
| 165 | NDN_LOG_DEBUG("Scheduling retry from NACK"); |
| 166 | m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); }); |
| 167 | } |
| 168 | }, |
| 169 | [=] (const Interest& i) { |
| 170 | if (nTriesLeft > 1) { |
| 171 | fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1); |
| 172 | } |
| 173 | else { |
| 174 | onFailure(ErrorCode::KekRetrievalTimeout, |
| 175 | "Retrieval of KEK [" + i.getName().toUri() + "] timed out"); |
| 176 | NDN_LOG_DEBUG("Scheduling retry after all timeouts"); |
| 177 | m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); }); |
| 178 | } |
| 179 | }); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 182 | bool |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 183 | Encryptor::makeAndPublishCkData(const ErrorCallback& onFailure) |
| 184 | { |
| 185 | try { |
| 186 | PublicKey kek; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 187 | kek.loadPkcs8(m_kek->getContent().value_bytes()); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 188 | |
| 189 | EncryptedContent content; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 190 | content.setPayload(kek.encrypt(m_ckBits)); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 191 | |
| 192 | auto ckData = make_shared<Data>(Name(m_ckName).append(ENCRYPTED_BY).append(m_kek->getName())); |
| 193 | ckData->setContent(content.wireEncode()); |
| 194 | // FreshnessPeriod can serve as a soft access control for revoking access |
| 195 | ckData->setFreshnessPeriod(DEFAULT_CK_FRESHNESS_PERIOD); |
| 196 | m_keyChain.sign(*ckData, m_ckDataSigningInfo); |
| 197 | m_ims.insert(*ckData); |
| 198 | |
| 199 | NDN_LOG_DEBUG("Publishing CK data: " << ckData->getName()); |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 200 | return true; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 201 | } |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame] | 202 | catch (const std::runtime_error&) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 203 | onFailure(ErrorCode::EncryptionFailure, |
| 204 | "Failed to encrypt generated CK with KEK " + m_kek->getName().toUri()); |
Alexander Afanasyev | c993428 | 2018-07-17 18:41:36 -0400 | [diff] [blame] | 205 | return false; |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Davide Pesavento | bde084f | 2022-04-17 00:21:35 -0400 | [diff] [blame] | 209 | } // namespace ndn::nac |