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 | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 21 | #include <iostream> |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 23 | #include <ndn-cxx/util/io.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 28 | _LOG_INIT(ndncert.challenge.credential); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 29 | NDNCERT_REGISTER_CHALLENGE(ChallengeCredential, "Credential"); |
| 30 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 31 | const std::string ChallengeCredential::PARAMETER_KEY_CREDENTIAL_CERT = "issued-cert"; |
| 32 | 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] | 33 | |
| 34 | ChallengeCredential::ChallengeCredential(const std::string& configPath) |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 35 | : ChallengeModule("Credential") |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 36 | { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 37 | if (configPath.empty()) { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 38 | m_configFile = std::string(SYSCONFDIR) + "/ndncert/challenge-credential.conf"; |
| 39 | } |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 40 | else { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 41 | m_configFile = configPath; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 42 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void |
| 46 | ChallengeCredential::parseConfigFile() |
| 47 | { |
| 48 | JsonSection config; |
| 49 | try { |
| 50 | boost::property_tree::read_json(m_configFile, config); |
| 51 | } |
| 52 | catch (const boost::property_tree::info_parser_error& error) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 53 | BOOST_THROW_EXCEPTION(std::runtime_error("Failed to parse configuration file " + m_configFile + |
| 54 | " " + error.message() + " line " + std::to_string(error.line()))); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (config.begin() == config.end()) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 58 | BOOST_THROW_EXCEPTION(std::runtime_error("Error processing configuration file: " + m_configFile + " no data")); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | m_trustAnchors.clear(); |
| 62 | auto anchorList = config.get_child("anchor-list"); |
| 63 | auto it = anchorList.begin(); |
| 64 | for (; it != anchorList.end(); it++) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 65 | std::istringstream ss(it->second.get("certificate", "")); |
| 66 | auto cert = io::load<security::v2::Certificate>(ss); |
| 67 | if (cert == nullptr) { |
| 68 | _LOG_ERROR("Cannot load the certificate from config file"); |
| 69 | continue; |
| 70 | } |
| 71 | m_trustAnchors.push_back(*cert); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 76 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 77 | ChallengeCredential::handleChallengeRequest(const Block& params, RequestState& request) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 78 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 79 | params.parse(); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 80 | if (m_trustAnchors.empty()) { |
| 81 | parseConfigFile(); |
| 82 | } |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 83 | shared_ptr<security::v2::Certificate> selfSigned, credential; |
| 84 | auto& elements = params.elements(); |
| 85 | for (size_t i = 0; i < elements.size(); i++) { |
| 86 | if (elements[i].type() == tlv_parameter_key) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 87 | if (readString(elements[i]) == PARAMETER_KEY_CREDENTIAL_CERT) { |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 88 | std::istringstream ss(readString(params.elements()[i + 1])); |
| 89 | credential = io::load<security::v2::Certificate>(ss); |
| 90 | if (credential == nullptr) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 91 | _LOG_ERROR("Cannot load challenge parameter: credential"); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 92 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot challenge credential: credential."); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 95 | else if (readString(elements[i]) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) { |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 96 | std::istringstream ss(readString(params.elements()[i + 1])); |
| 97 | selfSigned = io::load<security::v2::Certificate>(ss); |
| 98 | if (selfSigned == nullptr) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 99 | _LOG_ERROR("Cannot load challenge parameter: proof of private key"); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 100 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot load challenge parameter: proof of private key."); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | else { |
| 104 | continue; |
| 105 | } |
| 106 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 107 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 108 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 109 | // verify the credential and the self-signed cert |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 110 | Name signingKeyName = credential->getSignature().getKeyLocator().getName(); |
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 | 3fc7f06 | 2020-09-29 10:55:42 -0700 | [diff] [blame] | 113 | if (security::verifySignature(*credential, anchor) && |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 114 | security::verifySignature(*selfSigned, *credential) && |
| 115 | readString(selfSigned->getContent()) == request.m_requestId) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 116 | return returnWithSuccess(request); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 120 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 121 | _LOG_TRACE("Cannot verify the proof of private key against credential"); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 122 | 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] | 123 | } |
| 124 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 125 | // For Client |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 126 | std::vector<std::tuple<std::string, std::string>> |
| 127 | ChallengeCredential::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 128 | { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 129 | std::vector<std::tuple<std::string, std::string>> result; |
| 130 | if (status == Status::BEFORE_CHALLENGE) { |
| 131 | result.push_back(std::make_tuple(PARAMETER_KEY_CREDENTIAL_CERT, "Please provide the certificate issued by a trusted CA.")); |
| 132 | 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 | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 133 | } |
| 134 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 135 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 136 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 137 | return result; |
| 138 | } |
| 139 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 140 | Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 141 | ChallengeCredential::genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
| 142 | std::vector<std::tuple<std::string, std::string>>&& params) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 143 | { |
| 144 | Block request = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 145 | if (status == Status::BEFORE_CHALLENGE) { |
| 146 | if (params.size() != 2) { |
| 147 | BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided.")); |
| 148 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 149 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 150 | for (const auto& item : params) { |
| 151 | if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) { |
| 152 | request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CREDENTIAL_CERT)); |
| 153 | request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(item))); |
| 154 | } |
| 155 | else if (std::get<0>(item) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) { |
| 156 | request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_PROOF_OF_PRIVATE_KEY)); |
| 157 | request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(item))); |
| 158 | } |
| 159 | else { |
| 160 | BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided.")); |
| 161 | } |
| 162 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 163 | } |
| 164 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 165 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 166 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 167 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 168 | return request; |
| 169 | } |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 170 | } // namespace ndncert |
| 171 | } // namespace ndn |