blob: 224a30c3fd3b1afad2f7142c62bc8a029e0b901b [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
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
40unique_ptr<ChallengeModule>
41ChallengeModule::createChallengeModule(const std::string& canonicalName)
42{
43 ChallengeFactory& factory = getFactory();
44 auto i = factory.find(canonicalName);
45 return i == factory.end() ? nullptr : i->second();
46}
47
48JsonSection
49ChallengeModule::handleChallengeRequest(const Interest& interest, CertificateRequest& request)
50{
Zhiyi Zhange30eb352017-04-13 15:26:14 -070051 int pos = request.getCaName().size() + 1;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080052 const Name& interestName = interest.getName();
53 std::string interestType = interestName.get(pos).toUri();
Zhiyi Zhange30eb352017-04-13 15:26:14 -070054
55 _LOG_TRACE("Incoming challenge request. type: " << interestType);
56
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080057 if (interestType == "_SELECT") {
58 return processSelectInterest(interest, request);
59 }
60 else if (interestType == "_VALIDATE"){
61 return processValidateInterest(interest, request);
62 }
63 else {
64 return processStatusInterest(interest, request);
65 }
66}
67
68std::list<std::string>
69ChallengeModule::getRequirementForSelect()
70{
71 return getSelectRequirements();
72}
73
74std::list<std::string>
75ChallengeModule::getRequirementForValidate(const std::string& status)
76{
77 return getValidateRequirements(status);
78}
79
80JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070081ChallengeModule::genSelectParamsJson(const std::string& status,
82 const std::list<std::string>& paramList)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080083{
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070084 return doGenSelectParamsJson(status, paramList);
85}
86
87JsonSection
88ChallengeModule::genValidateParamsJson(const std::string& status,
89 const std::list<std::string>& paramList)
90{
91 return doGenValidateParamsJson(status, paramList);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080092}
93
94JsonSection
95ChallengeModule::processStatusInterest(const Interest& interest, const CertificateRequest& request)
96{
97 // interest format: /CA/_STATUS/{"request-id":"id"}/<signature>
98 if (request.getStatus() == SUCCESS) {
99 Name downloadName = genDownloadName(request.getCaName(), request.getStatus());
100 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
101 SUCCESS, downloadName);
102 }
103 else
104 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
105 request.getStatus());
106}
107
108JsonSection
109ChallengeModule::getJsonFromNameComponent(const Name& name, int pos)
110{
111 std::string jsonString = encoding::readString(name.get(pos));
112 std::istringstream ss(jsonString);
113 JsonSection json;
114 boost::property_tree::json_parser::read_json(ss, json);
115 return json;
116}
117
118Name
119ChallengeModule::genDownloadName(const Name& caName, const std::string& requestId)
120{
121 JsonSection json;
122 json.put(JSON_REQUEST_ID, requestId);
123 std::stringstream ss;
124 boost::property_tree::write_json(ss, json);
125 Block jsonBlock = makeStringBlock(ndn::tlv::NameComponent, ss.str());
126 Name name = caName;
127 name.append("_DOWNLOAD").append(jsonBlock);
128 return name;
129}
130
131ChallengeModule::ChallengeFactory&
132ChallengeModule::getFactory()
133{
134 static ChallengeModule::ChallengeFactory factory;
135 return factory;
136}
137
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -0800138std::string
139ChallengeModule::generateSecretCode()
140{
141 uint32_t securityCode = 0;
142 do {
143 securityCode = random::generateSecureWord32();
144 }
145 while (securityCode >= 4294000000);
146 securityCode /= 4294;
147 std::string result = std::to_string(securityCode);
148 while (result.length() < 6) {
149 result = "0" + result;
150 }
151 return result;
152}
153
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800154} // namespace ndncert
155} // namespace ndn