blob: 70aa887e0f958d0b19c5131bf6429cddfac1eb38 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
swa770de007bc2020-03-24 21:26:21 -07002/**
Davide Pesaventob48bbda2020-07-27 19:41:37 -04003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "ca-module.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070022#include "challenge-module.hpp"
23#include "crypto-support/enc-tlv.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070024#include "protocol-detail/challenge.hpp"
25#include "protocol-detail/error.hpp"
26#include "protocol-detail/info.hpp"
tylerliu4889a782020-09-30 22:47:49 -070027#include "protocol-detail/new-renew-revoke.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070028#include "protocol-detail/probe.hpp"
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070029#include <ndn-cxx/metadata-object.hpp>
30#include <ndn-cxx/security/signing-helpers.hpp>
31#include <ndn-cxx/security/verification-helpers.hpp>
32#include <ndn-cxx/util/io.hpp>
33#include <ndn-cxx/util/random.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080034
35namespace ndn {
36namespace ndncert {
37
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070038static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -070039static const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070040
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080041_LOG_INIT(ndncert.ca);
42
43CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
44 const std::string& configPath, const std::string& storageType)
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070045 : m_face(face)
46 , m_keyChain(keyChain)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070048 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049 m_config.load(configPath);
50 m_storage = CaStorage::createCaStorage(storageType);
51
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080052 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080053}
54
55CaModule::~CaModule()
56{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070057 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080059 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070060 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080062 }
63}
64
65void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080066CaModule::registerPrefix()
67{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068 // register localhop discovery prefix
swa77020643ac2020-03-26 02:24:45 -070069 Name localhopInfoPrefix("/localhop/CA/INFO");
70 auto prefixId = m_face.setInterestFilter(InterestFilter(localhopInfoPrefix),
71 bind(&CaModule::onInfo, this, _2),
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080072 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 m_registeredPrefixHandles.push_back(prefixId);
swa77020643ac2020-03-26 02:24:45 -070074 _LOG_TRACE("Prefix " << localhopInfoPrefix << " got registered");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080075
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 // register prefixes
Zhiyi Zhang9829da92020-09-30 16:19:34 -070077 Name prefix = m_config.m_caItem.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080079
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070080 prefixId = m_face.registerPrefix(
81 prefix,
82 [&](const Name& name) {
83 // register INFO prefix
84 auto filterId = m_face.setInterestFilter(Name(name).append("INFO"),
85 bind(&CaModule::onInfo, this, _2));
86 m_interestFilterHandles.push_back(filterId);
swa77020643ac2020-03-26 02:24:45 -070087
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070088 // register PROBE prefix
89 filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
90 bind(&CaModule::onProbe, this, _2));
91 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070093 // register NEW prefix
94 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070095 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070096 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070098 // register SELECT prefix
99 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
100 bind(&CaModule::onChallenge, this, _2));
101 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700103 // register REVOKE prefix
104 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700105 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700106 m_interestFilterHandles.push_back(filterId);
107 _LOG_TRACE("Prefix " << name << " got registered");
108 },
109 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800111}
112
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700113void
114CaModule::setNameAssignmentFunction(const NameAssignmentFunc& handler)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800115{
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700116 m_config.m_nameAssignmentFunc = handler;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800117}
118
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700119void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800121{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700122 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800123}
124
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700125shared_ptr<Data>
126CaModule::generateCaConfigMetaData()
swa77020643ac2020-03-26 02:24:45 -0700127{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700128 // @TODO
129 // make metadata a class member variable m_infoMetadata
130 // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one
131 // otherwise, directly reply m_infoMetadata.makeData
132
133 auto infoPacket = generateCaConfigData();
134 MetadataObject metadata;
135 metadata.setVersionedName(infoPacket->getName().getPrefix(-1));
136 Name discoveryInterestName(infoPacket->getName().getPrefix(-2));
137 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
138 discoveryInterestName.append(metadataComponent);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700139 auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700140 return make_shared<Data>(metadataData);
141}
142
143shared_ptr<Data>
144CaModule::generateCaConfigData()
145{
146 // @TODO
147 // make CaInfo Data packet a class member variable m_infoData
148 // check whether the m_infoData is still valid, if not, then generate a new one
149 // otherwise, directly reply m_infoData
Suyong Won19fba4d2020-05-09 13:39:46 -0700150
151 const auto& pib = m_keyChain.getPib();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700152 const auto& identity = pib.getIdentity(m_config.m_caItem.m_caPrefix);
Suyong Won19fba4d2020-05-09 13:39:46 -0700153 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700154 Block contentTLV = INFO::encodeDataContent(m_config.m_caItem, cert);
swa77020643ac2020-03-26 02:24:45 -0700155
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700156 Name infoPacketName(m_config.m_caItem.m_caPrefix);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700157 infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
158 Data infoData(infoPacketName);
159 infoData.setContent(contentTLV);
160 infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700161 m_keyChain.sign(infoData, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700162 return make_shared<Data>(infoData);
163}
swa77020643ac2020-03-26 02:24:45 -0700164
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700165void
166CaModule::onInfo(const Interest& request)
167{
168 _LOG_TRACE("Received INFO request");
169
170 if (request.getName().get(-1).type() == 32) {
171 m_face.put(*generateCaConfigMetaData());
172 }
173 else {
174 m_face.put(*generateCaConfigData());
175 }
swa77020643ac2020-03-26 02:24:45 -0700176
177 _LOG_TRACE("Handle INFO: send out the INFO response");
178}
179
180void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700181CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800182{
swa77020643ac2020-03-26 02:24:45 -0700183 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
184 _LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800185
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700186 // process PROBE requests: collect probe parameters
187 auto parameters = PROBE::decodeApplicationParameters(request.getApplicationParameters());
188 std::vector<std::string> availableComponents;
189 if (m_config.m_nameAssignmentFunc) {
190 try {
191 availableComponents = m_config.m_nameAssignmentFunc(parameters);
192 }
193 catch (const std::exception& e) {
194 _LOG_TRACE("Cannot parse probe parameters: " << e.what());
195 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
196 "Cannot parse probe parameters: " + std::string(e.what())));
197 return;
198 }
199 }
200 else {
201 // if there is no app-specified name lookup, use a random name id
202 availableComponents.push_back(std::to_string(random::generateSecureWord64()));
203 }
204 std::vector<Name> availableNames;
205 for (const auto& component : availableComponents) {
206 Name newIdentityName = m_config.m_caItem.m_caPrefix;
207 newIdentityName.append(component);
208 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700209 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700210
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700211 Data result;
212 result.setName(request.getName());
213 result.setContent(PROBE::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength));
214 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
215 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
216 m_face.put(result);
217 _LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800218}
219
220void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700221CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800222{
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700223 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700224 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700225 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700226 const auto& parameterTLV = request.getApplicationParameters();
Suyong Won7968f7a2020-05-12 01:01:25 -0700227 parameterTLV.parse();
228
Suyong Won19fba4d2020-05-09 13:39:46 -0700229 if (!parameterTLV.hasValue()) {
230 _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700231 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
232 "Empty TLV obtained from the Interest parameter."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700233 return;
234 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700235
236 std::string peerKeyBase64 = readString(parameterTLV.get(tlv_ecdh_pub));
237
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700238 if (peerKeyBase64 == "") {
Suyong Won19fba4d2020-05-09 13:39:46 -0700239 _LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700240 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
241 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700242 return;
243 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700244
245 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700246 ECDHState ecdh;
247 auto myEcdhPubKeyBase64 = ecdh.getBase64PubKey();
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700248 try {
tylerliu8e170d62020-09-30 01:31:53 -0700249 ecdh.deriveSecret(peerKeyBase64);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700250 }
251 catch (const std::exception& e) {
252 _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700253 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
254 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700255 return;
256 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700257 // generate salt for HKDF
258 auto saltInt = random::generateSecureWord64();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700259 // hkdf
tylerliu8e170d62020-09-30 01:31:53 -0700260 uint8_t aesKey[AES_128_KEY_LEN];
261 hkdf(ecdh.context->sharedSecret, ecdh.context->sharedSecretLen,
262 (uint8_t*)&saltInt, sizeof(saltInt), aesKey, sizeof(aesKey));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700263
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700264 shared_ptr<security::v2::Certificate> clientCert = nullptr;
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700265 // parse certificate request
Zhiyi Zhangb940aa12020-09-30 16:38:57 -0700266 Block requestPayload;
267 if (requestType == RequestType::NEW) {
268 requestPayload = parameterTLV.get(tlv_cert_request);
269 }
270 else if (requestType == RequestType::REVOKE) {
271 requestPayload = parameterTLV.get(tlv_cert_to_revoke);
272 }
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700273 requestPayload.parse();
274 try {
275 security::v2::Certificate cert = security::v2::Certificate(requestPayload.get(tlv::Data));
276 clientCert = make_shared<security::v2::Certificate>(cert);
277 }
278 catch (const std::exception& e) {
279 _LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
280 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
281 "Unrecognized self-signed certificate."));
282 return;
283 }
tylerliud9239f52020-09-30 17:25:16 -0700284
285 // verify identity name
286 if (!m_config.m_caItem.m_caPrefix.isPrefixOf(clientCert->getIdentity())
287 || !security::v2::Certificate::isValidName(clientCert->getName())
288 || clientCert->getIdentity().size() <= m_config.m_caItem.m_caPrefix.size()) {
289 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
290 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
291 "An invalid certificate name is being requested."));
292 return;
293 }
294 if (m_config.m_caItem.m_maxSuffixLength) {
295 if (clientCert->getIdentity().size() > m_config.m_caItem.m_caPrefix.size() + *m_config.m_caItem.m_maxSuffixLength) {
296 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
297 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
298 "An invalid certificate name is being requested."));
299 return;
300 }
301 }
302
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700303 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700304 // check the validity period
305 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
306 auto currentTime = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700307 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700308 expectedPeriod.second > currentTime + m_config.m_caItem.m_maxValidityPeriod ||
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700309 expectedPeriod.second <= expectedPeriod.first) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700310 _LOG_ERROR("An invalid validity period is being requested.");
311 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
312 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700313 return;
314 }
tylerliud9239f52020-09-30 17:25:16 -0700315
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700316 // verify signature
tylerliu4a00aad2020-09-26 02:03:17 -0700317 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700318 _LOG_ERROR("Invalid signature in the self-signed certificate.");
319 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
320 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700321 return;
322 }
323 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700324 _LOG_ERROR("Invalid signature in the Interest packet.");
325 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
326 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700327 return;
328 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700329 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700330 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700331 //verify cert is from this CA
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700332 const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caItem.m_caPrefix).getDefaultKey().getDefaultCertificate();
tylerliu4a00aad2020-09-26 02:03:17 -0700333 if (!security::verifySignature(*clientCert, cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700334 _LOG_ERROR("Invalid signature in the certificate to revoke.");
335 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
336 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700337 return;
338 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700339 }
340
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700341 // create new request instance
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800342 std::string requestId = std::to_string(random::generateWord64());
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700343 RequestState requestState(m_config.m_caItem.m_caPrefix, requestId, requestType, Status::BEFORE_CHALLENGE, *clientCert,
tylerliu8e170d62020-09-30 01:31:53 -0700344 makeBinaryBlock(tlv::ContentType_Key, aesKey, sizeof(aesKey)));
tylerliu63900d52020-09-30 03:01:18 -0700345 m_storage->addRequest(requestState);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800346 Data result;
347 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700348 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu4889a782020-09-30 22:47:49 -0700349 result.setContent(NEW_RENEW_REVOKE::encodeDataContent(myEcdhPubKeyBase64,
tylerliu4a00aad2020-09-26 02:03:17 -0700350 std::to_string(saltInt),
tylerliu63900d52020-09-30 03:01:18 -0700351 requestState,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700352 m_config.m_caItem.m_supportedChallenges));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700353 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800354 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700355 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700356 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800357 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800358}
359
360void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700361CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800362{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700363 // get certificate request state
tylerliu63900d52020-09-30 03:01:18 -0700364 RequestState requestState = getCertificateRequest(request);
365 if (requestState.m_requestId == "") {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700366 _LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700367 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700368 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800369 return;
370 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700371 // verify signature
tylerliu63900d52020-09-30 03:01:18 -0700372 if (!security::verifySignature(request, requestState.m_cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700373 _LOG_ERROR("Invalid Signature in the Interest packet.");
374 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
375 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800376 return;
377 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700378 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700379 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700380 try {
tylerliu63900d52020-09-30 03:01:18 -0700381 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), requestState.m_encryptionKey.value(),
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700382 (uint8_t*)"test", strlen("test"));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700383 }
384 catch (const std::exception& e) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700385 _LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu63900d52020-09-30 03:01:18 -0700386 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700387 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700388 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700389 return;
390 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700391 if (paramTLVPayload.size() == 0) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700392 _LOG_ERROR("No parameters are found after decryption.");
tylerliu63900d52020-09-30 03:01:18 -0700393 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700394 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700395 "No parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700396 return;
397 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700398 Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size());
399 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800400
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700401 // load the corresponding challenge module
Suyong Won19fba4d2020-05-09 13:39:46 -0700402 std::string challengeType = readString(paramTLV.get(tlv_selected_challenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800403 auto challenge = ChallengeModule::createChallengeModule(challengeType);
404 if (challenge == nullptr) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700405 _LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700406 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700407 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700408 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800409 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700410
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700411 _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700412 auto errorInfo = challenge->handleChallengeRequest(paramTLV, requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700413 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu63900d52020-09-30 03:01:18 -0700414 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700415 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
416 return;
417 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700418
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700419 Block payload;
tylerliu63900d52020-09-30 03:01:18 -0700420 if (requestState.m_status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700421 // if challenge succeeded
tylerliu63900d52020-09-30 03:01:18 -0700422 if (requestState.m_requestType == RequestType::NEW) {
423 auto issuedCert = issueCertificate(requestState);
424 requestState.m_cert = issuedCert;
425 requestState.m_status = Status::SUCCESS;
426 m_storage->addCertificate(requestState.m_requestId, issuedCert);
427 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700428
tylerliu63900d52020-09-30 03:01:18 -0700429 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700430 payload.parse();
431 payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
432 payload.encode();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700433 _LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800434 }
tylerliu63900d52020-09-30 03:01:18 -0700435 else if (requestState.m_requestType == RequestType::REVOKE) {
436 requestState.m_status = Status::SUCCESS;
437 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700438
tylerliu63900d52020-09-30 03:01:18 -0700439 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700440 _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
441 }
442 }
443 else {
tylerliu63900d52020-09-30 03:01:18 -0700444 m_storage->updateRequest(requestState);
445 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700446 _LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800447 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700448
449 Data result;
450 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700451 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu63900d52020-09-30 03:01:18 -0700452 auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, requestState.m_encryptionKey.value(), payload.value(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700453 payload.value_size(), (uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700454 result.setContent(contentBlock);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700455 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700456 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700457 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700458 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800459 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700460}
461
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800462security::v2::Certificate
tylerliu63900d52020-09-30 03:01:18 -0700463CaModule::issueCertificate(const RequestState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800464{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700465 auto expectedPeriod =
tylerliu63900d52020-09-30 03:01:18 -0700466 requestState.m_cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700467 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800468 security::v2::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700469
tylerliu63900d52020-09-30 03:01:18 -0700470 Name certName = requestState.m_cert.getKeyName();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700471 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800472 newCert.setName(certName);
tylerliu63900d52020-09-30 03:01:18 -0700473 newCert.setContent(requestState.m_cert.getContent());
474 _LOG_TRACE("cert request content " << requestState.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800475 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800476 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700477 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700478 m_config.m_caItem.m_caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800479
480 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700481 _LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800482 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800483}
484
Zhiyi Zhange232a742020-09-29 17:34:17 -0700485RequestState
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700486CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800487{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400488 std::string requestId;
tylerliu63900d52020-09-30 03:01:18 -0700489 RequestState requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800490 try {
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700491 requestId = readString(request.getName().at(m_config.m_caItem.m_caPrefix.size() + 2));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700492 }
493 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700494 _LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700495 }
496 try {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700497 _LOG_TRACE("Request Id to query the database " << requestId);
tylerliu63900d52020-09-30 03:01:18 -0700498 requestState = m_storage->getRequest(requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800499 }
500 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700501 _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800502 }
tylerliu63900d52020-09-30 03:01:18 -0700503 return requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800504}
505
506void
507CaModule::onRegisterFailed(const std::string& reason)
508{
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700509 _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800510}
511
512Block
513CaModule::dataContentFromJson(const JsonSection& jsonSection)
514{
515 std::stringstream ss;
516 boost::property_tree::write_json(ss, jsonSection);
517 return makeStringBlock(ndn::tlv::Content, ss.str());
518}
519
520JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700521CaModule::jsonFromBlock(const Block& block)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800522{
523 std::string jsonString;
524 try {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700525 jsonString = encoding::readString(block);
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700526 std::istringstream ss(jsonString);
527 JsonSection json;
528 boost::property_tree::json_parser::read_json(ss, json);
529 return json;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800530 }
531 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700532 _LOG_ERROR("Cannot read JSON string from TLV Value: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800533 return JsonSection();
534 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800535}
536
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700537Data
538CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
539{
540 Data result;
541 result.setName(name);
542 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
543 result.setContent(ErrorTLV::encodeDataContent(error, errorInfo));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700544 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700545 return result;
546}
547
548} // namespace ndncert
549} // namespace ndn