Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2021, Regents of the University of California. |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 4 | * |
| 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 Zhang | 34f03f0 | 2020-10-29 18:34:42 -0700 | [diff] [blame] | 21 | #include "detail/ca-request-state.hpp" |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 22 | |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/util/indented-stream.hpp> |
| 24 | |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 25 | #include <boost/property_tree/json_parser.hpp> |
| 26 | |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 27 | namespace ndncert { |
| 28 | |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 29 | std::string statusToString(Status status) |
| 30 | { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 31 | switch (status) { |
Zhiyi Zhang | 14f0bc8 | 2020-10-12 13:02:23 -0700 | [diff] [blame] | 32 | case Status::BEFORE_CHALLENGE: |
| 33 | return "Before challenge"; |
| 34 | case Status::CHALLENGE: |
| 35 | return "In challenge"; |
| 36 | case Status::PENDING: |
| 37 | return "Pending after challenge"; |
| 38 | case Status::SUCCESS: |
| 39 | return "Success"; |
| 40 | case Status::FAILURE: |
| 41 | return "Failure"; |
Zhiyi Zhang | 14f0bc8 | 2020-10-12 13:02:23 -0700 | [diff] [blame] | 42 | default: |
| 43 | return "Unrecognized status"; |
| 44 | } |
| 45 | } |
| 46 | |
tylerliu | bb63036 | 2020-11-10 11:31:35 -0800 | [diff] [blame] | 47 | Status |
| 48 | statusFromBlock(const Block& block) |
| 49 | { |
| 50 | auto status_int = readNonNegativeInteger(block); |
| 51 | if (status_int > 6) |
| 52 | NDN_THROW(std::runtime_error("Unrecognized Status")); |
| 53 | return static_cast<Status>(status_int); |
| 54 | } |
| 55 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 56 | namespace ca { |
| 57 | |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 58 | ChallengeState::ChallengeState(const std::string& challengeStatus, |
Zhiyi Zhang | 3243728 | 2020-10-10 16:15:37 -0700 | [diff] [blame] | 59 | const time::system_clock::TimePoint& challengeTp, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 60 | size_t remainingTries, time::seconds remainingTime, |
| 61 | JsonSection&& challengeSecrets) |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 62 | : challengeStatus(challengeStatus) |
| 63 | , timestamp(challengeTp) |
| 64 | , remainingTries(remainingTries) |
| 65 | , remainingTime(remainingTime) |
| 66 | , secrets(std::move(challengeSecrets)) |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 70 | std::ostream& |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 71 | operator<<(std::ostream& os, const RequestState& request) |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 72 | { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 73 | os << "Request's CA name: " << request.caPrefix << "\n"; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 74 | os << "Request's request ID: " << ndn::toHex(request.requestId.data(), request.requestId.size()) << "\n"; |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 75 | os << "Request's status: " << statusToString(request.status) << "\n"; |
| 76 | os << "Request's challenge type: " << request.challengeType << "\n"; |
| 77 | if (request.challengeState) { |
| 78 | os << "Challenge Status: " << request.challengeState->challengeStatus << "\n"; |
| 79 | os << "Challenge remaining tries:" << request.challengeState->remainingTries << " times\n"; |
| 80 | os << "Challenge remaining time: " << request.challengeState->remainingTime.count() << " seconds\n"; |
| 81 | os << "Challenge last update: " << time::toIsoString(request.challengeState->timestamp) << "\n"; |
Zhiyi Zhang | 659d3b0 | 2021-01-08 14:39:57 -0800 | [diff] [blame] | 82 | std::stringstream ss; |
| 83 | boost::property_tree::write_json(ss, request.challengeState->secrets); |
| 84 | os << "Challenge secret: " << ss.str() << "\n"; |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 85 | } |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 86 | os << "Certificate:\n"; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 87 | ndn::util::IndentedStream os2(os, " "); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 88 | os2 << request.cert; |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 89 | return os; |
| 90 | } |
| 91 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 92 | } // namespace ca |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 93 | } // namespace ndncert |