blob: 1cc22adb624511b3c9121ed3b2013062da559011 [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
61 /**
62 * @brief Handle the challenge related interest and update certificate request.
63 * @note Should be used by CA Module
64 * @note Signature of interest should already be validated by CA Module
65 *
66 * When CA receives a SELECT or a VALIDATE or a STATUS interest, CA should invoke the function
67 * to enable selected challenge to go on the verification process.
68 *
69 * @param interest The request interest packet
70 * @param request The CertificateRequest instance
71 * @return the JSON file as the response data content
72 */
73 JsonSection
74 handleChallengeRequest(const Interest& interest, CertificateRequest& request);
75
76 /**
77 * @brief Get requirements for requester before sending SELECT interest.
78 * @note Should be used by Client Module
79 *
80 * Before requester sends a USE interest, client should invoke the function to
81 * get input instruction and expose the instruction to requester.
82 *
83 * Every item in the return list requires a input from requester. The item itself is
84 * an instruction for requester.
85 *
86 * @return the input instruction for requester
87 */
88 std::list<std::string>
89 getRequirementForSelect();
90
91 /**
92 * @brief Get requirements for requester before sending VALIDATE interest.
93 * @note Should be used by Client Module
94 *
95 * Before requester sends a POLL interest, client should invoke the function to
96 * get input instruction and expose the instruction to requester.
97 *
98 * Every item in the return list requires a input from requester. The item itself is
99 * an instruction for requester.
100 *
101 * @param status of the challenge
102 * @return the input instruction for requester
103 */
104 std::list<std::string>
105 getRequirementForValidate(const std::string& status);
106
107 /**
108 * @brief Generate ChallengeInfo part for SELECT and VALIDATE interest.
109 * @note Should be used by Client Module
110 *
111 * After requester provides required information, client should invoke the function to
112 * generate the ChallengeInfo part of the interest.
113 *
114 * @param interestType of the request
115 * @param status of the challenge
116 * @param paramList contains all the input from requester
117 * @return the JSON file of ChallengeInfo
118 */
119 JsonSection
120 genRequestChallengeInfo(const std::string& interestType, const std::string& status,
121 const std::list<std::string>& paramList);
122
123PUBLIC_WITH_TESTS_ELSE_PROTECTED:
124 virtual JsonSection
125 processSelectInterest(const Interest& interest, CertificateRequest& request) = 0;
126
127 virtual JsonSection
128 processValidateInterest(const Interest& interest, CertificateRequest& request) = 0;
129
130 virtual std::list<std::string>
131 getSelectRequirements() = 0;
132
133 virtual std::list<std::string>
134 getValidateRequirements(const std::string& status) = 0;
135
136 virtual JsonSection
137 genChallengeInfo(const std::string& interestType, const std::string& status,
138 const std::list<std::string>& paramList) = 0;
139
140 virtual JsonSection
141 processStatusInterest(const Interest& interest, const CertificateRequest& request);
142
143 static JsonSection
144 getJsonFromNameComponent(const Name& name, int pos);
145
146 static Name
147 genDownloadName(const Name& caName, const std::string& requestId);
148
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -0800149 static std::string
150 generateSecretCode();
151
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800152public:
153 const std::string CHALLENGE_TYPE;
154 static const std::string WAIT_SELECTION;
155 static const std::string SUCCESS;
156 static const std::string PENDING;
157
158private:
159 typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
160 typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
161
162 static ChallengeFactory&
163 getFactory();
164};
165
166#define NDNCERT_REGISTER_CHALLENGE(C, T) \
167static class NdnCert ## C ## ChallengeRegistrationClass \
168{ \
169public: \
170 NdnCert ## C ## ChallengeRegistrationClass() \
171 { \
172 ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
173 } \
174} g_NdnCert ## C ## ChallengeRegistrationVariable
175
176} // namespace ndncert
177} // namespace ndn
178
179#endif // NDNCERT_CHALLENGE_MODULE_HPP