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 | #include "challenge-module.hpp" |
Junxiao Shi | 7c06803 | 2017-05-28 13:40:47 +0000 | [diff] [blame] | 22 | #include <ndn-cxx/util/random.hpp> |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 27 | ChallengeModule::ChallengeModule(const std::string& uniqueType) |
| 28 | : CHALLENGE_TYPE(uniqueType) |
| 29 | { |
| 30 | } |
| 31 | |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 32 | ChallengeModule::~ChallengeModule() = default; |
| 33 | |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 34 | bool |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 35 | ChallengeModule::isChallengeSupported(const std::string& challengeType) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 36 | { |
| 37 | ChallengeFactory& factory = getFactory(); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 38 | auto i = factory.find(challengeType); |
| 39 | return i == factory.end() ? false : true; |
| 40 | } |
| 41 | |
| 42 | unique_ptr<ChallengeModule> |
| 43 | ChallengeModule::createChallengeModule(const std::string& challengeType) |
| 44 | { |
| 45 | ChallengeFactory& factory = getFactory(); |
| 46 | auto i = factory.find(challengeType); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 47 | return i == factory.end() ? nullptr : i->second(); |
| 48 | } |
| 49 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 50 | ChallengeModule::ChallengeFactory& |
| 51 | ChallengeModule::getFactory() |
| 52 | { |
| 53 | static ChallengeModule::ChallengeFactory factory; |
| 54 | return factory; |
| 55 | } |
| 56 | |
Zhiyi Zhang | fb74ae2 | 2017-02-22 08:02:27 -0800 | [diff] [blame] | 57 | std::string |
| 58 | ChallengeModule::generateSecretCode() |
| 59 | { |
| 60 | uint32_t securityCode = 0; |
| 61 | do { |
| 62 | securityCode = random::generateSecureWord32(); |
| 63 | } |
| 64 | while (securityCode >= 4294000000); |
| 65 | securityCode /= 4294; |
| 66 | std::string result = std::to_string(securityCode); |
| 67 | while (result.length() < 6) { |
| 68 | result = "0" + result; |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 73 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 74 | ChallengeModule::returnWithError(RequestState& request, ErrorCode errorCode, std::string&& errorInfo) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 76 | request.m_status = Status::FAILURE; |
| 77 | request.m_challengeType = ""; |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 78 | request.m_challengeState = boost::none; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 79 | return std::make_tuple(errorCode, std::move(errorInfo)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 82 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 83 | ChallengeModule::returnWithNewChallengeStatus(RequestState& request, const std::string& challengeStatus, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 84 | JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime) |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 85 | { |
| 86 | request.m_status = Status::CHALLENGE; |
| 87 | request.m_challengeType = CHALLENGE_TYPE; |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 88 | request.m_challengeState = ChallengeState(challengeStatus, time::system_clock::now(), remainingTries, remainingTime, std::move(challengeSecret)); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 89 | return std::make_tuple(ErrorCode::NO_ERROR, ""); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 92 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 93 | ChallengeModule::returnWithSuccess(RequestState& request) |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 94 | { |
| 95 | request.m_status = Status::PENDING; |
| 96 | request.m_challengeType = CHALLENGE_TYPE; |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 97 | request.m_challengeState = boost::none; |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 98 | return std::make_tuple(ErrorCode::NO_ERROR, ""); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 99 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 100 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 101 | } // namespace ndncert |
| 102 | } // namespace ndn |