blob: bf3b966ede6c300bdb2a093b2d09df05d8dac6f6 [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/*
3 * Copyright (c) 2017-2021, 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"
Junxiao Shi7c068032017-05-28 13:40:47 +000022#include <ndn-cxx/util/random.hpp>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080023
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080024namespace ndncert {
25
Zhiyi Zhangead9f002020-10-03 15:42:52 -070026ChallengeModule::ChallengeModule(const std::string& challengeType,
27 size_t maxAttemptTimes,
28 time::seconds secretLifetime)
29 : CHALLENGE_TYPE(challengeType)
30 , m_maxAttemptTimes(maxAttemptTimes)
31 , m_secretLifetime(secretLifetime)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080032{
33}
34
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080035bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070036ChallengeModule::isChallengeSupported(const std::string& challengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080037{
38 ChallengeFactory& factory = getFactory();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080039 auto i = factory.find(challengeType);
40 return i == factory.end() ? false : true;
41}
42
Davide Pesavento0dc02012021-11-23 22:55:03 -050043std::unique_ptr<ChallengeModule>
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080044ChallengeModule::createChallengeModule(const std::string& challengeType)
45{
46 ChallengeFactory& factory = getFactory();
47 auto i = factory.find(challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080048 return i == factory.end() ? nullptr : i->second();
49}
50
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080051ChallengeModule::ChallengeFactory&
52ChallengeModule::getFactory()
53{
54 static ChallengeModule::ChallengeFactory factory;
55 return factory;
56}
57
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080058std::string
59ChallengeModule::generateSecretCode()
60{
61 uint32_t securityCode = 0;
62 do {
Davide Pesavento0dc02012021-11-23 22:55:03 -050063 securityCode = ndn::random::generateSecureWord32();
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080064 }
65 while (securityCode >= 4294000000);
66 securityCode /= 4294;
Davide Pesavento0dc02012021-11-23 22:55:03 -050067 std::string result = ndn::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 = "";
79 request.challengeState = nullopt;
Zhiyi Zhang46049832020-09-28 17:08:12 -070080 return std::make_tuple(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));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070092 return std::make_tuple(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;
100 request.challengeState = nullopt;
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700101 return std::make_tuple(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