blob: 6ab56ead65b136009b9b24b950067612e58d29e5 [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/*
3 * Copyright (c) 2017-2021, 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);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070061 auto interestName= metaData.getVersionedName();
62 interestName.appendSegment(0);
63 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070064 interest->setCanBePrefix(false);
65 return interest;
66}
67
Zhiyi Zhang997669a2020-10-28 21:15:40 -070068optional<CaProfile>
tylerliu4140fe82021-01-27 15:45:44 -080069Request::onCaProfileResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070070{
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080071 auto caItem = infotlv::decodeDataContent(reply.getContent());
Davide Pesavento0dc02012021-11-23 22:55:03 -050072 if (!ndn::security::verifySignature(reply, *caItem.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070073 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -070074 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070075 }
76 return caItem;
77}
78
Zhiyi Zhang997669a2020-10-28 21:15:40 -070079optional<CaProfile>
tylerliu4140fe82021-01-27 15:45:44 -080080Request::onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName)
Zhiyi Zhang837406d2020-10-05 22:01:31 -070081{
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080082 auto caItem = infotlv::decodeDataContent(reply.getContent());
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080083 auto certBlock = caItem.cert->wireEncode();
Davide Pesavento0dc02012021-11-23 22:55:03 -050084 caItem.cert = std::make_shared<Certificate>(certBlock);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080085 if (caItem.cert->getFullName() != caCertFullName) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070086 NDN_LOG_ERROR("Ca profile does not match the certificate information offered by the original CA.");
tylerliu41c11532020-10-10 16:14:45 -070087 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang837406d2020-10-05 22:01:31 -070088 }
89 return onCaProfileResponse(reply);
90}
91
Davide Pesavento0dc02012021-11-23 22:55:03 -050092std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -080093Request::genProbeInterest(const CaProfile& ca, std::multimap<std::string, std::string>&& probeInfo)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070094{
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080095 Name interestName = ca.caPrefix;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070096 interestName.append("CA").append("PROBE");
Davide Pesavento0dc02012021-11-23 22:55:03 -050097 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070098 interest->setMustBeFresh(true);
99 interest->setCanBePrefix(false);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800100 interest->setApplicationParameters(probetlv::encodeApplicationParameters(std::move(probeInfo)));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700101 return interest;
102}
103
104void
tylerliu4140fe82021-01-27 15:45:44 -0800105Request::onProbeResponse(const Data& reply, const CaProfile& ca,
106 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700107{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500108 if (!ndn::security::verifySignature(reply, *ca.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700109 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700110 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700111 return;
112 }
113 processIfError(reply);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800114 probetlv::decodeDataContent(reply.getContent(), identityNames, otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700115}
116
Davide Pesavento0dc02012021-11-23 22:55:03 -0500117Request::Request(ndn::KeyChain& keyChain, const CaProfile& profile, RequestType requestType)
118 : m_caProfile(profile)
119 , m_type(requestType)
120 , m_keyChain(keyChain)
121{
122}
tylerliu4140fe82021-01-27 15:45:44 -0800123
Davide Pesavento0dc02012021-11-23 22:55:03 -0500124std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800125Request::genNewInterest(const Name& newIdentityName,
126 const time::system_clock::TimePoint& notBefore,
127 const time::system_clock::TimePoint& notAfter)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700128{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800129 if (!m_caProfile.caPrefix.isPrefixOf(newIdentityName)) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700130 return nullptr;
131 }
tylerliu4140fe82021-01-27 15:45:44 -0800132 if (newIdentityName.empty()) {
133 NDN_LOG_TRACE("Randomly create a new name because newIdentityName is empty and the param is empty.");
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800134 m_identityName = m_caProfile.caPrefix;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500135 m_identityName.append(ndn::to_string(ndn::random::generateSecureWord64()));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700136 }
137 else {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800138 m_identityName = newIdentityName;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700139 }
140
141 // generate a newly key pair or use an existing key
tylerliu4140fe82021-01-27 15:45:44 -0800142 const auto& pib = m_keyChain.getPib();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500143 ndn::security::pib::Identity identity;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700144 try {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800145 identity = pib.getIdentity(m_identityName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700146 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500147 catch (const ndn::security::Pib::Error&) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800148 identity = m_keyChain.createIdentity(m_identityName);
tylerliu4140fe82021-01-27 15:45:44 -0800149 m_isNewlyCreatedIdentity = true;
150 m_isNewlyCreatedKey = true;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700151 }
152 try {
tylerliu4140fe82021-01-27 15:45:44 -0800153 m_keyPair = identity.getDefaultKey();
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700154 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500155 catch (const ndn::security::Pib::Error&) {
tylerliu4140fe82021-01-27 15:45:44 -0800156 m_keyPair = m_keyChain.createKey(identity);
157 m_isNewlyCreatedKey = true;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700158 }
tylerliu4140fe82021-01-27 15:45:44 -0800159 auto& keyName = m_keyPair.getName();
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700160
161 // generate certificate request
Davide Pesavento0dc02012021-11-23 22:55:03 -0500162 Certificate certRequest;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700163 certRequest.setName(Name(keyName).append("cert-request").appendVersion());
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700164 certRequest.setContentType(ndn::tlv::ContentType_Key);
tylerliu4140fe82021-01-27 15:45:44 -0800165 certRequest.setContent(m_keyPair.getPublicKey().data(), m_keyPair.getPublicKey().size());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700166 SignatureInfo signatureInfo;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500167 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(notBefore, notAfter));
tylerliu4140fe82021-01-27 15:45:44 -0800168 m_keyChain.sign(certRequest, signingByKey(keyName).setSignatureInfo(signatureInfo));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700169
170 // generate Interest packet
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800171 Name interestName = m_caProfile.caPrefix;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700172 interestName.append("CA").append("NEW");
Zhiyi Zhang32437282020-10-10 16:15:37 -0700173 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700174 interest->setMustBeFresh(true);
175 interest->setCanBePrefix(false);
176 interest->setApplicationParameters(
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800177 requesttlv::encodeApplicationParameters(RequestType::NEW, m_ecdh.getSelfPubKey(), certRequest));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700178
179 // sign the Interest packet
tylerliu4140fe82021-01-27 15:45:44 -0800180 m_keyChain.sign(*interest, signingByKey(keyName));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700181 return interest;
182}
183
Davide Pesavento0dc02012021-11-23 22:55:03 -0500184std::shared_ptr<Interest>
185Request::genRevokeInterest(const Certificate& certificate)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700186{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800187 if (!m_caProfile.caPrefix.isPrefixOf(certificate.getName())) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700188 return nullptr;
189 }
190 // generate Interest packet
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800191 Name interestName = m_caProfile.caPrefix;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700192 interestName.append("CA").append("REVOKE");
Zhiyi Zhang32437282020-10-10 16:15:37 -0700193 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700194 interest->setMustBeFresh(true);
195 interest->setCanBePrefix(false);
196 interest->setApplicationParameters(
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800197 requesttlv::encodeApplicationParameters(RequestType::REVOKE, m_ecdh.getSelfPubKey(), certificate));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700198 return interest;
199}
200
201std::list<std::string>
tylerliu4140fe82021-01-27 15:45:44 -0800202Request::onNewRenewRevokeResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700203{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500204 if (!ndn::security::verifySignature(reply, *m_caProfile.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700205 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700206 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700207 }
208 processIfError(reply);
209
tylerliu4140fe82021-01-27 15:45:44 -0800210 const auto& contentTLV = reply.getContent();
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700211 std::vector<uint8_t> ecdhKey;
212 std::array<uint8_t, 32> salt;
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800213 auto challenges = requesttlv::decodeDataContent(contentTLV, ecdhKey, salt, m_requestId);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700214
Zhiyi Zhang91f86ab2020-10-05 15:36:35 -0700215 // ECDH and HKDF
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800216 auto sharedSecret = m_ecdh.deriveSecret(ecdhKey);
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700217 hkdf(sharedSecret.data(), sharedSecret.size(),
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800218 salt.data(), salt.size(), m_aesKey.data(), m_aesKey.size(),
219 m_requestId.data(), m_requestId.size());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700220
221 // update state
Zhiyi Zhangbed854c2020-10-20 18:25:35 -0700222 return challenges;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700223}
224
tylerliu40226332020-11-11 15:37:16 -0800225std::multimap<std::string, std::string>
tylerliu4140fe82021-01-27 15:45:44 -0800226Request::selectOrContinueChallenge(const std::string& challengeSelected)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700227{
228 auto challenge = ChallengeModule::createChallengeModule(challengeSelected);
229 if (challenge == nullptr) {
tylerliu41c11532020-10-10 16:14:45 -0700230 NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700231 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800232 m_challengeType = challengeSelected;
233 return challenge->getRequestedParameterList(m_status, m_challengeStatus);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700234}
235
Davide Pesavento0dc02012021-11-23 22:55:03 -0500236std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800237Request::genChallengeInterest(std::multimap<std::string, std::string>&& parameters)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700238{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800239 if (m_challengeType == "") {
tylerliu41c11532020-10-10 16:14:45 -0700240 NDN_THROW(std::runtime_error("The challenge has not been selected."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700241 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800242 auto challenge = ChallengeModule::createChallengeModule(m_challengeType);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700243 if (challenge == nullptr) {
tylerliu41c11532020-10-10 16:14:45 -0700244 NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700245 }
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800246 auto challengeParams = challenge->genChallengeRequestTLV(m_status, m_challengeStatus, std::move(parameters));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700247
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800248 Name interestName = m_caProfile.caPrefix;
249 interestName.append("CA").append("CHALLENGE").append(m_requestId.data(), m_requestId.size());
Zhiyi Zhang32437282020-10-10 16:15:37 -0700250 auto interest =std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700251 interest->setMustBeFresh(true);
252 interest->setCanBePrefix(false);
253
254 // encrypt the Interest parameters
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800255 auto paramBlock = encodeBlockWithAesGcm128(ndn::tlv::ApplicationParameters, m_aesKey.data(),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700256 challengeParams.value(), challengeParams.value_size(),
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800257 m_requestId.data(), m_requestId.size(),
258 m_encryptionIv);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700259 interest->setApplicationParameters(paramBlock);
tylerliu4140fe82021-01-27 15:45:44 -0800260 m_keyChain.sign(*interest, signingByKey(m_keyPair.getName()));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700261 return interest;
262}
263
264void
tylerliu4140fe82021-01-27 15:45:44 -0800265Request::onChallengeResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700266{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500267 if (!ndn::security::verifySignature(reply, *m_caProfile.cert)) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700268 NDN_LOG_ERROR("Cannot verify replied Data packet signature.");
tylerliu41c11532020-10-10 16:14:45 -0700269 NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature."));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700270 }
271 processIfError(reply);
tylerliu4140fe82021-01-27 15:45:44 -0800272 challengetlv::decodeDataContent(reply.getContent(), *this);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700273}
274
Davide Pesavento0dc02012021-11-23 22:55:03 -0500275std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800276Request::genCertFetchInterest() const
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700277{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800278 Name interestName = m_issuedCertName;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500279 auto interest = std::make_shared<Interest>(interestName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700280 interest->setMustBeFresh(false);
281 interest->setCanBePrefix(false);
282 return interest;
283}
284
Davide Pesavento0dc02012021-11-23 22:55:03 -0500285std::shared_ptr<Certificate>
tylerliu4140fe82021-01-27 15:45:44 -0800286Request::onCertFetchResponse(const Data& reply)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700287{
288 try {
Davide Pesavento0dc02012021-11-23 22:55:03 -0500289 return std::make_shared<Certificate>(reply);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700290 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500291 catch (const std::exception&) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700292 NDN_LOG_ERROR("Cannot parse replied certificate ");
tylerliu41c11532020-10-10 16:14:45 -0700293 NDN_THROW(std::runtime_error("Cannot parse replied certificate "));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700294 return nullptr;
295 }
296}
297
298void
tylerliu4140fe82021-01-27 15:45:44 -0800299Request::endSession()
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700300{
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800301 if (m_status == Status::SUCCESS) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700302 return;
303 }
tylerliu4140fe82021-01-27 15:45:44 -0800304 if (m_isNewlyCreatedIdentity) {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700305 // put the identity into the if scope is because it may cause an error
306 // outside since when endSession is called, identity may not have been created yet.
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800307 auto identity = m_keyChain.getPib().getIdentity(m_identityName);
tylerliu4140fe82021-01-27 15:45:44 -0800308 m_keyChain.deleteIdentity(identity);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700309 }
tylerliu4140fe82021-01-27 15:45:44 -0800310 else if (m_isNewlyCreatedKey) {
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800311 auto identity = m_keyChain.getPib().getIdentity(m_identityName);
tylerliu4140fe82021-01-27 15:45:44 -0800312 m_keyChain.deleteKey(identity, m_keyPair);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700313 }
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700314}
315
316void
tylerliu4140fe82021-01-27 15:45:44 -0800317Request::processIfError(const Data& data)
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700318{
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800319 auto errorInfo = errortlv::decodefromDataContent(data.getContent());
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700320 if (std::get<0>(errorInfo) == ErrorCode::NO_ERROR) {
321 return;
322 }
Zhiyi Zhang1a222692020-10-16 11:35:49 -0700323 NDN_LOG_ERROR("Error info replied from the CA with Error code: " << std::get<0>(errorInfo) <<
324 " and Error Info: " << std::get<1>(errorInfo));
tylerliu41c11532020-10-10 16:14:45 -0700325 NDN_THROW(std::runtime_error("Error info replied from the CA with Error code: " +
Zhiyi Zhang1a222692020-10-16 11:35:49 -0700326 boost::lexical_cast<std::string>(std::get<0>(errorInfo)) +
327 " and Error Info: " + std::get<1>(errorInfo)));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700328}
329
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700330} // namespace requester
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700331} // namespace ndncert