blob: a0c7b60de58619943ab906308dbb69253359938b [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 Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2017-2022, 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>
27
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080028namespace ndncert {
29
Davide Pesavento0dc02012021-11-23 22:55:03 -050030class ChallengeModule : boost::noncopyable
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070031{
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080032public:
Zhiyi Zhangead9f002020-10-03 15:42:52 -070033 ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080034
Davide Pesavento0dc02012021-11-23 22:55:03 -050035 virtual
36 ~ChallengeModule() = default;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080037
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070038 // For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070039 virtual std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070040 handleChallengeRequest(const Block& params, ca::RequestState& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080041
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070042 // For Client
tylerliu40226332020-11-11 15:37:16 -080043 virtual std::multimap<std::string, std::string>
Zhiyi Zhang46049832020-09-28 17:08:12 -070044 getRequestedParameterList(Status status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080045
Suyong Won19fba4d2020-05-09 13:39:46 -070046 virtual Block
Zhiyi Zhang46049832020-09-28 17:08:12 -070047 genChallengeRequestTLV(Status status, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -080048 const std::multimap<std::string, std::string>& params) = 0;
Suyong Won19fba4d2020-05-09 13:39:46 -070049
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040050public: // factory
51 template<class ChallengeType>
52 static void
53 registerChallengeModule(const std::string& type)
54 {
55 auto& factory = getFactory();
56 BOOST_ASSERT(factory.count(type) == 0);
57 factory[type] = [] { return std::make_unique<ChallengeType>(); };
58 }
59
60 static bool
61 isChallengeSupported(const std::string& challengeType);
62
63 static std::unique_ptr<ChallengeModule>
64 createChallengeModule(const std::string& challengeType);
65
66protected: // helpers used by concrete challenge modules
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080067 static std::string
68 generateSecretCode();
69
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040070 static 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;
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040082
83protected:
Zhiyi Zhangead9f002020-10-03 15:42:52 -070084 const size_t m_maxAttemptTimes;
85 const time::seconds m_secretLifetime;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080086
87private:
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040088 using CreateFunc = std::function<std::unique_ptr<ChallengeModule>()>;
89 using ChallengeFactory = std::map<std::string, CreateFunc>;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080090
91 static ChallengeFactory&
92 getFactory();
93};
94
Zhiyi Zhange4891b72020-10-10 15:11:57 -070095} // namespace ndncert
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080096
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040097#define NDNCERT_REGISTER_CHALLENGE(C, T) \
98static class NdnCert##C##ChallengeRegistrationClass \
99{ \
100public: \
101 NdnCert##C##ChallengeRegistrationClass() \
102 { \
103 ::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
104 } \
105} g_NdnCert##C##ChallengeRegistrationVariable
106
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700107#endif // NDNCERT_CHALLENGE_MODULE_HPP