blob: 4349762a556a82a493b91fd6435583d70e2c9c06 [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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
tylerliu8704d032020-06-23 10:18:15 -070024#include "ca-state.hpp"
Zhiyi Zhang46049832020-09-28 17:08:12 -070025
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080026namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang46049832020-09-28 17:08:12 -070029class ChallengeModule : noncopyable {
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
Zhiyi Zhangead9f002020-10-03 15:42:52 -070034 virtual ~ChallengeModule() = default;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080035
Zhiyi Zhang46049832020-09-28 17:08:12 -070036 template <class ChallengeType>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080037 static void
38 registerChallengeModule(const std::string& typeName)
39 {
40 ChallengeFactory& factory = getFactory();
41 BOOST_ASSERT(factory.count(typeName) == 0);
42 factory[typeName] = [] { return make_unique<ChallengeType>(); };
43 }
44
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080045 static bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070046 isChallengeSupported(const std::string& challengeType);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080047
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080048 static unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080049 createChallengeModule(const std::string& challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080050
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070051 // For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070052 virtual std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070053 handleChallengeRequest(const Block& params, CaState& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080054
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070055 // For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -070056 virtual std::vector<std::tuple<std::string, std::string>>
57 getRequestedParameterList(Status status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080058
Suyong Won19fba4d2020-05-09 13:39:46 -070059 virtual Block
Zhiyi Zhang46049832020-09-28 17:08:12 -070060 genChallengeRequestTLV(Status status, const std::string& challengeStatus,
61 std::vector<std::tuple<std::string, std::string>>&& params) = 0;
Suyong Won19fba4d2020-05-09 13:39:46 -070062
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080064 static std::string
65 generateSecretCode();
66
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067protected:
Zhiyi Zhang46049832020-09-28 17:08:12 -070068 // used by challenge modules
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070069 std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070070 returnWithError(CaState& request, ErrorCode errorCode, std::string&& errorInfo);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070072 std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070073 returnWithNewChallengeStatus(CaState& request, const std::string& challengeStatus,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070074 JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime);
Zhiyi Zhang46049832020-09-28 17:08:12 -070075
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070076 std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070077 returnWithSuccess(CaState& request);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080079public:
80 const std::string CHALLENGE_TYPE;
Zhiyi Zhangead9f002020-10-03 15:42:52 -070081 const size_t m_maxAttemptTimes;
82 const time::seconds m_secretLifetime;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080083
84private:
Zhiyi Zhang46049832020-09-28 17:08:12 -070085 typedef function<unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080086 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
87
88 static ChallengeFactory&
89 getFactory();
90};
91
Zhiyi Zhang46049832020-09-28 17:08:12 -070092#define NDNCERT_REGISTER_CHALLENGE(C, T) \
93 static class NdnCert##C##ChallengeRegistrationClass { \
94 public: \
95 NdnCert##C##ChallengeRegistrationClass() \
96 { \
97 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
98 } \
99 } g_NdnCert##C##ChallengeRegistrationVariable
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800100
Zhiyi Zhang46049832020-09-28 17:08:12 -0700101} // namespace ndncert
102} // namespace ndn
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800103
Zhiyi Zhang46049832020-09-28 17:08:12 -0700104#endif // NDNCERT_CHALLENGE_MODULE_HPP