blob: 4cc5fa9377c65955c30163e2e172e01797d86aa9 [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
24#include "ndncert-common.hpp"
25#include "certificate-request.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080026
27namespace ndn {
28namespace ndncert {
29
30class ChallengeModule : noncopyable
31{
32public:
33 /**
34 * @brief Error that can be thrown from ChallengeModule
35 *
36 * ChallengeModule should throw Error to notice CA there's an Error. In this case, CA will
37 * generate an Error JSON file back to end entity.
38 */
39 class Error : public std::runtime_error
40 {
41 public:
42 using std::runtime_error::runtime_error;
43 };
44
45public:
Davide Pesavento08994782018-01-22 12:13:41 -050046 explicit
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080047 ChallengeModule(const std::string& uniqueType);
48
Davide Pesavento08994782018-01-22 12:13:41 -050049 virtual
50 ~ChallengeModule();
51
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080052 template<class ChallengeType>
53 static void
54 registerChallengeModule(const std::string& typeName)
55 {
56 ChallengeFactory& factory = getFactory();
57 BOOST_ASSERT(factory.count(typeName) == 0);
58 factory[typeName] = [] { return make_unique<ChallengeType>(); };
59 }
60
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080061 static bool
62 supportChallenge(const std::string& challengeType);
63
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080064 static unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080065 createChallengeModule(const std::string& challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080066
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070067 // For CA
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068 virtual void
Suyong Won19fba4d2020-05-09 13:39:46 -070069 handleChallengeRequest(const Block& params, CertificateRequest& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080070
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070071 // For Client
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080072 virtual JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 getRequirementForChallenge(int status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080074
75 virtual JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080077
Suyong Won19fba4d2020-05-09 13:39:46 -070078 virtual Block
79 genChallengeRequestTLV(int status, const std::string& challengeStatus, const JsonSection& params) = 0;
80
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080082 static std::string
83 generateSecretCode();
84
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085protected:
86
87 void
88 updateRequestOnChallengeEnd(CertificateRequest& request);
89
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080090public:
91 const std::string CHALLENGE_TYPE;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080092
93private:
94 typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
95 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
96
97 static ChallengeFactory&
98 getFactory();
99};
100
101#define NDNCERT_REGISTER_CHALLENGE(C, T) \
102static class NdnCert ## C ## ChallengeRegistrationClass \
103{ \
104public: \
105 NdnCert ## C ## ChallengeRegistrationClass() \
106 { \
107 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
108 } \
109} g_NdnCert ## C ## ChallengeRegistrationVariable
110
111} // namespace ndncert
112} // namespace ndn
113
114#endif // NDNCERT_CHALLENGE_MODULE_HPP