blob: 2170c5f86048b2f33e3f6b00f624a6d1c9d33936 [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
74ChallengeModule::genRequestChallengeInfo(const std::string& interestType, const std::string& status,
75 const std::list<std::string>& paramList)
76{
77 return genChallengeInfo(interestType, status, paramList);
78}
79
80JsonSection
81ChallengeModule::processStatusInterest(const Interest& interest, const CertificateRequest& request)
82{
83 // interest format: /CA/_STATUS/{"request-id":"id"}/<signature>
84 if (request.getStatus() == SUCCESS) {
85 Name downloadName = genDownloadName(request.getCaName(), request.getStatus());
86 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
87 SUCCESS, downloadName);
88 }
89 else
90 return genResponseChallengeJson(request.getRequestId(), request.getChallengeType(),
91 request.getStatus());
92}
93
94JsonSection
95ChallengeModule::getJsonFromNameComponent(const Name& name, int pos)
96{
97 std::string jsonString = encoding::readString(name.get(pos));
98 std::istringstream ss(jsonString);
99 JsonSection json;
100 boost::property_tree::json_parser::read_json(ss, json);
101 return json;
102}
103
104Name
105ChallengeModule::genDownloadName(const Name& caName, const std::string& requestId)
106{
107 JsonSection json;
108 json.put(JSON_REQUEST_ID, requestId);
109 std::stringstream ss;
110 boost::property_tree::write_json(ss, json);
111 Block jsonBlock = makeStringBlock(ndn::tlv::NameComponent, ss.str());
112 Name name = caName;
113 name.append("_DOWNLOAD").append(jsonBlock);
114 return name;
115}
116
117ChallengeModule::ChallengeFactory&
118ChallengeModule::getFactory()
119{
120 static ChallengeModule::ChallengeFactory factory;
121 return factory;
122}
123
124} // namespace ndncert
125} // namespace ndn