blob: 83a59364bfc4e4bfa7186c9a8af3c04c62dd7ce1 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
swa770de007bc2020-03-24 21:26:21 -07002/**
Davide Pesaventob48bbda2020-07-27 19:41:37 -04003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "ca-module.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070022#include "challenge-module.hpp"
23#include "crypto-support/enc-tlv.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070024#include "protocol-detail/challenge.hpp"
25#include "protocol-detail/error.hpp"
26#include "protocol-detail/info.hpp"
tylerliu4889a782020-09-30 22:47:49 -070027#include "protocol-detail/new-renew-revoke.hpp"
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070028#include "protocol-detail/probe.hpp"
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070029#include <ndn-cxx/metadata-object.hpp>
30#include <ndn-cxx/security/signing-helpers.hpp>
31#include <ndn-cxx/security/verification-helpers.hpp>
32#include <ndn-cxx/util/io.hpp>
33#include <ndn-cxx/util/random.hpp>
tylerliu8d9e7122020-10-06 18:57:06 -070034#include <name-assignments/assignment-funcs.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080035
36namespace ndn {
37namespace ndncert {
38
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070039static const time::seconds DEFAULT_DATA_FRESHNESS_PERIOD = 1_s;
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -070040static const time::seconds REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD = 120_s;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080042_LOG_INIT(ndncert.ca);
43
44CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
45 const std::string& configPath, const std::string& storageType)
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070046 : m_face(face)
47 , m_keyChain(keyChain)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080048{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070049 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050 m_config.load(configPath);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070051 m_storage = CaStorage::createCaStorage(storageType, m_config.m_caItem.m_caPrefix, "");
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -070052 random::generateSecureBytes(m_requestIdGenKey, 32);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080053 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080054}
55
56CaModule::~CaModule()
57{
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070058 for (auto& handle : m_interestFilterHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080060 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070061 for (auto& handle : m_registeredPrefixHandles) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080063 }
64}
65
66void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080067CaModule::registerPrefix()
68{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069 // register prefixes
Zhiyi Zhang9829da92020-09-30 16:19:34 -070070 Name prefix = m_config.m_caItem.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080072
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070073 auto prefixId = m_face.registerPrefix(
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070074 prefix,
75 [&](const Name& name) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070076 // register PROBE prefix
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070077 auto filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
78 bind(&CaModule::onProbe, this, _2));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070079 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070081 // register NEW prefix
82 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070083 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::NEW));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070084 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070086 // register SELECT prefix
87 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
88 bind(&CaModule::onChallenge, this, _2));
89 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070091 // register REVOKE prefix
92 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
Zhiyi Zhang01e91a32020-09-29 12:10:00 -070093 bind(&CaModule::onNewRenewRevoke, this, _2, RequestType::REVOKE));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070094 m_interestFilterHandles.push_back(filterId);
95 _LOG_TRACE("Prefix " << name << " got registered");
96 },
97 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080099}
100
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700101void
102CaModule::setNameAssignmentFunction(const NameAssignmentFunc& handler)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800103{
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700104 m_config.m_nameAssignmentFunc = handler;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800105}
106
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700107void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800109{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800111}
112
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700113shared_ptr<Data>
Zhiyi Zhangf61404d2020-10-07 15:41:07 -0700114CaModule::generateCaProfileMetaData()
swa77020643ac2020-03-26 02:24:45 -0700115{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700116 // @TODO
117 // make metadata a class member variable m_infoMetadata
118 // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one
119 // otherwise, directly reply m_infoMetadata.makeData
120
Zhiyi Zhangf61404d2020-10-07 15:41:07 -0700121 auto infoPacket = generateCaProfileData();
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700122 MetadataObject metadata;
123 metadata.setVersionedName(infoPacket->getName().getPrefix(-1));
124 Name discoveryInterestName(infoPacket->getName().getPrefix(-2));
125 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
126 discoveryInterestName.append(metadataComponent);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700127 auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700128 return make_shared<Data>(metadataData);
129}
130
131shared_ptr<Data>
Zhiyi Zhangf61404d2020-10-07 15:41:07 -0700132CaModule::generateCaProfileData()
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700133{
134 // @TODO
135 // make CaInfo Data packet a class member variable m_infoData
136 // check whether the m_infoData is still valid, if not, then generate a new one
137 // otherwise, directly reply m_infoData
Suyong Won19fba4d2020-05-09 13:39:46 -0700138
139 const auto& pib = m_keyChain.getPib();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700140 const auto& identity = pib.getIdentity(m_config.m_caItem.m_caPrefix);
Suyong Won19fba4d2020-05-09 13:39:46 -0700141 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700142 Block contentTLV = INFO::encodeDataContent(m_config.m_caItem, cert);
swa77020643ac2020-03-26 02:24:45 -0700143
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700144 Name infoPacketName(m_config.m_caItem.m_caPrefix);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700145 infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
146 Data infoData(infoPacketName);
147 infoData.setContent(contentTLV);
148 infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700149 m_keyChain.sign(infoData, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700150 return make_shared<Data>(infoData);
151}
swa77020643ac2020-03-26 02:24:45 -0700152
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700153void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700154CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800155{
swa77020643ac2020-03-26 02:24:45 -0700156 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
157 _LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800158
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700159 // process PROBE requests: collect probe parameters
160 auto parameters = PROBE::decodeApplicationParameters(request.getApplicationParameters());
tylerliu8fc08072020-10-06 15:37:40 -0700161 std::vector<PartialName> availableComponents;
tylerliu355370e2020-10-07 14:56:33 -0700162 if (m_config.m_nameAssignmentFunc) {
163 try {
164 availableComponents = m_config.m_nameAssignmentFunc(parameters);
165 }
166 catch (const std::exception &e) {
167 _LOG_TRACE("Cannot parse probe parameters: " << e.what());
168 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
169 "Cannot parse probe parameters: " + std::string(e.what())));
170 return;
171 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700172 }
173 std::vector<Name> availableNames;
174 for (const auto& component : availableComponents) {
175 Name newIdentityName = m_config.m_caItem.m_caPrefix;
176 newIdentityName.append(component);
177 availableNames.push_back(newIdentityName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700178 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700179
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700180 Data result;
181 result.setName(request.getName());
Suyong Wone2afeb52020-10-04 03:05:39 +0900182 result.setContent(PROBE::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength, m_config.m_redirection));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700183 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
184 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
185 m_face.put(result);
186 _LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800187}
188
189void
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700190CaModule::onNewRenewRevoke(const Interest& request, RequestType requestType)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800191{
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700192 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700193 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700194 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700195 const auto& parameterTLV = request.getApplicationParameters();
tylerliu0790bdb2020-10-02 00:50:51 -0700196 std::string ecdhPub;
197 shared_ptr<security::v2::Certificate> clientCert;
198 try {
199 NEW_RENEW_REVOKE::decodeApplicationParameters(parameterTLV, requestType, ecdhPub, clientCert);
200 } catch (const std::exception& e) {
201 if (!parameterTLV.hasValue()) {
202 _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
203 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
204 "Empty TLV obtained from the Interest parameter."));
205 return;
206 }
Suyong Won7968f7a2020-05-12 01:01:25 -0700207
tylerliu0790bdb2020-10-02 00:50:51 -0700208 _LOG_ERROR("Unrecognized self-signed certificate: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700209 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
tylerliu0790bdb2020-10-02 00:50:51 -0700210 "Unrecognized self-signed certificate."));
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700211 return;
212 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700213
tylerliu0790bdb2020-10-02 00:50:51 -0700214 if (ecdhPub == "") {
Suyong Won19fba4d2020-05-09 13:39:46 -0700215 _LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700216 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
217 "Empty ECDH PUB obtained from the Interest parameter."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700218 return;
219 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700220
221 // get server's ECDH pub key
tylerliu8e170d62020-09-30 01:31:53 -0700222 ECDHState ecdh;
223 auto myEcdhPubKeyBase64 = ecdh.getBase64PubKey();
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700224 try {
tylerliu0790bdb2020-10-02 00:50:51 -0700225 ecdh.deriveSecret(ecdhPub);
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700226 }
227 catch (const std::exception& e) {
228 _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700229 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
230 "Cannot derive a shared secret using the provided ECDH key."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700231 return;
232 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700233 // generate salt for HKDF
234 auto saltInt = random::generateSecureWord64();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700235 // hkdf
tylerliu8e170d62020-09-30 01:31:53 -0700236 uint8_t aesKey[AES_128_KEY_LEN];
237 hkdf(ecdh.context->sharedSecret, ecdh.context->sharedSecretLen,
238 (uint8_t*)&saltInt, sizeof(saltInt), aesKey, sizeof(aesKey));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700239
tylerliud9239f52020-09-30 17:25:16 -0700240 // verify identity name
241 if (!m_config.m_caItem.m_caPrefix.isPrefixOf(clientCert->getIdentity())
242 || !security::v2::Certificate::isValidName(clientCert->getName())
243 || clientCert->getIdentity().size() <= m_config.m_caItem.m_caPrefix.size()) {
244 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
245 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
246 "An invalid certificate name is being requested."));
247 return;
248 }
249 if (m_config.m_caItem.m_maxSuffixLength) {
250 if (clientCert->getIdentity().size() > m_config.m_caItem.m_caPrefix.size() + *m_config.m_caItem.m_maxSuffixLength) {
251 _LOG_ERROR("An invalid certificate name is being requested " << clientCert->getName());
252 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::NAME_NOT_ALLOWED,
253 "An invalid certificate name is being requested."));
254 return;
255 }
256 }
257
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700258 if (requestType == RequestType::NEW) {
tylerliu4a00aad2020-09-26 02:03:17 -0700259 // check the validity period
260 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
261 auto currentTime = time::system_clock::now();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700262 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD ||
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700263 expectedPeriod.second > currentTime + m_config.m_caItem.m_maxValidityPeriod ||
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700264 expectedPeriod.second <= expectedPeriod.first) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700265 _LOG_ERROR("An invalid validity period is being requested.");
266 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_VALIDITY_PERIOD,
267 "An invalid validity period is being requested."));
tylerliu4a00aad2020-09-26 02:03:17 -0700268 return;
269 }
tylerliud9239f52020-09-30 17:25:16 -0700270
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700271 // verify signature
tylerliu4a00aad2020-09-26 02:03:17 -0700272 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700273 _LOG_ERROR("Invalid signature in the self-signed certificate.");
274 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
275 "Invalid signature in the self-signed certificate."));
tylerliu4a00aad2020-09-26 02:03:17 -0700276 return;
277 }
278 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700279 _LOG_ERROR("Invalid signature in the Interest packet.");
280 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
281 "Invalid signature in the Interest packet."));
tylerliu4a00aad2020-09-26 02:03:17 -0700282 return;
283 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700284 }
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700285 else if (requestType == RequestType::REVOKE) {
tylerliud9239f52020-09-30 17:25:16 -0700286 //verify cert is from this CA
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700287 const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caItem.m_caPrefix).getDefaultKey().getDefaultCertificate();
tylerliu4a00aad2020-09-26 02:03:17 -0700288 if (!security::verifySignature(*clientCert, cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700289 _LOG_ERROR("Invalid signature in the certificate to revoke.");
290 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
291 "Invalid signature in the certificate to revoke."));
tylerliu4a00aad2020-09-26 02:03:17 -0700292 return;
293 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700294 }
295
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700296 // create new request instance
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700297 uint8_t requestIdData[32];
298 Block certNameTlv = clientCert->getName().wireEncode();
299 ndn_compute_hmac_sha256(certNameTlv.wire(), certNameTlv.size(), m_requestIdGenKey, 32, requestIdData);
300 CaState requestState(m_config.m_caItem.m_caPrefix, hexlify(requestIdData, 32),
301 requestType, Status::BEFORE_CHALLENGE, *clientCert,
302 makeBinaryBlock(tlv::ContentType_Key, aesKey, sizeof(aesKey)));
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700303 try {
304 m_storage->addRequest(requestState);
305 }
306 catch (const std::runtime_error& e) {
Zhiyi Zhanga2c39f72020-10-06 16:45:55 -0700307 _LOG_ERROR("Duplicate Request ID: The same request has been seen before.");
308 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
309 "Duplicate Request ID: The same request has been seen before.."));
310 return;
Zhiyi Zhang21d52b52020-10-05 18:29:15 -0700311 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800312 Data result;
313 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700314 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu4889a782020-09-30 22:47:49 -0700315 result.setContent(NEW_RENEW_REVOKE::encodeDataContent(myEcdhPubKeyBase64,
tylerliu4a00aad2020-09-26 02:03:17 -0700316 std::to_string(saltInt),
tylerliu63900d52020-09-30 03:01:18 -0700317 requestState,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700318 m_config.m_caItem.m_supportedChallenges));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700319 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800320 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700321 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700322 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800323 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800324}
325
326void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700327CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800328{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700329 // get certificate request state
tylerliu8704d032020-06-23 10:18:15 -0700330 CaState requestState = getCertificateRequest(request);
tylerliu63900d52020-09-30 03:01:18 -0700331 if (requestState.m_requestId == "") {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700332 _LOG_ERROR("No certificate request state can be found.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700333 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700334 "No certificate request state can be found."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800335 return;
336 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700337 // verify signature
tylerliu63900d52020-09-30 03:01:18 -0700338 if (!security::verifySignature(request, requestState.m_cert)) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700339 _LOG_ERROR("Invalid Signature in the Interest packet.");
340 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE,
341 "Invalid Signature in the Interest packet."));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800342 return;
343 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700344 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700345 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700346 try {
tylerliu63900d52020-09-30 03:01:18 -0700347 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), requestState.m_encryptionKey.value(),
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700348 (uint8_t*)"test", strlen("test"));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700349 }
350 catch (const std::exception& e) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700351 _LOG_ERROR("Interest paramaters decryption failed: " << e.what());
tylerliu63900d52020-09-30 03:01:18 -0700352 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700353 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700354 "Interest paramaters decryption failed."));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700355 return;
356 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700357 if (paramTLVPayload.size() == 0) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700358 _LOG_ERROR("No parameters are found after decryption.");
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 "No parameters are found after decryption."));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700362 return;
363 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700364 Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size());
365 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800366
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700367 // load the corresponding challenge module
Suyong Won19fba4d2020-05-09 13:39:46 -0700368 std::string challengeType = readString(paramTLV.get(tlv_selected_challenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800369 auto challenge = ChallengeModule::createChallengeModule(challengeType);
370 if (challenge == nullptr) {
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700371 _LOG_TRACE("Unrecognized challenge type: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700372 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700373 m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type."));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700374 return;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800375 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700376
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700377 _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
tylerliu63900d52020-09-30 03:01:18 -0700378 auto errorInfo = challenge->handleChallengeRequest(paramTLV, requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700379 if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
tylerliu63900d52020-09-30 03:01:18 -0700380 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700381 m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
382 return;
383 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700384
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700385 Block payload;
tylerliu63900d52020-09-30 03:01:18 -0700386 if (requestState.m_status == Status::PENDING) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700387 // if challenge succeeded
tylerliu63900d52020-09-30 03:01:18 -0700388 if (requestState.m_requestType == RequestType::NEW) {
389 auto issuedCert = issueCertificate(requestState);
390 requestState.m_cert = issuedCert;
391 requestState.m_status = Status::SUCCESS;
tylerliu63900d52020-09-30 03:01:18 -0700392 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700393
tylerliu63900d52020-09-30 03:01:18 -0700394 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700395 payload.parse();
396 payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
397 payload.encode();
Zhiyi Zhang01e91a32020-09-29 12:10:00 -0700398 _LOG_TRACE("Challenge succeeded. Certificate has been issued: " << issuedCert.getName());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800399 }
tylerliu63900d52020-09-30 03:01:18 -0700400 else if (requestState.m_requestType == RequestType::REVOKE) {
401 requestState.m_status = Status::SUCCESS;
402 m_storage->deleteRequest(requestState.m_requestId);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700403
tylerliu63900d52020-09-30 03:01:18 -0700404 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700405 _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
406 }
407 }
408 else {
tylerliu63900d52020-09-30 03:01:18 -0700409 m_storage->updateRequest(requestState);
410 payload = CHALLENGE::encodeDataPayload(requestState);
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700411 _LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800412 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700413
414 Data result;
415 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700416 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu63900d52020-09-30 03:01:18 -0700417 auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, requestState.m_encryptionKey.value(), payload.value(),
Suyong Won7968f7a2020-05-12 01:01:25 -0700418 payload.value_size(), (uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700419 result.setContent(contentBlock);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700420 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700421 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700422 if (m_config.m_statusUpdateCallback) {
tylerliu63900d52020-09-30 03:01:18 -0700423 m_config.m_statusUpdateCallback(requestState);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800424 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700425}
426
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800427security::v2::Certificate
tylerliu8704d032020-06-23 10:18:15 -0700428CaModule::issueCertificate(const CaState& requestState)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800429{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700430 auto expectedPeriod =
tylerliu63900d52020-09-30 03:01:18 -0700431 requestState.m_cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700432 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800433 security::v2::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700434
tylerliu63900d52020-09-30 03:01:18 -0700435 Name certName = requestState.m_cert.getKeyName();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700436 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800437 newCert.setName(certName);
tylerliu63900d52020-09-30 03:01:18 -0700438 newCert.setContent(requestState.m_cert.getContent());
439 _LOG_TRACE("cert request content " << requestState.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800440 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800441 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700442 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700443 m_config.m_caItem.m_caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800444
445 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700446 _LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800447 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800448}
449
tylerliu8704d032020-06-23 10:18:15 -0700450CaState
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700451CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800452{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400453 std::string requestId;
tylerliu8704d032020-06-23 10:18:15 -0700454 CaState requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800455 try {
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700456 requestId = readString(request.getName().at(m_config.m_caItem.m_caPrefix.size() + 2));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700457 }
458 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700459 _LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700460 }
461 try {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700462 _LOG_TRACE("Request Id to query the database " << requestId);
tylerliu63900d52020-09-30 03:01:18 -0700463 requestState = m_storage->getRequest(requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800464 }
465 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700466 _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800467 }
tylerliu63900d52020-09-30 03:01:18 -0700468 return requestState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800469}
470
471void
472CaModule::onRegisterFailed(const std::string& reason)
473{
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700474 _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800475}
476
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700477Data
478CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
479{
480 Data result;
481 result.setName(name);
482 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
483 result.setContent(ErrorTLV::encodeDataContent(error, errorInfo));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700484 m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700485 return result;
486}
487
488} // namespace ndncert
489} // namespace ndn