blob: c662617d68180fa32003ab537f7a1af9849c0128 [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 Zhang062be6d2020-10-14 17:13:43 -070022#include "detail/crypto-helper.hpp"
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070023#include "identity-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"
28#include "detail/new-renew-revoke-encoder.hpp"
29#include "detail/probe-encoder.hpp"
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070030#include <ndn-cxx/metadata-object.hpp>
31#include <ndn-cxx/security/signing-helpers.hpp>
32#include <ndn-cxx/security/verification-helpers.hpp>
33#include <ndn-cxx/util/io.hpp>
34#include <ndn-cxx/util/random.hpp>
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070035#include <ndn-cxx/util/string-helper.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080036
37namespace ndn {
38namespace ndncert {
39
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070040static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -070041static const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070042static const int AES_128_KEY_LEN = 16;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070044NDN_LOG_INIT(ndncert.ca);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080045
tylerliua7bea662020-10-08 18:51:02 -070046CaModule::CaModule(Face& face, security::KeyChain& keyChain,
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047 const std::string& configPath, const std::string& storageType)
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070048 : m_face(face)
49 , m_keyChain(keyChain)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070051 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080052 m_config.load(configPath);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070053 m_storage = CaStorage::createCaStorage(storageType, m_config.m_caItem.m_caPrefix, "");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070054 random::generateSecureBytes(m_requestIdGenKey, 32);
Zhiyi Zhang74c61142020-10-07 21:00:49 -070055 if (m_config.m_nameAssignmentFuncs.size() == 0) {
56 m_config.m_nameAssignmentFuncs.push_back(NameAssignmentFunc::createNameAssignmentFunc("random"));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070057 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080058 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080059}
60
61CaModule::~CaModule()
62{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070063 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080065 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070066 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080068 }
69}
70
71void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080072CaModule::registerPrefix()
73{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 // register prefixes
Zhiyi Zhang9829da92020-09-30 16:19:34 -070075 Name prefix = m_config.m_caItem.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080077
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070078 auto prefixId = m_face.registerPrefix(
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070079 prefix,
80 [&](const Name& name) {
Zhiyi Zhang696cd042020-10-07 21:27:36 -070081 // register INFO RDR metadata prefix
82 name::Component metaDataComp(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
83 auto filterId = m_face.setInterestFilter(Name(name).append("INFO").append(metaDataComp),
84 bind(&CaModule::onCaProfileDiscovery, this, _2));
85 m_interestFilterHandles.push_back(filterId);
86
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070087 // register PROBE prefix
Zhiyi Zhang696cd042020-10-07 21:27:36 -070088 filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070089 bind(&CaModule::onProbe, this, _2));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070090 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070092 // register NEW prefix
93 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070094 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070095 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070097 // register SELECT prefix
98 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
99 bind(&CaModule::onChallenge, this, _2));
100 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700102 // register REVOKE prefix
103 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700104 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700105 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700106 NDN_LOG_TRACE("Prefix " << name << " got registered");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700107 },
108 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800110}
111
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700112void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700113CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800114{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700115 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800116}
117
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700118Data
119CaModule::getCaProfileData()
swa77020643ac2020-03-26 02:24:45 -0700120{
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700121 if (m_profileData == nullptr) {
122 const auto& pib = m_keyChain.getPib();
123 const auto& identity = pib.getIdentity(m_config.m_caItem.m_caPrefix);
124 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700125 Block contentTLV = InfoEncoder::encodeDataContent(m_config.m_caItem, cert);
swa77020643ac2020-03-26 02:24:45 -0700126
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700127 Name infoPacketName(m_config.m_caItem.m_caPrefix);
128 infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
129 m_profileData = std::make_unique<Data>(infoPacketName);
130 m_profileData->setContent(contentTLV);
131 m_profileData->setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
132 m_keyChain.sign(*m_profileData, signingByIdentity(m_config.m_caItem.m_caPrefix));
133 }
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700134 return *m_profileData;
135}
136
137void
138CaModule::onCaProfileDiscovery(const Interest& request)
139{
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700140 NDN_LOG_TRACE("Received CA Profile MetaData discovery Interest");
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700141 if (m_profileData == nullptr) {
142 m_profileData = std::make_unique<Data>(getCaProfileData());
143 }
144 MetadataObject metadata;
145 metadata.setVersionedName(m_profileData->getName().getPrefix(-1));
146 Name discoveryInterestName(m_profileData->getName().getPrefix(-2));
147 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
148 discoveryInterestName.append(metadataComponent);
149 m_face.put(metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caItem.m_caPrefix)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700150}
swa77020643ac2020-03-26 02:24:45 -0700151
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700152void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700153CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800154{
swa77020643ac2020-03-26 02:24:45 -0700155 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700156 NDN_LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800157
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700158 // process PROBE requests: collect probe parameters
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700159 auto parameters = ProbeEncoder::decodeApplicationParameters(request.getApplicationParameters());
tylerliu8fc08072020-10-06 15:37:40 -0700160 std::vector<PartialName> availableComponents;
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700161 for (auto& item : m_config.m_nameAssignmentFuncs) {
162 auto names = item->assignName(parameters);
163 availableComponents.insert(availableComponents.end(), names.begin(), names.end());
164 }
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700165 if (availableComponents.size() == 0) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700166 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700167 "Cannot generate available names from parameters provided."));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700168 return;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700169 }
170 std::vector<Name> availableNames;
171 for (const auto& component : availableComponents) {
172 Name newIdentityName = m_config.m_caItem.m_caPrefix;
173 newIdentityName.append(component);
174 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700175 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700176
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700177 Data result;
178 result.setName(request.getName());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700179 result.setContent(ProbeEncoder::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength, m_config.m_redirection));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700180 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
181 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
182 m_face.put(result);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700183 NDN_LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800184}
185
186void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700187CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800188{
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700189 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700190 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700191 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700192 const auto& parameterTLV = request.getApplicationParameters();
tylerliu0790bdb2020-10-02 00:50:51 -0700193 std::string ecdhPub;
tylerliua7bea662020-10-08 18:51:02 -0700194 shared_ptr<security::Certificate> clientCert;
tylerliu0790bdb2020-10-02 00:50:51 -0700195 try {
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700196 NewRenewRevokeEncoder::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700197 }
198 catch (const std::exception& e) {
tylerliu0790bdb2020-10-02 00:50:51 -0700199 if (!parameterTLV.hasValue()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700200 NDN_LOG_ERROR("Empty TLV obtained from the Interest parameter.");
tylerliu0790bdb2020-10-02 00:50:51 -0700201 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
202 "Empty TLV obtained from the Interest parameter."));
203 return;
204 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700205
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700206 NDN_LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700207 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
tylerliu0790bdb2020-10-02 00:50:51 -0700208 "Unrecognized self-signed certificate."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700209 return;
210 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700211
tylerliu0790bdb2020-10-02 00:50:51 -0700212 if (ecdhPub == "") {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700213 NDN_LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700214 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
215 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700216 return;
217 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700218
219 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700220 ECDHState ecdh;
221 auto myEcdhPubKeyBase64 = ecdh.getBase64PubKey();
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700222 try {
tylerliu0790bdb2020-10-02 00:50:51 -0700223 ecdh.deriveSecret(ecdhPub);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700224 }
225 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700226 NDN_LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700227 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
228 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700229 return;
230 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700231 // generate salt for HKDF
232 auto saltInt = random::generateSecureWord64();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700233 // hkdf
tylerliu8e170d62020-09-30 01:31:53 -0700234 uint8_t aesKey[AES_128_KEY_LEN];
Zhiyi Zhangcfad98d2020-10-11 11:25:14 -0700235 hkdf(ecdh.m_sharedSecret, ecdh.m_sharedSecretLen, (uint8_t*)&saltInt, sizeof(saltInt), aesKey, sizeof(aesKey));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700236
tylerliud9239f52020-09-30 17:25:16 -0700237 // verify identity name
238 if (!m_config.m_caItem.m_caPrefix.isPrefixOf(clientCert->getIdentity())
tylerliua7bea662020-10-08 18:51:02 -0700239 || !security::Certificate::isValidName(clientCert->getName())
tylerliud9239f52020-09-30 17:25:16 -0700240 || clientCert->getIdentity().size() <= m_config.m_caItem.m_caPrefix.size()) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700241 NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
tylerliud9239f52020-09-30 17:25:16 -0700242 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
243 "An invalid certificate name is being requested."));
244 return;
245 }
246 if (m_config.m_caItem.m_maxSuffixLength) {
247 if (clientCert->getIdentity().size() > m_config.m_caItem.m_caPrefix.size() + *m_config.m_caItem.m_maxSuffixLength) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700248 NDN_LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
tylerliud9239f52020-09-30 17:25:16 -0700249 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
250 "An invalid certificate name is being requested."));
251 return;
252 }
253 }
254
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700255 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700256 // check the validity period
257 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
258 auto currentTime = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700259 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700260 expectedPeriod.second > currentTime + m_config.m_caItem.m_maxValidityPeriod ||
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700261 expectedPeriod.second <= expectedPeriod.first) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700262 NDN_LOG_ERROR("An invalid validity period is being requested.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700263 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
264 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700265 return;
266 }
tylerliud9239f52020-09-30 17:25:16 -0700267
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700268 // verify signature
tylerliu4a00aad2020-09-26 02:03:17 -0700269 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700270 NDN_LOG_ERROR("Invalid signature in the self-signed certificate.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700271 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
272 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700273 return;
274 }
275 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700276 NDN_LOG_ERROR("Invalid signature in the Interest packet.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700277 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
278 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700279 return;
280 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700281 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700282 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700283 //verify cert is from this CA
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700284 const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caItem.m_caPrefix).getDefaultKey().getDefaultCertificate();
tylerliu4a00aad2020-09-26 02:03:17 -0700285 if (!security::verifySignature(*clientCert, cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700286 NDN_LOG_ERROR("Invalid signature in the certificate to revoke.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700287 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
288 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700289 return;
290 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700291 }
292
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700293 // create new request instance
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700294 uint8_t requestIdData[32];
295 Block certNameTlv = clientCert->getName().wireEncode();
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700296 try {
297 hmac_sha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData);
298 }
299 catch (const std::runtime_error& e) {
300 NDN_LOG_ERROR("Error computing the request ID: " << std::string(e.what()));
301 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
302 "Error computing the request ID."));
303 return;
304 }
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700305 CaState requestState(m_config.m_caItem.m_caPrefix, toHex(requestIdData, 32),
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700306 requestType, Status::BEFORE_CHALLENGE, *clientCert,
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700307 makeBinaryBlock(ndn::tlv::ContentType_Key, aesKey, sizeof(aesKey)));
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700308 try {
309 m_storage->addRequest(requestState);
310 }
311 catch (const std::runtime_error& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700312 NDN_LOG_ERROR("Duplicate Request ID: The same request has been seen before.");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700313 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangf12ee302020-10-14 17:46:44 -0700314 "Duplicate Request ID: The same request has been seen before."));
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700315 return;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700316 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800317 Data result;
318 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700319 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700320 result.setContent(NewRenewRevokeEncoder::encodeDataContent(myEcdhPubKeyBase64,
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700321 std::to_string(saltInt),
322 requestState,
323 m_config.m_caItem.m_supportedChallenges));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700324 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800325 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700326 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700327 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800328 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800329}
330
331void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700332CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800333{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700334 // get certificate request state
tylerliu8704d032020-06-23 10:18:15 -0700335 CaState requestState = getCertificateRequest(request);
tylerliu63900d52020-09-30 03:01:18 -0700336 if (requestState.m_requestId == "") {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700337 NDN_LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700338 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700339 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800340 return;
341 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700342 // verify signature
tylerliu63900d52020-09-30 03:01:18 -0700343 if (!security::verifySignature(request, requestState.m_cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700344 NDN_LOG_ERROR("Invalid Signature in the Interest packet.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700345 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
346 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800347 return;
348 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700349 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700350 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700351 try {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700352 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(),
353 requestState.m_encryptionKey.value(),
354 (const uint8_t*)requestState.m_requestId.c_str(),
355 requestState.m_requestId.size());
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700356 }
357 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700358 NDN_LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu63900d52020-09-30 03:01:18 -0700359 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700360 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700361 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700362 return;
363 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700364 if (paramTLVPayload.size() == 0) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700365 NDN_LOG_ERROR("No parameters are found after decryption.");
tylerliu63900d52020-09-30 03:01:18 -0700366 m_storage->deleteRequest(requestState.m_requestId);
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 parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700369 return;
370 }
tylerliu50d679e2020-10-14 14:08:39 -0700371 Block paramTLV = makeBinaryBlock(tlv::EncryptedPayload, paramTLVPayload.data(), paramTLVPayload.size());
Suyong Won44d0cce2020-05-10 04:07:43 -0700372 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800373
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700374 // load the corresponding challenge module
tylerliu50d679e2020-10-14 14:08:39 -0700375 std::string challengeType = readString(paramTLV.get(tlv::SelectedChallenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800376 auto challenge = ChallengeModule::createChallengeModule(challengeType);
377 if (challenge == nullptr) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700378 NDN_LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700379 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700380 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700381 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800382 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700383
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700384 NDN_LOG_TRACE("CHALLENGE module to be load: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700385 auto errorInfo = challenge->handleChallengeRequest(paramTLV, requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700386 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu63900d52020-09-30 03:01:18 -0700387 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700388 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
389 return;
390 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700391
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700392 Block payload;
tylerliu63900d52020-09-30 03:01:18 -0700393 if (requestState.m_status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700394 // if challenge succeeded
tylerliu63900d52020-09-30 03:01:18 -0700395 if (requestState.m_requestType == RequestType::NEW) {
396 auto issuedCert = issueCertificate(requestState);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700397 requestState.m_cert = issuedCert;
398 requestState.m_status = Status::SUCCESS;
tylerliu63900d52020-09-30 03:01:18 -0700399 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700400
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700401 payload = ChallengeEncoder::encodeDataContent(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700402 payload.parse();
tylerliu50d679e2020-10-14 14:08:39 -0700403 payload.push_back(makeNestedBlock(tlv::IssuedCertName, issuedCert.getName()));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700404 payload.encode();
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700405 NDN_LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800406 }
tylerliu63900d52020-09-30 03:01:18 -0700407 else if (requestState.m_requestType == RequestType::REVOKE) {
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700408 requestState.m_status = Status::SUCCESS;
tylerliu63900d52020-09-30 03:01:18 -0700409 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700410
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700411 payload = ChallengeEncoder::encodeDataContent(requestState);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700412 NDN_LOG_TRACE("Challenge succeeded. Certificate has been revoked");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700413 }
414 }
415 else {
tylerliu63900d52020-09-30 03:01:18 -0700416 m_storage->updateRequest(requestState);
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700417 payload = ChallengeEncoder::encodeDataContent(requestState);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700418 NDN_LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800419 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700420
421 Data result;
422 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700423 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700424 auto contentBlock = encodeBlockWithAesGcm128(ndn::tlv::Content, requestState.m_encryptionKey.value(),
425 payload.value(), payload.value_size(),
426 (const uint8_t*)requestState.m_requestId.c_str(),
427 requestState.m_requestId.size(),
428 requestState.m_aesBlockCounter);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700429 result.setContent(contentBlock);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700430 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700431 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700432 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700433 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800434 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700435}
436
tylerliua7bea662020-10-08 18:51:02 -0700437security::Certificate
tylerliu8704d032020-06-23 10:18:15 -0700438CaModule::issueCertificate(const CaState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800439{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700440 auto expectedPeriod =
tylerliu63900d52020-09-30 03:01:18 -0700441 requestState.m_cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700442 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
tylerliua7bea662020-10-08 18:51:02 -0700443 security::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700444
tylerliu63900d52020-09-30 03:01:18 -0700445 Name certName = requestState.m_cert.getKeyName();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700446 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800447 newCert.setName(certName);
tylerliu63900d52020-09-30 03:01:18 -0700448 newCert.setContent(requestState.m_cert.getContent());
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700449 NDN_LOG_TRACE("cert request content " << requestState.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800450 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800451 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700452 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700453 m_config.m_caItem.m_caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800454
455 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700456 NDN_LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800457 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800458}
459
tylerliu8704d032020-06-23 10:18:15 -0700460CaState
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700461CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800462{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400463 std::string requestId;
tylerliu8704d032020-06-23 10:18:15 -0700464 CaState requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800465 try {
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700466 requestId = readString(request.getName().at(m_config.m_caItem.m_caPrefix.size() + 2));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700467 }
468 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700469 NDN_LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700470 }
471 try {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700472 NDN_LOG_TRACE("Request Id to query the database " << requestId);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -0700473 requestState = m_storage->getRequest(requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800474 }
475 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700476 NDN_LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800477 }
tylerliu63900d52020-09-30 03:01:18 -0700478 return requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800479}
480
481void
482CaModule::onRegisterFailed(const std::string& reason)
483{
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700484 NDN_LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800485}
486
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700487Data
488CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
489{
490 Data result;
491 result.setName(name);
492 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700493 result.setContent(ErrorEncoder::encodeDataContent(error, errorInfo));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700494 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700495 return result;
496}
497
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700498} // namespace ndncert
499} // namespace ndn