blob: 6a288661084a3783de2369d714e7c1955116bd91 [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"
22#include <random>
23
24namespace ndn {
25namespace ndncert {
26
27const std::string ChallengeModule::WAIT_SELECTION = "wait-selection";
28const std::string ChallengeModule::SUCCESS = "success";
29const std::string ChallengeModule::PENDING = "pending";
30
31ChallengeModule::ChallengeModule(const std::string& uniqueType)
32 : CHALLENGE_TYPE(uniqueType)
33{
34}
35
36unique_ptr<ChallengeModule>
37ChallengeModule::createChallengeModule(const std::string& canonicalName)
38{
39 ChallengeFactory& factory = getFactory();
40 auto i = factory.find(canonicalName);
41 return i == factory.end() ? nullptr : i->second();
42}
43
44JsonSection
45ChallengeModule::handleChallengeRequest(const Interest& interest, CertificateRequest& request)
46{
47 int pos = request.getCaName().size();
48 const Name& interestName = interest.getName();
49 std::string interestType = interestName.get(pos).toUri();
50 if (interestType == "_SELECT") {
51 return processSelectInterest(interest, request);
52 }
53 else if (interestType == "_VALIDATE"){
54 return processValidateInterest(interest, request);
55 }
56 else {
57 return processStatusInterest(interest, request);
58 }
59}
60
61std::list<std::string>
62ChallengeModule::getRequirementForSelect()
63{
64 return getSelectRequirements();
65}
66
67std::list<std::string>
68ChallengeModule::getRequirementForValidate(const std::string& status)
69{
70 return getValidateRequirements(status);
71}
72
73JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070074ChallengeModule::genSelectParamsJson(const std::string& status,
75 const std::list<std::string>& paramList)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080076{
Zhiyi Zhangf72c0542017-03-16 14:45:30 -070077 return doGenSelectParamsJson(status, paramList);
78}
79
80JsonSection
81ChallengeModule::genValidateParamsJson(const std::string& status,
82 const std::list<std::string>& paramList)
83{
84 return doGenValidateParamsJson(status, paramList);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080085}
86
87JsonSection
88ChallengeModule::processStatusInterest(const Interest& interest, const CertificateRequest& request)
89{
90 // interest format: /CA/_STATUS/{"request-id":"id"}/<signature>
91 if (request.getStatus() == SUCCESS) {
92 Name downloadName = genDownloadName(request.getCaName(), request.getStatus());
93 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
94 SUCCESS, downloadName);
95 }
96 else
97 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
98 request.getStatus());
99}
100
101JsonSection
102ChallengeModule::getJsonFromNameComponent(const Name& name, int pos)
103{
104 std::string jsonString = encoding::readString(name.get(pos));
105 std::istringstream ss(jsonString);
106 JsonSection json;
107 boost::property_tree::json_parser::read_json(ss, json);
108 return json;
109}
110
111Name
112ChallengeModule::genDownloadName(const Name& caName, const std::string& requestId)
113{
114 JsonSection json;
115 json.put(JSON_REQUEST_ID, requestId);
116 std::stringstream ss;
117 boost::property_tree::write_json(ss, json);
118 Block jsonBlock = makeStringBlock(ndn::tlv::NameComponent, ss.str());
119 Name name = caName;
120 name.append("_DOWNLOAD").append(jsonBlock);
121 return name;
122}
123
124ChallengeModule::ChallengeFactory&
125ChallengeModule::getFactory()
126{
127 static ChallengeModule::ChallengeFactory factory;
128 return factory;
129}
130
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -0800131std::string
132ChallengeModule::generateSecretCode()
133{
134 uint32_t securityCode = 0;
135 do {
136 securityCode = random::generateSecureWord32();
137 }
138 while (securityCode >= 4294000000);
139 securityCode /= 4294;
140 std::string result = std::to_string(securityCode);
141 while (result.length() < 6) {
142 result = "0" + result;
143 }
144 return result;
145}
146
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800147} // namespace ndncert
148} // namespace ndn