blob: 7c5ab3c4a91afa012bd9d6c845173e3807504c47 [file] [log] [blame]
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -07004 *
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
tylerliu4140fe82021-01-27 15:45:44 -080021#include "requester-request.hpp"
Davide Pesavento0dc02012021-11-23 22:55:03 -050022
Zhiyi Zhang84e11842020-11-19 20:03:23 -080023#include "challenge/challenge-module.hpp"
Zhiyi Zhangdc25ddf2020-10-20 14:28:55 -070024#include "detail/crypto-helpers.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070025#include "detail/challenge-encoder.hpp"
26#include "detail/error-encoder.hpp"
27#include "detail/info-encoder.hpp"
Zhiyi Zhang7cca76a2021-02-17 14:57:42 -080028#include "detail/request-encoder.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070029#include "detail/probe-encoder.hpp"
Davide Pesavento0dc02012021-11-23 22:55:03 -050030
31#include <ndn-cxx/metadata-object.hpp>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070032#include <ndn-cxx/security/signing-helpers.hpp>
33#include <ndn-cxx/security/transform/base64-encode.hpp>
34#include <ndn-cxx/security/transform/buffer-source.hpp>
35#include <ndn-cxx/security/transform/stream-sink.hpp>
36#include <ndn-cxx/security/verification-helpers.hpp>
37#include <ndn-cxx/util/io.hpp>
38#include <ndn-cxx/util/random.hpp>
Davide Pesavento0dc02012021-11-23 22:55:03 -050039
tylerliu96a67e82020-10-15 13:37:12 -070040#include <boost/lexical_cast.hpp>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070041
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070042namespace ndncert {
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -070043namespace requester {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070044
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070045NDN_LOG_INIT(ndncert.client);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070046
Davide Pesavento0dc02012021-11-23 22:55:03 -050047std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -080048Request::genCaProfileDiscoveryInterest(const Name& caName)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070049{
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070050 Name contentName = caName;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070051 if (readString(caName.at(-1)) != "CA")
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070052 contentName.append("CA");
53 contentName.append("INFO");
Davide Pesavento0dc02012021-11-23 22:55:03 -050054 return std::make_shared<Interest>(ndn::MetadataObject::makeDiscoveryInterest(contentName));
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070055}
56
Davide Pesavento0dc02012021-11-23 22:55:03 -050057std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -080058Request::genCaProfileInterestFromDiscoveryResponse(const Data& reply)
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070059{
Davide Pesavento0dc02012021-11-23 22:55:03 -050060 auto metaData = ndn::MetadataObject(reply);
Davide Pesavento64d5c8f2022-03-07 22:06:22 -050061 auto interestName = metaData.getVersionedName();
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070062 interestName.appendSegment(0);
Davide Pesavento64d5c8f2022-03-07 22:06:22 -050063 return std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070064}
65
Zhiyi Zhang997669a2020-10-28 21:15:40 -070066optional<CaProfile>
tylerliu4140fe82021-01-27 15:45:44 -080067Request::onCaProfileResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070068{
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080069 auto caItem = infotlv::decodeDataContent(reply.getContent());
Davide Pesavento0dc02012021-11-23 22:55:03 -050070 if (!ndn::security::verifySignature(reply, *caItem.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070071 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -070072 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070073 }
74 return caItem;
75}
76
Zhiyi Zhang997669a2020-10-28 21:15:40 -070077optional<CaProfile>
tylerliu4140fe82021-01-27 15:45:44 -080078Request::onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName)
Zhiyi Zhang837406d2020-10-05 22:01:31 -070079{
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080080 auto caItem = infotlv::decodeDataContent(reply.getContent());
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080081 auto certBlock = caItem.cert->wireEncode();
Davide Pesavento0dc02012021-11-23 22:55:03 -050082 caItem.cert = std::make_shared<Certificate>(certBlock);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080083 if (caItem.cert->getFullName() != caCertFullName) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070084 NDN_LOG_ERROR("Ca profile does not match the certificate information offered by the original CA.");
tylerliu41c11532020-10-10 16:14:45 -070085 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang837406d2020-10-05 22:01:31 -070086 }
87 return onCaProfileResponse(reply);
88}
89
Davide Pesavento0dc02012021-11-23 22:55:03 -050090std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -080091Request::genProbeInterest(const CaProfile& ca, std::multimap<std::string, std::string>&& probeInfo)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070092{
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080093 Name interestName = ca.caPrefix;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070094 interestName.append("CA").append("PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -050095 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070096 interest->setMustBeFresh(true);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080097 interest->setApplicationParameters(probetlv::encodeApplicationParameters(std::move(probeInfo)));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070098 return interest;
99}
100
101void
tylerliu4140fe82021-01-27 15:45:44 -0800102Request::onProbeResponse(const Data& reply, const CaProfile& ca,
103 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700104{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500105 if (!ndn::security::verifySignature(reply, *ca.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700106 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700107 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700108 }
109 processIfError(reply);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800110 probetlv::decodeDataContent(reply.getContent(), identityNames, otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700111}
112
Davide Pesavento0dc02012021-11-23 22:55:03 -0500113Request::Request(ndn::KeyChain& keyChain, const CaProfile& profile, RequestType requestType)
114 : m_caProfile(profile)
115 , m_type(requestType)
116 , m_keyChain(keyChain)
117{
118}
tylerliu4140fe82021-01-27 15:45:44 -0800119
Davide Pesavento0dc02012021-11-23 22:55:03 -0500120std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800121Request::genNewInterest(const Name& newIdentityName,
122 const time::system_clock::TimePoint& notBefore,
123 const time::system_clock::TimePoint& notAfter)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700124{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800125 if (!m_caProfile.caPrefix.isPrefixOf(newIdentityName)) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700126 return nullptr;
127 }
tylerliu4140fe82021-01-27 15:45:44 -0800128 if (newIdentityName.empty()) {
129 NDN_LOG_TRACE("Randomly create a new name because newIdentityName is empty and the param is empty.");
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800130 m_identityName = m_caProfile.caPrefix;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500131 m_identityName.append(ndn::to_string(ndn::random::generateSecureWord64()));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700132 }
133 else {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800134 m_identityName = newIdentityName;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700135 }
136
137 // generate a newly key pair or use an existing key
tylerliu4140fe82021-01-27 15:45:44 -0800138 const auto& pib = m_keyChain.getPib();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500139 ndn::security::pib::Identity identity;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700140 try {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800141 identity = pib.getIdentity(m_identityName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700142 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500143 catch (const ndn::security::Pib::Error&) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800144 identity = m_keyChain.createIdentity(m_identityName);
tylerliu4140fe82021-01-27 15:45:44 -0800145 m_isNewlyCreatedIdentity = true;
146 m_isNewlyCreatedKey = true;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700147 }
148 try {
tylerliu4140fe82021-01-27 15:45:44 -0800149 m_keyPair = identity.getDefaultKey();
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700150 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500151 catch (const ndn::security::Pib::Error&) {
tylerliu4140fe82021-01-27 15:45:44 -0800152 m_keyPair = m_keyChain.createKey(identity);
153 m_isNewlyCreatedKey = true;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700154 }
tylerliu4140fe82021-01-27 15:45:44 -0800155 auto& keyName = m_keyPair.getName();
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700156
157 // generate certificate request
Davide Pesavento0dc02012021-11-23 22:55:03 -0500158 Certificate certRequest;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700159 certRequest.setName(Name(keyName).append("cert-request").appendVersion());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700160 certRequest.setContentType(ndn::tlv::ContentType_Key);
tylerliu4140fe82021-01-27 15:45:44 -0800161 certRequest.setContent(m_keyPair.getPublicKey().data(), m_keyPair.getPublicKey().size());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700162 SignatureInfo signatureInfo;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500163 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(notBefore, notAfter));
tylerliu4140fe82021-01-27 15:45:44 -0800164 m_keyChain.sign(certRequest, signingByKey(keyName).setSignatureInfo(signatureInfo));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700165
166 // generate Interest packet
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800167 Name interestName = m_caProfile.caPrefix;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700168 interestName.append("CA").append("NEW");
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500169 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700170 interest->setMustBeFresh(true);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700171 interest->setApplicationParameters(
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500172 requesttlv::encodeApplicationParameters(RequestType::NEW, m_ecdh.getSelfPubKey(), certRequest));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700173
174 // sign the Interest packet
tylerliu4140fe82021-01-27 15:45:44 -0800175 m_keyChain.sign(*interest, signingByKey(keyName));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700176 return interest;
177}
178
Davide Pesavento0dc02012021-11-23 22:55:03 -0500179std::shared_ptr<Interest>
180Request::genRevokeInterest(const Certificate& certificate)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700181{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800182 if (!m_caProfile.caPrefix.isPrefixOf(certificate.getName())) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700183 return nullptr;
184 }
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500185
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700186 // generate Interest packet
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800187 Name interestName = m_caProfile.caPrefix;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700188 interestName.append("CA").append("REVOKE");
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500189 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700190 interest->setMustBeFresh(true);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700191 interest->setApplicationParameters(
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500192 requesttlv::encodeApplicationParameters(RequestType::REVOKE, m_ecdh.getSelfPubKey(), certificate));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700193 return interest;
194}
195
196std::list<std::string>
tylerliu4140fe82021-01-27 15:45:44 -0800197Request::onNewRenewRevokeResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700198{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500199 if (!ndn::security::verifySignature(reply, *m_caProfile.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700200 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700201 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700202 }
203 processIfError(reply);
204
tylerliu4140fe82021-01-27 15:45:44 -0800205 const auto& contentTLV = reply.getContent();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700206 std::vector<uint8_t> ecdhKey;
207 std::array<uint8_t, 32> salt;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800208 auto challenges = requesttlv::decodeDataContent(contentTLV, ecdhKey, salt, m_requestId);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700209
Zhiyi Zhang91f86ab2020-10-05 15:36:35 -0700210 // ECDH and HKDF
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800211 auto sharedSecret = m_ecdh.deriveSecret(ecdhKey);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700212 hkdf(sharedSecret.data(), sharedSecret.size(),
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800213 salt.data(), salt.size(), m_aesKey.data(), m_aesKey.size(),
214 m_requestId.data(), m_requestId.size());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700215
216 // update state
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700217 return challenges;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700218}
219
tylerliu40226332020-11-11 15:37:16 -0800220std::multimap<std::string, std::string>
tylerliu4140fe82021-01-27 15:45:44 -0800221Request::selectOrContinueChallenge(const std::string& challengeSelected)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700222{
223 auto challenge = ChallengeModule::createChallengeModule(challengeSelected);
224 if (challenge == nullptr) {
tylerliu41c11532020-10-10 16:14:45 -0700225 NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700226 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800227 m_challengeType = challengeSelected;
228 return challenge->getRequestedParameterList(m_status, m_challengeStatus);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700229}
230
Davide Pesavento0dc02012021-11-23 22:55:03 -0500231std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800232Request::genChallengeInterest(std::multimap<std::string, std::string>&& parameters)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700233{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800234 if (m_challengeType == "") {
tylerliu41c11532020-10-10 16:14:45 -0700235 NDN_THROW(std::runtime_error("The challenge has not been selected."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700236 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800237 auto challenge = ChallengeModule::createChallengeModule(m_challengeType);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700238 if (challenge == nullptr) {
tylerliu41c11532020-10-10 16:14:45 -0700239 NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700240 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800241 auto challengeParams = challenge->genChallengeRequestTLV(m_status, m_challengeStatus, std::move(parameters));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700242
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800243 Name interestName = m_caProfile.caPrefix;
244 interestName.append("CA").append("CHALLENGE").append(m_requestId.data(), m_requestId.size());
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500245 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700246 interest->setMustBeFresh(true);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700247
248 // encrypt the Interest parameters
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800249 auto paramBlock = encodeBlockWithAesGcm128(ndn::tlv::ApplicationParameters, m_aesKey.data(),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700250 challengeParams.value(), challengeParams.value_size(),
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800251 m_requestId.data(), m_requestId.size(),
252 m_encryptionIv);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700253 interest->setApplicationParameters(paramBlock);
tylerliu4140fe82021-01-27 15:45:44 -0800254 m_keyChain.sign(*interest, signingByKey(m_keyPair.getName()));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700255 return interest;
256}
257
258void
tylerliu4140fe82021-01-27 15:45:44 -0800259Request::onChallengeResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700260{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500261 if (!ndn::security::verifySignature(reply, *m_caProfile.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700262 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700263 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700264 }
265 processIfError(reply);
tylerliu4140fe82021-01-27 15:45:44 -0800266 challengetlv::decodeDataContent(reply.getContent(), *this);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700267}
268
Davide Pesavento0dc02012021-11-23 22:55:03 -0500269std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800270Request::genCertFetchInterest() const
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700271{
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500272 auto interest = std::make_shared<Interest>(m_issuedCertName);
Tianyuan Yu60775552022-03-07 17:10:10 -0800273 if (!m_forwardingHint.empty()) {
274 interest->setForwardingHint({m_forwardingHint});
275 }
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700276 return interest;
277}
278
Davide Pesavento0dc02012021-11-23 22:55:03 -0500279std::shared_ptr<Certificate>
tylerliu4140fe82021-01-27 15:45:44 -0800280Request::onCertFetchResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700281{
282 try {
Davide Pesavento0dc02012021-11-23 22:55:03 -0500283 return std::make_shared<Certificate>(reply);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700284 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500285 catch (const std::exception&) {
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500286 NDN_LOG_ERROR("Cannot parse replied certificate");
287 NDN_THROW(std::runtime_error("Cannot parse replied certificate"));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700288 }
289}
290
291void
tylerliu4140fe82021-01-27 15:45:44 -0800292Request::endSession()
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700293{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800294 if (m_status == Status::SUCCESS) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700295 return;
296 }
Davide Pesavento64d5c8f2022-03-07 22:06:22 -0500297
tylerliu4140fe82021-01-27 15:45:44 -0800298 if (m_isNewlyCreatedIdentity) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700299 // put the identity into the if scope is because it may cause an error
300 // outside since when endSession is called, identity may not have been created yet.
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800301 auto identity = m_keyChain.getPib().getIdentity(m_identityName);
tylerliu4140fe82021-01-27 15:45:44 -0800302 m_keyChain.deleteIdentity(identity);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700303 }
tylerliu4140fe82021-01-27 15:45:44 -0800304 else if (m_isNewlyCreatedKey) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800305 auto identity = m_keyChain.getPib().getIdentity(m_identityName);
tylerliu4140fe82021-01-27 15:45:44 -0800306 m_keyChain.deleteKey(identity, m_keyPair);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700307 }
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700308}
309
310void
tylerliu4140fe82021-01-27 15:45:44 -0800311Request::processIfError(const Data& data)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700312{
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800313 auto errorInfo = errortlv::decodefromDataContent(data.getContent());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700314 if (std::get<0>(errorInfo) == ErrorCode::NO_ERROR) {
315 return;
316 }
Zhiyi Zhang1a222692020-10-16 11:35:49 -0700317 NDN_LOG_ERROR("Error info replied from the CA with Error code: " << std::get<0>(errorInfo) <<
318 " and Error Info: " << std::get<1>(errorInfo));
tylerliu41c11532020-10-10 16:14:45 -0700319 NDN_THROW(std::runtime_error("Error info replied from the CA with Error code: " +
Zhiyi Zhang1a222692020-10-16 11:35:49 -0700320 boost::lexical_cast<std::string>(std::get<0>(errorInfo)) +
321 " and Error Info: " + std::get<1>(errorInfo)));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700322}
323
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700324} // namespace requester
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700325} // namespace ndncert