Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, 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 | 34f03f0 | 2020-10-29 18:34:42 -0700 | [diff] [blame] | 24 | #include "detail/ca-request-state.hpp" |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 26 | #include <map> |
| 27 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 28 | namespace ndncert { |
| 29 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 30 | class ChallengeModule : boost::noncopyable |
Zhiyi Zhang | 684c67c | 2020-10-12 14:28:17 -0700 | [diff] [blame] | 31 | { |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 32 | public: |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 33 | ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 34 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 35 | virtual |
| 36 | ~ChallengeModule() = default; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 37 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 38 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 39 | virtual std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 40 | handleChallengeRequest(const Block& params, ca::RequestState& request) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 41 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 42 | // For Client |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 43 | virtual std::multimap<std::string, std::string> |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 44 | getRequestedParameterList(Status status, const std::string& challengeStatus) = 0; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 45 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 46 | virtual Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 47 | genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 48 | const std::multimap<std::string, std::string>& params) = 0; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 49 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 50 | public: // factory |
| 51 | template<class ChallengeType> |
| 52 | static void |
| 53 | registerChallengeModule(const std::string& type) |
| 54 | { |
| 55 | auto& factory = getFactory(); |
| 56 | BOOST_ASSERT(factory.count(type) == 0); |
| 57 | factory[type] = [] { return std::make_unique<ChallengeType>(); }; |
| 58 | } |
| 59 | |
| 60 | static bool |
| 61 | isChallengeSupported(const std::string& challengeType); |
| 62 | |
| 63 | static std::unique_ptr<ChallengeModule> |
| 64 | createChallengeModule(const std::string& challengeType); |
| 65 | |
| 66 | protected: // helpers used by concrete challenge modules |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 67 | static std::string |
| 68 | generateSecretCode(); |
| 69 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 70 | static std::tuple<ErrorCode, std::string> |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 71 | returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string errorInfo); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 72 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 73 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 74 | returnWithNewChallengeStatus(ca::RequestState& request, const std::string& challengeStatus, |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 75 | JsonSection challengeSecret, size_t remainingTries, time::seconds remainingTime); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 76 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 77 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 78 | returnWithSuccess(ca::RequestState& request); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 80 | public: |
| 81 | const std::string CHALLENGE_TYPE; |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 82 | |
| 83 | protected: |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 84 | const size_t m_maxAttemptTimes; |
| 85 | const time::seconds m_secretLifetime; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 86 | |
| 87 | private: |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 88 | using CreateFunc = std::function<std::unique_ptr<ChallengeModule>()>; |
| 89 | using ChallengeFactory = std::map<std::string, CreateFunc>; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 90 | |
| 91 | static ChallengeFactory& |
| 92 | getFactory(); |
| 93 | }; |
| 94 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 95 | } // namespace ndncert |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 96 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 97 | #define NDNCERT_REGISTER_CHALLENGE(C, T) \ |
| 98 | static class NdnCert##C##ChallengeRegistrationClass \ |
| 99 | { \ |
| 100 | public: \ |
| 101 | NdnCert##C##ChallengeRegistrationClass() \ |
| 102 | { \ |
| 103 | ::ndncert::ChallengeModule::registerChallengeModule<C>(T); \ |
| 104 | } \ |
| 105 | } g_NdnCert##C##ChallengeRegistrationVariable |
| 106 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 107 | #endif // NDNCERT_CHALLENGE_MODULE_HPP |