blob: be17e107b9b655e4f8e9551af13302d798f8bed8 [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 Zhang46049832020-09-28 17:08:12 -070024#include <tuple>
25
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080026#include "certificate-request.hpp"
Zhiyi Zhang46049832020-09-28 17:08:12 -070027#include "ndncert-common.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080028
29namespace ndn {
30namespace ndncert {
31
Zhiyi Zhang46049832020-09-28 17:08:12 -070032class ChallengeModule : noncopyable {
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080033public:
Zhiyi Zhang46049832020-09-28 17:08:12 -070034 explicit ChallengeModule(const std::string& uniqueType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080035
Zhiyi Zhang46049832020-09-28 17:08:12 -070036 virtual ~ChallengeModule();
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080037
Zhiyi Zhang46049832020-09-28 17:08:12 -070038 template <class ChallengeType>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080039 static void
40 registerChallengeModule(const std::string& typeName)
41 {
42 ChallengeFactory& factory = getFactory();
43 BOOST_ASSERT(factory.count(typeName) == 0);
44 factory[typeName] = [] { return make_unique<ChallengeType>(); };
45 }
46
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080047 static bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070048 isChallengeSupported(const std::string& challengeType);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080049
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080050 static unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080051 createChallengeModule(const std::string& challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080052
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070053 // For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070054 virtual std::tuple<ErrorCode, std::string>
Suyong Won19fba4d2020-05-09 13:39:46 -070055 handleChallengeRequest(const Block& params, CertificateRequest& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080056
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070057 // For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -070058 virtual std::vector<std::tuple<std::string, std::string>>
59 getRequestedParameterList(Status status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080060
Suyong Won19fba4d2020-05-09 13:39:46 -070061 virtual Block
Zhiyi Zhang46049832020-09-28 17:08:12 -070062 genChallengeRequestTLV(Status status, const std::string& challengeStatus,
63 std::vector<std::tuple<std::string, std::string>>&& params) = 0;
Suyong Won19fba4d2020-05-09 13:39:46 -070064
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080066 static std::string
67 generateSecretCode();
68
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069protected:
Zhiyi Zhang46049832020-09-28 17:08:12 -070070 // used by challenge modules
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070071 std::tuple<ErrorCode, std::string>
72 returnWithError(CertificateRequest& request, ErrorCode errorCode, std::string&& errorInfo);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070074 std::tuple<ErrorCode, std::string>
Zhiyi Zhang46049832020-09-28 17:08:12 -070075 returnWithNewChallengeStatus(CertificateRequest& request, const std::string& challengeStatus,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070076 JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime);
Zhiyi Zhang46049832020-09-28 17:08:12 -070077
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070078 std::tuple<ErrorCode, std::string>
Zhiyi Zhang46049832020-09-28 17:08:12 -070079 returnWithSuccess(CertificateRequest& request);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080081public:
82 const std::string CHALLENGE_TYPE;
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