blob: b35a744b928372b425f020f1ee99afae1733d2ad [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
27namespace ndn {
28namespace nac {
29
30NDN_LOG_INIT(nac.Encryptor);
31
32const size_t N_RETRIES = 3;
33
34Encryptor::Encryptor(const Name& accessPrefix,
35 const Name& ckPrefix, SigningInfo ckDataSigningInfo,
36 const ErrorCallback& onFailure,
Davide Pesaventoba3f6892020-12-08 22:18:35 -050037 Validator&, KeyChain& keyChain, Face& face)
38 : m_accessPrefix(accessPrefix)
39 , m_ckPrefix(ckPrefix)
40 , m_ckBits(AES_KEY_SIZE)
41 , m_ckDataSigningInfo(std::move(ckDataSigningInfo))
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040042 , m_isKekRetrievalInProgress(false)
Alexander Afanasyevda366d82018-06-29 18:18:02 -040043 , m_onFailure(onFailure)
Davide Pesaventoba3f6892020-12-08 22:18:35 -050044 , m_keyChain(keyChain)
45 , m_face(face)
46 , m_scheduler(face.getIoService())
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040047{
Alexander Afanasyevda366d82018-06-29 18:18:02 -040048 regenerateCk();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040049
Davide Pesaventod51d9602019-07-20 23:33:06 -040050 auto serveFromIms = [this] (const Name&, const Interest& interest) {
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040051 auto data = m_ims.find(interest);
52 if (data != nullptr) {
53 NDN_LOG_DEBUG("Serving " << data->getName() << " from InMemoryStorage");
54 m_face.put(*data);
55 }
56 else {
57 NDN_LOG_DEBUG("Didn't find CK data for " << interest.getName());
58 // send NACK?
59 }
60 };
61
62 auto handleError = [] (const Name& prefix, const std::string& msg) {
63 NDN_LOG_ERROR("Failed to register prefix " << prefix << ": " << msg);
64 };
65
Davide Pesaventod51d9602019-07-20 23:33:06 -040066 m_ckReg = m_face.setInterestFilter(Name(ckPrefix).append(CK), serveFromIms, handleError);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040067}
68
69Encryptor::~Encryptor()
70{
Davide Pesaventod51d9602019-07-20 23:33:06 -040071 m_kekPendingInterest.cancel();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040072}
73
74void
Alexander Afanasyevda366d82018-06-29 18:18:02 -040075Encryptor::retryFetchingKek()
76{
77 if (m_isKekRetrievalInProgress) {
78 return;
79 }
80
81 NDN_LOG_DEBUG("Retrying fetching of KEK");
82 m_isKekRetrievalInProgress = true;
83 fetchKekAndPublishCkData([&] {
84 NDN_LOG_DEBUG("KEK retrieved and published");
85 m_isKekRetrievalInProgress = false;
86 },
87 [=] (const ErrorCode& code, const std::string& msg) {
88 NDN_LOG_ERROR("Failed to retrieved KEK: " + msg);
89 m_isKekRetrievalInProgress = false;
90 m_onFailure(code, msg);
91 },
92 N_RETRIES);
93}
94
95void
96Encryptor::regenerateCk()
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040097{
98 m_ckName = m_ckPrefix;
99 m_ckName
100 .append(CK)
101 .appendVersion(); // version = ID of CK
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400102 NDN_LOG_DEBUG("Generating new CK: " << m_ckName);
Davide Pesavento714dba02022-03-17 20:46:28 -0400103 random::generateSecureBytes(m_ckBits);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400104
105 // one implication: if CK updated before KEK fetched, KDK for the old CK will not be published
106 if (!m_kek) {
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400107 retryFetchingKek();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400108 }
109 else {
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400110 makeAndPublishCkData(m_onFailure);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400111 }
112}
113
114EncryptedContent
Davide Pesavento714dba02022-03-17 20:46:28 -0400115Encryptor::encrypt(span<const uint8_t> data)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400116{
Davide Pesaventoc2649492020-12-22 21:43:35 -0500117 // Generate IV
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400118 auto iv = make_shared<Buffer>(AES_IV_SIZE);
Davide Pesavento714dba02022-03-17 20:46:28 -0400119 random::generateSecureBytes(*iv);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400120
121 OBufferStream os;
Davide Pesavento714dba02022-03-17 20:46:28 -0400122 security::transform::bufferSource(data)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400123 >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC,
124 CipherOperator::ENCRYPT,
Davide Pesavento714dba02022-03-17 20:46:28 -0400125 m_ckBits, *iv)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400126 >> security::transform::streamSink(os);
127
128 EncryptedContent content;
129 content.setIv(iv);
130 content.setPayload(os.buf());
131 content.setKeyLocator(m_ckName);
132
133 return content;
134}
135
136void
137Encryptor::fetchKekAndPublishCkData(const std::function<void()>& onReady,
138 const ErrorCallback& onFailure,
139 size_t nTriesLeft)
140{
Davide Pesaventod51d9602019-07-20 23:33:06 -0400141 // interest for <access-prefix>/KEK to retrieve <access-prefix>/KEK/<key-id> KekData
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400142
143 NDN_LOG_DEBUG("Fetching KEK " << Name(m_accessPrefix).append(KEK));
144
Davide Pesaventod51d9602019-07-20 23:33:06 -0400145 auto kekInterest = Interest(Name(m_accessPrefix).append(KEK))
146 .setCanBePrefix(true)
147 .setMustBeFresh(true);
148 m_kekPendingInterest = m_face.expressInterest(kekInterest,
149 [=] (const Interest&, const Data& kek) {
150 // @todo verify if the key is legit
151 m_kek = kek;
152 if (makeAndPublishCkData(onFailure)) {
153 onReady();
154 }
155 // otherwise, failure has been already declared
156 },
157 [=] (const Interest& i, const lp::Nack& nack) {
158 if (nTriesLeft > 1) {
159 m_scheduler.schedule(RETRY_DELAY_AFTER_NACK, [=] {
160 fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
161 });
162 }
163 else {
164 onFailure(ErrorCode::KekRetrievalFailure, "Retrieval of KEK [" + i.getName().toUri() +
165 "] failed. Got NACK with reason " + boost::lexical_cast<std::string>(nack.getReason()));
166 NDN_LOG_DEBUG("Scheduling retry from NACK");
167 m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); });
168 }
169 },
170 [=] (const Interest& i) {
171 if (nTriesLeft > 1) {
172 fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
173 }
174 else {
175 onFailure(ErrorCode::KekRetrievalTimeout,
176 "Retrieval of KEK [" + i.getName().toUri() + "] timed out");
177 NDN_LOG_DEBUG("Scheduling retry after all timeouts");
178 m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); });
179 }
180 });
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400181}
182
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400183bool
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400184Encryptor::makeAndPublishCkData(const ErrorCallback& onFailure)
185{
186 try {
187 PublicKey kek;
Davide Pesavento714dba02022-03-17 20:46:28 -0400188 kek.loadPkcs8(m_kek->getContent().value_bytes());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400189
190 EncryptedContent content;
Davide Pesavento714dba02022-03-17 20:46:28 -0400191 content.setPayload(kek.encrypt(m_ckBits));
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400192
193 auto ckData = make_shared<Data>(Name(m_ckName).append(ENCRYPTED_BY).append(m_kek->getName()));
194 ckData->setContent(content.wireEncode());
195 // FreshnessPeriod can serve as a soft access control for revoking access
196 ckData->setFreshnessPeriod(DEFAULT_CK_FRESHNESS_PERIOD);
197 m_keyChain.sign(*ckData, m_ckDataSigningInfo);
198 m_ims.insert(*ckData);
199
200 NDN_LOG_DEBUG("Publishing CK data: " << ckData->getName());
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400201 return true;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400202 }
Davide Pesaventod51d9602019-07-20 23:33:06 -0400203 catch (const std::runtime_error&) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500204 onFailure(ErrorCode::EncryptionFailure,
205 "Failed to encrypt generated CK with KEK " + m_kek->getName().toUri());
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400206 return false;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400207 }
208}
209
210} // namespace nac
211} // namespace ndn