blob: adee8b08e69454bcae9d130e8fa63fd37688c390 [file] [log] [blame]
Davide Pesaventob48bbda2020-07-27 19:41:37 -04001/*
2 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang0a89b722017-04-28 17:56:01 -07003 *
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 Zhang0a89b722017-04-28 17:56:01 -070021#include <ndn-cxx/security/verification-helpers.hpp>
Zhiyi Zhangead9f002020-10-03 15:42:52 -070022#include <ndn-cxx/security/signing-helpers.hpp>
23#include <ndn-cxx/security/transform/public-key.hpp>
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070024#include <ndn-cxx/util/io.hpp>
25
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070029NDN_LOG_INIT(ndncert.challenge.credential);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070030NDNCERT_REGISTER_CHALLENGE(ChallengeCredential, "Credential");
31
Zhiyi Zhang46049832020-09-28 17:08:12 -070032const std::string ChallengeCredential::PARAMETER_KEY_CREDENTIAL_CERT = "issued-cert";
33const std::string ChallengeCredential::PARAMETER_KEY_PROOF_OF_PRIVATE_KEY = "proof-of-private-key";
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070034
35ChallengeCredential::ChallengeCredential(const std::string& configPath)
Zhiyi Zhangead9f002020-10-03 15:42:52 -070036 : ChallengeModule("Credential", 1, time::seconds(1))
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070037{
Davide Pesaventob48bbda2020-07-27 19:41:37 -040038 if (configPath.empty()) {
Zhiyi Zhang840afd92020-10-21 13:24:08 -070039 m_configFile = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/challenge-credential.conf";
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070040 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080041 else {
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070042 m_configFile = configPath;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080043 }
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070044}
45
46void
47ChallengeCredential::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) {
tylerliu41c11532020-10-10 16:14:45 -070054 NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile +
Zhiyi Zhang46049832020-09-28 17:08:12 -070055 " " + error.message() + " line " + std::to_string(error.line())));
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070056 }
57
58 if (config.begin() == config.end()) {
tylerliu41c11532020-10-10 16:14:45 -070059 NDN_THROW(std::runtime_error("Error processing configuration file: " + m_configFile + " no data"));
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070060 }
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 Zhang8da54d62019-11-21 00:03:05 -080066 std::istringstream ss(it->second.get("certificate", ""));
tylerliua7bea662020-10-08 18:51:02 -070067 auto cert = io::load<security::Certificate>(ss);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080068 if (cert == nullptr) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070069 NDN_LOG_ERROR("Cannot load the certificate from config file");
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080070 continue;
71 }
72 m_trustAnchors.push_back(*cert);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070073 }
74}
75
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076// For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070077std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070078ChallengeCredential::handleChallengeRequest(const Block& params, ca::RequestState& request)
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070079{
Suyong Won44d0cce2020-05-10 04:07:43 -070080 params.parse();
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070081 if (m_trustAnchors.empty()) {
82 parseConfigFile();
83 }
tylerliua7bea662020-10-08 18:51:02 -070084 security::Certificate credential;
Zhiyi Zhangead9f002020-10-03 15:42:52 -070085 const uint8_t* signature;
86 size_t signatureLen;
87 const auto& elements = params.elements();
Zhiyi Zhang22998612020-09-25 14:43:23 -070088 for (size_t i = 0; i < elements.size(); i++) {
tylerliu50d679e2020-10-14 14:08:39 -070089 if (elements[i].type() == tlv::ParameterKey) {
Zhiyi Zhang46049832020-09-28 17:08:12 -070090 if (readString(elements[i]) == PARAMETER_KEY_CREDENTIAL_CERT) {
Zhiyi Zhang34a8d432020-10-03 22:14:25 -070091 try {
92 credential.wireDecode(elements[i + 1].blockFromValue());
93 }
94 catch (const std::exception& e) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070095 NDN_LOG_ERROR("Cannot load challenge parameter: credential " << e.what());
Zhiyi Zhang34a8d432020-10-03 22:14:25 -070096 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot challenge credential: credential." + std::string(e.what()));
Zhiyi Zhang22998612020-09-25 14:43:23 -070097 }
98 }
Zhiyi Zhang46049832020-09-28 17:08:12 -070099 else if (readString(elements[i]) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) {
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700100 signature = elements[i + 1].value();
101 signatureLen = elements[i + 1].value_size();
Zhiyi Zhang22998612020-09-25 14:43:23 -0700102 }
103 }
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700104 }
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700105
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700106 // verify the credential and the self-signed cert
tylerliua7bea662020-10-08 18:51:02 -0700107 Name signingKeyName = credential.getSignatureInfo().getKeyLocator().getName();
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700108 security::transform::PublicKey key;
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700109 const auto& pubKeyBuffer = credential.getPublicKey();
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700110 key.loadPkcs8(pubKeyBuffer.data(), pubKeyBuffer.size());
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700111 for (auto anchor : m_trustAnchors) {
112 if (anchor.getKeyName() == signingKeyName) {
Zhiyi Zhang34a8d432020-10-03 22:14:25 -0700113 if (security::verifySignature(credential, anchor) &&
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700114 security::verifySignature(request.m_requestId.data(), request.m_requestId.size(), signature, signatureLen, key)) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700115 return returnWithSuccess(request);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700116 }
117 }
118 }
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700119 NDN_LOG_TRACE("Cannot verify the proof of private key against credential");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700120 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot verify the proof of private key against credential.");
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700121}
122
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123// For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -0700124std::vector<std::tuple<std::string, std::string>>
125ChallengeCredential::getRequestedParameterList(Status status, const std::string& challengeStatus)
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700126{
Zhiyi Zhang46049832020-09-28 17:08:12 -0700127 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 Zhangead9f002020-10-03 15:42:52 -0700131 return result;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700132 }
tylerliu41c11532020-10-10 16:14:45 -0700133 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700134}
135
Suyong Won19fba4d2020-05-09 13:39:46 -0700136Block
Zhiyi Zhang46049832020-09-28 17:08:12 -0700137ChallengeCredential::genChallengeRequestTLV(Status status, const std::string& challengeStatus,
138 std::vector<std::tuple<std::string, std::string>>&& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700139{
tylerliu50d679e2020-10-14 14:08:39 -0700140 Block request = makeEmptyBlock(tlv::EncryptedPayload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700141 if (status == Status::BEFORE_CHALLENGE) {
142 if (params.size() != 2) {
tylerliu41c11532020-10-10 16:14:45 -0700143 NDN_THROW(std::runtime_error("Wrong parameter provided."));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700144 }
tylerliu50d679e2020-10-14 14:08:39 -0700145 request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700146 for (const auto& item : params) {
147 if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) {
tylerliu50d679e2020-10-14 14:08:39 -0700148 request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT));
149 Block valueBlock = makeEmptyBlock(tlv::ParameterValue);
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700150 auto& certTlvStr = std::get<1>(item);
151 valueBlock.push_back(Block((uint8_t*)certTlvStr.c_str(), certTlvStr.size()));
152 request.push_back(valueBlock);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700153 }
154 else if (std::get<0>(item) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) {
tylerliu50d679e2020-10-14 14:08:39 -0700155 request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF_OF_PRIVATE_KEY));
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700156 auto& sigTlvStr = std::get<1>(item);
tylerliu50d679e2020-10-14 14:08:39 -0700157 Block valueBlock = makeBinaryBlock(tlv::ParameterValue, (uint8_t*)sigTlvStr.c_str(), sigTlvStr.size());
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700158 request.push_back(valueBlock);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700159 }
160 else {
tylerliu41c11532020-10-10 16:14:45 -0700161 NDN_THROW(std::runtime_error("Wrong parameter provided."));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700162 }
163 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700164 }
165 else {
tylerliu41c11532020-10-10 16:14:45 -0700166 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
Suyong Won19fba4d2020-05-09 13:39:46 -0700167 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700168 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700169 return request;
170}
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700171
172void
173ChallengeCredential::fulfillParameters(std::vector<std::tuple<std::string, std::string>>& params,
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700174 KeyChain& keyChain, const Name& issuedCertName, const RequestId& requestId)
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700175{
176 auto& pib = keyChain.getPib();
tylerliua7bea662020-10-08 18:51:02 -0700177 auto id = pib.getIdentity(security::extractIdentityFromCertName(issuedCertName));
178 auto issuedCert = id.getKey(security::extractKeyNameFromCertName(issuedCertName)).getCertificate(issuedCertName);
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700179 auto issuedCertTlv = issuedCert.wireEncode();
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700180 auto signatureTlv = keyChain.sign(requestId.data(), requestId.size(), security::signingByCertificate(issuedCertName));
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700181 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 Zhange4891b72020-10-10 15:11:57 -0700192} // namespace ndncert
193} // namespace ndn