blob: 82f0eec90655cdf85a07d0bfb9f8ec2027863a76 [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"
22#include "challenge-module.hpp"
23#include "logging.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070024#include "crypto-support/enc-tlv.hpp"
Suyong Won19fba4d2020-05-09 13:39:46 -070025#include "protocol-detail/info.hpp"
26#include "protocol-detail/probe.hpp"
27#include "protocol-detail/new.hpp"
28#include "protocol-detail/challenge.hpp"
tylerliu182bc532020-09-25 01:54:45 -070029#include "protocol-detail/revoke.hpp"
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080030#include <ndn-cxx/util/io.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080031#include <ndn-cxx/security/verification-helpers.hpp>
32#include <ndn-cxx/security/signing-helpers.hpp>
Junxiao Shi7c068032017-05-28 13:40:47 +000033#include <ndn-cxx/util/random.hpp>
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070034#include <ndn-cxx/metadata-object.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080035
36namespace ndn {
37namespace ndncert {
38
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070039static const int IS_SUBNAME_MIN_OFFSET = 5;
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)
47 : m_face(face)
48 , m_keyChain(keyChain)
49{
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);
52 m_storage = CaStorage::createCaStorage(storageType);
53
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080054 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080055}
56
57CaModule::~CaModule()
58{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 for (auto handle : m_interestFilterHandles) {
60 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080061 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 for (auto handle : m_registeredPrefixHandles) {
63 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080064 }
65}
66
67void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080068CaModule::registerPrefix()
69{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070070 // register localhop discovery prefix
swa77020643ac2020-03-26 02:24:45 -070071 Name localhopInfoPrefix("/localhop/CA/INFO");
72 auto prefixId = m_face.setInterestFilter(InterestFilter(localhopInfoPrefix),
73 bind(&CaModule::onInfo, this, _2),
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080074 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075 m_registeredPrefixHandles.push_back(prefixId);
swa77020643ac2020-03-26 02:24:45 -070076 _LOG_TRACE("Prefix " << localhopInfoPrefix << " got registered");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080077
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 // register prefixes
Suyong Won256c9062020-05-11 02:45:56 -070079 Name prefix = m_config.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080081
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070082 prefixId = m_face.registerPrefix(prefix,
83 [&] (const Name& name) {
swa77020643ac2020-03-26 02:24:45 -070084 // register INFO prefix
85 auto filterId = m_face.setInterestFilter(Name(name).append("INFO"),
86 bind(&CaModule::onInfo, this, _2));
87 m_interestFilterHandles.push_back(filterId);
88
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089 // register PROBE prefix
swa77020643ac2020-03-26 02:24:45 -070090 filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
91 bind(&CaModule::onProbe, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092 m_interestFilterHandles.push_back(filterId);
93
94 // register NEW prefix
swa770de007bc2020-03-24 21:26:21 -070095 filterId = m_face.setInterestFilter(Name(name).append("NEW"),
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096 bind(&CaModule::onNew, this, _2));
97 m_interestFilterHandles.push_back(filterId);
98
99 // register SELECT prefix
swa770de007bc2020-03-24 21:26:21 -0700100 filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101 bind(&CaModule::onChallenge, this, _2));
102 m_interestFilterHandles.push_back(filterId);
103
tylerliu182bc532020-09-25 01:54:45 -0700104 // register REVOKE prefix
105 filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
106 bind(&CaModule::onRevoke, this, _2));
107 m_interestFilterHandles.push_back(filterId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 _LOG_TRACE("Prefix " << name << " got registered");
109 },
110 bind(&CaModule::onRegisterFailed, this, _2));
111 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800112}
113
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700114void
115CaModule::setNameAssignmentFunction(const NameAssignmentFunc& handler)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800116{
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700117 m_config.m_nameAssignmentFunc = handler;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800118}
119
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800120bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800122{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800124 return false;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800125}
126
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700127shared_ptr<Data>
128CaModule::generateCaConfigMetaData()
swa77020643ac2020-03-26 02:24:45 -0700129{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700130 // @TODO
131 // make metadata a class member variable m_infoMetadata
132 // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one
133 // otherwise, directly reply m_infoMetadata.makeData
134
135 auto infoPacket = generateCaConfigData();
136 MetadataObject metadata;
137 metadata.setVersionedName(infoPacket->getName().getPrefix(-1));
138 Name discoveryInterestName(infoPacket->getName().getPrefix(-2));
139 name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
140 discoveryInterestName.append(metadataComponent);
141 auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caPrefix));
142 return make_shared<Data>(metadataData);
143}
144
145shared_ptr<Data>
146CaModule::generateCaConfigData()
147{
148 // @TODO
149 // make CaInfo Data packet a class member variable m_infoData
150 // check whether the m_infoData is still valid, if not, then generate a new one
151 // otherwise, directly reply m_infoData
Suyong Won19fba4d2020-05-09 13:39:46 -0700152
153 const auto& pib = m_keyChain.getPib();
Suyong Won256c9062020-05-11 02:45:56 -0700154 const auto& identity = pib.getIdentity(m_config.m_caPrefix);
Suyong Won19fba4d2020-05-09 13:39:46 -0700155 const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhang61c43a62020-09-28 12:56:01 -0700156 Block contentTLV = INFO::encodeDataContent(m_config, cert);
swa77020643ac2020-03-26 02:24:45 -0700157
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700158 Name infoPacketName(m_config.m_caPrefix);
159 infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
160 Data infoData(infoPacketName);
161 infoData.setContent(contentTLV);
162 infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
163 m_keyChain.sign(infoData, signingByIdentity(m_config.m_caPrefix));
164 return make_shared<Data>(infoData);
165}
swa77020643ac2020-03-26 02:24:45 -0700166
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700167void
168CaModule::onInfo(const Interest& request)
169{
170 _LOG_TRACE("Received INFO request");
171
172 if (request.getName().get(-1).type() == 32) {
173 m_face.put(*generateCaConfigMetaData());
174 }
175 else {
176 m_face.put(*generateCaConfigData());
177 }
swa77020643ac2020-03-26 02:24:45 -0700178
179 _LOG_TRACE("Handle INFO: send out the INFO response");
180}
181
182void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700183CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800184{
swa77020643ac2020-03-26 02:24:45 -0700185 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
186 _LOG_TRACE("Received PROBE request");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800187
swa77020643ac2020-03-26 02:24:45 -0700188 // process PROBE requests: find an available name
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700189 std::string availableId = "";
Suyong Won19fba4d2020-05-09 13:39:46 -0700190 const auto& parameterTLV = request.getApplicationParameters();
Suyong Won44d0cce2020-05-10 04:07:43 -0700191 parameterTLV.parse();
Suyong Won19fba4d2020-05-09 13:39:46 -0700192 if (!parameterTLV.hasValue()) {
193 _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
swa77020643ac2020-03-26 02:24:45 -0700194 return;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700195 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700196
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700197 // if (m_config.m_nameAssignmentFunc) {
198 // try {
199 // availableId = m_config.m_nameAssignmentFunc(parameterTLV);
200 // }
201 // catch (const std::exception& e) {
202 // _LOG_TRACE("Cannot find PROBE input from PROBE parameters: " << e.what());
203 // return;
204 // }
205 // }
206 // else {
207 // // if there is no app-specified name lookup, use a random name id
208 // availableId = std::to_string(random::generateSecureWord64());
209 // }
210 // Name newIdentityName = m_config.m_caPrefix;
211 // newIdentityName.append(availableId);
212 // _LOG_TRACE("Handle PROBE: generate an identity " << newIdentityName);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800213
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700214 // Block contentTLV = PROBE::encodeDataContent(newIdentityName.toUri(), m_config.m_probe, parameterTLV);
215
216 // Data result;
217 // result.setName(request.getName());
218 // result.setContent(contentTLV);
219 // result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
220 // m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix));
221 // m_face.put(result);
222 // _LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800223}
224
225void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700226CaModule::onNew(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800227{
tylerliu4a00aad2020-09-26 02:03:17 -0700228 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
229 onRequestInit(request, REQUEST_TYPE_NEW);
230}
231
232void
233CaModule::onRevoke(const Interest& request)
234{
235 // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
236 onRequestInit(request, REQUEST_TYPE_REVOKE);
237}
238
239void
240CaModule::onRequestInit(const Interest& request, int requestType)
241{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700242 // get ECDH pub key and cert request
Suyong Won19fba4d2020-05-09 13:39:46 -0700243 const auto& parameterTLV = request.getApplicationParameters();
Suyong Won7968f7a2020-05-12 01:01:25 -0700244 parameterTLV.parse();
245
Suyong Won19fba4d2020-05-09 13:39:46 -0700246 if (!parameterTLV.hasValue()) {
247 _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700248 return;
249 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700250
251 std::string peerKeyBase64 = readString(parameterTLV.get(tlv_ecdh_pub));
252
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700253 if (peerKeyBase64 == "") {
Suyong Won19fba4d2020-05-09 13:39:46 -0700254 _LOG_ERROR("Empty ECDH PUB obtained from the Interest parameter.");
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700255 return;
256 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700257
258 // get server's ECDH pub key
259 auto myEcdhPubKeyBase64 = m_ecdh.getBase64PubKey();
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700260 try {
261 m_ecdh.deriveSecret(peerKeyBase64);
262 }
263 catch (const std::exception& e) {
264 _LOG_ERROR("Cannot derive a shared secret using the provided ECDH key: " << e.what());
265 return;
266 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700267 // generate salt for HKDF
268 auto saltInt = random::generateSecureWord64();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700269 // hkdf
270 hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen,
Zhiyi Zhang36706832019-07-04 21:33:03 -0700271 (uint8_t*)&saltInt, sizeof(saltInt), m_aesKey, sizeof(m_aesKey));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700272
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700273 shared_ptr<security::v2::Certificate> clientCert = nullptr;
Suyong Won7968f7a2020-05-12 01:01:25 -0700274
tylerliu4a00aad2020-09-26 02:03:17 -0700275 if (requestType == REQUEST_TYPE_NEW) {
276 // parse certificate request
277 Block cert_req = parameterTLV.get(tlv_cert_request);
278 cert_req.parse();
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700279
tylerliu4a00aad2020-09-26 02:03:17 -0700280 try {
281 security::v2::Certificate cert = security::v2::Certificate(cert_req.get(tlv::Data));
282 clientCert = make_shared<security::v2::Certificate>(cert);
283 }
284 catch (const std::exception &e) {
285 _LOG_ERROR("Unrecognized certificate request: " << e.what());
286 return;
287 }
288 // check the validity period
289 auto expectedPeriod = clientCert->getValidityPeriod().getPeriod();
290 auto currentTime = time::system_clock::now();
291 if (expectedPeriod.first < currentTime - REQUEST_VALIDITY_PERIOD_NOT_BEFORE_GRACE_PERIOD) {
292 _LOG_ERROR("Client requests a too old notBefore timepoint.");
293 return;
294 }
295 if (expectedPeriod.second > currentTime + m_config.m_maxValidityPeriod ||
296 expectedPeriod.second <= expectedPeriod.first) {
297 _LOG_ERROR("Client requests an invalid validity period or a notAfter timepoint beyond the allowed time period.");
298 return;
299 }
300
301 // verify the self-signed certificate, the request, and the token
302 if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName()) // under ca prefix
303 || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name
tylerliu0b6d0db2020-09-28 17:52:02 -0700304 || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET
305 || clientCert->getName().size() >
306 m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET - 1 + m_config.m_maxSuffixLength) {
tylerliu4a00aad2020-09-26 02:03:17 -0700307 _LOG_ERROR("Invalid self-signed certificate name " << clientCert->getName());
308 return;
309 }
310 if (!security::verifySignature(*clientCert, *clientCert)) {
311 _LOG_ERROR("Cert request with bad signature.");
312 return;
313 }
314 if (!security::verifySignature(request, *clientCert)) {
315 _LOG_ERROR("Interest with bad signature.");
316 return;
317 }
318 } else if (requestType == REQUEST_TYPE_REVOKE) {
319 // parse certificate request
320 Block cert_revoke = parameterTLV.get(tlv_cert_to_revoke);
321 cert_revoke.parse();
322
323 try {
324 security::v2::Certificate cert = security::v2::Certificate(cert_revoke.get(tlv::Data));
325 clientCert = make_shared<security::v2::Certificate>(cert);
326 }
327 catch (const std::exception &e) {
328 _LOG_ERROR("Unrecognized certificate: " << e.what());
329 return;
330 }
331
332 // verify the certificate
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700333 if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName()) // under ca prefix
tylerliu4a00aad2020-09-26 02:03:17 -0700334 || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name
tylerliu0b6d0db2020-09-28 17:52:02 -0700335 || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET
336 || clientCert->getName().size() >
337 m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET - 1 + m_config.m_maxSuffixLength) {
tylerliu4a00aad2020-09-26 02:03:17 -0700338 _LOG_ERROR("Invalid certificate name " << clientCert->getName());
339 return;
340 }
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700341 const auto& cert = m_keyChain.getPib().getIdentity(m_config.m_caPrefix).getDefaultKey().getDefaultCertificate();
tylerliu4a00aad2020-09-26 02:03:17 -0700342 if (!security::verifySignature(*clientCert, cert)) {
343 _LOG_ERROR("Cert request with bad signature.");
344 return;
345 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700346 }
347
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700348 // create new request instance
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800349 std::string requestId = std::to_string(random::generateWord64());
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700350 CertificateRequest certRequest(m_config.m_caPrefix, requestId, requestType, Status::BEFORE_CHALLENGE, *clientCert);
Suyong Won19fba4d2020-05-09 13:39:46 -0700351
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800352 try {
353 m_storage->addRequest(certRequest);
354 }
355 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700356 _LOG_ERROR("Cannot add new request instance into the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800357 return;
358 }
359
360 Data result;
361 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700362 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
tylerliu4a00aad2020-09-26 02:03:17 -0700363 if (requestType == REQUEST_TYPE_NEW) {
364 result.setContent(NEW::encodeDataContent(myEcdhPubKeyBase64,
365 std::to_string(saltInt),
366 certRequest,
367 m_config.m_supportedChallenges));
368 } else if (requestType == REQUEST_TYPE_REVOKE) {
369 result.setContent(REVOKE::encodeDataContent(myEcdhPubKeyBase64,
370 std::to_string(saltInt),
371 certRequest,
372 m_config.m_supportedChallenges));
373 }
Suyong Won256c9062020-05-11 02:45:56 -0700374 m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800375 m_face.put(result);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700376
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700377 if (m_config.m_statusUpdateCallback) {
378 m_config.m_statusUpdateCallback(certRequest);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800379 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800380}
381
382void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700383CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800384{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700385 // get certificate request state
386 CertificateRequest certRequest = getCertificateRequest(request);
387 if (certRequest.m_requestId == "") {
388 // cannot get the request state
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700389 _LOG_ERROR("Cannot find certificate request state from CA's storage.");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800390 return;
391 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700392 // verify signature
393 if (!security::verifySignature(request, certRequest.m_cert)) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700394 _LOG_ERROR("Challenge Interest with bad signature.");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800395 return;
396 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700397 // decrypt the parameters
Suyong Won19fba4d2020-05-09 13:39:46 -0700398 Buffer paramTLVPayload;
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700399 try {
Suyong Won19fba4d2020-05-09 13:39:46 -0700400 paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), m_aesKey,
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700401 (uint8_t*)"test", strlen("test"));
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700402 }
403 catch (const std::exception& e) {
404 _LOG_ERROR("Cannot successfully decrypt the Interest parameters: " << e.what());
405 return;
406 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700407 if (paramTLVPayload.size() == 0) {
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700408 _LOG_ERROR("Got an empty buffer from content decryption.");
409 return;
410 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700411
Suyong Won44d0cce2020-05-10 04:07:43 -0700412 Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size());
413 paramTLV.parse();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800414
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700415 // load the corresponding challenge module
Suyong Won19fba4d2020-05-09 13:39:46 -0700416 std::string challengeType = readString(paramTLV.get(tlv_selected_challenge));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800417 auto challenge = ChallengeModule::createChallengeModule(challengeType);
Suyong Won19fba4d2020-05-09 13:39:46 -0700418
Suyong Won44d0cce2020-05-10 04:07:43 -0700419 Block payload;
Suyong Won19fba4d2020-05-09 13:39:46 -0700420
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800421 if (challenge == nullptr) {
422 _LOG_TRACE("Unrecognized challenge type " << challengeType);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700423 certRequest.m_status = Status::FAILURE;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700424 certRequest.m_challengeStatus = CHALLENGE_STATUS_UNKNOWN_CHALLENGE;
Suyong Won44d0cce2020-05-10 04:07:43 -0700425 payload = CHALLENGE::encodeDataPayload(certRequest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800426 }
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700427 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700428 _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
429 // let challenge module handle the request
Suyong Won19fba4d2020-05-09 13:39:46 -0700430 challenge->handleChallengeRequest(paramTLV, certRequest);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700431 if (certRequest.m_status == Status::FAILURE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700432 // if challenge failed
433 m_storage->deleteRequest(certRequest.m_requestId);
Suyong Won44d0cce2020-05-10 04:07:43 -0700434 payload = CHALLENGE::encodeDataPayload(certRequest);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700435 _LOG_TRACE("Challenge failed");
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700436 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700437 else if (certRequest.m_status == Status::PENDING) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700438 // if challenge succeeded
tylerliu182bc532020-09-25 01:54:45 -0700439 if (certRequest.m_requestType == REQUEST_TYPE_NEW) {
440 auto issuedCert = issueCertificate(certRequest);
441 certRequest.m_cert = issuedCert;
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700442 certRequest.m_status = Status::SUCCESS;
tylerliu182bc532020-09-25 01:54:45 -0700443 try {
444 m_storage->addCertificate(certRequest.m_requestId, issuedCert);
445 m_storage->deleteRequest(certRequest.m_requestId);
446 _LOG_TRACE("New Certificate Issued " << issuedCert.getName());
447 }
448 catch (const std::exception& e) {
449 _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what());
450 return;
451 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700452
tylerliu182bc532020-09-25 01:54:45 -0700453 payload = CHALLENGE::encodeDataPayload(certRequest);
454 payload.parse();
455 payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
456 payload.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700457
tylerliu182bc532020-09-25 01:54:45 -0700458 //contentJson.add(JSON_CA_CERT_ID, readString(issuedCert.getName().at(-1)));
459 _LOG_TRACE("Challenge succeeded. Certificate has been issued");
460 }
461 else if (certRequest.m_requestType == REQUEST_TYPE_REVOKE) {
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700462 certRequest.m_status = Status::SUCCESS;
tylerliu182bc532020-09-25 01:54:45 -0700463 try {
464 m_storage->deleteRequest(certRequest.m_requestId);
465 _LOG_TRACE("Certificate Revoked");
466 }
467 catch (const std::exception& e) {
468 _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what());
469 return;
470 }
471
472 payload = CHALLENGE::encodeDataPayload(certRequest);
473 payload.parse();
474 _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
475 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800476 }
477 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700478 try {
479 m_storage->updateRequest(certRequest);
480 }
481 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700482 _LOG_TRACE("Cannot update request instance: " << e.what());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700483 return;
484 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700485 payload = CHALLENGE::encodeDataPayload(certRequest);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700486 _LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800487 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800488 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700489
490 Data result;
491 result.setName(request.getName());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700492 result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700493
494 // encrypt the content
Suyong Won7968f7a2020-05-12 01:01:25 -0700495 auto contentBlock = encodeBlockWithAesGcm128(tlv::Content, m_aesKey, payload.value(),
496 payload.value_size(), (uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700497 result.setContent(contentBlock);
Suyong Won256c9062020-05-11 02:45:56 -0700498 m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700499 m_face.put(result);
500
501 if (m_config.m_statusUpdateCallback) {
502 m_config.m_statusUpdateCallback(certRequest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800503 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700504}
505
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800506security::v2::Certificate
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700507CaModule::issueCertificate(const CertificateRequest& certRequest)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800508{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700509 auto expectedPeriod =
510 certRequest.m_cert.getValidityPeriod().getPeriod();
Zhiyi Zhang1a735bc2019-07-04 21:36:49 -0700511 security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800512 security::v2::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700513
514 Name certName = certRequest.m_cert.getKeyName();
515 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800516 newCert.setName(certName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700517 newCert.setContent(certRequest.m_cert.getContent());
518 _LOG_TRACE("cert request content " << certRequest.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800519 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800520 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700521 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Suyong Won256c9062020-05-11 02:45:56 -0700522 m_config.m_caPrefix, signatureInfo);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800523
524 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700525 _LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800526 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800527}
528
529CertificateRequest
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700530CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800531{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400532 std::string requestId;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800533 CertificateRequest certRequest;
534 try {
Suyong Won256c9062020-05-11 02:45:56 -0700535 requestId = readString(request.getName().at(m_config.m_caPrefix.size() + 2));
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700536 }
537 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700538 _LOG_ERROR("Cannot read the request ID out from the request: " << e.what());
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700539 }
540 try {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700541 _LOG_TRACE("Request Id to query the database " << requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800542 certRequest = m_storage->getRequest(requestId);
543 }
544 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700545 _LOG_ERROR("Cannot get certificate request record from the storage: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800546 }
547 return certRequest;
548}
549
550void
551CaModule::onRegisterFailed(const std::string& reason)
552{
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700553 _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800554}
555
556Block
557CaModule::dataContentFromJson(const JsonSection& jsonSection)
558{
559 std::stringstream ss;
560 boost::property_tree::write_json(ss, jsonSection);
561 return makeStringBlock(ndn::tlv::Content, ss.str());
562}
563
564JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700565CaModule::jsonFromBlock(const Block& block)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800566{
567 std::string jsonString;
568 try {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700569 jsonString = encoding::readString(block);
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -0700570 std::istringstream ss(jsonString);
571 JsonSection json;
572 boost::property_tree::json_parser::read_json(ss, json);
573 return json;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800574 }
575 catch (const std::exception& e) {
Zhiyi Zhangbc2d4122019-09-10 12:10:54 -0700576 _LOG_ERROR("Cannot read JSON string from TLV Value: " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800577 return JsonSection();
578 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800579}
580
581} // namespace ndncert
582} // namespace ndn