blob: c1f34324f8ba62ab383c0cae066186deccb1899e [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento08994782018-01-22 12:13:41 -05003 * Copyright (c) 2017-2018, 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"
Zhiyi Zhange30eb352017-04-13 15:26:14 -070022#include "logging.hpp"
Junxiao Shi7c068032017-05-28 13:40:47 +000023#include <ndn-cxx/util/random.hpp>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080024
25namespace ndn {
26namespace ndncert {
27
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080028_LOG_INIT(ndncert.challenge-module);
Zhiyi Zhange30eb352017-04-13 15:26:14 -070029
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080030const std::string ChallengeModule::WAIT_SELECTION = "wait-selection";
31const std::string ChallengeModule::SUCCESS = "success";
32const std::string ChallengeModule::PENDING = "pending";
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070033const std::string ChallengeModule::FAILURE = "failure";
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080034
35ChallengeModule::ChallengeModule(const std::string& uniqueType)
36 : CHALLENGE_TYPE(uniqueType)
37{
38}
39
Davide Pesavento08994782018-01-22 12:13:41 -050040ChallengeModule::~ChallengeModule() = default;
41
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080042unique_ptr<ChallengeModule>
43ChallengeModule::createChallengeModule(const std::string& canonicalName)
44{
45 ChallengeFactory& factory = getFactory();
46 auto i = factory.find(canonicalName);
47 return i == factory.end() ? nullptr : i->second();
48}
49
50JsonSection
51ChallengeModule::handleChallengeRequest(const Interest& interest, CertificateRequest& request)
52{
Zhiyi Zhange30eb352017-04-13 15:26:14 -070053 int pos = request.getCaName().size() + 1;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080054 const Name& interestName = interest.getName();
55 std::string interestType = interestName.get(pos).toUri();
Zhiyi Zhange30eb352017-04-13 15:26:14 -070056
57 _LOG_TRACE("Incoming challenge request. type: " << interestType);
58
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080059 if (interestType == "_SELECT") {
60 return processSelectInterest(interest, request);
61 }
62 else if (interestType == "_VALIDATE"){
63 return processValidateInterest(interest, request);
64 }
65 else {
66 return processStatusInterest(interest, request);
67 }
68}
69
70std::list<std::string>
71ChallengeModule::getRequirementForSelect()
72{
73 return getSelectRequirements();
74}
75
76std::list<std::string>
77ChallengeModule::getRequirementForValidate(const std::string& status)
78{
79 return getValidateRequirements(status);
80}
81
82JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070083ChallengeModule::genSelectParamsJson(const std::string& status,
84 const std::list<std::string>& paramList)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080085{
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070086 return doGenSelectParamsJson(status, paramList);
87}
88
89JsonSection
90ChallengeModule::genValidateParamsJson(const std::string& status,
91 const std::list<std::string>& paramList)
92{
93 return doGenValidateParamsJson(status, paramList);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080094}
95
96JsonSection
97ChallengeModule::processStatusInterest(const Interest& interest, const CertificateRequest& request)
98{
99 // interest format: /CA/_STATUS/{"request-id":"id"}/<signature>
100 if (request.getStatus() == SUCCESS) {
101 Name downloadName = genDownloadName(request.getCaName(), request.getStatus());
102 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
103 SUCCESS, downloadName);
104 }
105 else
106 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
107 request.getStatus());
108}
109
110JsonSection
111ChallengeModule::getJsonFromNameComponent(const Name& name, int pos)
112{
113 std::string jsonString = encoding::readString(name.get(pos));
114 std::istringstream ss(jsonString);
115 JsonSection json;
116 boost::property_tree::json_parser::read_json(ss, json);
117 return json;
118}
119
120Name
121ChallengeModule::genDownloadName(const Name& caName, const std::string& requestId)
122{
123 JsonSection json;
124 json.put(JSON_REQUEST_ID, requestId);
125 std::stringstream ss;
126 boost::property_tree::write_json(ss, json);
127 Block jsonBlock = makeStringBlock(ndn::tlv::NameComponent, ss.str());
128 Name name = caName;
129 name.append("_DOWNLOAD").append(jsonBlock);
130 return name;
131}
132
133ChallengeModule::ChallengeFactory&
134ChallengeModule::getFactory()
135{
136 static ChallengeModule::ChallengeFactory factory;
137 return factory;
138}
139
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -0800140std::string
141ChallengeModule::generateSecretCode()
142{
143 uint32_t securityCode = 0;
144 do {
145 securityCode = random::generateSecureWord32();
146 }
147 while (securityCode >= 4294000000);
148 securityCode /= 4294;
149 std::string result = std::to_string(securityCode);
150 while (result.length() < 6) {
151 result = "0" + result;
152 }
153 return result;
154}
155
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800156} // namespace ndncert
157} // namespace ndn