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 | |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 24 | #include "request-state.hpp" |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 25 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 29 | class ChallengeModule : noncopyable { |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 30 | public: |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 31 | explicit ChallengeModule(const std::string& uniqueType); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 32 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 33 | virtual ~ChallengeModule(); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 34 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 35 | template <class ChallengeType> |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 36 | static void |
| 37 | registerChallengeModule(const std::string& typeName) |
| 38 | { |
| 39 | ChallengeFactory& factory = getFactory(); |
| 40 | BOOST_ASSERT(factory.count(typeName) == 0); |
| 41 | factory[typeName] = [] { return make_unique<ChallengeType>(); }; |
| 42 | } |
| 43 | |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 44 | static bool |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 45 | isChallengeSupported(const std::string& challengeType); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 46 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 47 | static unique_ptr<ChallengeModule> |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 48 | createChallengeModule(const std::string& challengeType); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 49 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 50 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 51 | virtual std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 52 | handleChallengeRequest(const Block& params, RequestState& request) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 53 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 54 | // For Client |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 55 | virtual std::vector<std::tuple<std::string, std::string>> |
| 56 | getRequestedParameterList(Status status, const std::string& challengeStatus) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 57 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 58 | virtual Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 59 | genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
| 60 | std::vector<std::tuple<std::string, std::string>>&& params) = 0; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 61 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | // helpers |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 63 | static std::string |
| 64 | generateSecretCode(); |
| 65 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 66 | protected: |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 67 | // used by challenge modules |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 68 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 69 | returnWithError(RequestState& request, ErrorCode errorCode, std::string&& errorInfo); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 70 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 71 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 72 | returnWithNewChallengeStatus(RequestState& request, const std::string& challengeStatus, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 73 | JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 74 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 75 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 76 | returnWithSuccess(RequestState& request); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 77 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 78 | public: |
| 79 | const std::string CHALLENGE_TYPE; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 82 | typedef function<unique_ptr<ChallengeModule>()> ChallengeCreateFunc; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 83 | typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory; |
| 84 | |
| 85 | static ChallengeFactory& |
| 86 | getFactory(); |
| 87 | }; |
| 88 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 89 | #define NDNCERT_REGISTER_CHALLENGE(C, T) \ |
| 90 | static class NdnCert##C##ChallengeRegistrationClass { \ |
| 91 | public: \ |
| 92 | NdnCert##C##ChallengeRegistrationClass() \ |
| 93 | { \ |
| 94 | ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T); \ |
| 95 | } \ |
| 96 | } g_NdnCert##C##ChallengeRegistrationVariable |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 97 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 98 | } // namespace ndncert |
| 99 | } // namespace ndn |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 100 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 101 | #endif // NDNCERT_CHALLENGE_MODULE_HPP |