blob: 80670f4199381972c121512d3ed06f2daa750cbb [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
69 handleChallengeRequest(const JsonSection& 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
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080079 static std::string
80 generateSecretCode();
81
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070082protected:
83
84 void
85 updateRequestOnChallengeEnd(CertificateRequest& request);
86
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080087public:
88 const std::string CHALLENGE_TYPE;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080089
90private:
91 typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
92 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
93
94 static ChallengeFactory&
95 getFactory();
96};
97
98#define NDNCERT_REGISTER_CHALLENGE(C, T) \
99static class NdnCert ## C ## ChallengeRegistrationClass \
100{ \
101public: \
102 NdnCert ## C ## ChallengeRegistrationClass() \
103 { \
104 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
105 } \
106} g_NdnCert ## C ## ChallengeRegistrationVariable
107
108} // namespace ndncert
109} // namespace ndn
110
111#endif // NDNCERT_CHALLENGE_MODULE_HPP