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