blob: 27d19fa2c817a978496e5102b34236bea934a3d8 [file] [log] [blame]
Zhiyi Zhanga41c5732017-01-18 14:06:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -04003 * 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
tylerliu6563f932020-10-30 11:13:38 -070021#ifndef NDNCERT_DETAIL_CA_REQUEST_STATE_HPP
22#define NDNCERT_DETAIL_CA_REQUEST_STATE_HPP
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080023
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070024#include "detail/ndncert-common.hpp"
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070025#include <array>
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080026
27namespace ndn {
28namespace ndncert {
29
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070030typedef std::array<uint8_t, 8> RequestId;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070031
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070032enum class Status : uint16_t {
33 BEFORE_CHALLENGE = 0,
34 CHALLENGE = 1,
35 PENDING = 2,
36 SUCCESS = 3,
37 FAILURE = 4,
38 NOT_STARTED = 5,
39 ENDED = 6
40};
41
Zhiyi Zhangee996152020-10-26 13:58:33 -070042/**
43 * @brief Convert request status to string.
44 */
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -070045std::string
46statusToString(Status status);
47
tylerliubb630362020-11-10 11:31:35 -080048/**
49 * @brief Convert request status to string.
50 */
51Status
52statusFromBlock(const Block& block);
53
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070054namespace ca {
55
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070056/**
Zhiyi Zhangee996152020-10-26 13:58:33 -070057 * @brief The state maintained by the Challenge module.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070058 */
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070059struct ChallengeState
60{
Zhiyi Zhang32437282020-10-10 16:15:37 -070061 ChallengeState(const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070062 size_t remainingTries, time::seconds remainingTime,
63 JsonSection&& challengeSecrets);
Zhiyi Zhangee996152020-10-26 13:58:33 -070064 /**
65 * @brief The status of the challenge.
66 */
Zhiyi Zhanga749f442020-09-29 17:19:51 -070067 std::string m_challengeStatus;
Zhiyi Zhangee996152020-10-26 13:58:33 -070068 /**
69 * @brief The timestamp of the last update of the challenge state.
70 */
Zhiyi Zhang32437282020-10-10 16:15:37 -070071 time::system_clock::TimePoint m_timestamp;
Zhiyi Zhangee996152020-10-26 13:58:33 -070072 /**
73 * @brief Remaining tries of the challenge.
74 */
Zhiyi Zhanga749f442020-09-29 17:19:51 -070075 size_t m_remainingTries;
Zhiyi Zhangee996152020-10-26 13:58:33 -070076 /**
77 * @brief Remaining time of the challenge.
78 */
Zhiyi Zhanga749f442020-09-29 17:19:51 -070079 time::seconds m_remainingTime;
Zhiyi Zhangee996152020-10-26 13:58:33 -070080 /**
81 * @brief The secret for the challenge.
82 */
Zhiyi Zhanga749f442020-09-29 17:19:51 -070083 JsonSection m_secrets;
84};
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080085
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080086/**
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070087 * @brief Represents a certificate request instance kept by the CA.
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080088 *
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070089 * ChallengeModule should take use of RequestState.ChallengeState to keep the challenge state.
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080090 */
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070091class RequestState
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070092{
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080093public:
Zhiyi Zhangee996152020-10-26 13:58:33 -070094 /**
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070095 * @brief Used to instantiate a RequestState when challenge is not started.
Zhiyi Zhangee996152020-10-26 13:58:33 -070096 */
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070097 RequestState(const Name& caName, const RequestId& requestId, RequestType requestType, Status status,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070098 const security::Certificate& cert, std::array<uint8_t, 16>&& m_encryptionKey, uint32_t aesBlockCounter = 0);
Zhiyi Zhangee996152020-10-26 13:58:33 -070099 /**
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700100 * @brief Used to instantiate a RequestState after challenge is started.
Zhiyi Zhangee996152020-10-26 13:58:33 -0700101 */
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700102 RequestState(const Name& caName, const RequestId& requestId, RequestType requestType, Status status,
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700103 const security::Certificate& cert, const std::string& challengeType,
104 const std::string& challengeStatus, const time::system_clock::TimePoint& challengeTp,
105 size_t remainingTries, time::seconds remainingTime, JsonSection&& challengeSecrets,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700106 std::array<uint8_t, 16>&& m_encryptionKey, uint32_t aesBlockCounter);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700107
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108public:
Zhiyi Zhangee996152020-10-26 13:58:33 -0700109 /**
110 * @brief The CA that the request is under.
111 */
Suyong Won256c9062020-05-11 02:45:56 -0700112 Name m_caPrefix;
Zhiyi Zhangee996152020-10-26 13:58:33 -0700113 /**
114 * @brief The ID of the request.
115 */
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700116 RequestId m_requestId;
Zhiyi Zhangee996152020-10-26 13:58:33 -0700117 /**
118 * @brief The type of the request.
119 */
120 RequestType m_requestType = RequestType::NOTINITIALIZED;
121 /**
122 * @brief The status of the request.
123 */
124 Status m_status = Status::NOT_STARTED;
125 /**
126 * @brief The self-signed certificate in the request.
127 */
tylerliua7bea662020-10-08 18:51:02 -0700128 security::Certificate m_cert;
Zhiyi Zhangee996152020-10-26 13:58:33 -0700129 /**
130 * @brief The encryption key for the requester.
131 */
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700132 std::array<uint8_t, 16> m_encryptionKey;
Zhiyi Zhangee996152020-10-26 13:58:33 -0700133 /**
134 * @brief The AES block counter for the requester.
135 */
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700136 uint32_t m_aesBlockCounter = 0;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700137
Zhiyi Zhangee996152020-10-26 13:58:33 -0700138 /**
139 * @brief The challenge type.
140 */
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700141 std::string m_challengeType;
Zhiyi Zhangee996152020-10-26 13:58:33 -0700142 /**
143 * @brief The challenge state.
144 */
Zhiyi Zhang997669a2020-10-28 21:15:40 -0700145 optional<ChallengeState> m_challengeState;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800146};
147
148std::ostream&
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700149operator<<(std::ostream& os, const RequestState& request);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800150
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700151} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700152} // namespace ndncert
153} // namespace ndn
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800154
tylerliu6563f932020-10-30 11:13:38 -0700155#endif // NDNCERT_DETAIL_CA_REQUEST_STATE_HPP