blob: 7b806b73e481b926569e988809c9de1d24de77a0 [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 Zhangdbd9d432020-10-07 15:56:27 -070022#include "identity-challenge/challenge-module.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070023#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 Zhang8683ec92020-10-07 18:18:35 -070034#include <ndn-cxx/util/string-helper.hpp>
35#include "name-assignments/assignment-funcs.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 Zhangaf7c2902019-03-14 22:13:21 -070042
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080043_LOG_INIT(ndncert.ca);
44
45CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
46 const std::string& configPath, const std::string& storageType)
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070047 : 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 Zhangd1d9f5a2020-10-05 18:04:23 -070052 m_storage = CaStorage::createCaStorage(storageType, m_config.m_caItem.m_caPrefix, "");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070053 random::generateSecureBytes(m_requestIdGenKey, 32);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070054 if (m_config.m_heuristic.size() == 0) {
55 m_config.m_heuristic.push_back(NameAssignmentFuncFactory::createNameAssignmentFuncFactory("random"));
56 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080057 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080058}
59
60CaModule::~CaModule()
61{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070062 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080064 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070065 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080067 }
68}
69
70void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080071CaModule::registerPrefix()
72{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 // register prefixes
Zhiyi Zhang9829da92020-09-30 16:19:34 -070074 Name prefix = m_config.m_caItem.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080076
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070077 auto prefixId = m_face.registerPrefix(
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070078 prefix,
79 [&](const Name& name) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070080 // register PROBE prefix
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070081 auto filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
82 bind(&CaModule::onProbe, this, _2));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070083 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070085 // register NEW prefix
86 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070087 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070088 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070090 // register SELECT prefix
91 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
92 bind(&CaModule::onChallenge, this, _2));
93 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070094
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070095 // register REVOKE prefix
96 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070097 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070098 m_interestFilterHandles.push_back(filterId);
99 _LOG_TRACE("Prefix " << name << " got registered");
100 },
101 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800103}
104
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700105void
106CaModule::setNameAssignmentFunction(const NameAssignmentFunc& handler)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800107{
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700108 m_config.m_nameAssignmentFunc = handler;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800109}
110
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700111void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700112CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800113{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700114 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800115}
116
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700117shared_ptr<Data>
Zhiyi Zhangf61404d2020-10-07 15:41:07 -0700118CaModule::generateCaProfileMetaData()
swa77020643ac2020-03-26 02:24:45 -0700119{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700120 // @TODO
121 // make metadata a class member variable m_infoMetadata
122 // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one
123 // otherwise, directly reply m_infoMetadata.makeData
124
Zhiyi Zhangf61404d2020-10-07 15:41:07 -0700125 auto infoPacket = generateCaProfileData();
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700126 MetadataObject metadata;
127 metadata.setVersionedName(infoPacket->getName().getPrefix(-1));
128 Name discoveryInterestName(infoPacket->getName().getPrefix(-2));
129 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
130 discoveryInterestName.append(metadataComponent);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700131 auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700132 return make_shared<Data>(metadataData);
133}
134
135shared_ptr<Data>
Zhiyi Zhangf61404d2020-10-07 15:41:07 -0700136CaModule::generateCaProfileData()
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700137{
138 // @TODO
139 // make CaInfo Data packet a class member variable m_infoData
140 // check whether the m_infoData is still valid, if not, then generate a new one
141 // otherwise, directly reply m_infoData
Suyong Won19fba4d2020-05-09 13:39:46 -0700142
143 const auto& pib = m_keyChain.getPib();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700144 const auto& identity = pib.getIdentity(m_config.m_caItem.m_caPrefix);
Suyong Won19fba4d2020-05-09 13:39:46 -0700145 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700146 Block contentTLV = INFO::encodeDataContent(m_config.m_caItem, cert);
swa77020643ac2020-03-26 02:24:45 -0700147
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700148 Name infoPacketName(m_config.m_caItem.m_caPrefix);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700149 infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
150 Data infoData(infoPacketName);
151 infoData.setContent(contentTLV);
152 infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700153 m_keyChain.sign(infoData, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700154 return make_shared<Data>(infoData);
155}
swa77020643ac2020-03-26 02:24:45 -0700156
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700157void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700158CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800159{
swa77020643ac2020-03-26 02:24:45 -0700160 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
161 _LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800162
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700163 // process PROBE requests: collect probe parameters
164 auto parameters = PROBE::decodeApplicationParameters(request.getApplicationParameters());
tylerliu8fc08072020-10-06 15:37:40 -0700165 std::vector<PartialName> availableComponents;
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700166 for (auto& item : m_config.m_heuristic) {
167 auto names = item->assignName(parameters);
168 availableComponents.insert(availableComponents.end(), names.begin(), names.end());
tylerliu355370e2020-10-07 14:56:33 -0700169 }
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700170 if (availableComponents.size() == 0) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700171 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -0700172 "Cannot generate available names from parameters provided."));
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700173 return;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700174 }
175 std::vector<Name> availableNames;
176 for (const auto& component : availableComponents) {
177 Name newIdentityName = m_config.m_caItem.m_caPrefix;
178 newIdentityName.append(component);
179 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700180 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700181
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700182 Data result;
183 result.setName(request.getName());
Suyong Wone2afeb52020-10-04 03:05:39 +0900184 result.setContent(PROBE::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength, m_config.m_redirection));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700185 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
186 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
187 m_face.put(result);
188 _LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800189}
190
191void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700192CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800193{
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700194 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700195 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700196 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700197 const auto& parameterTLV = request.getApplicationParameters();
tylerliu0790bdb2020-10-02 00:50:51 -0700198 std::string ecdhPub;
199 shared_ptr<security::v2::Certificate> clientCert;
200 try {
201 NEW_RENEW_REVOKE::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert);
202 } catch (const std::exception& e) {
203 if (!parameterTLV.hasValue()) {
204 _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
205 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
206 "Empty TLV obtained from the Interest parameter."));
207 return;
208 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700209
tylerliu0790bdb2020-10-02 00:50:51 -0700210 _LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700211 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
tylerliu0790bdb2020-10-02 00:50:51 -0700212 "Unrecognized self-signed certificate."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700213 return;
214 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700215
tylerliu0790bdb2020-10-02 00:50:51 -0700216 if (ecdhPub == "") {
Suyong Won19fba4d2020-05-09 13:39:46 -0700217 _LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700218 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
219 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700220 return;
221 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700222
223 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700224 ECDHState ecdh;
225 auto myEcdhPubKeyBase64 = ecdh.getBase64PubKey();
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700226 try {
tylerliu0790bdb2020-10-02 00:50:51 -0700227 ecdh.deriveSecret(ecdhPub);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700228 }
229 catch (const std::exception& e) {
230 _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700231 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
232 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700233 return;
234 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700235 // generate salt for HKDF
236 auto saltInt = random::generateSecureWord64();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700237 // hkdf
tylerliu8e170d62020-09-30 01:31:53 -0700238 uint8_t aesKey[AES_128_KEY_LEN];
239 hkdf(ecdh.context->sharedSecret, ecdh.context->sharedSecretLen,
240 (uint8_t*)&saltInt, sizeof(saltInt), aesKey, sizeof(aesKey));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700241
tylerliud9239f52020-09-30 17:25:16 -0700242 // verify identity name
243 if (!m_config.m_caItem.m_caPrefix.isPrefixOf(clientCert->getIdentity())
244 || !security::v2::Certificate::isValidName(clientCert->getName())
245 || clientCert->getIdentity().size() <= m_config.m_caItem.m_caPrefix.size()) {
246 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
247 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
248 "An invalid certificate name is being requested."));
249 return;
250 }
251 if (m_config.m_caItem.m_maxSuffixLength) {
252 if (clientCert->getIdentity().size() > m_config.m_caItem.m_caPrefix.size() + *m_config.m_caItem.m_maxSuffixLength) {
253 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
254 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
255 "An invalid certificate name is being requested."));
256 return;
257 }
258 }
259
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700260 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700261 // check the validity period
262 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
263 auto currentTime = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700264 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700265 expectedPeriod.second > currentTime + m_config.m_caItem.m_maxValidityPeriod ||
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700266 expectedPeriod.second <= expectedPeriod.first) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700267 _LOG_ERROR("An invalid validity period is being requested.");
268 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
269 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700270 return;
271 }
tylerliud9239f52020-09-30 17:25:16 -0700272
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700273 // verify signature
tylerliu4a00aad2020-09-26 02:03:17 -0700274 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700275 _LOG_ERROR("Invalid signature in the self-signed certificate.");
276 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
277 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700278 return;
279 }
280 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700281 _LOG_ERROR("Invalid signature in the Interest packet.");
282 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
283 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700284 return;
285 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700286 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700287 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700288 //verify cert is from this CA
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700289 const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caItem.m_caPrefix).getDefaultKey().getDefaultCertificate();
tylerliu4a00aad2020-09-26 02:03:17 -0700290 if (!security::verifySignature(*clientCert, cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700291 _LOG_ERROR("Invalid signature in the certificate to revoke.");
292 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
293 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700294 return;
295 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700296 }
297
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700298 // create new request instance
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700299 uint8_t requestIdData[32];
300 Block certNameTlv = clientCert->getName().wireEncode();
301 ndn_compute_hmac_sha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -0700302 CaState requestState(m_config.m_caItem.m_caPrefix, toHex(requestIdData, 32),
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700303 requestType, Status::BEFORE_CHALLENGE, *clientCert,
304 makeBinaryBlock(tlv::ContentType_Key, aesKey, sizeof(aesKey)));
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700305 try {
306 m_storage->addRequest(requestState);
307 }
308 catch (const std::runtime_error& e) {
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700309 _LOG_ERROR("Duplicate Request ID: The same request has been seen before.");
310 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
311 "Duplicate Request ID: The same request has been seen before.."));
312 return;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700313 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800314 Data result;
315 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700316 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu4889a782020-09-30 22:47:49 -0700317 result.setContent(NEW_RENEW_REVOKE::encodeDataContent(myEcdhPubKeyBase64,
tylerliu4a00aad2020-09-26 02:03:17 -0700318 std::to_string(saltInt),
tylerliu63900d52020-09-30 03:01:18 -0700319 requestState,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700320 m_config.m_caItem.m_supportedChallenges));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700321 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800322 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700323 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700324 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800325 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800326}
327
328void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700329CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800330{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700331 // get certificate request state
tylerliu8704d032020-06-23 10:18:15 -0700332 CaState requestState = getCertificateRequest(request);
tylerliu63900d52020-09-30 03:01:18 -0700333 if (requestState.m_requestId == "") {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700334 _LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700335 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700336 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800337 return;
338 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700339 // verify signature
tylerliu63900d52020-09-30 03:01:18 -0700340 if (!security::verifySignature(request, requestState.m_cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700341 _LOG_ERROR("Invalid Signature in the Interest packet.");
342 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
343 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800344 return;
345 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700346 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700347 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700348 try {
tylerliu63900d52020-09-30 03:01:18 -0700349 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), requestState.m_encryptionKey.value(),
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700350 (uint8_t*)"test", strlen("test"));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700351 }
352 catch (const std::exception& e) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700353 _LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu63900d52020-09-30 03:01:18 -0700354 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700355 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700356 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700357 return;
358 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700359 if (paramTLVPayload.size() == 0) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700360 _LOG_ERROR("No parameters are found after decryption.");
tylerliu63900d52020-09-30 03:01:18 -0700361 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700362 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700363 "No parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700364 return;
365 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700366 Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size());
367 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800368
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700369 // load the corresponding challenge module
Suyong Won19fba4d2020-05-09 13:39:46 -0700370 std::string challengeType = readString(paramTLV.get(tlv_selected_challenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800371 auto challenge = ChallengeModule::createChallengeModule(challengeType);
372 if (challenge == nullptr) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700373 _LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700374 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700375 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700376 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800377 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700378
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700379 _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700380 auto errorInfo = challenge->handleChallengeRequest(paramTLV, requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700381 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu63900d52020-09-30 03:01:18 -0700382 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700383 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
384 return;
385 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700386
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700387 Block payload;
tylerliu63900d52020-09-30 03:01:18 -0700388 if (requestState.m_status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700389 // if challenge succeeded
tylerliu63900d52020-09-30 03:01:18 -0700390 if (requestState.m_requestType == RequestType::NEW) {
391 auto issuedCert = issueCertificate(requestState);
392 requestState.m_cert = issuedCert;
393 requestState.m_status = Status::SUCCESS;
tylerliu63900d52020-09-30 03:01:18 -0700394 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700395
tylerliu63900d52020-09-30 03:01:18 -0700396 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700397 payload.parse();
398 payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
399 payload.encode();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700400 _LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800401 }
tylerliu63900d52020-09-30 03:01:18 -0700402 else if (requestState.m_requestType == RequestType::REVOKE) {
403 requestState.m_status = Status::SUCCESS;
404 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700405
tylerliu63900d52020-09-30 03:01:18 -0700406 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700407 _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
408 }
409 }
410 else {
tylerliu63900d52020-09-30 03:01:18 -0700411 m_storage->updateRequest(requestState);
412 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700413 _LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800414 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700415
416 Data result;
417 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700418 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu63900d52020-09-30 03:01:18 -0700419 auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, requestState.m_encryptionKey.value(), payload.value(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700420 payload.value_size(), (uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700421 result.setContent(contentBlock);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700422 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700423 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700424 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700425 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800426 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700427}
428
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800429security::v2::Certificate
tylerliu8704d032020-06-23 10:18:15 -0700430CaModule::issueCertificate(const CaState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800431{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700432 auto expectedPeriod =
tylerliu63900d52020-09-30 03:01:18 -0700433 requestState.m_cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700434 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800435 security::v2::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700436
tylerliu63900d52020-09-30 03:01:18 -0700437 Name certName = requestState.m_cert.getKeyName();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700438 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800439 newCert.setName(certName);
tylerliu63900d52020-09-30 03:01:18 -0700440 newCert.setContent(requestState.m_cert.getContent());
441 _LOG_TRACE("cert request content " << requestState.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800442 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800443 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700444 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700445 m_config.m_caItem.m_caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800446
447 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700448 _LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800449 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800450}
451
tylerliu8704d032020-06-23 10:18:15 -0700452CaState
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700453CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800454{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400455 std::string requestId;
tylerliu8704d032020-06-23 10:18:15 -0700456 CaState requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800457 try {
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700458 requestId = readString(request.getName().at(m_config.m_caItem.m_caPrefix.size() + 2));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700459 }
460 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700461 _LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700462 }
463 try {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700464 _LOG_TRACE("Request Id to query the database " << requestId);
tylerliu63900d52020-09-30 03:01:18 -0700465 requestState = m_storage->getRequest(requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800466 }
467 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700468 _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800469 }
tylerliu63900d52020-09-30 03:01:18 -0700470 return requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800471}
472
473void
474CaModule::onRegisterFailed(const std::string& reason)
475{
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700476 _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800477}
478
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700479Data
480CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
481{
482 Data result;
483 result.setName(name);
484 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
485 result.setContent(ErrorTLV::encodeDataContent(error, errorInfo));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700486 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700487 return result;
488}
489
490} // namespace ndncert
491} // namespace ndn