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