tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 1 | /* |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame^] | 2 | * Copyright (c) 2017-2024, Regents of the University of California. |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 3 | * |
| 4 | * This file is part of ndncert, a certificate management system based on NDN. |
| 5 | * |
| 6 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 7 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 8 | * version 3 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received copies of the GNU General Public License along with |
| 15 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include "challenge-possession.hpp" |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 21 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 22 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 23 | #include <ndn-cxx/security/transform/public-key.hpp> |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/security/verification-helpers.hpp> |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 25 | #include <ndn-cxx/util/io.hpp> |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame^] | 26 | #include <ndn-cxx/util/logger.hpp> |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 27 | #include <ndn-cxx/util/random.hpp> |
| 28 | |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 29 | #include <boost/property_tree/json_parser.hpp> |
| 30 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 31 | namespace ndncert { |
| 32 | |
| 33 | NDN_LOG_INIT(ndncert.challenge.possession); |
tylerliu | f26d41b | 2021-10-12 16:50:16 -0700 | [diff] [blame] | 34 | NDNCERT_REGISTER_CHALLENGE(ChallengePossession, "possession"); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 35 | |
| 36 | const std::string ChallengePossession::PARAMETER_KEY_CREDENTIAL_CERT = "issued-cert"; |
| 37 | const std::string ChallengePossession::PARAMETER_KEY_NONCE = "nonce"; |
| 38 | const std::string ChallengePossession::PARAMETER_KEY_PROOF = "proof"; |
| 39 | const std::string ChallengePossession::NEED_PROOF = "need-proof"; |
| 40 | |
| 41 | ChallengePossession::ChallengePossession(const std::string& configPath) |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 42 | : ChallengeModule("Possession", 1, time::seconds(60)) |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 43 | { |
| 44 | if (configPath.empty()) { |
| 45 | m_configFile = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/challenge-credential.conf"; |
| 46 | } |
| 47 | else { |
| 48 | m_configFile = configPath; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | ChallengePossession::parseConfigFile() |
| 54 | { |
| 55 | JsonSection config; |
| 56 | try { |
| 57 | boost::property_tree::read_json(m_configFile, config); |
| 58 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 59 | catch (const boost::property_tree::file_parser_error& error) { |
| 60 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + ": " + |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 61 | error.message() + " on line " + std::to_string(error.line()))); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (config.begin() == config.end()) { |
| 65 | NDN_THROW(std::runtime_error("Error processing configuration file: " + m_configFile + " no data")); |
| 66 | } |
| 67 | |
| 68 | m_trustAnchors.clear(); |
| 69 | auto anchorList = config.get_child("anchor-list"); |
| 70 | auto it = anchorList.begin(); |
| 71 | for (; it != anchorList.end(); it++) { |
| 72 | std::istringstream ss(it->second.get("certificate", "")); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 73 | auto cert = ndn::io::load<Certificate>(ss); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 74 | if (cert == nullptr) { |
| 75 | NDN_LOG_ERROR("Cannot load the certificate from config file"); |
| 76 | continue; |
| 77 | } |
| 78 | m_trustAnchors.push_back(*cert); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // For CA |
| 83 | std::tuple<ErrorCode, std::string> |
| 84 | ChallengePossession::handleChallengeRequest(const Block& params, ca::RequestState& request) |
| 85 | { |
| 86 | params.parse(); |
| 87 | if (m_trustAnchors.empty()) { |
| 88 | parseConfigFile(); |
| 89 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 90 | Certificate credential; |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 91 | const uint8_t* signature = nullptr; |
| 92 | size_t signatureLen = 0; |
| 93 | const auto& elements = params.elements(); |
| 94 | for (size_t i = 0; i < elements.size() - 1; i++) { |
| 95 | if (elements[i].type() == tlv::ParameterKey && elements[i + 1].type() == tlv::ParameterValue) { |
| 96 | if (readString(elements[i]) == PARAMETER_KEY_CREDENTIAL_CERT) { |
| 97 | try { |
| 98 | credential.wireDecode(elements[i + 1].blockFromValue()); |
| 99 | } |
| 100 | catch (const std::exception& e) { |
| 101 | NDN_LOG_ERROR("Cannot load challenge parameter: credential " << e.what()); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 102 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, |
| 103 | "Cannot challenge credential: credential."s + e.what()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | else if (readString(elements[i]) == PARAMETER_KEY_PROOF) { |
| 107 | signature = elements[i + 1].value(); |
| 108 | signatureLen = elements[i + 1].value_size(); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // verify the credential and the self-signed cert |
| 114 | if (request.status == Status::BEFORE_CHALLENGE) { |
| 115 | NDN_LOG_TRACE("Challenge Interest arrives. Check certificate and init the challenge"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 116 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 117 | // check the certificate |
Junxiao Shi | cc6889d | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 118 | if (!credential.hasContent() || signatureLen != 0) { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 119 | return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate"); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 120 | } |
Junxiao Shi | cc6889d | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 121 | auto keyLocator = credential.getSignatureInfo().getKeyLocator().getName(); |
| 122 | ndn::security::transform::PublicKey key; |
| 123 | key.loadPkcs8(credential.getPublicKey()); |
| 124 | bool checkOK = std::any_of(m_trustAnchors.begin(), m_trustAnchors.end(), [&] (const auto& anchor) { |
| 125 | return (anchor.getKeyName() == keyLocator || anchor.getName() == keyLocator) && |
| 126 | ndn::security::verifySignature(credential, anchor); |
| 127 | }); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 128 | if (!checkOK) { |
| 129 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Certificate cannot be verified"); |
| 130 | } |
| 131 | |
| 132 | // for the first time, init the challenge |
| 133 | std::array<uint8_t, 16> secretCode{}; |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame] | 134 | ndn::random::generateSecureBytes(secretCode); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 135 | JsonSection secretJson; |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 136 | secretJson.add(PARAMETER_KEY_NONCE, ndn::toHex(secretCode)); |
| 137 | secretJson.add(PARAMETER_KEY_CREDENTIAL_CERT, ndn::toHex(credential.wireEncode())); |
| 138 | NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " : " << ndn::toHex(secretCode)); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 139 | return returnWithNewChallengeStatus(request, NEED_PROOF, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 140 | } |
| 141 | else if (request.challengeState && request.challengeState->challengeStatus == NEED_PROOF) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 142 | NDN_LOG_TRACE("Challenge Interest (proof) arrives. Check the proof"); |
| 143 | //check the format and load credential |
| 144 | if (credential.hasContent() || signatureLen == 0) { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 145 | return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate"); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 146 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 147 | credential = Certificate(Block(ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_CREDENTIAL_CERT, "")))); |
| 148 | auto secretCode = *ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_NONCE, "")); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 149 | |
| 150 | //check the proof |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 151 | ndn::security::transform::PublicKey key; |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame] | 152 | key.loadPkcs8(credential.getPublicKey()); |
| 153 | if (ndn::security::verifySignature({secretCode}, {signature, signatureLen}, key)) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 154 | return returnWithSuccess(request); |
| 155 | } |
| 156 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 157 | "Cannot verify the proof of private key against credential."); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 158 | } |
| 159 | NDN_LOG_TRACE("Proof of possession: bad state"); |
| 160 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Fail to recognize the request."); |
| 161 | } |
| 162 | |
| 163 | // For Client |
| 164 | std::multimap<std::string, std::string> |
| 165 | ChallengePossession::getRequestedParameterList(Status status, const std::string& challengeStatus) |
| 166 | { |
| 167 | std::multimap<std::string, std::string> result; |
| 168 | if (status == Status::BEFORE_CHALLENGE) { |
| 169 | result.emplace(PARAMETER_KEY_CREDENTIAL_CERT, "Please provide the certificate issued by a trusted CA."); |
| 170 | return result; |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 171 | } |
| 172 | else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 173 | result.emplace(PARAMETER_KEY_PROOF, "Please sign a Data packet with request ID as the content."); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 174 | } |
| 175 | else { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 176 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
| 177 | } |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 178 | return result; |
| 179 | } |
| 180 | |
| 181 | Block |
| 182 | ChallengePossession::genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
| 183 | const std::multimap<std::string, std::string>& params) |
| 184 | { |
| 185 | Block request(tlv::EncryptedPayload); |
| 186 | if (status == Status::BEFORE_CHALLENGE) { |
| 187 | if (params.size() != 1) { |
| 188 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 189 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 190 | request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 191 | for (const auto& item : params) { |
| 192 | if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 193 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT)); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 194 | Block valueBlock(tlv::ParameterValue); |
| 195 | auto& certTlvStr = std::get<1>(item); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame] | 196 | valueBlock.push_back(Block(ndn::make_span(reinterpret_cast<const uint8_t*>(certTlvStr.data()), |
| 197 | certTlvStr.size()))); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 198 | request.push_back(valueBlock); |
| 199 | } |
| 200 | else { |
| 201 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 202 | } |
| 203 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 204 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 205 | else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 206 | if (params.size() != 1) { |
| 207 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 208 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 209 | for (const auto& item : params) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 210 | if (std::get<0>(item) == PARAMETER_KEY_PROOF) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 211 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF)); |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 212 | request.push_back(ndn::makeStringBlock(tlv::ParameterValue, std::get<1>(item))); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 213 | } |
| 214 | else { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 215 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 216 | } |
| 217 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 218 | } |
| 219 | else { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 220 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
| 221 | } |
| 222 | request.encode(); |
| 223 | return request; |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | ChallengePossession::fulfillParameters(std::multimap<std::string, std::string>& params, |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 228 | ndn::KeyChain& keyChain, const Name& issuedCertName, |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 229 | ndn::span<const uint8_t, 16> nonce) |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 230 | { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 231 | auto keyName = ndn::security::extractKeyNameFromCertName(issuedCertName); |
| 232 | auto id = keyChain.getPib().getIdentity(ndn::security::extractIdentityFromCertName(issuedCertName)); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 233 | auto issuedCert = id.getKey(keyName).getCertificate(issuedCertName); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame] | 234 | const auto& issuedCertTlv = issuedCert.wireEncode(); |
| 235 | auto signature = keyChain.getTpm().sign({nonce}, keyName, ndn::DigestAlgorithm::SHA256); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 236 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 237 | for (auto& [key, val] : params) { |
| 238 | if (key == PARAMETER_KEY_CREDENTIAL_CERT) { |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 239 | val = std::string(reinterpret_cast<const char*>(issuedCertTlv.data()), issuedCertTlv.size()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 240 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 241 | else if (key == PARAMETER_KEY_PROOF) { |
| 242 | val = std::string(signature->get<char>(), signature->size()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | } // namespace ndncert |