-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencryptor.cpp
212 lines (185 loc) · 6.68 KB
/
encryptor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California
*
* NAC library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of NAC library authors and contributors.
*/
#include "encryptor.hpp"
#include <ndn-cxx/security/transform/block-cipher.hpp>
#include <ndn-cxx/security/transform/buffer-source.hpp>
#include <ndn-cxx/security/transform/stream-sink.hpp>
#include <ndn-cxx/util/logger.hpp>
#include <ndn-cxx/util/random.hpp>
#include <boost/lexical_cast.hpp>
namespace ndn::nac {
NDN_LOG_INIT(nac.Encryptor);
constexpr size_t N_RETRIES = 3;
Encryptor::Encryptor(const Name& accessPrefix,
const Name& ckPrefix, SigningInfo ckDataSigningInfo,
const ErrorCallback& onFailure,
Validator&, KeyChain& keyChain, Face& face)
: m_accessPrefix(accessPrefix)
, m_ckPrefix(ckPrefix)
, m_ckBits(AES_KEY_SIZE)
, m_ckDataSigningInfo(std::move(ckDataSigningInfo))
, m_isKekRetrievalInProgress(false)
, m_onFailure(onFailure)
, m_keyChain(keyChain)
, m_face(face)
, m_scheduler(face.getIoContext())
{
regenerateCk();
auto serveFromIms = [this] (const Name&, const Interest& interest) {
auto data = m_ims.find(interest);
if (data != nullptr) {
NDN_LOG_DEBUG("Serving " << data->getName() << " from InMemoryStorage");
m_face.put(*data);
}
else {
NDN_LOG_DEBUG("Didn't find CK data for " << interest.getName());
// send NACK?
}
};
auto handleError = [] (const Name& prefix, const std::string& msg) {
NDN_LOG_ERROR("Failed to register prefix " << prefix << ": " << msg);
};
m_ckReg = m_face.setInterestFilter(Name(ckPrefix).append(CK), serveFromIms, handleError);
}
Encryptor::~Encryptor()
{
m_kekPendingInterest.cancel();
}
void
Encryptor::retryFetchingKek()
{
if (m_isKekRetrievalInProgress) {
return;
}
NDN_LOG_DEBUG("Retrying fetching of KEK");
m_isKekRetrievalInProgress = true;
fetchKekAndPublishCkData([&] {
NDN_LOG_DEBUG("KEK retrieved and published");
m_isKekRetrievalInProgress = false;
},
[=] (const ErrorCode& code, const std::string& msg) {
NDN_LOG_ERROR("Failed to retrieved KEK: " + msg);
m_isKekRetrievalInProgress = false;
m_onFailure(code, msg);
},
N_RETRIES);
}
void
Encryptor::regenerateCk()
{
m_ckName = m_ckPrefix;
m_ckName
.append(CK)
.appendVersion(); // version = ID of CK
NDN_LOG_DEBUG("Generating new CK: " << m_ckName);
random::generateSecureBytes(m_ckBits);
// one implication: if CK updated before KEK fetched, KDK for the old CK will not be published
if (!m_kek) {
retryFetchingKek();
}
else {
makeAndPublishCkData(m_onFailure);
}
}
EncryptedContent
Encryptor::encrypt(span<const uint8_t> data)
{
// Generate IV
auto iv = std::make_shared<Buffer>(AES_IV_SIZE);
random::generateSecureBytes(*iv);
OBufferStream os;
security::transform::bufferSource(data)
>> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC,
CipherOperator::ENCRYPT,
m_ckBits, *iv)
>> security::transform::streamSink(os);
EncryptedContent content;
content.setIv(iv);
content.setPayload(os.buf());
content.setKeyLocator(m_ckName);
return content;
}
void
Encryptor::fetchKekAndPublishCkData(const std::function<void()>& onReady,
const ErrorCallback& onFailure,
size_t nTriesLeft)
{
// interest for <access-prefix>/KEK to retrieve <access-prefix>/KEK/<key-id> KekData
NDN_LOG_DEBUG("Fetching KEK " << Name(m_accessPrefix).append(KEK));
auto kekInterest = Interest(Name(m_accessPrefix).append(KEK))
.setCanBePrefix(true)
.setMustBeFresh(true);
m_kekPendingInterest = m_face.expressInterest(kekInterest,
[=] (const Interest&, const Data& kek) {
// @todo verify if the key is legit
m_kek = kek;
if (makeAndPublishCkData(onFailure)) {
onReady();
}
// otherwise, failure has been already declared
},
[=] (const Interest& i, const lp::Nack& nack) {
if (nTriesLeft > 1) {
m_scheduler.schedule(RETRY_DELAY_AFTER_NACK, [=] {
fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
});
}
else {
onFailure(ErrorCode::KekRetrievalFailure, "Retrieval of KEK [" + i.getName().toUri() +
"] failed. Got NACK with reason " + boost::lexical_cast<std::string>(nack.getReason()));
NDN_LOG_DEBUG("Scheduling retry from NACK");
m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); });
}
},
[=] (const Interest& i) {
if (nTriesLeft > 1) {
fetchKekAndPublishCkData(onReady, onFailure, nTriesLeft - 1);
}
else {
onFailure(ErrorCode::KekRetrievalTimeout,
"Retrieval of KEK [" + i.getName().toUri() + "] timed out");
NDN_LOG_DEBUG("Scheduling retry after all timeouts");
m_scheduler.schedule(RETRY_DELAY_KEK_RETRIEVAL, [this] { retryFetchingKek(); });
}
});
}
bool
Encryptor::makeAndPublishCkData(const ErrorCallback& onFailure)
{
try {
PublicKey kek;
kek.loadPkcs8(m_kek->getContent().value_bytes());
EncryptedContent content;
content.setPayload(kek.encrypt(m_ckBits));
auto ckData = std::make_shared<Data>(Name(m_ckName).append(ENCRYPTED_BY).append(m_kek->getName()));
ckData->setContent(content.wireEncode());
// FreshnessPeriod can serve as a soft access control for revoking access
ckData->setFreshnessPeriod(DEFAULT_CK_FRESHNESS_PERIOD);
m_keyChain.sign(*ckData, m_ckDataSigningInfo);
m_ims.insert(*ckData);
NDN_LOG_DEBUG("Publishing CK data: " << ckData->getName());
return true;
}
catch (const std::runtime_error&) {
onFailure(ErrorCode::EncryptionFailure,
"Failed to encrypt generated CK with KEK " + m_kek->getName().toUri());
return false;
}
}
} // namespace ndn::nac