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