blob: 53783d42282b8a8d91ccf4e7ac5d699016a34ed4 [file] [log] [blame]
Zhiyi Zhanga41c5732017-01-18 14:06:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
tylerliu182bc532020-09-25 01:54:45 -07003 * Copyright (c) 2017-2020, 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"
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080022#include <ndn-cxx/util/indented-stream.hpp>
23
24namespace ndn {
25namespace ndncert {
26
tylerliubb630362020-11-10 11:31:35 -080027std::string statusToString(Status status)
28{
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070029 switch (status)
30 {
31 case Status::BEFORE_CHALLENGE:
32 return "Before challenge";
33 case Status::CHALLENGE:
34 return "In challenge";
35 case Status::PENDING:
36 return "Pending after challenge";
37 case Status::SUCCESS:
38 return "Success";
39 case Status::FAILURE:
40 return "Failure";
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070041 default:
42 return "Unrecognized status";
43 }
44}
45
tylerliubb630362020-11-10 11:31:35 -080046Status
47statusFromBlock(const Block& block)
48{
49 auto status_int = readNonNegativeInteger(block);
50 if (status_int > 6)
51 NDN_THROW(std::runtime_error("Unrecognized Status"));
52 return static_cast<Status>(status_int);
53}
54
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070055namespace ca {
56
Zhiyi Zhanga749f442020-09-29 17:19:51 -070057ChallengeState::ChallengeState(const std::string& challengeStatus,
Zhiyi Zhang32437282020-10-10 16:15:37 -070058 const time::system_clock::TimePoint& challengeTp,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070059 size_t remainingTries, time::seconds remainingTime,
60 JsonSection&& challengeSecrets)
tylerliu7b9185c2020-11-24 12:15:18 -080061 : challengeStatus(challengeStatus)
62 , timestamp(challengeTp)
63 , remainingTries(remainingTries)
64 , remainingTime(remainingTime)
65 , secrets(std::move(challengeSecrets))
Zhiyi Zhanga749f442020-09-29 17:19:51 -070066{
67}
68
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080069std::ostream&
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070070operator<<(std::ostream& os, const RequestState& request)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080071{
tylerliu7b9185c2020-11-24 12:15:18 -080072 os << "Request's CA name: " << request.caPrefix << "\n";
73 os << "Request's request ID: " << toHex(request.requestId.data(), request.requestId.size()) << "\n";
74 os << "Request's status: " << statusToString(request.status) << "\n";
75 os << "Request's challenge type: " << request.challengeType << "\n";
76 if (request.challengeState) {
77 os << "Challenge Status: " << request.challengeState->challengeStatus << "\n";
78 os << "Challenge remaining tries:" << request.challengeState->remainingTries << " times\n";
79 os << "Challenge remaining time: " << request.challengeState->remainingTime.count() << " seconds\n";
80 os << "Challenge last update: " << time::toIsoString(request.challengeState->timestamp) << "\n";
Zhiyi Zhang659d3b02021-01-08 14:39:57 -080081 std::stringstream ss;
82 boost::property_tree::write_json(ss, request.challengeState->secrets);
83 os << "Challenge secret: " << ss.str() << "\n";
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080084 }
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080085 os << "Certificate:\n";
86 util::IndentedStream os2(os, " ");
tylerliu7b9185c2020-11-24 12:15:18 -080087 os2 << request.cert;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080088 return os;
89}
90
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070091} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -070092} // namespace ndncert
93} // namespace ndn