Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
| 4 | * |
| 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 "requester.hpp" |
Zhiyi Zhang | dbd9d43 | 2020-10-07 15:56:27 -0700 | [diff] [blame] | 22 | #include "identity-challenge/challenge-module.hpp" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame^] | 23 | #include "detail/crypto-helper.hpp" |
| 24 | #include "detail/challenge-encoder.hpp" |
| 25 | #include "detail/error-encoder.hpp" |
| 26 | #include "detail/info-encoder.hpp" |
| 27 | #include "detail/new-renew-revoke-encoder.hpp" |
| 28 | #include "detail/probe-encoder.hpp" |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 30 | #include <ndn-cxx/security/transform/base64-encode.hpp> |
| 31 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
| 32 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
| 33 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 34 | #include <ndn-cxx/util/io.hpp> |
| 35 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 36 | #include <ndn-cxx/metadata-object.hpp> |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 37 | |
| 38 | namespace ndn { |
| 39 | namespace ndncert { |
| 40 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 41 | NDN_LOG_INIT(ndncert.client); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 42 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 43 | shared_ptr<Interest> |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 44 | Requester::genCaProfileDiscoveryInterest(const Name& caName) |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 45 | { |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 46 | Name contentName = caName; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 47 | if (readString(caName.at(-1)) != "CA") |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 48 | contentName.append("CA"); |
| 49 | contentName.append("INFO"); |
| 50 | return std::make_shared<Interest>(MetadataObject::makeDiscoveryInterest(contentName)); |
| 51 | } |
| 52 | |
| 53 | shared_ptr<Interest> |
| 54 | Requester::genCaProfileInterestFromDiscoveryResponse(const Data& reply) |
| 55 | { |
| 56 | auto metaData = MetadataObject(reply); |
| 57 | auto interestName= metaData.getVersionedName(); |
| 58 | interestName.appendSegment(0); |
| 59 | auto interest = std::make_shared<Interest>(interestName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 60 | interest->setCanBePrefix(false); |
| 61 | return interest; |
| 62 | } |
| 63 | |
| 64 | boost::optional<CaProfile> |
| 65 | Requester::onCaProfileResponse(const Data& reply) |
| 66 | { |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 67 | auto caItem = InfoEncoder::decodeDataContent(reply.getContent()); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 68 | if (!security::verifySignature(reply, *caItem.m_cert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 69 | NDN_LOG_ERROR("Cannot verify replied Data packet signature."); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 70 | NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 71 | } |
| 72 | return caItem; |
| 73 | } |
| 74 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 75 | |
| 76 | boost::optional<CaProfile> |
| 77 | Requester::onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName) |
| 78 | { |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 79 | auto caItem = InfoEncoder::decodeDataContent(reply.getContent()); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 80 | auto certBlock = caItem.m_cert->wireEncode(); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 81 | caItem.m_cert = std::make_shared<security::Certificate>(certBlock); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 82 | if (caItem.m_cert->getFullName() != caCertFullName) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 83 | NDN_LOG_ERROR("Ca profile does not match the certificate information offered by the original CA."); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 84 | NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature.")); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 85 | } |
| 86 | return onCaProfileResponse(reply); |
| 87 | } |
| 88 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 89 | shared_ptr<Interest> |
| 90 | Requester::genProbeInterest(const CaProfile& ca, std::vector<std::tuple<std::string, std::string>>&& probeInfo) |
| 91 | { |
| 92 | Name interestName = ca.m_caPrefix; |
| 93 | interestName.append("CA").append("PROBE"); |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 94 | auto interest =std::make_shared<Interest>(interestName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 95 | interest->setMustBeFresh(true); |
| 96 | interest->setCanBePrefix(false); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 97 | interest->setApplicationParameters(ProbeEncoder::encodeApplicationParameters(std::move(probeInfo))); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 98 | return interest; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | Requester::onProbeResponse(const Data& reply, const CaProfile& ca, |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 103 | std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas) |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 104 | { |
| 105 | if (!security::verifySignature(reply, *ca.m_cert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 106 | NDN_LOG_ERROR("Cannot verify replied Data packet signature."); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 107 | NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | processIfError(reply); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 111 | ProbeEncoder::decodeDataContent(reply.getContent(), identityNames, otherCas); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | shared_ptr<Interest> |
| 115 | Requester::genNewInterest(RequesterState& state, const Name& identityName, |
| 116 | const time::system_clock::TimePoint& notBefore, |
| 117 | const time::system_clock::TimePoint& notAfter) |
| 118 | { |
| 119 | if (!state.m_caItem.m_caPrefix.isPrefixOf(identityName)) { |
| 120 | return nullptr; |
| 121 | } |
| 122 | if (identityName.empty()) { |
| 123 | NDN_LOG_TRACE("Randomly create a new name because identityName is empty and the param is empty."); |
| 124 | state.m_identityName = state.m_caItem.m_caPrefix; |
| 125 | state.m_identityName.append(std::to_string(random::generateSecureWord64())); |
| 126 | } |
| 127 | else { |
| 128 | state.m_identityName = identityName; |
| 129 | } |
| 130 | |
| 131 | // generate a newly key pair or use an existing key |
| 132 | const auto& pib = state.m_keyChain.getPib(); |
| 133 | security::pib::Identity identity; |
| 134 | try { |
| 135 | identity = pib.getIdentity(state.m_identityName); |
| 136 | } |
| 137 | catch (const security::Pib::Error& e) { |
| 138 | identity = state.m_keyChain.createIdentity(state.m_identityName); |
| 139 | state.m_isNewlyCreatedIdentity = true; |
| 140 | state.m_isNewlyCreatedKey = true; |
| 141 | } |
| 142 | try { |
| 143 | state.m_keyPair = identity.getDefaultKey(); |
| 144 | } |
| 145 | catch (const security::Pib::Error& e) { |
| 146 | state.m_keyPair = state.m_keyChain.createKey(identity); |
| 147 | state.m_isNewlyCreatedKey = true; |
| 148 | } |
| 149 | auto& keyName = state.m_keyPair.getName(); |
| 150 | |
| 151 | // generate certificate request |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 152 | security::Certificate certRequest; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 153 | certRequest.setName(Name(keyName).append("cert-request").appendVersion()); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 154 | certRequest.setContentType(ndn::tlv::ContentType_Key); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 155 | certRequest.setContent(state.m_keyPair.getPublicKey().data(), state.m_keyPair.getPublicKey().size()); |
| 156 | SignatureInfo signatureInfo; |
| 157 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
| 158 | state.m_keyChain.sign(certRequest, signingByKey(keyName).setSignatureInfo(signatureInfo)); |
| 159 | |
| 160 | // generate Interest packet |
| 161 | Name interestName = state.m_caItem.m_caPrefix; |
| 162 | interestName.append("CA").append("NEW"); |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 163 | auto interest =std::make_shared<Interest>(interestName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 164 | interest->setMustBeFresh(true); |
| 165 | interest->setCanBePrefix(false); |
| 166 | interest->setApplicationParameters( |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 167 | NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::NEW, state.m_ecdh.getBase64PubKey(), certRequest)); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 168 | |
| 169 | // sign the Interest packet |
| 170 | state.m_keyChain.sign(*interest, signingByKey(keyName)); |
| 171 | return interest; |
| 172 | } |
| 173 | |
| 174 | shared_ptr<Interest> |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 175 | Requester::genRevokeInterest(RequesterState& state, const security::Certificate& certificate) |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 176 | { |
| 177 | if (!state.m_caItem.m_caPrefix.isPrefixOf(certificate.getName())) { |
| 178 | return nullptr; |
| 179 | } |
| 180 | // generate Interest packet |
| 181 | Name interestName = state.m_caItem.m_caPrefix; |
| 182 | interestName.append("CA").append("REVOKE"); |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 183 | auto interest =std::make_shared<Interest>(interestName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 184 | interest->setMustBeFresh(true); |
| 185 | interest->setCanBePrefix(false); |
| 186 | interest->setApplicationParameters( |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 187 | NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::REVOKE, state.m_ecdh.getBase64PubKey(), certificate)); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 188 | return interest; |
| 189 | } |
| 190 | |
| 191 | std::list<std::string> |
| 192 | Requester::onNewRenewRevokeResponse(RequesterState& state, const Data& reply) |
| 193 | { |
| 194 | if (!security::verifySignature(reply, *state.m_caItem.m_cert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 195 | NDN_LOG_ERROR("Cannot verify replied Data packet signature."); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 196 | NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 197 | } |
| 198 | processIfError(reply); |
| 199 | |
| 200 | auto contentTLV = reply.getContent(); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 201 | const auto& content = NewRenewRevokeEncoder::decodeDataContent(contentTLV); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 202 | |
Zhiyi Zhang | 91f86ab | 2020-10-05 15:36:35 -0700 | [diff] [blame] | 203 | // ECDH and HKDF |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 204 | state.m_ecdh.deriveSecret(content.ecdhKey); |
Zhiyi Zhang | cfad98d | 2020-10-11 11:25:14 -0700 | [diff] [blame] | 205 | hkdf(state.m_ecdh.m_sharedSecret, state.m_ecdh.m_sharedSecretLen, |
Zhiyi Zhang | 91f86ab | 2020-10-05 15:36:35 -0700 | [diff] [blame] | 206 | (uint8_t*)&content.salt, sizeof(content.salt), state.m_aesKey, sizeof(state.m_aesKey)); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 207 | |
| 208 | // update state |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 209 | state.m_status = content.requestStatus; |
| 210 | state.m_requestId = content.requestId; |
tylerliu | 0790bdb | 2020-10-02 00:50:51 -0700 | [diff] [blame] | 211 | return content.challenges; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | std::vector<std::tuple<std::string, std::string>> |
| 215 | Requester::selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected) |
| 216 | { |
| 217 | auto challenge = ChallengeModule::createChallengeModule(challengeSelected); |
| 218 | if (challenge == nullptr) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 219 | NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 220 | } |
| 221 | state.m_challengeType = challengeSelected; |
| 222 | return challenge->getRequestedParameterList(state.m_status, state.m_challengeStatus); |
| 223 | } |
| 224 | |
| 225 | shared_ptr<Interest> |
| 226 | Requester::genChallengeInterest(const RequesterState& state, |
| 227 | std::vector<std::tuple<std::string, std::string>>&& parameters) |
| 228 | { |
| 229 | if (state.m_challengeType == "") { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 230 | NDN_THROW(std::runtime_error("The challenge has not been selected.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 231 | } |
| 232 | auto challenge = ChallengeModule::createChallengeModule(state.m_challengeType); |
| 233 | if (challenge == nullptr) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 234 | NDN_THROW(std::runtime_error("The challenge selected is not supported by your current version of NDNCERT.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 235 | } |
| 236 | auto challengeParams = challenge->genChallengeRequestTLV(state.m_status, state.m_challengeStatus, std::move(parameters)); |
| 237 | |
| 238 | Name interestName = state.m_caItem.m_caPrefix; |
| 239 | interestName.append("CA").append("CHALLENGE").append(state.m_requestId); |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 240 | auto interest =std::make_shared<Interest>(interestName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 241 | interest->setMustBeFresh(true); |
| 242 | interest->setCanBePrefix(false); |
| 243 | |
| 244 | // encrypt the Interest parameters |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 245 | auto paramBlock = encodeBlockWithAesGcm128(ndn::tlv::ApplicationParameters, state.m_aesKey, |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 246 | challengeParams.value(), challengeParams.value_size(), |
| 247 | (const uint8_t*)"test", strlen("test")); |
| 248 | interest->setApplicationParameters(paramBlock); |
| 249 | state.m_keyChain.sign(*interest, signingByKey(state.m_keyPair.getName())); |
| 250 | return interest; |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | Requester::onChallengeResponse(RequesterState& state, const Data& reply) |
| 255 | { |
| 256 | if (!security::verifySignature(reply, *state.m_caItem.m_cert)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 257 | NDN_LOG_ERROR("Cannot verify replied Data packet signature."); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 258 | NDN_THROW(std::runtime_error("Cannot verify replied Data packet signature.")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 259 | } |
| 260 | processIfError(reply); |
| 261 | auto result = decodeBlockWithAesGcm128(reply.getContent(), state.m_aesKey, (const uint8_t*)"test", strlen("test")); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 262 | Block contentTLV = makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size()); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 263 | ChallengeEncoder::decodeDataContent(contentTLV, state); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | shared_ptr<Interest> |
| 267 | Requester::genCertFetchInterest(const RequesterState& state) |
| 268 | { |
| 269 | Name interestName = state.m_issuedCertName; |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 270 | auto interest =std::make_shared<Interest>(interestName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 271 | interest->setMustBeFresh(false); |
| 272 | interest->setCanBePrefix(false); |
| 273 | return interest; |
| 274 | } |
| 275 | |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 276 | shared_ptr<security::Certificate> |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 277 | Requester::onCertFetchResponse(const Data& reply) |
| 278 | { |
| 279 | try { |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 280 | return std::make_shared<security::Certificate>(reply); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 281 | } |
| 282 | catch (const std::exception& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 283 | NDN_LOG_ERROR("Cannot parse replied certificate "); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 284 | NDN_THROW(std::runtime_error("Cannot parse replied certificate ")); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 285 | return nullptr; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | Requester::endSession(RequesterState& state) |
| 291 | { |
| 292 | if (state.m_status == Status::SUCCESS || state.m_status == Status::ENDED) { |
| 293 | return; |
| 294 | } |
| 295 | if (state.m_isNewlyCreatedIdentity) { |
| 296 | // put the identity into the if scope is because it may cause an error |
| 297 | // outside since when endSession is called, identity may not have been created yet. |
| 298 | auto identity = state.m_keyChain.getPib().getIdentity(state.m_identityName); |
| 299 | state.m_keyChain.deleteIdentity(identity); |
| 300 | } |
| 301 | else if (state.m_isNewlyCreatedKey) { |
| 302 | auto identity = state.m_keyChain.getPib().getIdentity(state.m_identityName); |
| 303 | state.m_keyChain.deleteKey(identity, state.m_keyPair); |
| 304 | } |
| 305 | state.m_status = Status::ENDED; |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | Requester::processIfError(const Data& data) |
| 310 | { |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 311 | auto errorInfo = ErrorEncoder::decodefromDataContent(data.getContent()); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 312 | if (std::get<0>(errorInfo) == ErrorCode::NO_ERROR) { |
| 313 | return; |
| 314 | } |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 315 | NDN_LOG_ERROR("Error info replied from the CA with Error code: " + |
tylerliu | 2f5d28f | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 316 | errorCodeToString(std::get<0>(errorInfo)) + |
| 317 | " and Error Info: " + std::get<1>(errorInfo)); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 318 | NDN_THROW(std::runtime_error("Error info replied from the CA with Error code: " + |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 319 | errorCodeToString(std::get<0>(errorInfo)) + |
| 320 | " and Error Info: " + std::get<1>(errorInfo))); |
| 321 | } |
| 322 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 323 | } // namespace ndncert |
| 324 | } // namespace ndn |