blob: b2d39ad26c977180925284b8ebdd952d23695baf [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento08994782018-01-22 12:13:41 -05003 * Copyright (c) 2017-2018, 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"
26#include "json-helper.hpp"
27
28namespace ndn {
29namespace ndncert {
30
31class ChallengeModule : noncopyable
32{
33public:
34 /**
35 * @brief Error that can be thrown from ChallengeModule
36 *
37 * ChallengeModule should throw Error to notice CA there's an Error. In this case, CA will
38 * generate an Error JSON file back to end entity.
39 */
40 class Error : public std::runtime_error
41 {
42 public:
43 using std::runtime_error::runtime_error;
44 };
45
46public:
Davide Pesavento08994782018-01-22 12:13:41 -050047 explicit
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080048 ChallengeModule(const std::string& uniqueType);
49
Davide Pesavento08994782018-01-22 12:13:41 -050050 virtual
51 ~ChallengeModule();
52
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080053 template<class ChallengeType>
54 static void
55 registerChallengeModule(const std::string& typeName)
56 {
57 ChallengeFactory& factory = getFactory();
58 BOOST_ASSERT(factory.count(typeName) == 0);
59 factory[typeName] = [] { return make_unique<ChallengeType>(); };
60 }
61
62 static unique_ptr<ChallengeModule>
63 createChallengeModule(const std::string& ChallengeType);
64
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070065 // For CA
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080066 /**
67 * @brief Handle the challenge related interest and update certificate request.
68 * @note Should be used by CA Module
69 * @note Signature of interest should already be validated by CA Module
70 *
71 * When CA receives a SELECT or a VALIDATE or a STATUS interest, CA should invoke the function
72 * to enable selected challenge to go on the verification process.
73 *
74 * @param interest The request interest packet
75 * @param request The CertificateRequest instance
76 * @return the JSON file as the response data content
77 */
78 JsonSection
79 handleChallengeRequest(const Interest& interest, CertificateRequest& request);
80
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070081 // For Client
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080082 /**
83 * @brief Get requirements for requester before sending SELECT interest.
84 * @note Should be used by Client Module
85 *
86 * Before requester sends a USE interest, client should invoke the function to
87 * get input instruction and expose the instruction to requester.
88 *
89 * Every item in the return list requires a input from requester. The item itself is
90 * an instruction for requester.
91 *
92 * @return the input instruction for requester
93 */
94 std::list<std::string>
95 getRequirementForSelect();
96
97 /**
98 * @brief Get requirements for requester before sending VALIDATE interest.
99 * @note Should be used by Client Module
100 *
101 * Before requester sends a POLL interest, client should invoke the function to
102 * get input instruction and expose the instruction to requester.
103 *
104 * Every item in the return list requires a input from requester. The item itself is
105 * an instruction for requester.
106 *
107 * @param status of the challenge
108 * @return the input instruction for requester
109 */
110 std::list<std::string>
111 getRequirementForValidate(const std::string& status);
112
113 /**
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700114 * @brief Generate ChallengeInfo part for SELECT interest.
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800115 * @note Should be used by Client Module
116 *
117 * After requester provides required information, client should invoke the function to
118 * generate the ChallengeInfo part of the interest.
119 *
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800120 * @param status of the challenge
121 * @param paramList contains all the input from requester
122 * @return the JSON file of ChallengeInfo
123 */
124 JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700125 genSelectParamsJson(const std::string& status, const std::list<std::string>& paramList);
126
127 /**
128 * @brief Generate ChallengeInfo part for VALIDATE interest.
129 * @note Should be used by Client Module
130 *
131 * After requester provides required information, client should invoke the function to
132 * generate the ChallengeInfo part of the interest.
133 *
134 * @param status of the challenge
135 * @param paramList contains all the input from requester
136 * @return the JSON file of ChallengeInfo
137 */
138 JsonSection
139 genValidateParamsJson(const std::string& status, const std::list<std::string>& paramList);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800140
141PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700142 // For CA
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800143 virtual JsonSection
144 processSelectInterest(const Interest& interest, CertificateRequest& request) = 0;
145
146 virtual JsonSection
147 processValidateInterest(const Interest& interest, CertificateRequest& request) = 0;
148
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700149 virtual JsonSection
150 processStatusInterest(const Interest& interest, const CertificateRequest& request);
151
152 // For Client
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800153 virtual std::list<std::string>
154 getSelectRequirements() = 0;
155
156 virtual std::list<std::string>
157 getValidateRequirements(const std::string& status) = 0;
158
159 virtual JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700160 doGenSelectParamsJson(const std::string& status, const std::list<std::string>& paramList) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800161
162 virtual JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700163 doGenValidateParamsJson(const std::string& status, const std::list<std::string>& paramList) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800164
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700165 // Helpers
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800166 static JsonSection
167 getJsonFromNameComponent(const Name& name, int pos);
168
169 static Name
170 genDownloadName(const Name& caName, const std::string& requestId);
171
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -0800172 static std::string
173 generateSecretCode();
174
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800175public:
176 const std::string CHALLENGE_TYPE;
177 static const std::string WAIT_SELECTION;
178 static const std::string SUCCESS;
179 static const std::string PENDING;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700180 static const std::string FAILURE;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800181
182private:
183 typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
184 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
185
186 static ChallengeFactory&
187 getFactory();
188};
189
190#define NDNCERT_REGISTER_CHALLENGE(C, T) \
191static class NdnCert ## C ## ChallengeRegistrationClass \
192{ \
193public: \
194 NdnCert ## C ## ChallengeRegistrationClass() \
195 { \
196 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
197 } \
198} g_NdnCert ## C ## ChallengeRegistrationVariable
199
200} // namespace ndncert
201} // namespace ndn
202
203#endif // NDNCERT_CHALLENGE_MODULE_HPP