blob: 22c6a4c297c4e6b9711192e65b3d7c0a6e2740de [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, 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 Zhang062be6d2020-10-14 17:13:43 -070024#include "detail/ca-state.hpp"
Zhiyi Zhang46049832020-09-28 17:08:12 -070025
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080026namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070029class ChallengeModule : noncopyable
30{
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080031public:
Zhiyi Zhangead9f002020-10-03 15:42:52 -070032 explicit
33 ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080034
Zhiyi Zhangead9f002020-10-03 15:42:52 -070035 virtual ~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
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080049 static 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>
tylerliu8704d032020-06-23 10:18:15 -070054 handleChallengeRequest(const Block& params, CaState& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080055
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070056 // For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -070057 virtual std::vector<std::tuple<std::string, std::string>>
58 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,
62 std::vector<std::tuple<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>
tylerliu8704d032020-06-23 10:18:15 -070071 returnWithError(CaState& 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>
tylerliu8704d032020-06-23 10:18:15 -070074 returnWithNewChallengeStatus(CaState& request, const std::string& challengeStatus,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070075 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>
tylerliu8704d032020-06-23 10:18:15 -070078 returnWithSuccess(CaState& 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:
Zhiyi Zhang46049832020-09-28 17:08:12 -070086 typedef function<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) \
94 static class NdnCert##C##ChallengeRegistrationClass { \
95 public: \
96 NdnCert##C##ChallengeRegistrationClass() \
97 { \
98 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
99 } \
100 } g_NdnCert##C##ChallengeRegistrationVariable
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800101
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700102} // namespace ndncert
103} // namespace ndn
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800104
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700105#endif // NDNCERT_CHALLENGE_MODULE_HPP