blob: b8a6b279641eb5a0a32b59d7c53b21270ba4016e [file] [log] [blame]
Zhiyi Zhanga41c5732017-01-18 14:06:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe5b43692021-11-15 22:05:03 -05002/*
3 * Copyright (c) 2017-2021, Regents of the University of California.
Zhiyi Zhanga41c5732017-01-18 14:06:44 -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
Zhiyi Zhang34f03f02020-10-29 18:34:42 -070021#include "detail/ca-request-state.hpp"
Davide Pesaventoe5b43692021-11-15 22:05:03 -050022
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080023#include <ndn-cxx/util/indented-stream.hpp>
24
Davide Pesaventoe5b43692021-11-15 22:05:03 -050025#include <boost/property_tree/json_parser.hpp>
26
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080027namespace ndn {
28namespace ndncert {
29
tylerliubb630362020-11-10 11:31:35 -080030std::string statusToString(Status status)
31{
Davide Pesaventoe5b43692021-11-15 22:05:03 -050032 switch (status) {
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070033 case Status::BEFORE_CHALLENGE:
34 return "Before challenge";
35 case Status::CHALLENGE:
36 return "In challenge";
37 case Status::PENDING:
38 return "Pending after challenge";
39 case Status::SUCCESS:
40 return "Success";
41 case Status::FAILURE:
42 return "Failure";
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070043 default:
44 return "Unrecognized status";
45 }
46}
47
tylerliubb630362020-11-10 11:31:35 -080048Status
49statusFromBlock(const Block& block)
50{
51 auto status_int = readNonNegativeInteger(block);
52 if (status_int > 6)
53 NDN_THROW(std::runtime_error("Unrecognized Status"));
54 return static_cast<Status>(status_int);
55}
56
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070057namespace ca {
58
Zhiyi Zhanga749f442020-09-29 17:19:51 -070059ChallengeState::ChallengeState(const std::string& challengeStatus,
Zhiyi Zhang32437282020-10-10 16:15:37 -070060 const time::system_clock::TimePoint& challengeTp,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070061 size_t remainingTries, time::seconds remainingTime,
62 JsonSection&& challengeSecrets)
Davide Pesaventoe5b43692021-11-15 22:05:03 -050063 : challengeStatus(challengeStatus)
64 , timestamp(challengeTp)
65 , remainingTries(remainingTries)
66 , remainingTime(remainingTime)
67 , secrets(std::move(challengeSecrets))
Zhiyi Zhanga749f442020-09-29 17:19:51 -070068{
69}
70
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080071std::ostream&
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070072operator<<(std::ostream& os, const RequestState& request)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080073{
tylerliu7b9185c2020-11-24 12:15:18 -080074 os << "Request's CA name: " << request.caPrefix << "\n";
75 os << "Request's request ID: " << toHex(request.requestId.data(), request.requestId.size()) << "\n";
76 os << "Request's status: " << statusToString(request.status) << "\n";
77 os << "Request's challenge type: " << request.challengeType << "\n";
78 if (request.challengeState) {
79 os << "Challenge Status: " << request.challengeState->challengeStatus << "\n";
80 os << "Challenge remaining tries:" << request.challengeState->remainingTries << " times\n";
81 os << "Challenge remaining time: " << request.challengeState->remainingTime.count() << " seconds\n";
82 os << "Challenge last update: " << time::toIsoString(request.challengeState->timestamp) << "\n";
Zhiyi Zhang659d3b02021-01-08 14:39:57 -080083 std::stringstream ss;
84 boost::property_tree::write_json(ss, request.challengeState->secrets);
85 os << "Challenge secret: " << ss.str() << "\n";
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080086 }
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080087 os << "Certificate:\n";
88 util::IndentedStream os2(os, " ");
tylerliu7b9185c2020-11-24 12:15:18 -080089 os2 << request.cert;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080090 return os;
91}
92
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070093} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -070094} // namespace ndncert
95} // namespace ndn