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" |
| 21 | #include "../logging.hpp" |
| 22 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 23 | #include <ndn-cxx/util/io.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
| 28 | _LOG_INIT(ndncert.ChallengeCredential); |
| 29 | |
| 30 | NDNCERT_REGISTER_CHALLENGE(ChallengeCredential, "Credential"); |
| 31 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 32 | const std::string ChallengeCredential::FAILURE_INVALID_FORMAT_CREDENTIAL = "failure-cannot-parse-credential"; |
| 33 | const std::string ChallengeCredential::FAILURE_INVALID_FORMAT_SELF_SIGNED = "failure-cannot-parse-self-signed"; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 34 | const std::string ChallengeCredential::FAILURE_INVALID_CREDENTIAL = "failure-invalid-credential"; |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 35 | const std::string ChallengeCredential::JSON_CREDENTIAL_CERT = "issued-cert"; |
| 36 | const std::string ChallengeCredential::JSON_CREDENTIAL_SELF = "self-signed"; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 37 | |
| 38 | ChallengeCredential::ChallengeCredential(const std::string& configPath) |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 39 | : ChallengeModule("Credential") |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 41 | if (configPath.empty()) { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 42 | m_configFile = std::string(SYSCONFDIR) + "/ndncert/challenge-credential.conf"; |
| 43 | } |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 44 | else { |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 45 | m_configFile = configPath; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 46 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
| 50 | ChallengeCredential::parseConfigFile() |
| 51 | { |
| 52 | JsonSection config; |
| 53 | try { |
| 54 | boost::property_tree::read_json(m_configFile, config); |
| 55 | } |
| 56 | catch (const boost::property_tree::info_parser_error& error) { |
| 57 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + m_configFile + |
| 58 | " " + error.message() + " line " + std::to_string(error.line()))); |
| 59 | } |
| 60 | |
| 61 | if (config.begin() == config.end()) { |
| 62 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + m_configFile + " no data")); |
| 63 | } |
| 64 | |
| 65 | m_trustAnchors.clear(); |
| 66 | auto anchorList = config.get_child("anchor-list"); |
| 67 | auto it = anchorList.begin(); |
| 68 | for (; it != anchorList.end(); it++) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 69 | std::istringstream ss(it->second.get("certificate", "")); |
| 70 | auto cert = io::load<security::v2::Certificate>(ss); |
| 71 | if (cert == nullptr) { |
| 72 | _LOG_ERROR("Cannot load the certificate from config file"); |
| 73 | continue; |
| 74 | } |
| 75 | m_trustAnchors.push_back(*cert); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | // For CA |
| 80 | void |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 81 | ChallengeCredential::handleChallengeRequest(const Block& params, CertificateRequest& request) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 82 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 83 | params.parse(); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 84 | if (m_trustAnchors.empty()) { |
| 85 | parseConfigFile(); |
| 86 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 87 | // load credential parameter |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 88 | // TODO: instead of string, should pass certificate byte value directly |
| 89 | std::istringstream ss1(readString(params.elements().at(1))); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 90 | auto cert = io::load<security::v2::Certificate>(ss1); |
| 91 | if (cert == nullptr) { |
| 92 | _LOG_ERROR("Cannot load credential parameter: cert"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 93 | request.m_status = STATUS_FAILURE; |
| 94 | request.m_challengeStatus = FAILURE_INVALID_FORMAT_CREDENTIAL; |
| 95 | updateRequestOnChallengeEnd(request); |
| 96 | return; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 97 | } |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 98 | ss1.str(""); |
| 99 | ss1.clear(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 100 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 101 | // load self-signed data |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 102 | std::istringstream ss2(readString(params.elements().at(3))); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 103 | auto self = io::load<Data>(ss2); |
| 104 | if (self == nullptr) { |
| 105 | _LOG_TRACE("Cannot load credential parameter: self-signed cert"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 106 | request.m_status = STATUS_FAILURE; |
| 107 | request.m_challengeStatus = FAILURE_INVALID_FORMAT_SELF_SIGNED; |
| 108 | updateRequestOnChallengeEnd(request); |
| 109 | return; |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 110 | } |
| 111 | ss2.str(""); |
| 112 | ss2.clear(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 113 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 114 | // verify the credential and the self-signed cert |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 115 | Name signingKeyName = cert->getSignature().getKeyLocator().getName(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 116 | for (auto anchor : m_trustAnchors) { |
| 117 | if (anchor.getKeyName() == signingKeyName) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 118 | if (security::verifySignature(*cert, anchor) && security::verifySignature(*self, *cert) |
| 119 | && readString(self->getContent()) == request.m_requestId) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | request.m_status = STATUS_PENDING; |
| 121 | request.m_challengeStatus = CHALLENGE_STATUS_SUCCESS; |
| 122 | updateRequestOnChallengeEnd(request); |
| 123 | return; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 127 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 128 | _LOG_TRACE("Cannot verify the credential + self-signed Data + data content"); |
| 129 | request.m_status = STATUS_FAILURE; |
| 130 | request.m_challengeStatus = FAILURE_INVALID_CREDENTIAL; |
| 131 | updateRequestOnChallengeEnd(request); |
| 132 | return; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 135 | // For Client |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 136 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 137 | ChallengeCredential::getRequirementForChallenge(int status, const std::string& challengeStatus) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 138 | { |
| 139 | JsonSection result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 140 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
| 141 | result.put(JSON_CREDENTIAL_CERT, "Please_copy_anchor_signed_cert_here"); |
| 142 | result.put(JSON_CREDENTIAL_SELF, "Please_copy_key_signed_request_id_data_here"); |
| 143 | } |
| 144 | else { |
| 145 | _LOG_ERROR("Client's status and challenge status are wrong"); |
| 146 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 147 | return result; |
| 148 | } |
| 149 | |
| 150 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 151 | ChallengeCredential::genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 152 | { |
| 153 | JsonSection result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 154 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 155 | result.put(JSON_CREDENTIAL_CERT, params.get(JSON_CREDENTIAL_CERT, "")); |
| 156 | result.put(JSON_CREDENTIAL_SELF, params.get(JSON_CREDENTIAL_SELF, "")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 157 | } |
| 158 | else { |
| 159 | _LOG_ERROR("Client's status and challenge status are wrong"); |
| 160 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 161 | return result; |
| 162 | } |
| 163 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 164 | Block |
| 165 | ChallengeCredential::genChallengeRequestTLV(int status, const std::string& challengeStatus, const JsonSection& params) |
| 166 | { |
| 167 | Block request = makeEmptyBlock(tlv_encrypted_payload); |
| 168 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
| 169 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
| 170 | request.push_back(makeStringBlock(tlv_parameter_key, JSON_CREDENTIAL_CERT)); |
| 171 | request.push_back(makeStringBlock(tlv_parameter_value, params.get(JSON_CREDENTIAL_CERT,""))); |
| 172 | request.push_back(makeStringBlock(tlv_parameter_key, JSON_CREDENTIAL_SELF)); |
| 173 | request.push_back(makeStringBlock(tlv_parameter_value, params.get(JSON_CREDENTIAL_SELF,""))); |
| 174 | } |
| 175 | else { |
| 176 | _LOG_ERROR("Client's status and challenge status are wrong"); |
| 177 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 178 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 179 | return request; |
| 180 | } |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 181 | } // namespace ndncert |
| 182 | } // namespace ndn |