blob: b4bcc45f5ce0abfa29a04e4b9215bdfd6c8ceed8 [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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
21#include "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
24namespace ndn {
25namespace 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{
39 ChallengeFactory& factory = getFactory();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080040 auto i = factory.find(challengeType);
41 return i == factory.end() ? false : true;
42}
43
44unique_ptr<ChallengeModule>
45ChallengeModule::createChallengeModule(const std::string& challengeType)
46{
47 ChallengeFactory& factory = getFactory();
48 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{
55 static ChallengeModule::ChallengeFactory factory;
56 return factory;
57}
58
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080059std::string
60ChallengeModule::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 Zhangaafc55e2020-09-28 17:54:48 -070075std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070076ChallengeModule::returnWithError(CaState& request, ErrorCode errorCode, std::string&& errorInfo)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077{
Zhiyi Zhang46049832020-09-28 17:08:12 -070078 request.m_status = Status::FAILURE;
79 request.m_challengeType = "";
Zhiyi Zhanga749f442020-09-29 17:19:51 -070080 request.m_challengeState = boost::none;
Zhiyi Zhang46049832020-09-28 17:08:12 -070081 return std::make_tuple(errorCode, std::move(errorInfo));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070082}
83
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070084std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070085ChallengeModule::returnWithNewChallengeStatus(CaState& request, const std::string& challengeStatus,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070086 JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime)
Zhiyi Zhang46049832020-09-28 17:08:12 -070087{
88 request.m_status = Status::CHALLENGE;
89 request.m_challengeType = CHALLENGE_TYPE;
Zhiyi Zhanga749f442020-09-29 17:19:51 -070090 request.m_challengeState = ChallengeState(challengeStatus, time::system_clock::now(), remainingTries, remainingTime, std::move(challengeSecret));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070091 return std::make_tuple(ErrorCode::NO_ERROR, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -070092}
93
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070094std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070095ChallengeModule::returnWithSuccess(CaState& request)
Zhiyi Zhang46049832020-09-28 17:08:12 -070096{
97 request.m_status = Status::PENDING;
98 request.m_challengeType = CHALLENGE_TYPE;
Zhiyi Zhanga749f442020-09-29 17:19:51 -070099 request.m_challengeState = boost::none;
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700100 return std::make_tuple(ErrorCode::NO_ERROR, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700101}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800103} // namespace ndncert
104} // namespace ndn