blob: d4c8185be962320ee9aba9dcc5bf19b67ea3ce48 [file] [log] [blame]
tylerliuf51e3162020-12-20 19:22:59 -08001/*
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -04002 * Copyright (c) 2017-2022, Regents of the University of California.
tylerliuf51e3162020-12-20 19:22:59 -08003 *
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 Pesaventoe5b43692021-11-15 22:05:03 -050021
tylerliuf51e3162020-12-20 19:22:59 -080022#include <ndn-cxx/security/signing-helpers.hpp>
23#include <ndn-cxx/security/transform/public-key.hpp>
Davide Pesaventoe5b43692021-11-15 22:05:03 -050024#include <ndn-cxx/security/verification-helpers.hpp>
tylerliuf51e3162020-12-20 19:22:59 -080025#include <ndn-cxx/util/io.hpp>
26#include <ndn-cxx/util/random.hpp>
27
Davide Pesaventoe5b43692021-11-15 22:05:03 -050028#include <boost/property_tree/json_parser.hpp>
29
tylerliuf51e3162020-12-20 19:22:59 -080030namespace ndncert {
31
32NDN_LOG_INIT(ndncert.challenge.possession);
tylerliuf26d41b2021-10-12 16:50:16 -070033NDNCERT_REGISTER_CHALLENGE(ChallengePossession, "possession");
tylerliuf51e3162020-12-20 19:22:59 -080034
35const std::string ChallengePossession::PARAMETER_KEY_CREDENTIAL_CERT = "issued-cert";
36const std::string ChallengePossession::PARAMETER_KEY_NONCE = "nonce";
37const std::string ChallengePossession::PARAMETER_KEY_PROOF = "proof";
38const std::string ChallengePossession::NEED_PROOF = "need-proof";
39
40ChallengePossession::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
51void
52ChallengePossession::parseConfigFile()
53{
54 JsonSection config;
55 try {
56 boost::property_tree::read_json(m_configFile, config);
57 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -050058 catch (const boost::property_tree::file_parser_error& error) {
59 NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + ": " +
Davide Pesavento0dc02012021-11-23 22:55:03 -050060 error.message() + " on line " + ndn::to_string(error.line())));
tylerliuf51e3162020-12-20 19:22:59 -080061 }
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 Pesavento0dc02012021-11-23 22:55:03 -050072 auto cert = ndn::io::load<Certificate>(ss);
tylerliuf51e3162020-12-20 19:22:59 -080073 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
82std::tuple<ErrorCode, std::string>
83ChallengePossession::handleChallengeRequest(const Block& params, ca::RequestState& request)
84{
85 params.parse();
86 if (m_trustAnchors.empty()) {
87 parseConfigFile();
88 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050089 Certificate credential;
tylerliuf51e3162020-12-20 19:22:59 -080090 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 Pesaventoe5b43692021-11-15 22:05:03 -0500101 return returnWithError(request, ErrorCode::INVALID_PARAMETER,
102 "Cannot challenge credential: credential."s + e.what());
tylerliuf51e3162020-12-20 19:22:59 -0800103 }
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 Pesavento0dc02012021-11-23 22:55:03 -0500115 using ndn::toHex;
116
tylerliuf51e3162020-12-20 19:22:59 -0800117 // check the certificate
118 bool checkOK = false;
119 if (credential.hasContent() && signatureLen == 0) {
120 Name signingKeyName = credential.getSignatureInfo().getKeyLocator().getName();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500121 ndn::security::transform::PublicKey key;
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400122 key.loadPkcs8(credential.getPublicKey());
123 for (const auto& anchor : m_trustAnchors) {
tylerliuf51e3162020-12-20 19:22:59 -0800124 if (anchor.getKeyName() == signingKeyName) {
Davide Pesavento0dc02012021-11-23 22:55:03 -0500125 if (ndn::security::verifySignature(credential, anchor)) {
tylerliuf51e3162020-12-20 19:22:59 -0800126 checkOK = true;
127 }
128 }
129 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500130 }
131 else {
132 return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate");
tylerliuf51e3162020-12-20 19:22:59 -0800133 }
134 if (!checkOK) {
135 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Certificate cannot be verified");
136 }
137
138 // for the first time, init the challenge
139 std::array<uint8_t, 16> secretCode{};
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400140 ndn::random::generateSecureBytes(secretCode);
tylerliuf51e3162020-12-20 19:22:59 -0800141 JsonSection secretJson;
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400142 secretJson.add(PARAMETER_KEY_NONCE, toHex(secretCode));
143 const auto& credBlock = credential.wireEncode();
144 secretJson.add(PARAMETER_KEY_CREDENTIAL_CERT, toHex({credBlock.wire(), credBlock.size()}));
145 NDN_LOG_TRACE("Secret for request " << toHex(request.requestId) << " : " << toHex(secretCode));
tylerliuf51e3162020-12-20 19:22:59 -0800146 return returnWithNewChallengeStatus(request, NEED_PROOF, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500147 }
148 else if (request.challengeState && request.challengeState->challengeStatus == NEED_PROOF) {
tylerliuf51e3162020-12-20 19:22:59 -0800149 NDN_LOG_TRACE("Challenge Interest (proof) arrives. Check the proof");
150 //check the format and load credential
151 if (credential.hasContent() || signatureLen == 0) {
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500152 return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate");
tylerliuf51e3162020-12-20 19:22:59 -0800153 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500154 credential = Certificate(Block(ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_CREDENTIAL_CERT, ""))));
155 auto secretCode = *ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_NONCE, ""));
tylerliuf51e3162020-12-20 19:22:59 -0800156
157 //check the proof
Davide Pesavento0dc02012021-11-23 22:55:03 -0500158 ndn::security::transform::PublicKey key;
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400159 key.loadPkcs8(credential.getPublicKey());
160 if (ndn::security::verifySignature({secretCode}, {signature, signatureLen}, key)) {
tylerliuf51e3162020-12-20 19:22:59 -0800161 return returnWithSuccess(request);
162 }
163 return returnWithError(request, ErrorCode::INVALID_PARAMETER,
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500164 "Cannot verify the proof of private key against credential.");
tylerliuf51e3162020-12-20 19:22:59 -0800165 }
166 NDN_LOG_TRACE("Proof of possession: bad state");
167 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Fail to recognize the request.");
168}
169
170// For Client
171std::multimap<std::string, std::string>
172ChallengePossession::getRequestedParameterList(Status status, const std::string& challengeStatus)
173{
174 std::multimap<std::string, std::string> result;
175 if (status == Status::BEFORE_CHALLENGE) {
176 result.emplace(PARAMETER_KEY_CREDENTIAL_CERT, "Please provide the certificate issued by a trusted CA.");
177 return result;
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500178 }
179 else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) {
tylerliuf51e3162020-12-20 19:22:59 -0800180 result.emplace(PARAMETER_KEY_PROOF, "Please sign a Data packet with request ID as the content.");
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500181 }
182 else {
tylerliuf51e3162020-12-20 19:22:59 -0800183 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
184 }
tylerliuf51e3162020-12-20 19:22:59 -0800185 return result;
186}
187
188Block
189ChallengePossession::genChallengeRequestTLV(Status status, const std::string& challengeStatus,
190 const std::multimap<std::string, std::string>& params)
191{
192 Block request(tlv::EncryptedPayload);
193 if (status == Status::BEFORE_CHALLENGE) {
194 if (params.size() != 1) {
195 NDN_THROW(std::runtime_error("Wrong parameter provided."));
196 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500197 request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
tylerliuf51e3162020-12-20 19:22:59 -0800198 for (const auto& item : params) {
199 if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) {
Davide Pesavento0dc02012021-11-23 22:55:03 -0500200 request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT));
tylerliuf51e3162020-12-20 19:22:59 -0800201 Block valueBlock(tlv::ParameterValue);
202 auto& certTlvStr = std::get<1>(item);
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400203 valueBlock.push_back(Block(ndn::make_span(reinterpret_cast<const uint8_t*>(certTlvStr.data()),
204 certTlvStr.size())));
tylerliuf51e3162020-12-20 19:22:59 -0800205 request.push_back(valueBlock);
206 }
207 else {
208 NDN_THROW(std::runtime_error("Wrong parameter provided."));
209 }
210 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500211 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500212 else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) {
tylerliuf51e3162020-12-20 19:22:59 -0800213 if (params.size() != 1) {
214 NDN_THROW(std::runtime_error("Wrong parameter provided."));
215 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500216 for (const auto& item : params) {
tylerliuf51e3162020-12-20 19:22:59 -0800217 if (std::get<0>(item) == PARAMETER_KEY_PROOF) {
Davide Pesavento0dc02012021-11-23 22:55:03 -0500218 request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF));
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500219 auto& sigTlvStr = std::get<1>(item);
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400220 auto valueBlock = ndn::makeBinaryBlock(tlv::ParameterValue, sigTlvStr.data(), sigTlvStr.size());
tylerliuf51e3162020-12-20 19:22:59 -0800221 request.push_back(valueBlock);
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500222 }
223 else {
tylerliuf51e3162020-12-20 19:22:59 -0800224 NDN_THROW(std::runtime_error("Wrong parameter provided."));
225 }
226 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500227 }
228 else {
tylerliuf51e3162020-12-20 19:22:59 -0800229 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
230 }
231 request.encode();
232 return request;
233}
234
235void
236ChallengePossession::fulfillParameters(std::multimap<std::string, std::string>& params,
Davide Pesavento0dc02012021-11-23 22:55:03 -0500237 ndn::KeyChain& keyChain, const Name& issuedCertName,
tylerliuf51e3162020-12-20 19:22:59 -0800238 const std::array<uint8_t, 16>& nonce)
239{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500240 auto keyName = ndn::security::extractKeyNameFromCertName(issuedCertName);
241 auto id = keyChain.getPib().getIdentity(ndn::security::extractIdentityFromCertName(issuedCertName));
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500242 auto issuedCert = id.getKey(keyName).getCertificate(issuedCertName);
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400243 const auto& issuedCertTlv = issuedCert.wireEncode();
244 auto signature = keyChain.getTpm().sign({nonce}, keyName, ndn::DigestAlgorithm::SHA256);
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500245
tylerliuf51e3162020-12-20 19:22:59 -0800246 for (auto& item : params) {
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500247 if (item.first == PARAMETER_KEY_CREDENTIAL_CERT) {
248 item.second = std::string(reinterpret_cast<const char*>(issuedCertTlv.wire()), issuedCertTlv.size());
tylerliuf51e3162020-12-20 19:22:59 -0800249 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -0500250 else if (item.first == PARAMETER_KEY_PROOF) {
251 item.second = std::string(signature->get<char>(), signature->size());
tylerliuf51e3162020-12-20 19:22:59 -0800252 }
253 }
tylerliuf51e3162020-12-20 19:22:59 -0800254}
255
256} // namespace ndncert