blob: 7e36d4c17ee25b51976b40a369af4d927c01bcd9 [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
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:
47 ChallengeModule(const std::string& uniqueType);
48
49 template<class ChallengeType>
50 static void
51 registerChallengeModule(const std::string& typeName)
52 {
53 ChallengeFactory& factory = getFactory();
54 BOOST_ASSERT(factory.count(typeName) == 0);
55 factory[typeName] = [] { return make_unique<ChallengeType>(); };
56 }
57
58 static unique_ptr<ChallengeModule>
59 createChallengeModule(const std::string& ChallengeType);
60
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070061 // For CA
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080062 /**
63 * @brief Handle the challenge related interest and update certificate request.
64 * @note Should be used by CA Module
65 * @note Signature of interest should already be validated by CA Module
66 *
67 * When CA receives a SELECT or a VALIDATE or a STATUS interest, CA should invoke the function
68 * to enable selected challenge to go on the verification process.
69 *
70 * @param interest The request interest packet
71 * @param request The CertificateRequest instance
72 * @return the JSON file as the response data content
73 */
74 JsonSection
75 handleChallengeRequest(const Interest& interest, CertificateRequest& request);
76
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070077 // For Client
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080078 /**
79 * @brief Get requirements for requester before sending SELECT interest.
80 * @note Should be used by Client Module
81 *
82 * Before requester sends a USE interest, client should invoke the function to
83 * get input instruction and expose the instruction to requester.
84 *
85 * Every item in the return list requires a input from requester. The item itself is
86 * an instruction for requester.
87 *
88 * @return the input instruction for requester
89 */
90 std::list<std::string>
91 getRequirementForSelect();
92
93 /**
94 * @brief Get requirements for requester before sending VALIDATE interest.
95 * @note Should be used by Client Module
96 *
97 * Before requester sends a POLL interest, client should invoke the function to
98 * get input instruction and expose the instruction to requester.
99 *
100 * Every item in the return list requires a input from requester. The item itself is
101 * an instruction for requester.
102 *
103 * @param status of the challenge
104 * @return the input instruction for requester
105 */
106 std::list<std::string>
107 getRequirementForValidate(const std::string& status);
108
109 /**
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700110 * @brief Generate ChallengeInfo part for SELECT interest.
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800111 * @note Should be used by Client Module
112 *
113 * After requester provides required information, client should invoke the function to
114 * generate the ChallengeInfo part of the interest.
115 *
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800116 * @param status of the challenge
117 * @param paramList contains all the input from requester
118 * @return the JSON file of ChallengeInfo
119 */
120 JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700121 genSelectParamsJson(const std::string& status, const std::list<std::string>& paramList);
122
123 /**
124 * @brief Generate ChallengeInfo part for VALIDATE interest.
125 * @note Should be used by Client Module
126 *
127 * After requester provides required information, client should invoke the function to
128 * generate the ChallengeInfo part of the interest.
129 *
130 * @param status of the challenge
131 * @param paramList contains all the input from requester
132 * @return the JSON file of ChallengeInfo
133 */
134 JsonSection
135 genValidateParamsJson(const std::string& status, const std::list<std::string>& paramList);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800136
137PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700138 // For CA
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800139 virtual JsonSection
140 processSelectInterest(const Interest& interest, CertificateRequest& request) = 0;
141
142 virtual JsonSection
143 processValidateInterest(const Interest& interest, CertificateRequest& request) = 0;
144
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700145 virtual JsonSection
146 processStatusInterest(const Interest& interest, const CertificateRequest& request);
147
148 // For Client
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800149 virtual std::list<std::string>
150 getSelectRequirements() = 0;
151
152 virtual std::list<std::string>
153 getValidateRequirements(const std::string& status) = 0;
154
155 virtual JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700156 doGenSelectParamsJson(const std::string& status, const std::list<std::string>& paramList) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800157
158 virtual JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700159 doGenValidateParamsJson(const std::string& status, const std::list<std::string>& paramList) = 0;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800160
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700161 // Helpers
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800162 static JsonSection
163 getJsonFromNameComponent(const Name& name, int pos);
164
165 static Name
166 genDownloadName(const Name& caName, const std::string& requestId);
167
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -0800168 static std::string
169 generateSecretCode();
170
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800171public:
172 const std::string CHALLENGE_TYPE;
173 static const std::string WAIT_SELECTION;
174 static const std::string SUCCESS;
175 static const std::string PENDING;
176
177private:
178 typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
179 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
180
181 static ChallengeFactory&
182 getFactory();
183};
184
185#define NDNCERT_REGISTER_CHALLENGE(C, T) \
186static class NdnCert ## C ## ChallengeRegistrationClass \
187{ \
188public: \
189 NdnCert ## C ## ChallengeRegistrationClass() \
190 { \
191 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
192 } \
193} g_NdnCert ## C ## ChallengeRegistrationVariable
194
195} // namespace ndncert
196} // namespace ndn
197
198#endif // NDNCERT_CHALLENGE_MODULE_HPP