blob: ef41efce1df0298057c797d369a692f419ec3c16 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe5b43692021-11-15 22:05:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, 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 Zhangdc25ddf2020-10-20 14:28:55 -070022#include "detail/crypto-helpers.hpp"
Zhiyi Zhang84e11842020-11-19 20:03:23 -080023#include "challenge/challenge-module.hpp"
Zhiyi Zhangc2c4ed22020-10-14 17:16:28 -070024#include "name-assignment/assignment-func.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070025#include "detail/challenge-encoder.hpp"
26#include "detail/error-encoder.hpp"
27#include "detail/info-encoder.hpp"
Zhiyi Zhang7cca76a2021-02-17 14:57:42 -080028#include "detail/request-encoder.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070029#include "detail/probe-encoder.hpp"
Davide Pesaventoe5b43692021-11-15 22:05:03 -050030
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070031#include <ndn-cxx/metadata-object.hpp>
32#include <ndn-cxx/security/signing-helpers.hpp>
33#include <ndn-cxx/security/verification-helpers.hpp>
34#include <ndn-cxx/util/io.hpp>
35#include <ndn-cxx/util/random.hpp>
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070036#include <ndn-cxx/util/string-helper.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080037
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040038namespace ndncert::ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080039
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040040const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
41const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070043NDN_LOG_INIT(ndncert.ca);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080044
Davide Pesavento0dc02012021-11-23 22:55:03 -050045CaModule::CaModule(ndn::Face& face, ndn::KeyChain& keyChain,
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080046 const std::string& configPath, const std::string& storageType)
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080047 : m_face(face)
48 , m_keyChain(keyChain)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070050 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080051 m_config.load(configPath);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080052 m_storage = CaStorage::createCaStorage(storageType, m_config.caProfile.caPrefix, "");
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040053
54 ndn::random::generateSecureBytes(m_requestIdGenKey);
55
56 if (m_config.nameAssignmentFuncs.empty()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080057 m_config.nameAssignmentFuncs.push_back(NameAssignmentFunc::createNameAssignmentFunc("random"));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070058 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040059
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080060 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080061}
62
63CaModule::~CaModule()
64{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070065 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080067 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070068 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080070 }
71}
72
73void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080074CaModule::registerPrefix()
75{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 // register prefixes
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080077 Name prefix = m_config.caProfile.caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080079
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040080 auto prefixId = m_face.registerPrefix(prefix,
Davide Pesaventoe5b43692021-11-15 22:05:03 -050081 [&] (const Name& name) {
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080082 // register INFO RDR metadata prefix
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040083 const auto& metaDataComp = ndn::MetadataObject::getKeywordComponent();
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080084 auto filterId = m_face.setInterestFilter(Name(name).append("INFO").append(metaDataComp),
Davide Pesaventoe5b43692021-11-15 22:05:03 -050085 [this] (auto&&, const auto& i) { onCaProfileDiscovery(i); });
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080086 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhang696cd042020-10-07 21:27:36 -070087
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080088 // register PROBE prefix
89 filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
Davide Pesaventoe5b43692021-11-15 22:05:03 -050090 [this] (auto&&, const auto& i) { onProbe(i); });
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080091 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080093 // register NEW prefix
94 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Davide Pesaventoe5b43692021-11-15 22:05:03 -050095 [this] (auto&&, const auto& i) { onNewRenewRevoke(i, RequestType::NEW); });
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080096 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097
Zhiyi Zhang1b101b32021-02-17 14:36:03 -080098 // register SELECT prefix
99 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500100 [this] (auto&&, const auto& i) { onChallenge(i); });
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800101 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800103 // register REVOKE prefix
104 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500105 [this] (auto&&, const auto& i) { onNewRenewRevoke(i, RequestType::REVOKE); });
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800106 m_interestFilterHandles.push_back(filterId);
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500107
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800108 NDN_LOG_TRACE("Prefix " << name << " got registered");
109 },
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500110 [this] (auto&&, const auto& reason) { onRegisterFailed(reason); });
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800112}
113
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700114void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700115CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800116{
tylerliu1f480be2020-11-10 13:02:53 -0800117 m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800118}
119
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700120Data
121CaModule::getCaProfileData()
swa77020643ac2020-03-26 02:24:45 -0700122{
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700123 if (m_profileData == nullptr) {
124 const auto& pib = m_keyChain.getPib();
Zhiyi Zhangcac27f12021-02-01 13:48:14 -0800125 const auto& identity = pib.getIdentity(m_config.caProfile.caPrefix);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700126 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
tylerliuf2e6bb52020-12-13 13:23:05 -0800127 Block contentTLV = infotlv::encodeDataContent(m_config.caProfile, cert);
swa77020643ac2020-03-26 02:24:45 -0700128
Zhiyi Zhang615b6b72021-01-11 14:25:32 -0800129 Name infoPacketName(m_config.caProfile.caPrefix);
Davide Pesavento0dc02012021-11-23 22:55:03 -0500130 auto segmentComp = ndn::name::Component::fromSegment(0);
Zhiyi Zhang615b6b72021-01-11 14:25:32 -0800131 infoPacketName.append("CA").append("INFO").appendVersion().append(segmentComp);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700132 m_profileData = std::make_unique<Data>(infoPacketName);
Zhiyi Zhang615b6b72021-01-11 14:25:32 -0800133 m_profileData->setFinalBlock(segmentComp);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700134 m_profileData->setContent(contentTLV);
135 m_profileData->setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800136 m_keyChain.sign(*m_profileData, signingByIdentity(m_config.caProfile.caPrefix));
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700137 }
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700138 return *m_profileData;
139}
140
141void
Davide Pesavento0dc02012021-11-23 22:55:03 -0500142CaModule::onCaProfileDiscovery(const Interest&)
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700143{
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700144 NDN_LOG_TRACE("Received CA Profile MetaData discovery Interest");
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700145 if (m_profileData == nullptr) {
146 m_profileData = std::make_unique<Data>(getCaProfileData());
147 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500148 ndn::MetadataObject metadata;
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700149 metadata.setVersionedName(m_profileData->getName().getPrefix(-1));
150 Name discoveryInterestName(m_profileData->getName().getPrefix(-2));
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400151 discoveryInterestName.append(ndn::MetadataObject::getKeywordComponent());
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800152 m_face.put(metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.caProfile.caPrefix)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700153}
swa77020643ac2020-03-26 02:24:45 -0700154
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700155void
Tianyuan Yu13aac732022-03-03 20:59:54 -0800156CaModule::onProbe(const Interest& request) {
swa77020643ac2020-03-26 02:24:45 -0700157 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700158 NDN_LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800159
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700160 // process PROBE requests: collect probe parameters
Tianyuan Yu13aac732022-03-03 20:59:54 -0800161 std::vector<ndn::Name> redirectionNames;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500162 std::vector<ndn::PartialName> availableComponents;
Tianyuan Yu13aac732022-03-03 20:59:54 -0800163 try {
164 auto parameters = probetlv::decodeApplicationParameters(request.getApplicationParameters());
165
166 //collect redirections
167 for (auto &item : m_config.redirection) {
168 if (item.second->isRedirecting(parameters)) {
169 redirectionNames.push_back(item.first->getFullName());
170 }
171 }
172
173 //collect name assignments
174 for (auto &item : m_config.nameAssignmentFuncs) {
175 auto names = item->assignName(parameters);
176 availableComponents.insert(availableComponents.end(), names.begin(), names.end());
177 }
178 } catch (const std::exception& e) {
179 NDN_LOG_ERROR("[CaModule::onProbe]Error in decoding TLV: " << e.what());
180 return;
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700181 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800182
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400183 if (availableComponents.empty() && redirectionNames.empty()) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700184 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700185 "Cannot generate available names from parameters provided."));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700186 return;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700187 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400188
Tianyuan Yu13aac732022-03-03 20:59:54 -0800189 std::vector<Name> availableNames;
190 for (const auto &component : availableComponents) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800191 Name newIdentityName = m_config.caProfile.caPrefix;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700192 newIdentityName.append(component);
193 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700194 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700195
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700196 Data result;
197 result.setName(request.getName());
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800198 result.setContent(
Tianyuan Yu13aac732022-03-03 20:59:54 -0800199 probetlv::encodeDataContent(availableNames, m_config.caProfile.maxSuffixLength, redirectionNames));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700200 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800201 m_keyChain.sign(result, signingByIdentity(m_config.caProfile.caPrefix));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700202 m_face.put(result);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700203 NDN_LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800204}
205
206void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700207CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800208{
tylerliuf62cfeb2021-01-28 11:01:33 -0800209 //verify ca cert validity
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800210 const auto& caCert = m_keyChain.getPib()
211 .getIdentity(m_config.caProfile.caPrefix)
212 .getDefaultKey()
213 .getDefaultCertificate();
tylerliuf62cfeb2021-01-28 11:01:33 -0800214 if (!caCert.isValid()) {
215 NDN_LOG_ERROR("Server certificate invalid/expired");
216 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
217 "Server certificate invalid/expired"));
218 return;
219 }
220
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700221 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700222 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700223 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700224 const auto& parameterTLV = request.getApplicationParameters();
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800225 std::vector <uint8_t> ecdhPub;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500226 std::shared_ptr<Certificate> clientCert;
tylerliu0790bdb2020-10-02 00:50:51 -0700227 try {
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800228 requesttlv::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700229 }
230 catch (const std::exception& e) {
tylerliu0790bdb2020-10-02 00:50:51 -0700231 if (!parameterTLV.hasValue()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700232 NDN_LOG_ERROR("Empty TLV obtained from the Interest parameter.");
tylerliu0790bdb2020-10-02 00:50:51 -0700233 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
234 "Empty TLV obtained from the Interest parameter."));
235 return;
236 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700237
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700238 NDN_LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700239 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
tylerliu0790bdb2020-10-02 00:50:51 -0700240 "Unrecognized self-signed certificate."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700241 return;
242 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700243
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700244 if (ecdhPub.empty()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700245 NDN_LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700246 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
247 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700248 return;
249 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700250
251 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700252 ECDHState ecdh;
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800253 std::vector <uint8_t> sharedSecret;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700254 try {
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700255 sharedSecret = ecdh.deriveSecret(ecdhPub);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700256 }
257 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700258 NDN_LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700259 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
260 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700261 return;
262 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700263
tylerliud9239f52020-09-30 17:25:16 -0700264 // verify identity name
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800265 if (!m_config.caProfile.caPrefix.isPrefixOf(clientCert->getIdentity())
Davide Pesavento0dc02012021-11-23 22:55:03 -0500266 || !Certificate::isValidName(clientCert->getName())
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800267 || clientCert->getIdentity().size() <= m_config.caProfile.caPrefix.size()) {
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800268 NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
269 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
270 "An invalid certificate name is being requested."));
271 return;
tylerliud9239f52020-09-30 17:25:16 -0700272 }
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800273 if (m_config.caProfile.maxSuffixLength) {
274 if (clientCert->getIdentity().size() > m_config.caProfile.caPrefix.size() + *m_config.caProfile.maxSuffixLength) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700275 NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
tylerliud9239f52020-09-30 17:25:16 -0700276 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
277 "An invalid certificate name is being requested."));
278 return;
279 }
280 }
281
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700282 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700283 // check the validity period
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400284 auto [notBefore, notAfter] = clientCert->getValidityPeriod().getPeriod();
tylerliu4a00aad2020-09-26 02:03:17 -0700285 auto currentTime = time::system_clock::now();
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400286 if (notBefore < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
287 notAfter > currentTime + m_config.caProfile.maxValidityPeriod ||
288 notAfter <= notBefore) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700289 NDN_LOG_ERROR("An invalid validity period is being requested.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700290 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
291 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700292 return;
293 }
tylerliud9239f52020-09-30 17:25:16 -0700294
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700295 // verify signature
Davide Pesavento0dc02012021-11-23 22:55:03 -0500296 if (!ndn::security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700297 NDN_LOG_ERROR("Invalid signature in the self-signed certificate.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700298 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
299 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700300 return;
301 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500302 if (!ndn::security::verifySignature(request, *clientCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700303 NDN_LOG_ERROR("Invalid signature in the Interest packet.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700304 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
305 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700306 return;
307 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700308 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700309 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700310 //verify cert is from this CA
Davide Pesavento0dc02012021-11-23 22:55:03 -0500311 if (!ndn::security::verifySignature(*clientCert, caCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700312 NDN_LOG_ERROR("Invalid signature in the certificate to revoke.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700313 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
314 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700315 return;
316 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700317 }
318
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700319 // create new request instance
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700320 uint8_t requestIdData[32];
321 Block certNameTlv = clientCert->getName().wireEncode();
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700322 try {
Zhiyi Zhang11cf7eb2020-10-20 15:43:36 -0700323 hmacSha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData);
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700324 }
325 catch (const std::runtime_error& e) {
326 NDN_LOG_ERROR("Error computing the request ID: " << std::string(e.what()));
327 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
328 "Error computing the request ID."));
329 return;
330 }
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700331 RequestId id;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700332 std::memcpy(id.data(), requestIdData, id.size());
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800333 // initialize request state
334 RequestState requestState;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800335 requestState.caPrefix = m_config.caProfile.caPrefix;
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800336 requestState.requestId = id;
337 requestState.requestType = requestType;
338 requestState.cert = *clientCert;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800339 // generate salt for HKDF
340 std::array<uint8_t, 32> salt;
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400341 ndn::random::generateSecureBytes(salt);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800342 // hkdf
343 std::array<uint8_t, 16> aesKey;
344 hkdf(sharedSecret.data(), sharedSecret.size(), salt.data(), salt.size(),
345 aesKey.data(), aesKey.size(), id.data(), id.size());
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800346 requestState.encryptionKey = aesKey;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700347 try {
348 m_storage->addRequest(requestState);
349 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500350 catch (const std::runtime_error&) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700351 NDN_LOG_ERROR("Duplicate Request ID: The same request has been seen before.");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700352 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700353 "Duplicate Request ID: The same request has been seen before."));
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700354 return;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700355 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400356
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800357 Data result;
358 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700359 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800360 result.setContent(requesttlv::encodeDataContent(ecdh.getSelfPubKey(),
361 salt, requestState.requestId,
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800362 m_config.caProfile.supportedChallenges));
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800363 m_keyChain.sign(result, signingByIdentity(m_config.caProfile.caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800364 m_face.put(result);
tylerliu1f480be2020-11-10 13:02:53 -0800365 if (m_statusUpdateCallback) {
366 m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800367 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800368}
369
370void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700371CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800372{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700373 // get certificate request state
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700374 auto requestState = getCertificateRequest(request);
Zhiyi Zhang84207c52020-10-28 21:45:44 -0700375 if (requestState == nullptr) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700376 NDN_LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700377 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700378 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800379 return;
380 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400381
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700382 // verify signature
Davide Pesavento0dc02012021-11-23 22:55:03 -0500383 if (!ndn::security::verifySignature(request, requestState->cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700384 NDN_LOG_ERROR("Invalid Signature in the Interest packet.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700385 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
386 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800387 return;
388 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400389
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700390 // decrypt the parameters
Davide Pesavento0dc02012021-11-23 22:55:03 -0500391 ndn::Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700392 try {
Zhiyi Zhang3b9a5032021-02-18 11:15:09 -0800393 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), requestState->encryptionKey.data(),
394 requestState->requestId.data(), requestState->requestId.size(),
395 requestState->decryptionIv, requestState->encryptionIv);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700396 }
397 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700398 NDN_LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu7b9185c2020-11-24 12:15:18 -0800399 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700400 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700401 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700402 return;
403 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400404 if (paramTLVPayload.empty()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700405 NDN_LOG_ERROR("No parameters are found after decryption.");
tylerliu7b9185c2020-11-24 12:15:18 -0800406 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700407 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700408 "No parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700409 return;
410 }
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400411
412 auto paramTLV = ndn::makeBinaryBlock(tlv::EncryptedPayload, paramTLVPayload);
Suyong Won44d0cce2020-05-10 04:07:43 -0700413 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800414
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700415 // load the corresponding challenge module
tylerliu50d679e2020-10-14 14:08:39 -0700416 std::string challengeType = readString(paramTLV.get(tlv::SelectedChallenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800417 auto challenge = ChallengeModule::createChallengeModule(challengeType);
418 if (challenge == nullptr) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700419 NDN_LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu7b9185c2020-11-24 12:15:18 -0800420 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800421 m_face.put(
422 generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700423 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800424 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700425
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700426 NDN_LOG_TRACE("CHALLENGE module to be load: " << challengeType);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700427 auto errorInfo = challenge->handleChallengeRequest(paramTLV, *requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700428 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu7b9185c2020-11-24 12:15:18 -0800429 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700430 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
431 return;
432 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700433
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700434 Block payload;
tylerliu7b9185c2020-11-24 12:15:18 -0800435 if (requestState->status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700436 // if challenge succeeded
tylerliud82cc5c2020-12-21 23:58:39 -0800437 if (requestState->requestType == RequestType::NEW || requestState->requestType == RequestType::RENEW) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700438 auto issuedCert = issueCertificate(*requestState);
tylerliu7b9185c2020-11-24 12:15:18 -0800439 requestState->cert = issuedCert;
440 requestState->status = Status::SUCCESS;
441 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700442
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800443 payload = challengetlv::encodeDataContent(*requestState, issuedCert.getName());
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700444 NDN_LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800445 }
tylerliu7b9185c2020-11-24 12:15:18 -0800446 else if (requestState->requestType == RequestType::REVOKE) {
447 requestState->status = Status::SUCCESS;
448 m_storage->deleteRequest(requestState->requestId);
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -0800449 // TODO: where is the code to revoke?
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800450 payload = challengetlv::encodeDataContent(*requestState);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700451 NDN_LOG_TRACE("Challenge succeeded. Certificate has been revoked");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700452 }
453 }
454 else {
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800455 payload = challengetlv::encodeDataContent(*requestState);
Zhiyi Zhangc1e98152020-12-21 16:15:39 -0800456 m_storage->updateRequest(*requestState);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700457 NDN_LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800458 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700459
460 Data result;
461 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700462 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700463 result.setContent(payload);
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800464 m_keyChain.sign(result, signingByIdentity(m_config.caProfile.caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700465 m_face.put(result);
tylerliu1f480be2020-11-10 13:02:53 -0800466 if (m_statusUpdateCallback) {
467 m_statusUpdateCallback(*requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800468 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700469}
470
Davide Pesavento0dc02012021-11-23 22:55:03 -0500471Certificate
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700472CaModule::issueCertificate(const RequestState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800473{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400474 auto period = requestState.cert.getValidityPeriod();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500475 Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700476
tylerliu7b9185c2020-11-24 12:15:18 -0800477 Name certName = requestState.cert.getKeyName();
Tianyuan Yu13aac732022-03-03 20:59:54 -0800478 certName.append("NDNCERT").appendVersion();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800479 newCert.setName(certName);
tylerliu7b9185c2020-11-24 12:15:18 -0800480 newCert.setContent(requestState.cert.getContent());
481 NDN_LOG_TRACE("cert request content " << requestState.cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800482 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800483 signatureInfo.setValidityPeriod(period);
Davide Pesavento0dc02012021-11-23 22:55:03 -0500484 ndn::security::SigningInfo signingInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
485 m_config.caProfile.caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800486
487 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700488 NDN_LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800489 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800490}
491
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800492std::unique_ptr <RequestState>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700493CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800494{
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700495 RequestId requestId;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800496 try {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800497 auto& component = request.getName().at(m_config.caProfile.caPrefix.size() + 2);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700498 std::memcpy(requestId.data(), component.value(), component.value_size());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700499 }
500 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700501 NDN_LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700502 return nullptr;
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700503 }
504 try {
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400505 NDN_LOG_TRACE("Request Id to query the database " << ndn::toHex(requestId));
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700506 return std::make_unique<RequestState>(m_storage->getRequest(requestId));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800507 }
508 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700509 NDN_LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700510 return nullptr;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800511 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800512}
513
514void
515CaModule::onRegisterFailed(const std::string& reason)
516{
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700517 NDN_LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800518}
519
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700520Data
521CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
522{
523 Data result;
524 result.setName(name);
525 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800526 result.setContent(errortlv::encodeDataContent(error, errorInfo));
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800527 m_keyChain.sign(result, signingByIdentity(m_config.caProfile.caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700528 return result;
529}
530
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400531} // namespace ndncert::ca