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>
25 #include <ndn-cxx/util/logger.hpp>
26 #include <ndn-cxx/util/random.hpp>
28 #include <boost/lexical_cast.hpp>
32 NDN_LOG_INIT(nac.Encryptor);
37 const Name& ckPrefix, SigningInfo ckDataSigningInfo,
39 Validator&, KeyChain& keyChain, Face& face)
40 : m_accessPrefix(accessPrefix)
41 , m_ckPrefix(ckPrefix)
43 , m_ckDataSigningInfo(std::move(ckDataSigningInfo))
44 , m_isKekRetrievalInProgress(false)
45 , m_onFailure(onFailure)
46 , m_keyChain(keyChain)
48 , m_scheduler(face.getIoContext())
52 auto serveFromIms = [
this] (
const Name&,
const Interest& interest) {
53 auto data = m_ims.find(interest);
54 if (data !=
nullptr) {
55 NDN_LOG_DEBUG(
"Serving " << data->getName() <<
" from InMemoryStorage");
59 NDN_LOG_DEBUG(
"Didn't find CK data for " << interest.getName());
64 auto handleError = [] (
const Name& prefix,
const std::string& msg) {
65 NDN_LOG_ERROR(
"Failed to register prefix " << prefix <<
": " << msg);
68 m_ckReg = m_face.setInterestFilter(Name(ckPrefix).append(
CK), serveFromIms, handleError);
73 m_kekPendingInterest.cancel();
77 Encryptor::retryFetchingKek()
79 if (m_isKekRetrievalInProgress) {
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;
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);
100 m_ckName = m_ckPrefix;
104 NDN_LOG_DEBUG(
"Generating new CK: " << m_ckName);
105 random::generateSecureBytes(m_ckBits);
112 makeAndPublishCkData(m_onFailure);
121 random::generateSecureBytes(*iv);
124 security::transform::bufferSource(data)
125 >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC,
126 CipherOperator::ENCRYPT,
128 >> security::transform::streamSink(os);
139 Encryptor::fetchKekAndPublishCkData(
const std::function<
void()>& onReady,
145 NDN_LOG_DEBUG(
"Fetching KEK " << Name(m_accessPrefix).append(
KEK));
147 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) {
154 if (makeAndPublishCkData(onFailure)) {
159 [=] (
const Interest& i,
const lp::Nack& nack) {
160 if (nTriesLeft > 1) {
162 fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
167 "] failed. Got NACK with reason " + boost::lexical_cast<std::string>(nack.getReason()));
168 NDN_LOG_DEBUG(
"Scheduling retry from NACK");
172 [=] (
const Interest& i) {
173 if (nTriesLeft > 1) {
174 fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
178 "Retrieval of KEK [" + i.getName().toUri() +
"] timed out");
179 NDN_LOG_DEBUG(
"Scheduling retry after all timeouts");
186 Encryptor::makeAndPublishCkData(
const ErrorCallback& onFailure)
190 kek.loadPkcs8(m_kek->getContent().value_bytes());
193 content.setPayload(kek.encrypt(m_ckBits));
195 auto ckData = std::make_shared<Data>(Name(m_ckName).append(
ENCRYPTED_BY).append(m_kek->getName()));
196 ckData->setContent(content.wireEncode());
199 m_keyChain.sign(*ckData, m_ckDataSigningInfo);
200 m_ims.insert(*ckData);
202 NDN_LOG_DEBUG(
"Publishing CK data: " << ckData->getName());
205 catch (
const std::runtime_error&) {
207 "Failed to encrypt generated CK with KEK " + m_kek->getName().toUri());
EncryptedContent & setPayload(Block payload)
EncryptedContent & setIv(Block iv)
EncryptedContent & setKeyLocator(Name keyLocator)
Encryptor(const Name &accessPrefix, const Name &ckPrefix, SigningInfo ckDataSigningInfo, const ErrorCallback &onFailure, Validator &validator, KeyChain &keyChain, Face &face)
EncryptedContent encrypt(span< const uint8_t > data)
Synchronously encrypt supplied data.
void regenerateCk()
Create a new content key and publish the corresponding CK data.
constexpr time::seconds DEFAULT_CK_FRESHNESS_PERIOD
std::function< void(const ErrorCode &, const std::string &)> ErrorCallback
constexpr size_t N_RETRIES
constexpr size_t AES_IV_SIZE
const name::Component ENCRYPTED_BY
const name::Component KEK
constexpr time::seconds RETRY_DELAY_KEK_RETRIEVAL
constexpr size_t AES_KEY_SIZE
constexpr time::seconds RETRY_DELAY_AFTER_NACK