tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 1 | /* |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 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> |
| 26 | #include <ndn-cxx/util/random.hpp> |
| 27 | |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 28 | #include <boost/property_tree/json_parser.hpp> |
| 29 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 30 | namespace ndncert { |
| 31 | |
| 32 | NDN_LOG_INIT(ndncert.challenge.possession); |
tylerliu | f26d41b | 2021-10-12 16:50:16 -0700 | [diff] [blame] | 33 | NDNCERT_REGISTER_CHALLENGE(ChallengePossession, "possession"); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 34 | |
| 35 | const std::string ChallengePossession::PARAMETER_KEY_CREDENTIAL_CERT = "issued-cert"; |
| 36 | const std::string ChallengePossession::PARAMETER_KEY_NONCE = "nonce"; |
| 37 | const std::string ChallengePossession::PARAMETER_KEY_PROOF = "proof"; |
| 38 | const std::string ChallengePossession::NEED_PROOF = "need-proof"; |
| 39 | |
| 40 | ChallengePossession::ChallengePossession(const std::string& configPath) |
| 41 | : ChallengeModule("Possession", 1, time::seconds(60)) |
| 42 | { |
| 43 | if (configPath.empty()) { |
| 44 | m_configFile = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/challenge-credential.conf"; |
| 45 | } |
| 46 | else { |
| 47 | m_configFile = configPath; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | ChallengePossession::parseConfigFile() |
| 53 | { |
| 54 | JsonSection config; |
| 55 | try { |
| 56 | boost::property_tree::read_json(m_configFile, config); |
| 57 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 58 | catch (const boost::property_tree::file_parser_error& error) { |
| 59 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + ": " + |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 60 | error.message() + " on line " + ndn::to_string(error.line()))); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (config.begin() == config.end()) { |
| 64 | NDN_THROW(std::runtime_error("Error processing configuration file: " + m_configFile + " no data")); |
| 65 | } |
| 66 | |
| 67 | m_trustAnchors.clear(); |
| 68 | auto anchorList = config.get_child("anchor-list"); |
| 69 | auto it = anchorList.begin(); |
| 70 | for (; it != anchorList.end(); it++) { |
| 71 | std::istringstream ss(it->second.get("certificate", "")); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 72 | auto cert = ndn::io::load<Certificate>(ss); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 73 | if (cert == nullptr) { |
| 74 | NDN_LOG_ERROR("Cannot load the certificate from config file"); |
| 75 | continue; |
| 76 | } |
| 77 | m_trustAnchors.push_back(*cert); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // For CA |
| 82 | std::tuple<ErrorCode, std::string> |
| 83 | ChallengePossession::handleChallengeRequest(const Block& params, ca::RequestState& request) |
| 84 | { |
| 85 | params.parse(); |
| 86 | if (m_trustAnchors.empty()) { |
| 87 | parseConfigFile(); |
| 88 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 89 | Certificate credential; |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 90 | const uint8_t* signature = nullptr; |
| 91 | size_t signatureLen = 0; |
| 92 | const auto& elements = params.elements(); |
| 93 | for (size_t i = 0; i < elements.size() - 1; i++) { |
| 94 | if (elements[i].type() == tlv::ParameterKey && elements[i + 1].type() == tlv::ParameterValue) { |
| 95 | if (readString(elements[i]) == PARAMETER_KEY_CREDENTIAL_CERT) { |
| 96 | try { |
| 97 | credential.wireDecode(elements[i + 1].blockFromValue()); |
| 98 | } |
| 99 | catch (const std::exception& e) { |
| 100 | NDN_LOG_ERROR("Cannot load challenge parameter: credential " << e.what()); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 101 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, |
| 102 | "Cannot challenge credential: credential."s + e.what()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | else if (readString(elements[i]) == PARAMETER_KEY_PROOF) { |
| 106 | signature = elements[i + 1].value(); |
| 107 | signatureLen = elements[i + 1].value_size(); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // verify the credential and the self-signed cert |
| 113 | if (request.status == Status::BEFORE_CHALLENGE) { |
| 114 | NDN_LOG_TRACE("Challenge Interest arrives. Check certificate and init the challenge"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 115 | using ndn::toHex; |
| 116 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 117 | // check the certificate |
| 118 | bool checkOK = false; |
| 119 | if (credential.hasContent() && signatureLen == 0) { |
| 120 | Name signingKeyName = credential.getSignatureInfo().getKeyLocator().getName(); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 121 | ndn::security::transform::PublicKey key; |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 122 | const auto& pubKeyBuffer = credential.getPublicKey(); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 123 | key.loadPkcs8(pubKeyBuffer.data(), pubKeyBuffer.size()); |
| 124 | for (auto anchor : m_trustAnchors) { |
| 125 | if (anchor.getKeyName() == signingKeyName) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 126 | if (ndn::security::verifySignature(credential, anchor)) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 127 | checkOK = true; |
| 128 | } |
| 129 | } |
| 130 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 131 | } |
| 132 | else { |
| 133 | return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate"); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 134 | } |
| 135 | if (!checkOK) { |
| 136 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Certificate cannot be verified"); |
| 137 | } |
| 138 | |
| 139 | // for the first time, init the challenge |
| 140 | std::array<uint8_t, 16> secretCode{}; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 141 | ndn::random::generateSecureBytes(secretCode.data(), 16); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 142 | JsonSection secretJson; |
| 143 | secretJson.add(PARAMETER_KEY_NONCE, toHex(secretCode.data(), 16)); |
| 144 | auto credential_block = credential.wireEncode(); |
| 145 | secretJson.add(PARAMETER_KEY_CREDENTIAL_CERT, toHex(credential_block.wire(), credential_block.size())); |
| 146 | NDN_LOG_TRACE("Secret for request " << toHex(request.requestId.data(), request.requestId.size()) |
| 147 | << " : " << toHex(secretCode.data(), 16)); |
| 148 | return returnWithNewChallengeStatus(request, NEED_PROOF, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 149 | } |
| 150 | else if (request.challengeState && request.challengeState->challengeStatus == NEED_PROOF) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 151 | NDN_LOG_TRACE("Challenge Interest (proof) arrives. Check the proof"); |
| 152 | //check the format and load credential |
| 153 | if (credential.hasContent() || signatureLen == 0) { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 154 | return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate"); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 155 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 156 | credential = Certificate(Block(ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_CREDENTIAL_CERT, "")))); |
| 157 | auto secretCode = *ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_NONCE, "")); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 158 | |
| 159 | //check the proof |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 160 | ndn::security::transform::PublicKey key; |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 161 | const auto& pubKeyBuffer = credential.getPublicKey(); |
| 162 | key.loadPkcs8(pubKeyBuffer.data(), pubKeyBuffer.size()); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 163 | if (ndn::security::verifySignature({{secretCode.data(), secretCode.size()}}, signature, signatureLen, key)) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 164 | return returnWithSuccess(request); |
| 165 | } |
| 166 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 167 | "Cannot verify the proof of private key against credential."); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 168 | } |
| 169 | NDN_LOG_TRACE("Proof of possession: bad state"); |
| 170 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Fail to recognize the request."); |
| 171 | } |
| 172 | |
| 173 | // For Client |
| 174 | std::multimap<std::string, std::string> |
| 175 | ChallengePossession::getRequestedParameterList(Status status, const std::string& challengeStatus) |
| 176 | { |
| 177 | std::multimap<std::string, std::string> result; |
| 178 | if (status == Status::BEFORE_CHALLENGE) { |
| 179 | result.emplace(PARAMETER_KEY_CREDENTIAL_CERT, "Please provide the certificate issued by a trusted CA."); |
| 180 | return result; |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 181 | } |
| 182 | else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 183 | 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] | 184 | } |
| 185 | else { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 186 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
| 187 | } |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 188 | return result; |
| 189 | } |
| 190 | |
| 191 | Block |
| 192 | ChallengePossession::genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
| 193 | const std::multimap<std::string, std::string>& params) |
| 194 | { |
| 195 | Block request(tlv::EncryptedPayload); |
| 196 | if (status == Status::BEFORE_CHALLENGE) { |
| 197 | if (params.size() != 1) { |
| 198 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 199 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 200 | request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 201 | for (const auto& item : params) { |
| 202 | if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 203 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT)); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 204 | Block valueBlock(tlv::ParameterValue); |
| 205 | auto& certTlvStr = std::get<1>(item); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 206 | valueBlock.push_back(Block(reinterpret_cast<const uint8_t*>(certTlvStr.data()), certTlvStr.size())); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 207 | request.push_back(valueBlock); |
| 208 | } |
| 209 | else { |
| 210 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 211 | } |
| 212 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 213 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 214 | else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 215 | if (params.size() != 1) { |
| 216 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 217 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 218 | for (const auto& item : params) { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 219 | if (std::get<0>(item) == PARAMETER_KEY_PROOF) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 220 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF)); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 221 | auto& sigTlvStr = std::get<1>(item); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 222 | auto valueBlock = ndn::makeBinaryBlock(tlv::ParameterValue, |
| 223 | reinterpret_cast<const uint8_t*>(sigTlvStr.data()), |
| 224 | sigTlvStr.size()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 225 | request.push_back(valueBlock); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 226 | } |
| 227 | else { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 228 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
| 229 | } |
| 230 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 231 | } |
| 232 | else { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 233 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
| 234 | } |
| 235 | request.encode(); |
| 236 | return request; |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | ChallengePossession::fulfillParameters(std::multimap<std::string, std::string>& params, |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 241 | ndn::KeyChain& keyChain, const Name& issuedCertName, |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 242 | const std::array<uint8_t, 16>& nonce) |
| 243 | { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 244 | auto keyName = ndn::security::extractKeyNameFromCertName(issuedCertName); |
| 245 | auto id = keyChain.getPib().getIdentity(ndn::security::extractIdentityFromCertName(issuedCertName)); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 246 | auto issuedCert = id.getKey(keyName).getCertificate(issuedCertName); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 247 | auto issuedCertTlv = issuedCert.wireEncode(); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 248 | auto signature = keyChain.getTpm().sign({{nonce.data(), nonce.size()}}, keyName, ndn::DigestAlgorithm::SHA256); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 249 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 250 | for (auto& item : params) { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 251 | if (item.first == PARAMETER_KEY_CREDENTIAL_CERT) { |
| 252 | item.second = std::string(reinterpret_cast<const char*>(issuedCertTlv.wire()), issuedCertTlv.size()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 253 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 254 | else if (item.first == PARAMETER_KEY_PROOF) { |
| 255 | item.second = std::string(signature->get<char>(), signature->size()); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | } // namespace ndncert |