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