blob: 25431bdbb027644a413dc0a48c2bf2e37a907020 [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>
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040026#include <ndn-cxx/util/random.hpp>
27
28#include <boost/lexical_cast.hpp>
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040029
Davide Pesaventobde084f2022-04-17 00:21:35 -040030namespace ndn::nac {
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040031
32NDN_LOG_INIT(nac.Encryptor);
33
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040034constexpr size_t N_RETRIES = 3;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040035
36Encryptor::Encryptor(const Name& accessPrefix,
37 const Name& ckPrefix, SigningInfo ckDataSigningInfo,
38 const ErrorCallback& onFailure,
Davide Pesaventoba3f6892020-12-08 22:18:35 -050039 Validator&, KeyChain& keyChain, Face& face)
40 : m_accessPrefix(accessPrefix)
41 , m_ckPrefix(ckPrefix)
42 , m_ckBits(AES_KEY_SIZE)
43 , m_ckDataSigningInfo(std::move(ckDataSigningInfo))
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040044 , m_isKekRetrievalInProgress(false)
Alexander Afanasyevda366d82018-06-29 18:18:02 -040045 , m_onFailure(onFailure)
Davide Pesaventoba3f6892020-12-08 22:18:35 -050046 , m_keyChain(keyChain)
47 , m_face(face)
48 , m_scheduler(face.getIoService())
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040049{
Alexander Afanasyevda366d82018-06-29 18:18:02 -040050 regenerateCk();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040051
Davide Pesaventod51d9602019-07-20 23:33:06 -040052 auto serveFromIms = [this] (const Name&, const Interest& interest) {
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040053 auto data = m_ims.find(interest);
54 if (data != nullptr) {
55 NDN_LOG_DEBUG("Serving " << data->getName() << " from InMemoryStorage");
56 m_face.put(*data);
57 }
58 else {
59 NDN_LOG_DEBUG("Didn't find CK data for " << interest.getName());
60 // send NACK?
61 }
62 };
63
64 auto handleError = [] (const Name& prefix, const std::string& msg) {
65 NDN_LOG_ERROR("Failed to register prefix " << prefix << ": " << msg);
66 };
67
Davide Pesaventod51d9602019-07-20 23:33:06 -040068 m_ckReg = m_face.setInterestFilter(Name(ckPrefix).append(CK), serveFromIms, handleError);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040069}
70
71Encryptor::~Encryptor()
72{
Davide Pesaventod51d9602019-07-20 23:33:06 -040073 m_kekPendingInterest.cancel();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040074}
75
76void
Alexander Afanasyevda366d82018-06-29 18:18:02 -040077Encryptor::retryFetchingKek()
78{
79 if (m_isKekRetrievalInProgress) {
80 return;
81 }
82
83 NDN_LOG_DEBUG("Retrying fetching of KEK");
84 m_isKekRetrievalInProgress = true;
85 fetchKekAndPublishCkData([&] {
86 NDN_LOG_DEBUG("KEK retrieved and published");
87 m_isKekRetrievalInProgress = false;
88 },
89 [=] (const ErrorCode& code, const std::string& msg) {
90 NDN_LOG_ERROR("Failed to retrieved KEK: " + msg);
91 m_isKekRetrievalInProgress = false;
92 m_onFailure(code, msg);
93 },
94 N_RETRIES);
95}
96
97void
98Encryptor::regenerateCk()
Alexander Afanasyev1a21e102018-06-13 20:33:21 -040099{
100 m_ckName = m_ckPrefix;
101 m_ckName
102 .append(CK)
103 .appendVersion(); // version = ID of CK
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400104 NDN_LOG_DEBUG("Generating new CK: " << m_ckName);
Davide Pesavento714dba02022-03-17 20:46:28 -0400105 random::generateSecureBytes(m_ckBits);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400106
107 // one implication: if CK updated before KEK fetched, KDK for the old CK will not be published
108 if (!m_kek) {
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400109 retryFetchingKek();
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400110 }
111 else {
Alexander Afanasyevda366d82018-06-29 18:18:02 -0400112 makeAndPublishCkData(m_onFailure);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400113 }
114}
115
116EncryptedContent
Davide Pesavento714dba02022-03-17 20:46:28 -0400117Encryptor::encrypt(span<const uint8_t> data)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400118{
Davide Pesaventoc2649492020-12-22 21:43:35 -0500119 // Generate IV
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400120 auto iv = make_shared<Buffer>(AES_IV_SIZE);
Davide Pesavento714dba02022-03-17 20:46:28 -0400121 random::generateSecureBytes(*iv);
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400122
123 OBufferStream os;
Davide Pesavento714dba02022-03-17 20:46:28 -0400124 security::transform::bufferSource(data)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400125 >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC,
126 CipherOperator::ENCRYPT,
Davide Pesavento714dba02022-03-17 20:46:28 -0400127 m_ckBits, *iv)
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400128 >> security::transform::streamSink(os);
129
130 EncryptedContent content;
131 content.setIv(iv);
132 content.setPayload(os.buf());
133 content.setKeyLocator(m_ckName);
134
135 return content;
136}
137
138void
139Encryptor::fetchKekAndPublishCkData(const std::function<void()>& onReady,
140 const ErrorCallback& onFailure,
141 size_t nTriesLeft)
142{
Davide Pesaventod51d9602019-07-20 23:33:06 -0400143 // interest for <access-prefix>/KEK to retrieve <access-prefix>/KEK/<key-id> KekData
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400144
145 NDN_LOG_DEBUG("Fetching KEK " << Name(m_accessPrefix).append(KEK));
146
Davide Pesaventod51d9602019-07-20 23:33:06 -0400147 auto kekInterest = Interest(Name(m_accessPrefix).append(KEK))
148 .setCanBePrefix(true)
149 .setMustBeFresh(true);
150 m_kekPendingInterest = m_face.expressInterest(kekInterest,
151 [=] (const Interest&, const Data& kek) {
152 // @todo verify if the key is legit
153 m_kek = kek;
154 if (makeAndPublishCkData(onFailure)) {
155 onReady();
156 }
157 // otherwise, failure has been already declared
158 },
159 [=] (const Interest& i, const lp::Nack& nack) {
160 if (nTriesLeft > 1) {
161 m_scheduler.schedule(RETRY_DELAY_AFTER_NACK, [=] {
162 fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
163 });
164 }
165 else {
166 onFailure(ErrorCode::KekRetrievalFailure, "Retrieval of KEK [" + i.getName().toUri() +
167 "] failed. Got NACK with reason " + boost::lexical_cast<std::string>(nack.getReason()));
168 NDN_LOG_DEBUG("Scheduling retry from NACK");
169 m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); });
170 }
171 },
172 [=] (const Interest& i) {
173 if (nTriesLeft > 1) {
174 fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
175 }
176 else {
177 onFailure(ErrorCode::KekRetrievalTimeout,
178 "Retrieval of KEK [" + i.getName().toUri() + "] timed out");
179 NDN_LOG_DEBUG("Scheduling retry after all timeouts");
180 m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); });
181 }
182 });
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400183}
184
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400185bool
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400186Encryptor::makeAndPublishCkData(const ErrorCallback& onFailure)
187{
188 try {
189 PublicKey kek;
Davide Pesavento714dba02022-03-17 20:46:28 -0400190 kek.loadPkcs8(m_kek->getContent().value_bytes());
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400191
192 EncryptedContent content;
Davide Pesavento714dba02022-03-17 20:46:28 -0400193 content.setPayload(kek.encrypt(m_ckBits));
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400194
195 auto ckData = make_shared<Data>(Name(m_ckName).append(ENCRYPTED_BY).append(m_kek->getName()));
196 ckData->setContent(content.wireEncode());
197 // FreshnessPeriod can serve as a soft access control for revoking access
198 ckData->setFreshnessPeriod(DEFAULT_CK_FRESHNESS_PERIOD);
199 m_keyChain.sign(*ckData, m_ckDataSigningInfo);
200 m_ims.insert(*ckData);
201
202 NDN_LOG_DEBUG("Publishing CK data: " << ckData->getName());
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400203 return true;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400204 }
Davide Pesaventod51d9602019-07-20 23:33:06 -0400205 catch (const std::runtime_error&) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500206 onFailure(ErrorCode::EncryptionFailure,
207 "Failed to encrypt generated CK with KEK " + m_kek->getName().toUri());
Alexander Afanasyevc9934282018-07-17 18:41:36 -0400208 return false;
Alexander Afanasyev1a21e102018-06-13 20:33:21 -0400209 }
210}
211
Davide Pesaventobde084f2022-04-17 00:21:35 -0400212} // namespace ndn::nac