blob: ab15be8fbcfa11c93a519c1272d8199ab637c232 [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
61 static unique_ptr<ChallengeModule>
62 createChallengeModule(const std::string& ChallengeType);
63
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070064 // For CA
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 virtual void
66 handleChallengeRequest(const JsonSection& params, CertificateRequest& request) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080067
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070068 // For Client
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080069 virtual JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070070 getRequirementForChallenge(int status, const std::string& challengeStatus) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080071
72 virtual JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080074
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075 // helpers
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080076 static std::string
77 generateSecretCode();
78
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079protected:
80
81 void
82 updateRequestOnChallengeEnd(CertificateRequest& request);
83
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080084public:
85 const std::string CHALLENGE_TYPE;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080086
87private:
88 typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
89 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
90
91 static ChallengeFactory&
92 getFactory();
93};
94
95#define NDNCERT_REGISTER_CHALLENGE(C, T) \
96static class NdnCert ## C ## ChallengeRegistrationClass \
97{ \
98public: \
99 NdnCert ## C ## ChallengeRegistrationClass() \
100 { \
101 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
102 } \
103} g_NdnCert ## C ## ChallengeRegistrationVariable
104
105} // namespace ndncert
106} // namespace ndn
107
108#endif // NDNCERT_CHALLENGE_MODULE_HPP