blob: d6fdc146081abb827144c918bd3699253e49b70b [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);
191 certRequest.setFreshnessPeriod(time::hours(24));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700192 certRequest.setContent(m_key.getPublicKey().data(), m_key.getPublicKey().size());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800193 SignatureInfo signatureInfo;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700194 signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter));
195 m_keyChain.sign(certRequest, signingByKey(m_key.getName()).setSignatureInfo(signatureInfo));
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800196
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700197 // generate Interest packet
Suyong Won256c9062020-05-11 02:45:56 -0700198 Name interestName = m_ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700199 interestName.append("CA").append("NEW");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700200 auto interest = make_shared<Interest>(interestName);
201 interest->setMustBeFresh(true);
202 interest->setCanBePrefix(false);
Suyong Won19fba4d2020-05-09 13:39:46 -0700203 interest->setApplicationParameters(
204 NEW::encodeApplicationParameters(m_ecdh.getBase64PubKey(), certRequest, probeToken)
205 );
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800206
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700207 // sign the Interest packet
208 m_keyChain.sign(*interest, signingByKey(m_key.getName()));
209 return interest;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800210}
211
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700212std::list<std::string>
213ClientModule::onNewResponse(const Data& reply)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800214{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700215 if (!security::verifySignature(reply, m_ca.m_anchor)) {
Suyong Won256c9062020-05-11 02:45:56 -0700216 _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700217 return std::list<std::string>();
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800218 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700219 auto contentTLV = reply.getContent();
Suyong Won44d0cce2020-05-10 04:07:43 -0700220 contentTLV.parse();
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800221
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700222 // ECDH
Suyong Won19fba4d2020-05-09 13:39:46 -0700223 const auto& peerKeyBase64Str = readString(contentTLV.get(tlv_ecdh_pub));
224 const auto& saltStr = readString(contentTLV.get(tlv_salt));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700225 uint64_t saltInt = std::stoull(saltStr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700226 m_ecdh.deriveSecret(peerKeyBase64Str);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800227
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700228 // HKDF
Zhiyi Zhang36706832019-07-04 21:33:03 -0700229 hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen,
230 (uint8_t*)&saltInt, sizeof(saltInt), m_aesKey, sizeof(m_aesKey));
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800231
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700232 // update state
Suyong Won19fba4d2020-05-09 13:39:46 -0700233 m_status = readNonNegativeInteger(contentTLV.get(tlv_status));
234 m_requestId = readString(contentTLV.get(tlv_request_id));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700235 m_challengeList.clear();
Suyong Won19fba4d2020-05-09 13:39:46 -0700236 for (auto const& element : contentTLV.elements()) {
237 if (element.type() == tlv_challenge) {
238 m_challengeList.push_back(readString(element));
239 }
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800240 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700241 return m_challengeList;
242}
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800243
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700244shared_ptr<Interest>
Suyong Won19fba4d2020-05-09 13:39:46 -0700245ClientModule::generateChallengeInterest(const Block& challengeRequest)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700246{
Suyong Won44d0cce2020-05-10 04:07:43 -0700247 challengeRequest.parse();
Suyong Won19fba4d2020-05-09 13:39:46 -0700248 m_challengeType = readString(challengeRequest.get(tlv_selected_challenge));
Suyong Won44d0cce2020-05-10 04:07:43 -0700249
Suyong Won256c9062020-05-11 02:45:56 -0700250 Name interestName = m_ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700251 interestName.append("CA").append("CHALLENGE").append(m_requestId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700252 auto interest = make_shared<Interest>(interestName);
253 interest->setMustBeFresh(true);
254 interest->setCanBePrefix(false);
255
256 // encrypt the Interest parameters
Suyong Won44d0cce2020-05-10 04:07:43 -0700257 auto payload = challengeRequest.get(tlv_encrypted_payload).getBuffer();
258 auto paramBlock = encodeBlockWithAesGcm128(tlv_encrypted_payload, m_aesKey,
Suyong Won19fba4d2020-05-09 13:39:46 -0700259 payload->data(), payload->size(), (const uint8_t*)"test", strlen("test"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700260 interest->setApplicationParameters(paramBlock);
261
262 m_keyChain.sign(*interest, signingByKey(m_key.getName()));
263 return interest;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800264}
265
266void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700267ClientModule::onChallengeResponse(const Data& reply)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800268{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700269 if (!security::verifySignature(reply, m_ca.m_anchor)) {
Suyong Won256c9062020-05-11 02:45:56 -0700270 _LOG_ERROR("Cannot verify data signature from " << m_ca.m_caPrefix.toUri());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800271 return;
272 }
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -0700273 auto result = decodeBlockWithAesGcm128(reply.getContent(), m_aesKey, (const uint8_t*)"test", strlen("test"));
Suyong Won19fba4d2020-05-09 13:39:46 -0700274
Suyong Won44d0cce2020-05-10 04:07:43 -0700275 Block contentTLV = makeBinaryBlock(tlv_encrypted_payload, result.data(), result.size());
276 contentTLV.parse();
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800277
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700278 // update state
Suyong Won19fba4d2020-05-09 13:39:46 -0700279 m_status = readNonNegativeInteger(contentTLV.get(tlv_status));
280 m_challengeStatus = readString(contentTLV.get(tlv_challenge_status));
281 m_remainingTries = readNonNegativeInteger(contentTLV.get(tlv_remaining_tries));
282 m_freshBefore = time::system_clock::now() +
283 time::seconds(readNonNegativeInteger(contentTLV.get(tlv_remaining_time)));
284
285 m_issuedCertName.wireDecode(contentTLV.get(tlv_issued_cert_name));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700286}
Zhiyi Zhange30eb352017-04-13 15:26:14 -0700287
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700288shared_ptr<Interest>
289ClientModule::generateDownloadInterest()
290{
Suyong Won256c9062020-05-11 02:45:56 -0700291 Name interestName = m_ca.m_caPrefix;
swa770de007bc2020-03-24 21:26:21 -0700292 interestName.append("CA").append("DOWNLOAD").append(m_requestId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700293 auto interest = make_shared<Interest>(interestName);
294 interest->setMustBeFresh(true);
295 interest->setCanBePrefix(false);
296 return interest;
297}
Zhiyi Zhange30eb352017-04-13 15:26:14 -0700298
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700299shared_ptr<Interest>
300ClientModule::generateCertFetchInterest()
301{
swa770cf1d8f72020-04-21 23:12:39 -0700302 Name interestName = m_issuedCertName;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700303 auto interest = make_shared<Interest>(interestName);
304 interest->setMustBeFresh(true);
305 interest->setCanBePrefix(false);
306 return interest;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800307}
308
swa770cf1d8f72020-04-21 23:12:39 -0700309void
310ClientModule::onCertFetchResponse(const Data& reply)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800311{
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800312 try {
313 security::v2::Certificate cert(reply.getContent().blockFromValue());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700314 m_keyChain.addCertificate(m_key, cert);
swa770cf1d8f72020-04-21 23:12:39 -0700315 _LOG_TRACE("Fetched and installed the cert " << cert.getName());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800316 }
317 catch (const std::exception& e) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700318 _LOG_ERROR("Cannot add replied certificate into the keychain " << e.what());
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700319 return nullptr;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800320 }
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800321}
322
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800323JsonSection
324ClientModule::getJsonFromData(const Data& data)
325{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700326 std::istringstream ss(encoding::readString(data.getContent()));
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800327 JsonSection json;
328 boost::property_tree::json_parser::read_json(ss, json);
329 return json;
330}
331
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700332std::vector<std::string>
333ClientModule::parseProbeComponents(const std::string& probe)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700334{
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700335 std::vector<std::string> components;
Yufeng Zhang424d0362019-06-12 16:48:27 -0700336 std::string delimiter = ":";
337 size_t last = 0;
338 size_t next = 0;
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700339 while ((next = probe.find(delimiter, last)) != std::string::npos) {
340 components.push_back(probe.substr(last, next - last));
341 last = next + 1;
342 }
343 components.push_back(probe.substr(last));
344 return components;
345}
Yufeng Zhang424d0362019-06-12 16:48:27 -0700346
swa77020643ac2020-03-26 02:24:45 -0700347JsonSection
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700348ClientModule::genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo)
349{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700350 JsonSection root;
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700351 std::vector<std::string> fields = parseProbeComponents(ca.m_probe);
352 std::vector<std::string> arguments = parseProbeComponents(probeInfo);;
Yufeng Zhang424d0362019-06-12 16:48:27 -0700353
354 if (arguments.size() != fields.size()) {
355 BOOST_THROW_EXCEPTION(Error("Error in genProbeRequestJson: argument list does not match field list in the config file."));
356 }
Yufeng Zhang424d0362019-06-12 16:48:27 -0700357 for (size_t i = 0; i < fields.size(); ++i) {
358 root.put(fields.at(i), arguments.at(i));
359 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700360 return root;
361}
362
swa77020643ac2020-03-26 02:24:45 -0700363JsonSection
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700364ClientModule::genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
365 const shared_ptr<Data>& probeToken)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700366{
367 JsonSection root;
368 std::stringstream ss;
369 try {
370 security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size())
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800371 >> security::transform::base64Encode(false)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700372 >> security::transform::streamSink(ss);
373 }
374 catch (const security::transform::Error& e) {
375 _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
376 return root;
377 }
378 root.put(JSON_CLIENT_ECDH, ecdhPub);
379 root.put(JSON_CLIENT_CERT_REQ, ss.str());
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700380 if (probeToken != nullptr) {
381 // clear the stringstream
382 ss.str("");
383 ss.clear();
384 // transform the probe data into a base64 string
385 try {
386 security::transform::bufferSource(probeToken->wireEncode().wire(), probeToken->wireEncode().size())
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800387 >> security::transform::base64Encode(false)
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700388 >> security::transform::streamSink(ss);
389 }
390 catch (const security::transform::Error& e) {
391 _LOG_ERROR("Cannot convert self-signed cert into BASE64 string " << e.what());
392 return root;
393 }
394 // add the token into the JSON
395 root.put("probe-token", ss.str());
396 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700397 return root;
398}
399
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800400Block
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700401ClientModule::paramFromJson(const JsonSection& json)
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800402{
403 std::stringstream ss;
404 boost::property_tree::write_json(ss, json);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700405 return makeStringBlock(ndn::tlv::ApplicationParameters, ss.str());
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800406}
407
408} // namespace ndncert
409} // namespace ndn