blob: dd572a1965404eee95b8339e309033cf59eed873 [file] [log] [blame]
Zhiyi Zhang23564c82017-03-01 10:22:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang23564c82017-03-01 10:22:22 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "client-module.hpp"
22#include "logging.hpp"
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070023#include "challenge-module.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070024#include "crypto-support/enc-tlv.hpp"
Suyong Won19fba4d2020-05-09 13:39:46 -070025#include "protocol-detail/info.hpp"
26#include "protocol-detail/probe.hpp"
27#include "protocol-detail/new.hpp"
28#include "protocol-detail/challenge.hpp"
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080029#include <ndn-cxx/util/io.hpp>
Zhiyi Zhang23564c82017-03-01 10:22:22 -080030#include <ndn-cxx/security/signing-helpers.hpp>
31#include <ndn-cxx/security/verification-helpers.hpp>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070032#include <ndn-cxx/util/random.hpp>
33#include <ndn-cxx/security/transform/base64-encode.hpp>
34#include <ndn-cxx/security/transform/buffer-source.hpp>
35#include <ndn-cxx/security/transform/stream-sink.hpp>
Zhiyi Zhang23564c82017-03-01 10:22:22 -080036
37namespace ndn {
38namespace ndncert {
39
40_LOG_INIT(ndncert.client);
41
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042ClientModule::ClientModule(security::v2::KeyChain& keyChain)
43 : m_keyChain(keyChain)
Zhiyi Zhang23564c82017-03-01 10:22:22 -080044{
45}
46
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -070047ClientModule::~ClientModule()
48{
49 endSession();
50}
Davide Pesavento08994782018-01-22 12:13:41 -050051
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052shared_ptr<Interest>
swa77020643ac2020-03-26 02:24:45 -070053ClientModule::generateInfoInterest(const Name& caName)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080054{
55 Name interestName = caName;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056 if (readString(caName.at(-1)) != "CA")
57 interestName.append("CA");
swa77020643ac2020-03-26 02:24:45 -070058 interestName.append("INFO");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 auto interest = make_shared<Interest>(interestName);
60 interest->setMustBeFresh(true);
61 interest->setCanBePrefix(false);
62 return interest;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080063}
64
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070065bool
Suyong Won19fba4d2020-05-09 13:39:46 -070066ClientModule::verifyInfoResponse(const Data& reply)
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070067{
68 // parse the ca item
Suyong Won19fba4d2020-05-09 13:39:46 -070069 auto caItem = INFO::decodeClientConfigFromContent(reply.getContent());
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070070
71 // verify the probe Data's sig
72 if (!security::verifySignature(reply, caItem.m_anchor)) {
Suyong Won256c9062020-05-11 02:45:56 -070073 _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri());
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070074 return false;
75 }
76 return true;
77}
78
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080079void
Suyong Won19fba4d2020-05-09 13:39:46 -070080ClientModule::addCaFromInfoResponse(const Data& reply)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080081{
Suyong Won57462ca2020-05-05 22:20:09 -070082 const Block& contentBlock = reply.getContent();
83
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084 // parse the ca item
Suyong Won19fba4d2020-05-09 13:39:46 -070085 auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080086
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070087 // update the local config
88 bool findItem = false;
89 for (auto& item : m_config.m_caItems) {
Suyong Won256c9062020-05-11 02:45:56 -070090 if (item.m_caPrefix == caItem.m_caPrefix) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091 findItem = true;
92 item = caItem;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080093 }
94 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070095 if (!findItem) {
96 m_config.m_caItems.push_back(caItem);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080097 }
Zhiyi Zhang23564c82017-03-01 10:22:22 -080098}
99
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700100shared_ptr<Interest>
101ClientModule::generateProbeInterest(const ClientCaItem& ca, const std::string& probeInfo)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800102{
Suyong Won256c9062020-05-11 02:45:56 -0700103 Name interestName = ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700104 interestName.append("CA").append("PROBE");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700105 auto interest = make_shared<Interest>(interestName);
106 interest->setMustBeFresh(true);
107 interest->setCanBePrefix(false);
Suyong Won19fba4d2020-05-09 13:39:46 -0700108 interest->setApplicationParameters(
109 PROBE::encodeApplicationParametersFromProbeInfo(ca, probeInfo)
110 );
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111
112 // update local state
113 m_ca = ca;
114 return interest;
115}
116
117void
118ClientModule::onProbeResponse(const Data& reply)
119{
120 if (!security::verifySignature(reply, m_ca.m_anchor)) {
Suyong Won256c9062020-05-11 02:45:56 -0700121 _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700122 return;
123 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700124
125 auto contentTLV = reply.getContent();
Suyong Won44d0cce2020-05-10 04:07:43 -0700126 contentTLV.parse();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700127
128 // read the available name and put it into the state
Suyong Won19fba4d2020-05-09 13:39:46 -0700129 if (contentTLV.get(tlv_probe_response).hasValue()) {
130 m_identityName.wireDecode(contentTLV.get(tlv_probe_response));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700131 }
Zhiyi Zhang781a5602019-06-26 19:05:04 -0700132 else {
133 NDN_LOG_TRACE("The JSON_CA_NAME is empty.");
134 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700135}
136
137shared_ptr<Interest>
138ClientModule::generateNewInterest(const time::system_clock::TimePoint& notBefore,
139 const time::system_clock::TimePoint& notAfter,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700140 const Name& identityName, const shared_ptr<Data>& probeToken)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700141{
142 // Name requestedName = identityName;
143 if (!identityName.empty()) { // if identityName is not empty, find the corresponding CA
144 bool findCa = false;
145 for (const auto& caItem : m_config.m_caItems) {
Suyong Won256c9062020-05-11 02:45:56 -0700146 if (caItem.m_caPrefix.isPrefixOf(identityName)) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700147 m_ca = caItem;
148 findCa = true;
149 }
150 }
151 if (!findCa) { // if cannot find, cannot proceed
152 return nullptr;
153 }
154 m_identityName = identityName;
155 }
156 else { // if identityName is empty, check m_identityName or generate a random name
157 if (!m_identityName.empty()) {
158 // do nothing
159 }
160 else {
Zhiyi Zhang781a5602019-06-26 19:05:04 -0700161 NDN_LOG_TRACE("Randomly create a new name because m_identityName is empty and the param is empty.");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700162 auto id = std::to_string(random::generateSecureWord64());
Suyong Won256c9062020-05-11 02:45:56 -0700163 m_identityName = m_ca.m_caPrefix;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700164 m_identityName.append(id);
165 }
166 }
167
168 // generate a newly key pair or use an existing key
Zhiyi Zhang10130782018-02-01 18:28:49 -0800169 const auto& pib = m_keyChain.getPib();
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700170 security::pib::Identity identity;
Zhiyi Zhang10130782018-02-01 18:28:49 -0800171 try {
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700172 identity = pib.getIdentity(m_identityName);
Zhiyi Zhang10130782018-02-01 18:28:49 -0800173 }
174 catch (const security::Pib::Error& e) {
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700175 identity = m_keyChain.createIdentity(m_identityName);
176 m_isNewlyCreatedIdentity = true;
177 m_isNewlyCreatedKey = true;
178 }
179 try {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700180 m_key = identity.getDefaultKey();
Zhiyi Zhang10130782018-02-01 18:28:49 -0800181 }
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700182 catch (const security::Pib::Error& e) {
183 m_key = m_keyChain.createKey(identity);
184 m_isNewlyCreatedKey = true;
185 }
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800186
187 // generate certificate request
188 security::v2::Certificate certRequest;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700189 certRequest.setName(Name(m_key.getName()).append("cert-request").appendVersion());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800190 certRequest.setContentType(tlv::ContentType_Key);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700191 certRequest.setContent(m_key.getPublicKey().data(), m_key.getPublicKey().size());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800192 SignatureInfo signatureInfo;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700193 signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter));
194 m_keyChain.sign(certRequest, signingByKey(m_key.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800195
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700196 // generate Interest packet
Suyong Won256c9062020-05-11 02:45:56 -0700197 Name interestName = m_ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700198 interestName.append("CA").append("NEW");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700199 auto interest = make_shared<Interest>(interestName);
200 interest->setMustBeFresh(true);
201 interest->setCanBePrefix(false);
Suyong Won19fba4d2020-05-09 13:39:46 -0700202 interest->setApplicationParameters(
203 NEW::encodeApplicationParameters(m_ecdh.getBase64PubKey(), certRequest, probeToken)
204 );
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800205
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700206 // sign the Interest packet
207 m_keyChain.sign(*interest, signingByKey(m_key.getName()));
208 return interest;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800209}
210
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700211std::list<std::string>
212ClientModule::onNewResponse(const Data& reply)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800213{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700214 if (!security::verifySignature(reply, m_ca.m_anchor)) {
Suyong Won256c9062020-05-11 02:45:56 -0700215 _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700216 return std::list<std::string>();
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800217 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700218 auto contentTLV = reply.getContent();
Suyong Won44d0cce2020-05-10 04:07:43 -0700219 contentTLV.parse();
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800220
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700221 // ECDH
Suyong Won19fba4d2020-05-09 13:39:46 -0700222 const auto& peerKeyBase64Str = readString(contentTLV.get(tlv_ecdh_pub));
223 const auto& saltStr = readString(contentTLV.get(tlv_salt));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700224 uint64_t saltInt = std::stoull(saltStr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700225 m_ecdh.deriveSecret(peerKeyBase64Str);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800226
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700227 // HKDF
Zhiyi Zhang36706832019-07-04 21:33:03 -0700228 hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen,
229 (uint8_t*)&saltInt, sizeof(saltInt), m_aesKey, sizeof(m_aesKey));
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800230
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700231 // update state
Suyong Won19fba4d2020-05-09 13:39:46 -0700232 m_status = readNonNegativeInteger(contentTLV.get(tlv_status));
233 m_requestId = readString(contentTLV.get(tlv_request_id));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700234 m_challengeList.clear();
Suyong Won19fba4d2020-05-09 13:39:46 -0700235 for (auto const& element : contentTLV.elements()) {
236 if (element.type() == tlv_challenge) {
237 m_challengeList.push_back(readString(element));
238 }
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800239 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700240 return m_challengeList;
241}
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800242
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700243shared_ptr<Interest>
Suyong Won19fba4d2020-05-09 13:39:46 -0700244ClientModule::generateChallengeInterest(const Block& challengeRequest)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700245{
Suyong Won44d0cce2020-05-10 04:07:43 -0700246 challengeRequest.parse();
Suyong Won19fba4d2020-05-09 13:39:46 -0700247 m_challengeType = readString(challengeRequest.get(tlv_selected_challenge));
Suyong Won44d0cce2020-05-10 04:07:43 -0700248
Suyong Won256c9062020-05-11 02:45:56 -0700249 Name interestName = m_ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700250 interestName.append("CA").append("CHALLENGE").append(m_requestId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700251 auto interest = make_shared<Interest>(interestName);
252 interest->setMustBeFresh(true);
253 interest->setCanBePrefix(false);
254
255 // encrypt the Interest parameters
Suyong Won7968f7a2020-05-12 01:01:25 -0700256 auto paramBlock = encodeBlockWithAesGcm128(tlv::ApplicationParameters, m_aesKey,
257 challengeRequest.value(), challengeRequest.value_size(), (const uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700258 interest->setApplicationParameters(paramBlock);
259
260 m_keyChain.sign(*interest, signingByKey(m_key.getName()));
261 return interest;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800262}
263
264void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700265ClientModule::onChallengeResponse(const Data& reply)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800266{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700267 if (!security::verifySignature(reply, m_ca.m_anchor)) {
Suyong Won256c9062020-05-11 02:45:56 -0700268 _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800269 return;
270 }
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700271 auto result = decodeBlockWithAesGcm128(reply.getContent(), m_aesKey, (const uint8_t*)"test", strlen("test"));
Suyong Won19fba4d2020-05-09 13:39:46 -0700272
Suyong Won44d0cce2020-05-10 04:07:43 -0700273 Block contentTLV = makeBinaryBlock(tlv_encrypted_payload, result.data(), result.size());
274 contentTLV.parse();
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800275
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700276 // update state
Suyong Won19fba4d2020-05-09 13:39:46 -0700277 m_status = readNonNegativeInteger(contentTLV.get(tlv_status));
278 m_challengeStatus = readString(contentTLV.get(tlv_challenge_status));
279 m_remainingTries = readNonNegativeInteger(contentTLV.get(tlv_remaining_tries));
280 m_freshBefore = time::system_clock::now() +
281 time::seconds(readNonNegativeInteger(contentTLV.get(tlv_remaining_time)));
282
Suyong Won7968f7a2020-05-12 01:01:25 -0700283 if (contentTLV.find(tlv_issued_cert_name) != contentTLV.elements_end()) {
284 Block issuedCertNameBlock = contentTLV.get(tlv_issued_cert_name);
285 issuedCertNameBlock.parse();
286 m_issuedCertName.wireDecode(issuedCertNameBlock.get(tlv::Name));
287 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700288}
Zhiyi Zhange30eb352017-04-13 15:26:14 -0700289
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700290shared_ptr<Interest>
291ClientModule::generateDownloadInterest()
292{
Suyong Won256c9062020-05-11 02:45:56 -0700293 Name interestName = m_ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700294 interestName.append("CA").append("DOWNLOAD").append(m_requestId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700295 auto interest = make_shared<Interest>(interestName);
296 interest->setMustBeFresh(true);
297 interest->setCanBePrefix(false);
298 return interest;
299}
Zhiyi Zhange30eb352017-04-13 15:26:14 -0700300
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700301shared_ptr<Interest>
302ClientModule::generateCertFetchInterest()
303{
swa770cf1d8f72020-04-21 23:12:39 -0700304 Name interestName = m_issuedCertName;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700305 auto interest = make_shared<Interest>(interestName);
306 interest->setMustBeFresh(true);
307 interest->setCanBePrefix(false);
308 return interest;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800309}
310
swa770cf1d8f72020-04-21 23:12:39 -0700311void
312ClientModule::onCertFetchResponse(const Data& reply)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800313{
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800314 try {
315 security::v2::Certificate cert(reply.getContent().blockFromValue());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700316 m_keyChain.addCertificate(m_key, cert);
swa770cf1d8f72020-04-21 23:12:39 -0700317 _LOG_TRACE("Fetched and installed the cert " << cert.getName());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800318 }
319 catch (const std::exception& e) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700320 _LOG_ERROR("Cannot add replied certificate into the keychain " << e.what());
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700321 return nullptr;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800322 }
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800323}
324
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800325JsonSection
326ClientModule::getJsonFromData(const Data& data)
327{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700328 std::istringstream ss(encoding::readString(data.getContent()));
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800329 JsonSection json;
330 boost::property_tree::json_parser::read_json(ss, json);
331 return json;
332}
333
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700334std::vector<std::string>
335ClientModule::parseProbeComponents(const std::string& probe)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700336{
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700337 std::vector<std::string> components;
Yufeng Zhang424d0362019-06-12 16:48:27 -0700338 std::string delimiter = ":";
339 size_t last = 0;
340 size_t next = 0;
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700341 while ((next = probe.find(delimiter, last)) != std::string::npos) {
342 components.push_back(probe.substr(last, next - last));
343 last = next + 1;
344 }
345 components.push_back(probe.substr(last));
346 return components;
347}
Yufeng Zhang424d0362019-06-12 16:48:27 -0700348
swa77020643ac2020-03-26 02:24:45 -0700349JsonSection
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700350ClientModule::genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo)
351{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700352 JsonSection root;
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700353 std::vector<std::string> fields = parseProbeComponents(ca.m_probe);
354 std::vector<std::string> arguments = parseProbeComponents(probeInfo);;
Yufeng Zhang424d0362019-06-12 16:48:27 -0700355
356 if (arguments.size() != fields.size()) {
357 BOOST_THROW_EXCEPTION(Error("Error in genProbeRequestJson: argument list does not match field list in the config file."));
358 }
Yufeng Zhang424d0362019-06-12 16:48:27 -0700359 for (size_t i = 0; i < fields.size(); ++i) {
360 root.put(fields.at(i), arguments.at(i));
361 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700362 return root;
363}
364
swa77020643ac2020-03-26 02:24:45 -0700365JsonSection
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700366ClientModule::genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
367 const shared_ptr<Data>& probeToken)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700368{
369 JsonSection root;
370 std::stringstream ss;
371 try {
372 security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size())
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800373 >> security::transform::base64Encode(false)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700374 >> security::transform::streamSink(ss);
375 }
376 catch (const security::transform::Error& e) {
377 _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
378 return root;
379 }
380 root.put(JSON_CLIENT_ECDH, ecdhPub);
381 root.put(JSON_CLIENT_CERT_REQ, ss.str());
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700382 if (probeToken != nullptr) {
383 // clear the stringstream
384 ss.str("");
385 ss.clear();
386 // transform the probe data into a base64 string
387 try {
388 security::transform::bufferSource(probeToken->wireEncode().wire(), probeToken->wireEncode().size())
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800389 >> security::transform::base64Encode(false)
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700390 >> security::transform::streamSink(ss);
391 }
392 catch (const security::transform::Error& e) {
393 _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
394 return root;
395 }
396 // add the token into the JSON
397 root.put("probe-token", ss.str());
398 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700399 return root;
400}
401
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800402Block
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700403ClientModule::paramFromJson(const JsonSection& json)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800404{
405 std::stringstream ss;
406 boost::property_tree::write_json(ss, json);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700407 return makeStringBlock(ndn::tlv::ApplicationParameters, ss.str());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800408}
409
410} // namespace ndncert
411} // namespace ndn