Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, 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" |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | |
| 30 | class ChallengeModule : noncopyable |
| 31 | { |
| 32 | public: |
| 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 | |
| 45 | public: |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 46 | explicit |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 47 | ChallengeModule(const std::string& uniqueType); |
| 48 | |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 49 | virtual |
| 50 | ~ChallengeModule(); |
| 51 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 52 | 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 Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 64 | // For CA |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 65 | virtual void |
| 66 | handleChallengeRequest(const JsonSection& params, CertificateRequest& request) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 67 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 68 | // For Client |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 69 | virtual JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 70 | getRequirementForChallenge(int status, const std::string& challengeStatus) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 71 | |
| 72 | virtual JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 73 | genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 74 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | // helpers |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 76 | static std::string |
| 77 | generateSecretCode(); |
| 78 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | protected: |
| 80 | |
| 81 | void |
| 82 | updateRequestOnChallengeEnd(CertificateRequest& request); |
| 83 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 84 | public: |
| 85 | const std::string CHALLENGE_TYPE; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 86 | |
| 87 | private: |
| 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) \ |
| 96 | static class NdnCert ## C ## ChallengeRegistrationClass \ |
| 97 | { \ |
| 98 | public: \ |
| 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 |