blob: b3baefb5e99f8194caaa50ccf07227879e95c41e [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Davide Pesavento9510c912024-02-25 17:50:05 -05003 * Copyright (c) 2017-2024, Regents of the University of California.
Zhiyi Zhang65ba9322017-01-19 14:15:03 -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
21#ifndef NDNCERT_CHALLENGE_MODULE_HPP
22#define NDNCERT_CHALLENGE_MODULE_HPP
23
Zhiyi Zhang34f03f02020-10-29 18:34:42 -070024#include "detail/ca-request-state.hpp"
Zhiyi Zhang46049832020-09-28 17:08:12 -070025
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026#include <map>
Davide Pesavento9510c912024-02-25 17:50:05 -050027#include <tuple>
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040028
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080029namespace ndncert {
30
Davide Pesavento0dc02012021-11-23 22:55:03 -050031class ChallengeModule : boost::noncopyable
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070032{
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080033public:
Zhiyi Zhangead9f002020-10-03 15:42:52 -070034 ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080035
Davide Pesavento0dc02012021-11-23 22:55:03 -050036 virtual
37 ~ChallengeModule() = default;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080038
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070039 // For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070040 virtual std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070041 handleChallengeRequest(const Block& params, ca::RequestState& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080042
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070043 // For Client
tylerliu40226332020-11-11 15:37:16 -080044 virtual std::multimap<std::string, std::string>
Zhiyi Zhang46049832020-09-28 17:08:12 -070045 getRequestedParameterList(Status status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080046
Suyong Won19fba4d2020-05-09 13:39:46 -070047 virtual Block
Zhiyi Zhang46049832020-09-28 17:08:12 -070048 genChallengeRequestTLV(Status status, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -080049 const std::multimap<std::string, std::string>& params) = 0;
Suyong Won19fba4d2020-05-09 13:39:46 -070050
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040051public: // factory
52 template<class ChallengeType>
53 static void
54 registerChallengeModule(const std::string& type)
55 {
56 auto& factory = getFactory();
57 BOOST_ASSERT(factory.count(type) == 0);
58 factory[type] = [] { return std::make_unique<ChallengeType>(); };
59 }
60
61 static bool
62 isChallengeSupported(const std::string& challengeType);
63
64 static std::unique_ptr<ChallengeModule>
65 createChallengeModule(const std::string& challengeType);
66
67protected: // helpers used by concrete challenge modules
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080068 static std::string
69 generateSecretCode();
70
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040071 static std::tuple<ErrorCode, std::string>
tylerliuf2e6bb52020-12-13 13:23:05 -080072 returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string errorInfo);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070074 std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070075 returnWithNewChallengeStatus(ca::RequestState& request, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -080076 JsonSection challengeSecret, size_t remainingTries, time::seconds remainingTime);
Zhiyi Zhang46049832020-09-28 17:08:12 -070077
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070078 std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070079 returnWithSuccess(ca::RequestState& request);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080081public:
82 const std::string CHALLENGE_TYPE;
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040083
84protected:
Zhiyi Zhangead9f002020-10-03 15:42:52 -070085 const size_t m_maxAttemptTimes;
86 const time::seconds m_secretLifetime;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080087
88private:
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040089 using CreateFunc = std::function<std::unique_ptr<ChallengeModule>()>;
90 using ChallengeFactory = std::map<std::string, CreateFunc>;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080091
92 static ChallengeFactory&
93 getFactory();
94};
95
Zhiyi Zhange4891b72020-10-10 15:11:57 -070096} // namespace ndncert
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080097
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040098#define NDNCERT_REGISTER_CHALLENGE(C, T) \
99static class NdnCert##C##ChallengeRegistrationClass \
100{ \
101public: \
102 NdnCert##C##ChallengeRegistrationClass() \
103 { \
104 ::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
105 } \
106} g_NdnCert##C##ChallengeRegistrationVariable
107
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700108#endif // NDNCERT_CHALLENGE_MODULE_HPP