blob: 334429a28c438456beef6fb39b058400c5a8e143 [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
tylerliu8704d032020-06-23 10:18:15 -070021#include "ca-state.hpp"
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080022#include <ndn-cxx/util/indented-stream.hpp>
23
24namespace ndn {
25namespace ndncert {
26
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070027std::string statusToString(Status status) {
28 switch (status)
29 {
30 case Status::BEFORE_CHALLENGE:
31 return "Before challenge";
32 case Status::CHALLENGE:
33 return "In challenge";
34 case Status::PENDING:
35 return "Pending after challenge";
36 case Status::SUCCESS:
37 return "Success";
38 case Status::FAILURE:
39 return "Failure";
40 case Status::NOT_STARTED:
41 return "Not started";
42 case Status::ENDED:
43 return "Ended";
44 default:
45 return "Unrecognized status";
46 }
47}
48
Zhiyi Zhanga749f442020-09-29 17:19:51 -070049ChallengeState::ChallengeState(const std::string& challengeStatus,
Zhiyi Zhang32437282020-10-10 16:15:37 -070050 const time::system_clock::TimePoint& challengeTp,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070051 size_t remainingTries, time::seconds remainingTime,
52 JsonSection&& challengeSecrets)
53 : m_challengeStatus(challengeStatus)
54 , m_timestamp(challengeTp)
55 , m_remainingTries(remainingTries)
56 , m_remainingTime(remainingTime)
57 , m_secrets(std::move(challengeSecrets))
58{
59}
60
tylerliu8704d032020-06-23 10:18:15 -070061CaState::CaState()
Zhiyi Zhanga749f442020-09-29 17:19:51 -070062 : m_requestType(RequestType::NOTINITIALIZED)
63 , m_status(Status::NOT_STARTED)
64{
65}
Zhiyi Zhang00a4a002017-03-01 10:17:36 -080066
tylerliu8704d032020-06-23 10:18:15 -070067CaState::CaState(const Name& caName, const std::string& requestId, RequestType requestType, Status status,
Zhiyi Zhang222810b2020-10-16 21:50:35 -070068 const security::Certificate& cert, Block encryptionKey, uint32_t aesBlockCounter)
Zhiyi Zhang48f23782020-09-28 12:11:24 -070069 : m_caPrefix(caName)
70 , m_requestId(requestId)
71 , m_requestType(requestType)
72 , m_status(status)
73 , m_cert(cert)
tylerliu8e170d62020-09-30 01:31:53 -070074 , m_encryptionKey(std::move(encryptionKey))
Zhiyi Zhang222810b2020-10-16 21:50:35 -070075 , m_aesBlockCounter(aesBlockCounter)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080076{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077}
78
tylerliu8704d032020-06-23 10:18:15 -070079CaState::CaState(const Name& caName, const std::string& requestId, RequestType requestType, Status status,
tylerliua7bea662020-10-08 18:51:02 -070080 const security::Certificate& cert, const std::string& challengeType,
Zhiyi Zhang32437282020-10-10 16:15:37 -070081 const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
tylerliu8704d032020-06-23 10:18:15 -070082 size_t remainingTries, time::seconds remainingTime, JsonSection&& challengeSecrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -070083 Block encryptionKey, uint32_t aesBlockCounter)
Zhiyi Zhang48f23782020-09-28 12:11:24 -070084 : m_caPrefix(caName)
85 , m_requestId(requestId)
86 , m_requestType(requestType)
87 , m_status(status)
88 , m_cert(cert)
Zhiyi Zhang156bf352020-09-30 17:45:43 -070089 , m_encryptionKey(std::move(encryptionKey))
Zhiyi Zhang222810b2020-10-16 21:50:35 -070090 , m_aesBlockCounter(aesBlockCounter)
Zhiyi Zhang48f23782020-09-28 12:11:24 -070091 , m_challengeType(challengeType)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070092 , m_challengeState(ChallengeState(challengeStatus, challengeTp, remainingTries, remainingTime, std::move(challengeSecrets)))
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070093{
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080094}
95
96std::ostream&
tylerliu8704d032020-06-23 10:18:15 -070097operator<<(std::ostream& os, const CaState& request)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080098{
Zhiyi Zhangd93c0bb2020-10-05 22:06:05 -070099 os << "Request's CA name: " << request.m_caPrefix << "\n";
100 os << "Request's request ID: " << request.m_requestId << "\n";
101 os << "Request's status: " << statusToString(request.m_status) << "\n";
102 os << "Request's challenge type: " << request.m_challengeType << "\n";
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700103 if (request.m_challengeState) {
Zhiyi Zhangd93c0bb2020-10-05 22:06:05 -0700104 os << "Challenge Status: " << request.m_challengeState->m_challengeStatus << "\n";
105 os << "Challenge remaining tries:" << request.m_challengeState->m_remainingTries << " times\n";
106 os << "Challenge remaining time: " << request.m_challengeState->m_remainingTime.count() << " seconds\n";
107 os << "Challenge last update: " << time::toIsoString(request.m_challengeState->m_timestamp) << "\n";
Zhiyi Zhang59812232020-10-12 13:11:35 -0700108 std::stringstream ss;
109 boost::property_tree::write_json(ss, request.m_challengeState->m_secrets);
110 os << "Challenge secret:\n" << ss.str() << "\n";
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800111 }
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800112 os << "Certificate:\n";
113 util::IndentedStream os2(os, " ");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700114 os2 << request.m_cert;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800115 return os;
116}
117
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700118} // namespace ndncert
119} // namespace ndn