Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [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-credential.hpp" |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 21 | #include <ndn-cxx/security/verification-helpers.hpp> |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 23 | #include <ndn-cxx/security/transform/public-key.hpp> |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/util/io.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 29 | NDN_LOG_INIT(ndncert.challenge.credential); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 30 | NDNCERT_REGISTER_CHALLENGE(ChallengeCredential, "Credential"); |
| 31 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 32 | const std::string ChallengeCredential::PARAMETER_KEY_CREDENTIAL_CERT = "issued-cert"; |
| 33 | const std::string ChallengeCredential::PARAMETER_KEY_PROOF_OF_PRIVATE_KEY = "proof-of-private-key"; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 34 | |
| 35 | ChallengeCredential::ChallengeCredential(const std::string& configPath) |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 36 | : ChallengeModule("Credential", 1, time::seconds(1)) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 37 | { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 38 | if (configPath.empty()) { |
Zhiyi Zhang | 840afd9 | 2020-10-21 13:24:08 -0700 | [diff] [blame] | 39 | m_configFile = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/challenge-credential.conf"; |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 40 | } |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 41 | else { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 42 | m_configFile = configPath; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 43 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
| 47 | ChallengeCredential::parseConfigFile() |
| 48 | { |
| 49 | JsonSection config; |
| 50 | try { |
| 51 | boost::property_tree::read_json(m_configFile, config); |
| 52 | } |
| 53 | catch (const boost::property_tree::info_parser_error& error) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 54 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 55 | " " + error.message() + " line " + std::to_string(error.line()))); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (config.begin() == config.end()) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 59 | NDN_THROW(std::runtime_error("Error processing configuration file: " + m_configFile + " no data")); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | m_trustAnchors.clear(); |
| 63 | auto anchorList = config.get_child("anchor-list"); |
| 64 | auto it = anchorList.begin(); |
| 65 | for (; it != anchorList.end(); it++) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 66 | std::istringstream ss(it->second.get("certificate", "")); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 67 | auto cert = io::load<security::Certificate>(ss); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 68 | if (cert == nullptr) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 69 | NDN_LOG_ERROR("Cannot load the certificate from config file"); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 70 | continue; |
| 71 | } |
| 72 | m_trustAnchors.push_back(*cert); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 76 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 77 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 78 | ChallengeCredential::handleChallengeRequest(const Block& params, ca::RequestState& request) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 79 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 80 | params.parse(); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 81 | if (m_trustAnchors.empty()) { |
| 82 | parseConfigFile(); |
| 83 | } |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 84 | security::Certificate credential; |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 85 | const uint8_t* signature; |
| 86 | size_t signatureLen; |
| 87 | const auto& elements = params.elements(); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 88 | for (size_t i = 0; i < elements.size(); i++) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 89 | if (elements[i].type() == tlv::ParameterKey) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 90 | if (readString(elements[i]) == PARAMETER_KEY_CREDENTIAL_CERT) { |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 91 | try { |
| 92 | credential.wireDecode(elements[i + 1].blockFromValue()); |
| 93 | } |
| 94 | catch (const std::exception& e) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 95 | NDN_LOG_ERROR("Cannot load challenge parameter: credential " << e.what()); |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 96 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot challenge credential: credential." + std::string(e.what())); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 99 | else if (readString(elements[i]) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) { |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 100 | signature = elements[i + 1].value(); |
| 101 | signatureLen = elements[i + 1].value_size(); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 104 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 105 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 106 | // verify the credential and the self-signed cert |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 107 | Name signingKeyName = credential.getSignatureInfo().getKeyLocator().getName(); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 108 | security::transform::PublicKey key; |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 109 | const auto& pubKeyBuffer = credential.getPublicKey(); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 110 | key.loadPkcs8(pubKeyBuffer.data(), pubKeyBuffer.size()); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 111 | for (auto anchor : m_trustAnchors) { |
| 112 | if (anchor.getKeyName() == signingKeyName) { |
Zhiyi Zhang | 34a8d43 | 2020-10-03 22:14:25 -0700 | [diff] [blame] | 113 | if (security::verifySignature(credential, anchor) && |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 114 | security::verifySignature(request.m_requestId.data(), request.m_requestId.size(), signature, signatureLen, key)) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 115 | return returnWithSuccess(request); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 119 | NDN_LOG_TRACE("Cannot verify the proof of private key against credential"); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 120 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot verify the proof of private key against credential."); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | // For Client |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 124 | std::vector<std::tuple<std::string, std::string>> |
| 125 | ChallengeCredential::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 126 | { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 127 | std::vector<std::tuple<std::string, std::string>> result; |
| 128 | if (status == Status::BEFORE_CHALLENGE) { |
| 129 | result.push_back(std::make_tuple(PARAMETER_KEY_CREDENTIAL_CERT, "Please provide the certificate issued by a trusted CA.")); |
| 130 | result.push_back(std::make_tuple(PARAMETER_KEY_PROOF_OF_PRIVATE_KEY, "Please sign a Data packet with request ID as the content.")); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 131 | return result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 132 | } |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 133 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 136 | Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 137 | ChallengeCredential::genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
| 138 | std::vector<std::tuple<std::string, std::string>>&& params) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 139 | { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 140 | Block request = makeEmptyBlock(tlv::EncryptedPayload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 141 | if (status == Status::BEFORE_CHALLENGE) { |
| 142 | if (params.size() != 2) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 143 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 144 | } |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 145 | request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 146 | for (const auto& item : params) { |
| 147 | if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 148 | request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT)); |
| 149 | Block valueBlock = makeEmptyBlock(tlv::ParameterValue); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 150 | auto& certTlvStr = std::get<1>(item); |
| 151 | valueBlock.push_back(Block((uint8_t*)certTlvStr.c_str(), certTlvStr.size())); |
| 152 | request.push_back(valueBlock); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 153 | } |
| 154 | else if (std::get<0>(item) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 155 | request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF_OF_PRIVATE_KEY)); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 156 | auto& sigTlvStr = std::get<1>(item); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 157 | Block valueBlock = makeBinaryBlock(tlv::ParameterValue, (uint8_t*)sigTlvStr.c_str(), sigTlvStr.size()); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 158 | request.push_back(valueBlock); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 159 | } |
| 160 | else { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 161 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 164 | } |
| 165 | else { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 166 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 167 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 168 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 169 | return request; |
| 170 | } |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 171 | |
| 172 | void |
| 173 | ChallengeCredential::fulfillParameters(std::vector<std::tuple<std::string, std::string>>& params, |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame^] | 174 | KeyChain& keyChain, const Name& issuedCertName, const RequestId& requestId) |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 175 | { |
| 176 | auto& pib = keyChain.getPib(); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 177 | auto id = pib.getIdentity(security::extractIdentityFromCertName(issuedCertName)); |
| 178 | auto issuedCert = id.getKey(security::extractKeyNameFromCertName(issuedCertName)).getCertificate(issuedCertName); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 179 | auto issuedCertTlv = issuedCert.wireEncode(); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 180 | auto signatureTlv = keyChain.sign(requestId.data(), requestId.size(), security::signingByCertificate(issuedCertName)); |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 181 | for (auto& item : params) { |
| 182 | if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) { |
| 183 | std::get<1>(item) = std::string((char*)issuedCertTlv.wire(), issuedCertTlv.size()); |
| 184 | } |
| 185 | else if (std::get<0>(item) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) { |
| 186 | std::get<1>(item) = std::string((char*)signatureTlv.value(), signatureTlv.value_size()); |
| 187 | } |
| 188 | } |
| 189 | return; |
| 190 | } |
| 191 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 192 | } // namespace ndncert |
| 193 | } // namespace ndn |