blob: 04febf03b5a70a4f2fa98dd005e6460c7c293e8c [file] [log] [blame]
Alexander Afanasyev1a21e102018-06-13 20:33:21 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento899b2782019-04-29 20:00:03 -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#include "encryptor.hpp"
21
Davide Pesaventoc2649492020-12-22 21:43:35 -050022#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 Afanasyev1a21e102018-06-13 20:33:21 -040025#include <ndn-cxx/util/logger.hpp>
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040026
Davide Pesaventobde084f2022-04-17 00:21:35 -040027namespace ndn::nac {
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040028
29NDN_LOG_INIT(nac.Encryptor);
30
31const size_t N_RETRIES = 3;
32
33Encryptor::Encryptor(const Name& accessPrefix,
34 const Name& ckPrefix, SigningInfo ckDataSigningInfo,
35 const ErrorCallback& onFailure,
Davide Pesaventoba3f6892020-12-08 22:18:35 -050036 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 Afanasyev1a21e102018-06-13 20:33:21 -040041 , m_isKekRetrievalInProgress(false)
Alexander Afanasyevda366d82018-06-29 18:18:02 -040042 , m_onFailure(onFailure)
Davide Pesaventoba3f6892020-12-08 22:18:35 -050043 , m_keyChain(keyChain)
44 , m_face(face)
45 , m_scheduler(face.getIoService())
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040046{
Alexander Afanasyevda366d82018-06-29 18:18:02 -040047 regenerateCk();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040048
Davide Pesaventod51d9602019-07-20 23:33:06 -040049 auto serveFromIms = [this] (const Name&, const Interest& interest) {
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040050 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 Pesaventod51d9602019-07-20 23:33:06 -040065 m_ckReg = m_face.setInterestFilter(Name(ckPrefix).append(CK), serveFromIms, handleError);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040066}
67
68Encryptor::~Encryptor()
69{
Davide Pesaventod51d9602019-07-20 23:33:06 -040070 m_kekPendingInterest.cancel();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040071}
72
73void
Alexander Afanasyevda366d82018-06-29 18:18:02 -040074Encryptor::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
94void
95Encryptor::regenerateCk()
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040096{
97 m_ckName = m_ckPrefix;
98 m_ckName
99 .append(CK)
100 .appendVersion(); // version = ID of CK
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400101 NDN_LOG_DEBUG("Generating new CK: " << m_ckName);
Davide Pesavento714dba02022-03-17 20:46:28 -0400102 random::generateSecureBytes(m_ckBits);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400103
104 // one implication: if CK updated before KEK fetched, KDK for the old CK will not be published
105 if (!m_kek) {
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400106 retryFetchingKek();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400107 }
108 else {
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400109 makeAndPublishCkData(m_onFailure);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400110 }
111}
112
113EncryptedContent
Davide Pesavento714dba02022-03-17 20:46:28 -0400114Encryptor::encrypt(span<const uint8_t> data)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400115{
Davide Pesaventoc2649492020-12-22 21:43:35 -0500116 // Generate IV
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400117 auto iv = make_shared<Buffer>(AES_IV_SIZE);
Davide Pesavento714dba02022-03-17 20:46:28 -0400118 random::generateSecureBytes(*iv);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400119
120 OBufferStream os;
Davide Pesavento714dba02022-03-17 20:46:28 -0400121 security::transform::bufferSource(data)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400122 >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC,
123 CipherOperator::ENCRYPT,
Davide Pesavento714dba02022-03-17 20:46:28 -0400124 m_ckBits, *iv)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400125 >> 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
135void
136Encryptor::fetchKekAndPublishCkData(const std::function<void()>& onReady,
137 const ErrorCallback& onFailure,
138 size_t nTriesLeft)
139{
Davide Pesaventod51d9602019-07-20 23:33:06 -0400140 // interest for <access-prefix>/KEK to retrieve <access-prefix>/KEK/<key-id> KekData
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400141
142 NDN_LOG_DEBUG("Fetching KEK " << Name(m_accessPrefix).append(KEK));
143
Davide Pesaventod51d9602019-07-20 23:33:06 -0400144 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 Afanasyev1a21e102018-06-13 20:33:21 -0400180}
181
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400182bool
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400183Encryptor::makeAndPublishCkData(const ErrorCallback& onFailure)
184{
185 try {
186 PublicKey kek;
Davide Pesavento714dba02022-03-17 20:46:28 -0400187 kek.loadPkcs8(m_kek->getContent().value_bytes());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400188
189 EncryptedContent content;
Davide Pesavento714dba02022-03-17 20:46:28 -0400190 content.setPayload(kek.encrypt(m_ckBits));
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400191
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 Afanasyevc9934282018-07-17 18:41:36 -0400200 return true;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400201 }
Davide Pesaventod51d9602019-07-20 23:33:06 -0400202 catch (const std::runtime_error&) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500203 onFailure(ErrorCode::EncryptionFailure,
204 "Failed to encrypt generated CK with KEK " + m_kek->getName().toUri());
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400205 return false;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400206 }
207}
208
Davide Pesaventobde084f2022-04-17 00:21:35 -0400209} // namespace ndn::nac