blob: 28624d722c860477100def4f2bd2972b9ac2c857 [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, 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 Zhangdbd9d432020-10-07 15:56:27 -070021#include "identity-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
24namespace ndn {
25namespace ndncert {
26
tylerliuab9cba22020-10-09 00:13:46 -070027const std::string ChallengeModule::SUCCESS = "success";
28
Zhiyi Zhangead9f002020-10-03 15:42:52 -070029ChallengeModule::ChallengeModule(const std::string& challengeType,
30 size_t maxAttemptTimes,
31 time::seconds secretLifetime)
32 : CHALLENGE_TYPE(challengeType)
33 , m_maxAttemptTimes(maxAttemptTimes)
34 , m_secretLifetime(secretLifetime)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080035{
36}
37
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080038bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070039ChallengeModule::isChallengeSupported(const std::string& challengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080040{
41 ChallengeFactory& factory = getFactory();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080042 auto i = factory.find(challengeType);
43 return i == factory.end() ? false : true;
44}
45
46unique_ptr<ChallengeModule>
47ChallengeModule::createChallengeModule(const std::string& challengeType)
48{
49 ChallengeFactory& factory = getFactory();
50 auto i = factory.find(challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080051 return i == factory.end() ? nullptr : i->second();
52}
53
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080054ChallengeModule::ChallengeFactory&
55ChallengeModule::getFactory()
56{
57 static ChallengeModule::ChallengeFactory factory;
58 return factory;
59}
60
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080061std::string
62ChallengeModule::generateSecretCode()
63{
64 uint32_t securityCode = 0;
65 do {
66 securityCode = random::generateSecureWord32();
67 }
68 while (securityCode >= 4294000000);
69 securityCode /= 4294;
70 std::string result = std::to_string(securityCode);
71 while (result.length() < 6) {
72 result = "0" + result;
73 }
74 return result;
75}
76
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070077std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070078ChallengeModule::returnWithError(CaState& request, ErrorCode errorCode, std::string&& errorInfo)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079{
Zhiyi Zhang46049832020-09-28 17:08:12 -070080 request.m_status = Status::FAILURE;
81 request.m_challengeType = "";
Zhiyi Zhanga749f442020-09-29 17:19:51 -070082 request.m_challengeState = boost::none;
Zhiyi Zhang46049832020-09-28 17:08:12 -070083 return std::make_tuple(errorCode, std::move(errorInfo));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084}
85
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070086std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070087ChallengeModule::returnWithNewChallengeStatus(CaState& request, const std::string& challengeStatus,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070088 JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime)
Zhiyi Zhang46049832020-09-28 17:08:12 -070089{
90 request.m_status = Status::CHALLENGE;
91 request.m_challengeType = CHALLENGE_TYPE;
Zhiyi Zhanga749f442020-09-29 17:19:51 -070092 request.m_challengeState = ChallengeState(challengeStatus, time::system_clock::now(), remainingTries, remainingTime, std::move(challengeSecret));
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070093 return std::make_tuple(ErrorCode::NO_ERROR, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -070094}
95
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070096std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070097ChallengeModule::returnWithSuccess(CaState& request)
Zhiyi Zhang46049832020-09-28 17:08:12 -070098{
99 request.m_status = Status::PENDING;
100 request.m_challengeType = CHALLENGE_TYPE;
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700101 request.m_challengeState = boost::none;
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700102 return std::make_tuple(ErrorCode::NO_ERROR, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700103}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700104
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800105} // namespace ndncert
106} // namespace ndn