blob: 923f9be88c65f4095d307d56a0c1140e4b77b10b [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";
41 case Status::NOT_STARTED:
42 return "Not started";
43 case Status::ENDED:
44 return "Ended";
45 default:
46 return "Unrecognized status";
47 }
48}
49
tylerliubb630362020-11-10 11:31:35 -080050Status
51statusFromBlock(const Block& block)
52{
53 auto status_int = readNonNegativeInteger(block);
54 if (status_int > 6)
55 NDN_THROW(std::runtime_error("Unrecognized Status"));
56 return static_cast<Status>(status_int);
57}
58
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070059namespace ca {
60
Zhiyi Zhanga749f442020-09-29 17:19:51 -070061ChallengeState::ChallengeState(const std::string& challengeStatus,
Zhiyi Zhang32437282020-10-10 16:15:37 -070062 const time::system_clock::TimePoint& challengeTp,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070063 size_t remainingTries, time::seconds remainingTime,
64 JsonSection&& challengeSecrets)
65 : m_challengeStatus(challengeStatus)
66 , m_timestamp(challengeTp)
67 , m_remainingTries(remainingTries)
68 , m_remainingTime(remainingTime)
69 , m_secrets(std::move(challengeSecrets))
70{
71}
72
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070073RequestState::RequestState(const Name& caName, const RequestId& requestId, RequestType requestType, Status status,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070074 const security::Certificate& cert, std::array<uint8_t, 16>&& encryptionKey, uint32_t aesBlockCounter)
Zhiyi Zhang48f23782020-09-28 12:11:24 -070075 : m_caPrefix(caName)
76 , m_requestId(requestId)
77 , m_requestType(requestType)
78 , m_status(status)
79 , m_cert(cert)
tylerliu8e170d62020-09-30 01:31:53 -070080 , m_encryptionKey(std::move(encryptionKey))
Zhiyi Zhang222810b2020-10-16 21:50:35 -070081 , m_aesBlockCounter(aesBlockCounter)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080082{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070083}
84
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070085RequestState::RequestState(const Name& caName, const RequestId& requestId, RequestType requestType, Status status,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070086 const security::Certificate& cert, const std::string& challengeType,
87 const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
88 size_t remainingTries, time::seconds remainingTime, JsonSection&& challengeSecrets,
89 std::array<uint8_t, 16>&& encryptionKey, uint32_t aesBlockCounter)
Zhiyi Zhang48f23782020-09-28 12:11:24 -070090 : m_caPrefix(caName)
91 , m_requestId(requestId)
92 , m_requestType(requestType)
93 , m_status(status)
94 , m_cert(cert)
Zhiyi Zhang156bf352020-09-30 17:45:43 -070095 , m_encryptionKey(std::move(encryptionKey))
Zhiyi Zhang222810b2020-10-16 21:50:35 -070096 , m_aesBlockCounter(aesBlockCounter)
Zhiyi Zhang48f23782020-09-28 12:11:24 -070097 , m_challengeType(challengeType)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070098 , m_challengeState(ChallengeState(challengeStatus, challengeTp, remainingTries, remainingTime, std::move(challengeSecrets)))
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099{
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800100}
101
102std::ostream&
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700103operator<<(std::ostream& os, const RequestState& request)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800104{
Zhiyi Zhangd93c0bb2020-10-05 22:06:05 -0700105 os << "Request's CA name: " << request.m_caPrefix << "\n";
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700106 os << "Request's request ID: " << toHex(request.m_requestId.data(), request.m_requestId.size()) << "\n";
Zhiyi Zhangd93c0bb2020-10-05 22:06:05 -0700107 os << "Request's status: " << statusToString(request.m_status) << "\n";
108 os << "Request's challenge type: " << request.m_challengeType << "\n";
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700109 if (request.m_challengeState) {
Zhiyi Zhangd93c0bb2020-10-05 22:06:05 -0700110 os << "Challenge Status: " << request.m_challengeState->m_challengeStatus << "\n";
111 os << "Challenge remaining tries:" << request.m_challengeState->m_remainingTries << " times\n";
112 os << "Challenge remaining time: " << request.m_challengeState->m_remainingTime.count() << " seconds\n";
113 os << "Challenge last update: " << time::toIsoString(request.m_challengeState->m_timestamp) << "\n";
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800114 }
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800115 os << "Certificate:\n";
116 util::IndentedStream os2(os, " ");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700117 os2 << request.m_cert;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800118 return os;
119}
120
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700121} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700122} // namespace ndncert
123} // namespace ndn