Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2018, Regents of the University of California. |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 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 | |
| 28 | namespace ndn { |
| 29 | namespace ndncert { |
| 30 | |
| 31 | class ChallengeModule : noncopyable |
| 32 | { |
| 33 | public: |
| 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 | |
| 46 | public: |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 47 | explicit |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 48 | ChallengeModule(const std::string& uniqueType); |
| 49 | |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 50 | virtual |
| 51 | ~ChallengeModule(); |
| 52 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 53 | 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 Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 65 | // For CA |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 66 | /** |
| 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 Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 81 | // For Client |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 82 | /** |
| 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 Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 114 | * @brief Generate ChallengeInfo part for SELECT interest. |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 115 | * @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 Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 120 | * @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 Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 125 | 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 Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 140 | |
| 141 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 142 | // For CA |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 143 | virtual JsonSection |
| 144 | processSelectInterest(const Interest& interest, CertificateRequest& request) = 0; |
| 145 | |
| 146 | virtual JsonSection |
| 147 | processValidateInterest(const Interest& interest, CertificateRequest& request) = 0; |
| 148 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 149 | virtual JsonSection |
| 150 | processStatusInterest(const Interest& interest, const CertificateRequest& request); |
| 151 | |
| 152 | // For Client |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 153 | 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 Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 160 | doGenSelectParamsJson(const std::string& status, const std::list<std::string>& paramList) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 161 | |
| 162 | virtual JsonSection |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 163 | doGenValidateParamsJson(const std::string& status, const std::list<std::string>& paramList) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 164 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 165 | // Helpers |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 166 | static JsonSection |
| 167 | getJsonFromNameComponent(const Name& name, int pos); |
| 168 | |
| 169 | static Name |
| 170 | genDownloadName(const Name& caName, const std::string& requestId); |
| 171 | |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 172 | static std::string |
| 173 | generateSecretCode(); |
| 174 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 175 | public: |
| 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 Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 180 | static const std::string FAILURE; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 181 | |
| 182 | private: |
| 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) \ |
| 191 | static class NdnCert ## C ## ChallengeRegistrationClass \ |
| 192 | { \ |
| 193 | public: \ |
| 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 |