blob: b91a03cd6eefa72ed0d7df541767730cb394c4b0 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -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 "ca-module.hpp"
22#include "challenge-module.hpp"
23#include "logging.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070024#include "crypto-support/enc-tlv.hpp"
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080025#include <ndn-cxx/util/io.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080026#include <ndn-cxx/security/verification-helpers.hpp>
27#include <ndn-cxx/security/signing-helpers.hpp>
Junxiao Shi7c068032017-05-28 13:40:47 +000028#include <ndn-cxx/util/random.hpp>
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080029
30namespace ndn {
31namespace ndncert {
32
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070033static const int IS_SUBNAME_MIN_OFFSET = 5;
34
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080035_LOG_INIT(ndncert.ca);
36
37CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
38 const std::string& configPath, const std::string& storageType)
39 : m_face(face)
40 , m_keyChain(keyChain)
41{
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070042 // load the config and create storage
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080043 m_config.load(configPath);
44 m_storage = CaStorage::createCaStorage(storageType);
45
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080046 registerPrefix();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047}
48
49CaModule::~CaModule()
50{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 for (auto handle : m_interestFilterHandles) {
52 handle.cancel();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080053 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054 for (auto handle : m_registeredPrefixHandles) {
55 handle.unregister();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080056 }
57}
58
59void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080060CaModule::registerPrefix()
61{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 // register localhop discovery prefix
63 Name localhopProbePrefix("/localhop/CA/PROBE/INFO");
64 auto prefixId = m_face.setInterestFilter(InterestFilter(localhopProbePrefix),
65 bind(&CaModule::onProbe, this, _2),
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080066 bind(&CaModule::onRegisterFailed, this, _2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 m_registeredPrefixHandles.push_back(prefixId);
68 _LOG_TRACE("Prefix " << localhopProbePrefix << " got registered");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080069
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070070 // register prefixes
71 Name prefix = m_config.m_caName;
72 prefix.append("CA");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080073
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 prefixId = m_face.registerPrefix(prefix,
75 [&] (const Name& name) {
76 // register PROBE prefix
77 auto filterId = m_face.setInterestFilter(Name(name).append("_PROBE"),
78 bind(&CaModule::onProbe, this, _2));
79 m_interestFilterHandles.push_back(filterId);
80
81 // register NEW prefix
82 filterId = m_face.setInterestFilter(Name(name).append("_NEW"),
83 bind(&CaModule::onNew, this, _2));
84 m_interestFilterHandles.push_back(filterId);
85
86 // register SELECT prefix
87 filterId = m_face.setInterestFilter(Name(name).append("_CHALLENGE"),
88 bind(&CaModule::onChallenge, this, _2));
89 m_interestFilterHandles.push_back(filterId);
90
91 // register DOWNLOAD prefix
92 filterId = m_face.setInterestFilter(Name(name).append("_DOWNLOAD"),
93 bind(&CaModule::onDownload, this, _2));
94 m_interestFilterHandles.push_back(filterId);
95 _LOG_TRACE("Prefix " << name << " got registered");
96 },
97 bind(&CaModule::onRegisterFailed, this, _2));
98 m_registeredPrefixHandles.push_back(prefixId);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080099}
100
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800101bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102CaModule::setProbeHandler(const ProbeHandler& handler)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800103{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700104 m_config.m_probeHandler = handler;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800105 return false;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800106}
107
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800108bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800110{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111 m_config.m_statusUpdateCallback = onUpdateCallback;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800112 return false;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800113}
114
115void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700116CaModule::onProbe(const Interest& request)
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800117{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700118 // PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent|INFO]
119 _LOG_TRACE("Receive PROBE request");
120 JsonSection contentJson;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800121
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700122 // process PROBE INFO requests
123 if (readString(request.getName().at(-1)) == "INFO") {
124 contentJson = genProbeResponseJson();
125 }
126 else {
127 // if not a PROBE INFO, find an available name
128 std::string availableId = "";
129 const auto& parameterJson = jsonFromBlock(request.getApplicationParameters());
Yufeng Zhang424d0362019-06-12 16:48:27 -0700130 //m_config.m_probe
131
132 //std::string probeInfoStr = parameterJson.get(JSON_CLIENT_PROBE_INFO, "");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700133 if (m_config.m_probeHandler) {
134 try {
Yufeng Zhang424d0362019-06-12 16:48:27 -0700135 availableId = m_config.m_probeHandler(parameterJson);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700136 }
137 catch (const std::exception& e) {
138 _LOG_TRACE("Cannot find PROBE input from PROBE parameters " << e.what());
139 return;
140 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800141 }
142 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700143 // if there is no app-specified name lookup, use a random name id
144 availableId = std::to_string(random::generateSecureWord64());
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800145 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700146 Name newIdentityName = m_config.m_caName;
147 _LOG_TRACE("Handle PROBE: generate an identity " << newIdentityName);
148 newIdentityName.append(availableId);
Yufeng Zhang424d0362019-06-12 16:48:27 -0700149 contentJson = genProbeResponseJson(newIdentityName.toUri(), m_config.m_probe, parameterJson);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800150 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800151
152 Data result;
153 result.setName(request.getName());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700154 result.setContent(dataContentFromJson(contentJson));
155 m_keyChain.sign(result, signingByIdentity(m_config.m_caName));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800156 m_face.put(result);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700157 _LOG_TRACE("Handle PROBE: send out the PROBE response");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800158}
159
160void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700161CaModule::onNew(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800162{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700163 // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800164
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700165 // get ECDH pub key and cert request
166 const auto& parameterJson = jsonFromBlock(request.getApplicationParameters());
167 std::string peerKeyBase64 = parameterJson.get(JSON_CLIENT_ECDH, "");
168
169 // get server's ECDH pub key
170 auto myEcdhPubKeyBase64 = m_ecdh.getBase64PubKey();
171 m_ecdh.deriveSecret(peerKeyBase64);
172 // generate salt for HKDF
173 auto saltInt = random::generateSecureWord64();
174 uint8_t salt[sizeof(saltInt)];
175 std::memcpy(salt, &saltInt, sizeof(saltInt));
176 // hkdf
177 hkdf(m_ecdh.context->sharedSecret, m_ecdh.context->sharedSecretLen,
178 salt, sizeof(saltInt), m_aesKey, 32);
179
180 // parse certificate request
181 std::string certRequestStr = parameterJson.get(JSON_CLIENT_CERT_REQ, "");
182 shared_ptr<security::v2::Certificate> clientCert = nullptr;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800183 try {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700184 std::stringstream ss(certRequestStr);
185 clientCert = io::load<security::v2::Certificate>(ss);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800186 }
187 catch (const std::exception& e) {
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700188 _LOG_ERROR("Unrecognized certificate request " << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800189 return;
190 }
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700191
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700192 // verify the self-signed certificate and the request
193 if (!m_config.m_caName.isPrefixOf(clientCert->getName()) // under ca prefix
194 || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name
195 || clientCert->getName().size() != m_config.m_caName.size() + IS_SUBNAME_MIN_OFFSET) {
196 _LOG_ERROR("Invalid self-signed certificate name " << clientCert->getName());
197 return;
198 }
199 if (!security::verifySignature(*clientCert, *clientCert)) {
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700200 _LOG_TRACE("Cert request with bad signature.");
201 return;
202 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700203 if (!security::verifySignature(request, *clientCert)) {
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700204 _LOG_TRACE("Interest with bad signature.");
205 return;
206 }
207
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700208 // create new request instance
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800209 std::string requestId = std::to_string(random::generateWord64());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700210 CertificateRequest certRequest(m_config.m_caName, requestId, STATUS_BEFORE_CHALLENGE, *clientCert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800211 try {
212 m_storage->addRequest(certRequest);
213 }
214 catch (const std::exception& e) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700215 _LOG_TRACE("Cannot add new request instance into the storage" << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800216 return;
217 }
218
219 Data result;
220 result.setName(request.getName());
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700221 result.setContent(dataContentFromJson(genNewResponseJson(myEcdhPubKeyBase64,
222 std::to_string(saltInt),
223 certRequest,
224 m_config.m_supportedChallenges)));
225 m_keyChain.sign(result, signingByIdentity(m_config.m_caName));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800226 m_face.put(result);
Zhiyi Zhanga63b7372017-05-17 14:14:34 -0700227
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700228 if (m_config.m_statusUpdateCallback) {
229 m_config.m_statusUpdateCallback(certRequest);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800230 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800231}
232
233void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700234CaModule::onChallenge(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800235{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700236 // get certificate request state
237 CertificateRequest certRequest = getCertificateRequest(request);
238 if (certRequest.m_requestId == "") {
239 // cannot get the request state
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800240 return;
241 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700242 // verify signature
243 if (!security::verifySignature(request, certRequest.m_cert)) {
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700244 _LOG_TRACE("Interest with bad signature.");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800245 return;
246 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700247 // decrypt the parameters
248 auto paramJsonPayload = parseEncBlock(m_ecdh.context->sharedSecret,
249 m_ecdh.context->sharedSecretLen,
250 request.getApplicationParameters());
251 std::string paramJsonStr((const char*)paramJsonPayload.data(), paramJsonPayload.size());
252 std::istringstream ss(paramJsonStr);
253 JsonSection paramJson;
254 boost::property_tree::json_parser::read_json(ss, paramJson);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800255
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700256 // load the corresponding challenge module
257 std::string challengeType = paramJson.get<std::string>(JSON_CLIENT_SELECTED_CHALLENGE);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800258 auto challenge = ChallengeModule::createChallengeModule(challengeType);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700259 JsonSection contentJson;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800260 if (challenge == nullptr) {
261 _LOG_TRACE("Unrecognized challenge type " << challengeType);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700262 certRequest.m_status = STATUS_FAILURE;
263 certRequest.m_challengeStatus = CHALLENGE_STATUS_UNKNOWN_CHALLENGE;
264 contentJson = genChallengeResponseJson(certRequest);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800265 }
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700266 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700267 _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
268 // let challenge module handle the request
269 challenge->handleChallengeRequest(paramJson, certRequest);
270 if (certRequest.m_status == STATUS_FAILURE) {
271 // if challenge failed
272 m_storage->deleteRequest(certRequest.m_requestId);
273 contentJson = genChallengeResponseJson(certRequest);
274 _LOG_TRACE("Challenge failed");
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700275 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700276 else if (certRequest.m_status == STATUS_PENDING) {
277 // if challenge succeeded
278 auto issuedCert = issueCertificate(certRequest);
279 certRequest.m_cert = issuedCert;
280 certRequest.m_status = STATUS_SUCCESS;
281 try {
282 m_storage->addCertificate(certRequest.m_requestId, issuedCert);
283 m_storage->deleteRequest(certRequest.m_requestId);
284 _LOG_TRACE("New Certificate Issued " << issuedCert.getName());
285 }
286 catch (const std::exception& e) {
287 _LOG_ERROR("Cannot add issued cert and remove the request " << e.what());
288 return;
289 }
290 if (m_config.m_statusUpdateCallback) {
291 m_config.m_statusUpdateCallback(certRequest);
292 }
293 contentJson = genChallengeResponseJson(certRequest);
294 contentJson.add(JSON_CA_CERT_ID, readString(issuedCert.getName().at(-1)));
295 _LOG_TRACE("Challenge succeeded. Certificate has been issued");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800296 }
297 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700298 try {
299 m_storage->updateRequest(certRequest);
300 }
301 catch (const std::exception& e) {
302 _LOG_TRACE("Cannot update request instance " << e.what());
303 return;
304 }
305 contentJson = genChallengeResponseJson(certRequest);
306 _LOG_TRACE("No failure no success. Challenge moves on");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800307 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800308 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700309
310 Data result;
311 result.setName(request.getName());
312
313 // encrypt the content
314 std::stringstream ss2;
315 boost::property_tree::write_json(ss2, contentJson);
316 auto payload = ss2.str();
317 auto contentBlock = genEncBlock(tlv::Content, m_ecdh.context->sharedSecret,
318 m_ecdh.context->sharedSecretLen,
319 (const uint8_t*)payload.c_str(), payload.size());
320 result.setContent(contentBlock);
321 m_keyChain.sign(result, signingByIdentity(m_config.m_caName));
322 m_face.put(result);
323
324 if (m_config.m_statusUpdateCallback) {
325 m_config.m_statusUpdateCallback(certRequest);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800326 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700327}
328
329void
330CaModule::onDownload(const Interest& request)
331{
332 auto requestId = readString(request.getName().at(-1));
333 security::v2::Certificate signedCert;
334 try {
335 signedCert = m_storage->getCertificate(requestId);
336 }
337 catch (const std::exception& e) {
338 _LOG_ERROR("Cannot read signed cert " << requestId << " from ca database " << e.what());
339 return;
340 }
341 Data result;
342 result.setName(request.getName());
343 result.setContent(signedCert.wireEncode());
344 m_keyChain.sign(result, signingByIdentity(m_config.m_caName));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800345 m_face.put(result);
346}
347
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800348security::v2::Certificate
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700349CaModule::issueCertificate(const CertificateRequest& certRequest)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800350{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700351 auto expectedPeriod =
352 certRequest.m_cert.getValidityPeriod().getPeriod();
353
354 time::system_clock::TimePoint startingTime, endingTime;
355 if (expectedPeriod.first > time::system_clock::now()
356 && expectedPeriod.first < time::system_clock::now()
357 + m_config.m_validityPeriod)
358 {
359 startingTime = expectedPeriod.first;
360 }
361 else {
362 startingTime = time::system_clock::now();
363 }
364 if (expectedPeriod.second < time::system_clock::now() + m_config.m_validityPeriod) {
365 endingTime = expectedPeriod.second;
366 }
367 else {
368 endingTime = time::system_clock::now() + m_config.m_validityPeriod;
369 }
370 security::ValidityPeriod period(startingTime, endingTime);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800371 security::v2::Certificate newCert;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700372
373 Name certName = certRequest.m_cert.getKeyName();
374 certName.append("NDNCERT").append(std::to_string(random::generateSecureWord64()));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800375 newCert.setName(certName);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700376 newCert.setContent(certRequest.m_cert.getContent());
377 _LOG_TRACE("cert request content " << certRequest.m_cert);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800378 SignatureInfo signatureInfo;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800379 signatureInfo.setValidityPeriod(period);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700380 security::SigningInfo signingInfo(security::SigningInfo::SIGNER_TYPE_ID,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700381 m_config.m_caName, signatureInfo);
382 newCert.setFreshnessPeriod(m_config.m_freshnessPeriod);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800383
384 m_keyChain.sign(newCert, signingInfo);
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700385 _LOG_TRACE("new cert got signed" << newCert);
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800386 return newCert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800387}
388
389CertificateRequest
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700390CaModule::getCertificateRequest(const Interest& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800391{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700392 std::string requestId = readString(request.getName().at(m_config.m_caName.size() + 2));
393 _LOG_TRACE("Requet Id to query the database " << requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800394 CertificateRequest certRequest;
395 try {
396 certRequest = m_storage->getRequest(requestId);
397 }
398 catch (const std::exception& e) {
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700399 _LOG_ERROR(e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800400 }
401 return certRequest;
402}
403
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700404/**
405 * @brief Generate JSON file to response PROBE insterest
406 *
407 * PROBE response JSON format:
408 * {
Yufeng Zhang424d0362019-06-12 16:48:27 -0700409 * "name": "@p identifier"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700410 * }
411 */
412const JsonSection
Yufeng Zhang424d0362019-06-12 16:48:27 -0700413CaModule::genProbeResponseJson(const Name& identifier, const std::string& m_probe, const JsonSection& parameterJson)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700414{
Yufeng Zhang424d0362019-06-12 16:48:27 -0700415 std::vector<std::string> fields;
416 std::string delimiter = ":";
417 size_t last = 0;
418 size_t next = 0;
419 while ((next = m_probe.find(delimiter, last)) != std::string::npos) {
420 fields.push_back(m_probe.substr(last, next - last));
421 last = next + 1;
422 }
423 fields.push_back(m_probe.substr(last));
424
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700425 JsonSection root;
Yufeng Zhang424d0362019-06-12 16:48:27 -0700426
427 for (size_t i = 0; i < fields.size(); ++i) {
428 root.put(fields.at(i), parameterJson.get(fields.at(i), ""));
429 }
430
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700431 root.put(JSON_CA_NAME, identifier.toUri());
432 return root;
433}
434
435/**
436 * @brief Generate JSON file to response NEW interest
437 *
438 * Target JSON format:
439 * {
440 * "ecdh-pub": "@p echdPub",
441 * "salt": "@p salt"
442 * "request-id": "@p requestId",
443 * "status": "@p status",
444 * "challenges": [
445 * {
446 * "challenge-id": ""
447 * },
448 * {
449 * "challenge-id": ""
450 * },
451 * ...
452 * ]
453 * }
454 */
455const JsonSection
456CaModule::genProbeResponseJson()
457{
458 JsonSection root;
459 // ca-prefix
460 Name caName = m_config.m_caName;
461 root.put("ca-prefix", caName.toUri());
462
463 // ca-info
464 const auto& pib = m_keyChain.getPib();
465 auto identity = pib.getIdentity(m_config.m_caName);
466 auto cert = identity.getDefaultKey().getDefaultCertificate();
467 std::string caInfo = "";
468 if (m_config.m_caInfo == "") {
469 caInfo = "Issued by " + cert.getSignature().getKeyLocator().getName().toUri();
470 }
471 else {
472 caInfo = m_config.m_caInfo;
473 }
474 root.put("ca-info", caInfo);
475
476 // probe
477 root.put("probe", m_config.m_probe);
478
479 // certificate
480 std::stringstream ss;
481 io::save(cert, ss);
482 root.put("certificate", ss.str());
483
484 return root;
485}
486
487const JsonSection
488CaModule::genNewResponseJson(const std::string& ecdhKey, const std::string& salt,
489 const CertificateRequest& request,
490 const std::list<std::string>& challenges)
491{
492 JsonSection root;
493 JsonSection challengesSection;
494 root.put(JSON_CA_ECDH, ecdhKey);
495 root.put(JSON_CA_SALT, salt);
496 root.put(JSON_CA_EQUEST_ID, request.m_requestId);
497 root.put(JSON_CA_STATUS, std::to_string(request.m_status));
498
499 for (const auto& entry : challenges) {
500 JsonSection challenge;
501 challenge.put(JSON_CA_CHALLENGE_ID, entry);
502 challengesSection.push_back(std::make_pair("", challenge));
503 }
504 root.add_child(JSON_CA_CHALLENGES, challengesSection);
505 return root;
506}
507
508const JsonSection
509CaModule::genChallengeResponseJson(const CertificateRequest& request)
510{
511 JsonSection root;
512 JsonSection challengesSection;
513 root.put(JSON_CA_STATUS, request.m_status);
514 root.put(JSON_CHALLENGE_STATUS, request.m_challengeStatus);
515 root.put(JSON_CHALLENGE_REMAINING_TRIES, std::to_string(request.m_remainingTries));
516 root.put(JSON_CHALLENGE_REMAINING_TIME, std::to_string(request.m_remainingTime));
517 return root;
518}
519
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800520void
521CaModule::onRegisterFailed(const std::string& reason)
522{
Zhiyi Zhang693c1272017-05-20 22:58:55 -0700523 _LOG_ERROR("Failed to register prefix in local hub's daemon, REASON: " << reason);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800524}
525
526Block
527CaModule::dataContentFromJson(const JsonSection& jsonSection)
528{
529 std::stringstream ss;
530 boost::property_tree::write_json(ss, jsonSection);
531 return makeStringBlock(ndn::tlv::Content, ss.str());
532}
533
534JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700535CaModule::jsonFromBlock(const Block& block)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800536{
537 std::string jsonString;
538 try {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700539 jsonString = encoding::readString(block);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800540 }
541 catch (const std::exception& e) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700542 _LOG_ERROR("Cannot read JSON string from TLV Value" << e.what());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800543 return JsonSection();
544 }
545 std::istringstream ss(jsonString);
546 JsonSection json;
547 boost::property_tree::json_parser::read_json(ss, json);
548 return json;
549}
550
551} // namespace ndncert
552} // namespace ndn