blob: 73594c0511a08f51b936bdab54eea2f2315baf9b [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08004 *
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
Zhiyi Zhang84e11842020-11-19 20:03:23 -080021#include "challenge/challenge-module.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040022
Junxiao Shi7c068032017-05-28 13:40:47 +000023#include <ndn-cxx/util/random.hpp>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080024
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080025namespace ndncert {
26
Zhiyi Zhangead9f002020-10-03 15:42:52 -070027ChallengeModule::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 Zhang65ba9322017-01-19 14:15:03 -080033{
34}
35
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080036bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070037ChallengeModule::isChallengeSupported(const std::string& challengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080038{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040039 auto& factory = getFactory();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080040 auto i = factory.find(challengeType);
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040041 return i != factory.end();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080042}
43
Davide Pesavento0dc02012021-11-23 22:55:03 -050044std::unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080045ChallengeModule::createChallengeModule(const std::string& challengeType)
46{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040047 auto& factory = getFactory();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080048 auto i = factory.find(challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080049 return i == factory.end() ? nullptr : i->second();
50}
51
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080052ChallengeModule::ChallengeFactory&
53ChallengeModule::getFactory()
54{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040055 static ChallengeFactory factory;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080056 return factory;
57}
58
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080059std::string
60ChallengeModule::generateSecretCode()
61{
62 uint32_t securityCode = 0;
63 do {
Davide Pesavento0dc02012021-11-23 22:55:03 -050064 securityCode = ndn::random::generateSecureWord32();
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040065 } while (securityCode >= 4294000000);
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080066 securityCode /= 4294;
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040067 std::string result = std::to_string(securityCode);
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080068 while (result.length() < 6) {
69 result = "0" + result;
70 }
71 return result;
72}
73
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070074std::tuple<ErrorCode, std::string>
tylerliuf2e6bb52020-12-13 13:23:05 -080075ChallengeModule::returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string errorInfo)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076{
tylerliu7b9185c2020-11-24 12:15:18 -080077 request.status = Status::FAILURE;
78 request.challengeType = "";
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040079 request.challengeState = std::nullopt;
80 return {errorCode, std::move(errorInfo)};
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081}
82
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070083std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070084ChallengeModule::returnWithNewChallengeStatus(ca::RequestState& request, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -080085 JsonSection challengeSecret, size_t remainingTries,
86 time::seconds remainingTime)
Zhiyi Zhang46049832020-09-28 17:08:12 -070087{
tylerliu7b9185c2020-11-24 12:15:18 -080088 request.status = Status::CHALLENGE;
89 request.challengeType = CHALLENGE_TYPE;
tylerliuf2e6bb52020-12-13 13:23:05 -080090 request.challengeState = ca::ChallengeState(challengeStatus, time::system_clock::now(), remainingTries,
91 remainingTime, std::move(challengeSecret));
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040092 return {ErrorCode::NO_ERROR, ""};
Zhiyi Zhang46049832020-09-28 17:08:12 -070093}
94
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070095std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070096ChallengeModule::returnWithSuccess(ca::RequestState& request)
Zhiyi Zhang46049832020-09-28 17:08:12 -070097{
tylerliu7b9185c2020-11-24 12:15:18 -080098 request.status = Status::PENDING;
99 request.challengeType = CHALLENGE_TYPE;
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400100 request.challengeState = std::nullopt;
101 return {ErrorCode::NO_ERROR, ""};
Zhiyi Zhang46049832020-09-28 17:08:12 -0700102}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800104} // namespace ndncert