blob: 1e33bfc4bc3805cafc1115a0ffde0b79886eb0ae [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/*
3 * Copyright (c) 2017-2021, 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
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080026namespace ndncert {
27
Davide Pesavento0dc02012021-11-23 22:55:03 -050028class ChallengeModule : boost::noncopyable
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070029{
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080030public:
Zhiyi Zhangead9f002020-10-03 15:42:52 -070031 explicit
32 ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080033
Davide Pesavento0dc02012021-11-23 22:55:03 -050034 virtual
35 ~ChallengeModule() = default;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080036
Zhiyi Zhang46049832020-09-28 17:08:12 -070037 template <class ChallengeType>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080038 static void
39 registerChallengeModule(const std::string& typeName)
40 {
41 ChallengeFactory& factory = getFactory();
42 BOOST_ASSERT(factory.count(typeName) == 0);
Zhiyi Zhang32437282020-10-10 16:15:37 -070043 factory[typeName] = [] { return std::make_unique<ChallengeType>(); };
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080044 }
45
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080046 static bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070047 isChallengeSupported(const std::string& challengeType);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080048
Davide Pesavento0dc02012021-11-23 22:55:03 -050049 static std::unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080050 createChallengeModule(const std::string& challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080051
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070052 // For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070053 virtual std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070054 handleChallengeRequest(const Block& params, ca::RequestState& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080055
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070056 // For Client
tylerliu40226332020-11-11 15:37:16 -080057 virtual std::multimap<std::string, std::string>
Zhiyi Zhang46049832020-09-28 17:08:12 -070058 getRequestedParameterList(Status status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080059
Suyong Won19fba4d2020-05-09 13:39:46 -070060 virtual Block
Zhiyi Zhang46049832020-09-28 17:08:12 -070061 genChallengeRequestTLV(Status status, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -080062 const std::multimap<std::string, std::string>& params) = 0;
Suyong Won19fba4d2020-05-09 13:39:46 -070063
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080065 static std::string
66 generateSecretCode();
67
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068protected:
Zhiyi Zhang46049832020-09-28 17:08:12 -070069 // used by challenge modules
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070070 std::tuple<ErrorCode, std::string>
tylerliuf2e6bb52020-12-13 13:23:05 -080071 returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string errorInfo);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070072
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070073 std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070074 returnWithNewChallengeStatus(ca::RequestState& request, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -080075 JsonSection challengeSecret, size_t remainingTries, time::seconds remainingTime);
Zhiyi Zhang46049832020-09-28 17:08:12 -070076
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070077 std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070078 returnWithSuccess(ca::RequestState& request);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080080public:
81 const std::string CHALLENGE_TYPE;
Zhiyi Zhangead9f002020-10-03 15:42:52 -070082 const size_t m_maxAttemptTimes;
83 const time::seconds m_secretLifetime;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080084
85private:
Davide Pesavento0dc02012021-11-23 22:55:03 -050086 typedef std::function<std::unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080087 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
88
89 static ChallengeFactory&
90 getFactory();
91};
92
Zhiyi Zhang46049832020-09-28 17:08:12 -070093#define NDNCERT_REGISTER_CHALLENGE(C, T) \
Davide Pesavento0dc02012021-11-23 22:55:03 -050094 static class NdnCert##C##ChallengeRegistrationClass \
95 { \
Zhiyi Zhang46049832020-09-28 17:08:12 -070096 public: \
97 NdnCert##C##ChallengeRegistrationClass() \
98 { \
Davide Pesavento0dc02012021-11-23 22:55:03 -050099 ::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
Zhiyi Zhang46049832020-09-28 17:08:12 -0700100 } \
101 } g_NdnCert##C##ChallengeRegistrationVariable
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800102
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700103} // namespace ndncert
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800104
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700105#endif // NDNCERT_CHALLENGE_MODULE_HPP