blob: 12193998cc5121c02047a939a3954012fed6dddd [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
Zhiyi Zhange232a742020-09-29 17:34:17 -070024#include "request-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 Zhang46049832020-09-28 17:08:12 -070031 explicit ChallengeModule(const std::string& uniqueType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080032
Zhiyi Zhang46049832020-09-28 17:08:12 -070033 virtual ~ChallengeModule();
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080034
Zhiyi Zhang46049832020-09-28 17:08:12 -070035 template <class ChallengeType>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080036 static void
37 registerChallengeModule(const std::string& typeName)
38 {
39 ChallengeFactory& factory = getFactory();
40 BOOST_ASSERT(factory.count(typeName) == 0);
41 factory[typeName] = [] { return make_unique<ChallengeType>(); };
42 }
43
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080044 static bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070045 isChallengeSupported(const std::string& challengeType);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080046
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080047 static unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080048 createChallengeModule(const std::string& challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080049
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070050 // For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070051 virtual std::tuple<ErrorCode, std::string>
Zhiyi Zhange232a742020-09-29 17:34:17 -070052 handleChallengeRequest(const Block& params, RequestState& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080053
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070054 // For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -070055 virtual std::vector<std::tuple<std::string, std::string>>
56 getRequestedParameterList(Status status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080057
Suyong Won19fba4d2020-05-09 13:39:46 -070058 virtual Block
Zhiyi Zhang46049832020-09-28 17:08:12 -070059 genChallengeRequestTLV(Status status, const std::string& challengeStatus,
60 std::vector<std::tuple<std::string, std::string>>&& params) = 0;
Suyong Won19fba4d2020-05-09 13:39:46 -070061
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080063 static std::string
64 generateSecretCode();
65
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066protected:
Zhiyi Zhang46049832020-09-28 17:08:12 -070067 // used by challenge modules
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070068 std::tuple<ErrorCode, std::string>
Zhiyi Zhange232a742020-09-29 17:34:17 -070069 returnWithError(RequestState& request, ErrorCode errorCode, std::string&& errorInfo);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070070
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070071 std::tuple<ErrorCode, std::string>
Zhiyi Zhange232a742020-09-29 17:34:17 -070072 returnWithNewChallengeStatus(RequestState& request, const std::string& challengeStatus,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070073 JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime);
Zhiyi Zhang46049832020-09-28 17:08:12 -070074
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070075 std::tuple<ErrorCode, std::string>
Zhiyi Zhange232a742020-09-29 17:34:17 -070076 returnWithSuccess(RequestState& request);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080078public:
79 const std::string CHALLENGE_TYPE;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080080
81private:
Zhiyi Zhang46049832020-09-28 17:08:12 -070082 typedef function<unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080083 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
84
85 static ChallengeFactory&
86 getFactory();
87};
88
Zhiyi Zhang46049832020-09-28 17:08:12 -070089#define NDNCERT_REGISTER_CHALLENGE(C, T) \
90 static class NdnCert##C##ChallengeRegistrationClass { \
91 public: \
92 NdnCert##C##ChallengeRegistrationClass() \
93 { \
94 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
95 } \
96 } g_NdnCert##C##ChallengeRegistrationVariable
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080097
Zhiyi Zhang46049832020-09-28 17:08:12 -070098} // namespace ndncert
99} // namespace ndn
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800100
Zhiyi Zhang46049832020-09-28 17:08:12 -0700101#endif // NDNCERT_CHALLENGE_MODULE_HPP