blob: 47dca5d8abdc21991374c623ca788fc2af54e3e6 [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 Zhang65ba9322017-01-19 14:15:03 -080027ChallengeModule::ChallengeModule(const std::string& uniqueType)
28 : CHALLENGE_TYPE(uniqueType)
29{
30}
31
Davide Pesavento08994782018-01-22 12:13:41 -050032ChallengeModule::~ChallengeModule() = default;
33
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080034bool
Zhiyi Zhang46049832020-09-28 17:08:12 -070035ChallengeModule::isChallengeSupported(const std::string& challengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080036{
37 ChallengeFactory& factory = getFactory();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080038 auto i = factory.find(challengeType);
39 return i == factory.end() ? false : true;
40}
41
42unique_ptr<ChallengeModule>
43ChallengeModule::createChallengeModule(const std::string& challengeType)
44{
45 ChallengeFactory& factory = getFactory();
46 auto i = factory.find(challengeType);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080047 return i == factory.end() ? nullptr : i->second();
48}
49
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080050ChallengeModule::ChallengeFactory&
51ChallengeModule::getFactory()
52{
53 static ChallengeModule::ChallengeFactory factory;
54 return factory;
55}
56
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080057std::string
58ChallengeModule::generateSecretCode()
59{
60 uint32_t securityCode = 0;
61 do {
62 securityCode = random::generateSecureWord32();
63 }
64 while (securityCode >= 4294000000);
65 securityCode /= 4294;
66 std::string result = std::to_string(securityCode);
67 while (result.length() < 6) {
68 result = "0" + result;
69 }
70 return result;
71}
72
Zhiyi Zhang46049832020-09-28 17:08:12 -070073std::tuple<Error, std::string>
74ChallengeModule::returnWithError(CertificateRequest& request, Error errorCode, std::string&& errorInfo)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075{
Zhiyi Zhang46049832020-09-28 17:08:12 -070076 request.m_status = Status::FAILURE;
77 request.m_challengeType = "";
78 request.m_challengeStatus = "";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079 request.m_challengeSecrets = JsonSection();
80 request.m_challengeTp = "";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081 request.m_remainingTime = 0;
82 request.m_remainingTries = 0;
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 Zhang46049832020-09-28 17:08:12 -070086std::tuple<Error, std::string>
87ChallengeModule::returnWithNewChallengeStatus(CertificateRequest& request, const std::string& challengeStatus,
88 JsonSection&& challengeSecret, size_t remainingTries, size_t remainingTime)
89{
90 request.m_status = Status::CHALLENGE;
91 request.m_challengeType = CHALLENGE_TYPE;
92 request.m_challengeStatus = std::move(challengeStatus);
93 request.m_challengeSecrets = std::move(challengeSecret);
94 request.m_challengeTp = time::toIsoString(time::system_clock::now());
95 request.m_remainingTime = remainingTries;
96 request.m_remainingTries = remainingTime;
97 return std::make_tuple(Error::NO_ERROR, "");
98}
99
100std::tuple<Error, std::string>
101ChallengeModule::returnWithSuccess(CertificateRequest& request)
102{
103 request.m_status = Status::PENDING;
104 request.m_challengeType = CHALLENGE_TYPE;
105 request.m_challengeStatus = "";
106 request.m_challengeSecrets = JsonSection();
107 request.m_challengeTp = "";
108 request.m_remainingTime = 0;
109 request.m_remainingTries = 0;
110 return std::make_tuple(Error::NO_ERROR, "");
111}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700112
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800113} // namespace ndncert
114} // namespace ndn